436 lines
15 KiB
Python
436 lines
15 KiB
Python
from core.builtin_concepts_ids import BuiltinConcepts
|
|
from core.concept import Concept, ConceptParts
|
|
from core.global_symbols import ErrorObj
|
|
|
|
|
|
class UserInputConcept(Concept):
|
|
ALL_ATTRIBUTES = ["text", "user_name"]
|
|
|
|
def __init__(self, text=None, user_name=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.USER_INPUT,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.USER_INPUT, bound_body="text")
|
|
self.set_value("text", text)
|
|
self.set_value("user_name", user_name)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"({self.id}){self.name}: '{self.body}'"
|
|
|
|
|
|
class ErrorConcept(Concept, ErrorObj):
|
|
ALL_ATTRIBUTES = ["error"]
|
|
|
|
def __init__(self, error=None, concept_id=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.ERROR,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.ERROR,
|
|
id=concept_id,
|
|
bound_body="error")
|
|
self.set_value("error", error)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"({self.id}){self.name}: {self.body}"
|
|
|
|
|
|
class UnknownConcept(Concept, ErrorObj):
|
|
ALL_ATTRIBUTES = ["concept_ref"]
|
|
|
|
def __init__(self, concept_ref=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.UNKNOWN_CONCEPT,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.UNKNOWN_CONCEPT, bound_body="concept_ref")
|
|
self.set_value("concept_ref", concept_ref)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"({self.id}){self.name}: {self.body}"
|
|
|
|
|
|
class ReturnValueConcept(Concept):
|
|
"""
|
|
This class represents the result of a data flow processing
|
|
It's the main input for the evaluators
|
|
"""
|
|
|
|
ALL_ATTRIBUTES = ["who", "status", "value", "parents"]
|
|
|
|
def __init__(self, who=None, status=None, value=None, parents=None, concept_id=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.RETURN_VALUE,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.RETURN_VALUE,
|
|
id=concept_id,
|
|
bound_body="value")
|
|
self.set_value("who", who)
|
|
self.set_value("status", status)
|
|
self.set_value("value", value)
|
|
self.set_value("parents", parents)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"ReturnValue(who={self.who}, status={self.status}, value={self.value})"
|
|
|
|
def __eq__(self, other):
|
|
if id(self) == id(other):
|
|
return True
|
|
|
|
if not isinstance(other, ReturnValueConcept):
|
|
return False
|
|
|
|
return self.who == other.who and \
|
|
self.status == other.status and \
|
|
self.value == other.value
|
|
|
|
def __hash__(self):
|
|
if hasattr(self.value, "__iter__") and not isinstance(self.value, str):
|
|
value_hash = hash(tuple(self.value))
|
|
else:
|
|
value_hash = hash(self.value)
|
|
|
|
return hash((self.who, self.status, value_hash))
|
|
|
|
|
|
class UnknownPropertyConcept(Concept, ErrorObj):
|
|
"""
|
|
This error is raised when, during sheerka.new(), an unknown property is asked
|
|
"""
|
|
ALL_ATTRIBUTES = ["property_name", "concept"]
|
|
|
|
def __init__(self, property_name=None, concept=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.UNKNOWN_PROPERTY,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.UNKNOWN_PROPERTY,
|
|
bound_body="property_name")
|
|
self.set_value("property_name", property_name)
|
|
self.set_value("concept", concept)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"UnknownProperty(property={self.property_name}, concept={self.concept})"
|
|
|
|
|
|
class ParserResultConcept(Concept):
|
|
"""
|
|
Result of a parsing
|
|
"""
|
|
|
|
ALL_ATTRIBUTES = ["parser", "source", "tokens", "value", "try_parsed"]
|
|
|
|
def __init__(self, parser=None, source=None, tokens=None, value=None, try_parsed=None, concept_id=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.PARSER_RESULT,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.PARSER_RESULT,
|
|
id=concept_id,
|
|
bound_body="value")
|
|
self.set_value("parser", parser)
|
|
self.set_value("source", source)
|
|
self.set_value("tokens", tokens)
|
|
self.set_value("value", value)
|
|
self.set_value("try_parsed", try_parsed)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
text = f"ParserResult(parser={self.parser}"
|
|
text += f", source='{self.source}')" if self.source else f", body='{self.value}')"
|
|
return text
|
|
|
|
def __eq__(self, other):
|
|
if not isinstance(other, ParserResultConcept):
|
|
return False
|
|
|
|
self_parser_name = self.get_parser_name(self.parser)
|
|
other_parser_name = self.get_parser_name(other.parser)
|
|
|
|
return self.source == other.source and \
|
|
self_parser_name == other_parser_name and \
|
|
self.value == other.value
|
|
|
|
def __hash__(self):
|
|
return hash(self._metadata.name)
|
|
|
|
@staticmethod
|
|
def get_parser_name(parser):
|
|
from parsers.BaseParser import BaseParser
|
|
return parser.name if isinstance(parser, BaseParser) else str(parser)
|
|
|
|
|
|
class RuleEvaluationResultConcept(Concept):
|
|
"""
|
|
Result of the evaluation of a rule, using the Rete algorithm
|
|
"""
|
|
|
|
ALL_ATTRIBUTES = ["rule"]
|
|
|
|
def __init__(self, rule=None, concept_id=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.RULE_EVALUATION_RESULT,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.RULE_EVALUATION_RESULT,
|
|
id=concept_id,
|
|
bound_body="rule")
|
|
self.set_value("rule", rule)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"RuleEvaluationResult(rule={self.rule})"
|
|
|
|
def __eq__(self, other):
|
|
if not isinstance(other, RuleEvaluationResultConcept):
|
|
return False
|
|
|
|
return self.rule == other.rule
|
|
|
|
def __hash__(self):
|
|
return hash((self._metadata.name, self.rule))
|
|
|
|
|
|
class InvalidReturnValueConcept(Concept, ErrorObj):
|
|
"""
|
|
Error returned when an evaluator is not correctly coded
|
|
The accepted return value are
|
|
ReturnValueConcept, list of ReturnValueConcept or None
|
|
"""
|
|
|
|
ALL_ATTRIBUTES = ["return_value", "evaluator"]
|
|
|
|
def __init__(self, return_value=None, evaluator=None):
|
|
super().__init__(
|
|
BuiltinConcepts.INVALID_RETURN_VALUE,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.INVALID_RETURN_VALUE,
|
|
bound_body="return_value")
|
|
self.set_value("return_value", return_value)
|
|
self.set_value("evaluator", evaluator)
|
|
self._metadata.is_evaluated = True
|
|
|
|
|
|
class ConceptEvalError(Concept, ErrorObj):
|
|
ALL_ATTRIBUTES = ["error", "concept", "property_name"]
|
|
|
|
def __init__(self, error=None, concept=None, property_name=None):
|
|
super().__init__(BuiltinConcepts.CONCEPT_EVAL_ERROR,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.CONCEPT_EVAL_ERROR,
|
|
bound_body="error")
|
|
self.set_value("error", error)
|
|
self.set_value("concept", concept)
|
|
self.set_value("property_name", property_name)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"ConceptEvalError(error={self.error}, concept={self.concept}, property={self.property_name})"
|
|
|
|
|
|
class ListConcept(Concept):
|
|
ALL_ATTRIBUTES = ["items"]
|
|
|
|
def __init__(self, items=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.LIST,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.LIST,
|
|
bound_body="items")
|
|
self.set_value("items", items or [])
|
|
self._metadata.is_evaluated = True
|
|
|
|
def append(self, obj):
|
|
self.body.append(obj)
|
|
|
|
|
|
class FilteredConcept(Concept):
|
|
ALL_ATTRIBUTES = ["filtered", "iterable", "predicate"]
|
|
|
|
def __init__(self, filtered=None, iterable=None, predicate=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.FILTERED,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.FILTERED,
|
|
bound_body="filtered")
|
|
self.set_value("filtered", filtered)
|
|
self.set_value("iterable", iterable)
|
|
self.set_value("predicate", predicate)
|
|
self._metadata.is_evaluated = True
|
|
|
|
|
|
class ConceptAlreadyInSet(Concept, ErrorObj):
|
|
ALL_ATTRIBUTES = ["concept", "concept_set"]
|
|
|
|
def __init__(self, concept=None, concept_set=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.CONCEPT_ALREADY_IN_SET,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.CONCEPT_ALREADY_IN_SET,
|
|
bound_body="concept")
|
|
self.set_value("concept", concept)
|
|
self.set_value("concept_set", concept_set)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"ConceptAlreadyInSet(concept={self.concept}, concept_set={self.concept_set})"
|
|
|
|
|
|
class PropertyAlreadyDefined(Concept, ErrorObj):
|
|
ALL_ATTRIBUTES = ["property_name", "property_value", "concept"]
|
|
|
|
def __init__(self, property_name=None, property_value=None, concept=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.PROPERTY_ALREADY_DEFINED,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.PROPERTY_ALREADY_DEFINED,
|
|
bound_body="property_name")
|
|
self.set_value("property_name", property_name)
|
|
self.set_value("property_value", property_value)
|
|
self.set_value("concept", concept)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"PropertyAlreadyDefined(property={self.property_name}, value={self.property_value}, concept={self.concept})"
|
|
|
|
|
|
class ConditionFailed(Concept, ErrorObj):
|
|
ALL_ATTRIBUTES = ["condition", "concept", "prop", "reason"]
|
|
|
|
def __init__(self, condition=None, concept=None, prop=None, reason=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.CONDITION_FAILED,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.CONDITION_FAILED,
|
|
bound_body="condition")
|
|
self.set_value("condition", condition)
|
|
self.set_value("concept", concept)
|
|
self.set_value("prop", prop)
|
|
self.set_value("reason", reason)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"ConditionFailed(condition='{self.body}', concept='{self.concept}', prop='{self.prop}')"
|
|
|
|
|
|
class NotForMeConcept(Concept): # Not considered as an error ?
|
|
ALL_ATTRIBUTES = ["source", "reason"]
|
|
|
|
def __init__(self, source=None, reason=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.NOT_FOR_ME,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.NOT_FOR_ME,
|
|
bound_body="source")
|
|
self.set_value("source", source)
|
|
self.set_value("reason", reason)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
return f"NotForMeConcept(source={self.body}, reason={self.get_value('reason')})"
|
|
|
|
|
|
class ExplanationConcept(Concept):
|
|
ALL_ATTRIBUTES = ["digest", "command", "title", "instructions", "execution_result"]
|
|
|
|
def __init__(self, digest=None, command=None, title=None, instructions=None, execution_result=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.EXPLANATION,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.EXPLANATION,
|
|
bound_body="execution_result")
|
|
self.set_value("digest", digest) # event digest
|
|
self.set_value("command", command) # explain command parameters
|
|
self.set_value("title", title) # a title to the explanation
|
|
self.set_value("instructions", instructions) # instructions for SheerkaPrint
|
|
self.set_value("execution_result", execution_result) # list of results
|
|
self._metadata.is_evaluated = True
|
|
|
|
|
|
class PythonSecurityError(Concept, ErrorObj):
|
|
ALL_ATTRIBUTES = ["prop", "source_code", "source", "line", "column"]
|
|
|
|
def __init__(self, prop=None, source_code=None, source=None, line=None, column=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.PYTHON_SECURITY_ERROR,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.PYTHON_SECURITY_ERROR,
|
|
bound_body="source_code")
|
|
|
|
self.set_value("prop", prop) # property or variable that was evaluated
|
|
self.set_value("source", source) # origin of the source code (eg. file name)
|
|
self.set_value("line", line) # line number
|
|
self.set_value("column", column) # column number
|
|
self.set_value("source_code", source_code) # code being executed
|
|
self._metadata.is_evaluated = True
|
|
|
|
|
|
class NotFoundConcept(Concept, ErrorObj):
|
|
ALL_ATTRIBUTES = []
|
|
|
|
def __init__(self, body=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.NOT_FOUND,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.NOT_FOUND)
|
|
self.set_value(ConceptParts.BODY, body)
|
|
|
|
def __repr__(self):
|
|
return f"({self._metadata.id}){self._metadata.name}, body={self.get_value(ConceptParts.BODY)}"
|
|
|
|
|
|
class ToListConcept(Concept):
|
|
ALL_ATTRIBUTES = ["items", "recursion_depth", "recurse_on", "tab"]
|
|
|
|
def __init__(self, items=None, recursion_depth=None, recurse_on=None, tab=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.TO_LIST,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.TO_LIST,
|
|
bound_body="items")
|
|
self.set_value("items", items) # items to display
|
|
self.set_value("recursion_depth", recursion_depth) # recursion depth when showing children
|
|
self.set_value("recurse_on", recurse_on) # which sub items should we display
|
|
self.set_value("tab", tab) # customise tab (content and length)
|
|
self._metadata.is_evaluated = True
|
|
|
|
|
|
class NewConceptConcept(Concept):
|
|
ALL_ATTRIBUTES = ["concept"]
|
|
|
|
def __init__(self, concept=None):
|
|
Concept.__init__(self,
|
|
BuiltinConcepts.NEW_CONCEPT,
|
|
True,
|
|
False,
|
|
BuiltinConcepts.NEW_CONCEPT,
|
|
bound_body="concept")
|
|
|
|
self.set_value("concept", concept)
|
|
self._metadata.is_evaluated = True
|
|
|
|
def __repr__(self):
|
|
if self.concept:
|
|
return f"NewConcept(concept={self.concept}, key='{self.concept.key}')"
|
|
else:
|
|
return super().__repr__()
|