Fixed #49 : ExpressionParser: Implement ExpressionParser
This commit is contained in:
+22
-14
@@ -230,10 +230,17 @@ class ReteNetwork:
|
||||
for cond in conditions:
|
||||
# update requested attributes for a fact
|
||||
if isinstance(cond, Condition):
|
||||
# Manage list of requested attributes
|
||||
|
||||
# Manage list of requested attributes when using __name__ indirection
|
||||
if isinstance(cond.identifier, V) and cond.attribute == "__name__":
|
||||
vars_ids_mappings[cond.identifier] = cond.value
|
||||
|
||||
# Manage list of requested attributes when bounding a new variable
|
||||
if (cond.identifier in vars_ids_mappings and
|
||||
isinstance(cond.attribute, str) and
|
||||
isinstance(cond.value, V)):
|
||||
vars_ids_mappings[cond.value] = f"{vars_ids_mappings[cond.identifier]}.{cond.attribute}"
|
||||
|
||||
identifier = vars_ids_mappings[cond.identifier] if cond.identifier in vars_ids_mappings else \
|
||||
cond.identifier if not isinstance(cond.identifier, V) else \
|
||||
None
|
||||
@@ -365,22 +372,23 @@ class ReteNetwork:
|
||||
for wme in to_remove:
|
||||
self.remove_wme(wme)
|
||||
|
||||
def add_obj(self, name, obj, use_bag=False, root=True):
|
||||
def add_obj(self, name, obj, fact_id=None, use_bag=False):
|
||||
"""
|
||||
Adds a new object to the working memory
|
||||
"""
|
||||
|
||||
def inner_add_vme(ident, attr, val):
|
||||
if val is NotInit:
|
||||
def inner_add_vme(name_, fact_id_, attr_, value_):
|
||||
if value_ is NotInit:
|
||||
pass
|
||||
elif attr != "self" and isinstance(val, Concept):
|
||||
new_name = f"{ident}.{attr}"
|
||||
self.add_wme(WME(ident, attr, new_name))
|
||||
self.add_obj(new_name, val, use_bag=True, root=False)
|
||||
elif attr_ != "self" and isinstance(value_, Concept):
|
||||
new_name = f"{name_}.{attr_}"
|
||||
new_fact_id = f"{fact_id_}.{attr_}"
|
||||
self.add_wme(WME(fact_id_, attr_, new_fact_id))
|
||||
self.add_obj(new_name, value_, new_fact_id)
|
||||
else:
|
||||
self.add_wme(WME(ident, attr, val))
|
||||
self.add_wme(WME(fact_id_, attr_, value_))
|
||||
|
||||
if root:
|
||||
if fact_id is None:
|
||||
if hasattr(obj, FACT_ID):
|
||||
raise ValueError("Object already has an id, cannot add")
|
||||
|
||||
@@ -388,8 +396,6 @@ class ReteNetwork:
|
||||
setattr(obj, FACT_ID, fact_id)
|
||||
self.facts[fact_id] = obj
|
||||
self.fact_counter += 1
|
||||
else:
|
||||
fact_id = name
|
||||
|
||||
requested_attributes = "*" if use_bag else \
|
||||
self.attributes_by_id[name] if name in self.attributes_by_id else \
|
||||
@@ -399,13 +405,15 @@ class ReteNetwork:
|
||||
if attribute == "*":
|
||||
bag = as_bag(obj)
|
||||
for k, v in bag.items():
|
||||
inner_add_vme(fact_id, k, v)
|
||||
inner_add_vme(name, fact_id, k, v)
|
||||
elif attribute == "__name__":
|
||||
self.add_wme(WME(fact_id, "__name__", name))
|
||||
elif attribute == "__is_concept__":
|
||||
self.add_wme(WME(fact_id, "__is_concept__", isinstance(obj, Concept)))
|
||||
else:
|
||||
try:
|
||||
value = getattr(obj, attribute)
|
||||
inner_add_vme(fact_id, attribute, value)
|
||||
inner_add_vme(name, fact_id, attribute, value)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user