Skip to content

Commit

Permalink
Example of EXISTS CE to help with #8
Browse files Browse the repository at this point in the history
  • Loading branch information
nilp0inter committed Oct 18, 2019
1 parent bb69435 commit 59fcaec
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions docs/examples/EXISTS.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EXISTS Conditional Element\n",
"\n",
"With exists you can test if a group of patterns is satisfied by at least one set of facts.\n",
"\n",
"The rule will be fired once.\n",
"\n",
"Please note that you although you can use MATCH to pattern match between matches inside the EXISTS group, **those matches can't be used as parameters in the RHS of the rule** (because more than one group of patterns can match and we are only firing once). Trying to use the matched field as parameter will result in a **TypeError**.\n",
"\n",
"This is a direct translation to Experta of [this](https://www.csie.ntu.edu.tw/~sylee/courses/clips/bpg/node5.4.6.html) Clips example about the EXISTS Conditional Element"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from experta import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class Goal(Fact):\n",
" pass\n",
"\n",
"class Hero(Fact):\n",
" name = Field(str)\n",
" status = Field(str, default=\"unoccupied\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"class KE(KnowledgeEngine):\n",
" @DefFacts()\n",
" def goal_and_heroes(self):\n",
" yield Goal('save-the-day')\n",
" yield Hero(name=\"Death Defying Man\")\n",
" yield Hero(name=\"Stupendous Man\")\n",
" yield Hero(name=\"Incredible Man\")\n",
" @Rule(\n",
" Goal('save-the-day'),\n",
" EXISTS(\n",
" Hero(status=\"unoccupied\")\n",
" )\n",
" )\n",
" def save_the_day(self):\n",
" print(\"The day is saved\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"ke = KE()\n",
"ke.reset()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0: save_the_day {InitialFact(), Goal('save-the-day')}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ke.agenda"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FactList([(0, InitialFact()),\n",
" (1, Goal('save-the-day')),\n",
" (2, Hero(name='Death Defying Man')),\n",
" (3, Hero(name='Stupendous Man')),\n",
" (4, Hero(name='Incredible Man'))])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ke.facts"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Implement (matches ) function"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The day is saved\n"
]
}
],
"source": [
"ke.run()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 59fcaec

Please sign in to comment.