Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #17 #18

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions experta/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def modify(self, declared_fact, **modifiers):
>>> ke.modify(my_fact, _0="hello", _1="world", other_key="!")

"""
self.retract(declared_fact)
idx = self.retract(declared_fact)

newfact = declared_fact.copy()
newfact.update(dict(self._get_real_modifiers(**modifiers)))

return self.declare(newfact)
return self.declare(newfact, idx=idx)

def duplicate(self, template_fact, **modifiers):
"""Create a new fact from an existing one."""
Expand Down Expand Up @@ -121,11 +121,12 @@ def retract(self, idx_or_declared_fact):
.. note::
This updates the agenda
"""
self.facts.retract(idx_or_declared_fact)
idx = self.facts.retract(idx_or_declared_fact)

if not self.running:
added, removed = self.get_activations()
self.strategy.update_agenda(self.agenda, added, removed)
return idx

def run(self, steps=float('inf')):
"""
Expand Down Expand Up @@ -209,7 +210,7 @@ def reset(self, **kwargs):

self.running = False

def __declare(self, *facts):
def __declare(self, *facts, idx=None):
"""
Internal declaration method. Used for ``declare`` and ``deffacts``
"""
Expand All @@ -222,15 +223,15 @@ def __declare(self, *facts):
else:
last_inserted = None
for fact in facts:
last_inserted = self.facts.declare(fact)
last_inserted = self.facts.declare(fact, idx=idx)

if not self.running:
added, removed = self.get_activations()
self.strategy.update_agenda(self.agenda, added, removed)

return last_inserted

def declare(self, *facts):
def declare(self, *facts, idx=None):
"""
Declare from inside a fact, equivalent to ``assert`` in clips.

Expand All @@ -241,4 +242,4 @@ def declare(self, *facts):

if not self.facts:
watchers.ENGINE.warning("Declaring fact before reset()")
return self.__declare(*facts)
return self.__declare(*facts, idx=idx)
7 changes: 5 additions & 2 deletions experta/factlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _get_fact_id(fact):
for k, v in fact.items()
if not fact.is_special(k)])

def declare(self, fact):
def declare(self, fact, idx=None):
"""
Assert (in clips terminology) a fact.

Expand All @@ -73,7 +73,10 @@ def declare(self, fact):

if self.duplication or fact_id not in self.reference_counter:
# Assign the ID to the fact
idx = self.last_index
if idx is None:
idx = self.last_index
else:
idx = idx
fact.__factid__ = idx

# Insert the fact in the factlist
Expand Down