Enhanced complex concepts handling

This commit is contained in:
2020-01-11 08:03:35 +01:00
parent a62c1f0f13
commit 40416ac337
24 changed files with 1647 additions and 961 deletions
+10 -1
View File
@@ -129,6 +129,15 @@ class Concept:
def __hash__(self):
return hash(self.metadata.name)
def __getattr__(self, item):
# I have this complicated implementation because of the usage of Pickle
if 'props' in vars(self) and item in self.props:
return self.props[item].value
name = self.name if 'metadata' in vars(self) else 'Concept'
raise AttributeError(f"'{name}' concept has no attribute '{item}'")
@property
def name(self):
return self.metadata.name
@@ -166,7 +175,7 @@ class Concept:
if token.type == TokenKind.WHITESPACE:
continue
if not first:
key += " "
key += " " # spaces are normalized
if variables is not None and token.value in variables:
key += VARIABLE_PREFIX + str(variables.index(token.value))
else:
+21 -10
View File
@@ -735,7 +735,12 @@ class Sheerka(Concept):
:param kwargs:
:return:
"""
template = self.get(concept_key)
if isinstance(concept_key, tuple):
concept_key, concept_id = concept_key[0], concept_key[1]
else:
concept_id = None
template = self.get(concept_key, concept_id)
# manage concept not found
if self.isinstance(template, BuiltinConcepts.UNKNOWN_CONCEPT) and \
@@ -747,7 +752,7 @@ class Sheerka(Concept):
# if template is a list, it means that there a multiple concepts under the same key
concepts = [self.new_from_template(t, concept_key, **kwargs) for t in template]
return self.new(BuiltinConcepts.ENUMERATION, body=concepts)
return concepts
def new_from_template(self, template, key, **kwargs):
# manage singleton
@@ -759,15 +764,15 @@ class Sheerka(Concept):
concept.update_from(template)
# update the properties
for key, v in kwargs.items():
if key in concept.props:
concept.set_prop(key, v)
elif key in PROPERTIES_FOR_NEW:
setattr(concept.metadata, key, v)
elif hasattr(concept, key):
setattr(concept, key, v)
for k, v in kwargs.items():
if k in concept.props:
concept.set_prop(k, v)
elif k in PROPERTIES_FOR_NEW:
setattr(concept.metadata, k, v)
elif hasattr(concept, k):
setattr(concept, k, v)
else:
return self.new(BuiltinConcepts.UNKNOWN_PROPERTY, body=key, concept=concept)
return self.new(BuiltinConcepts.UNKNOWN_PROPERTY, body=k, concept=concept)
# TODO : add the concept to the list of known concepts (self.instances)
return concept
@@ -830,6 +835,12 @@ class Sheerka(Concept):
return obj
def is_unknown(self, obj):
if not isinstance(obj, Concept):
return True
return obj.key == BuiltinConcepts.UNKNOWN_CONCEPT
def isinstance(self, a, b):
"""
return true if the concept a is an instance of the concept b