Fixed #48 : RelationalExpressionParser: Implement relational operator parser

Fixed #49 : ExpressionParser: Implement ExpressionParser
Fixed #50 : Implement ReteConditionExprVisitor
Fixed #51 : Implement PythonConditionExprVisitor
Fixed #52 : SheerkaConceptManager: I can get and set concept property
This commit is contained in:
2021-03-23 11:35:10 +01:00
parent f8e47e2b38
commit 6cda2686fb
25 changed files with 1083 additions and 978 deletions
+40 -6
View File
@@ -5,7 +5,7 @@ from core.builtin_concepts import BuiltinConcepts
from core.builtin_helpers import ensure_bnf
from core.concept import PROPERTIES_TO_SERIALIZE, Concept, DEFINITION_TYPE_DEF, get_concept_attrs, \
DEFINITION_TYPE_BNF
from core.global_symbols import NotInit, NotFound
from core.global_symbols import NotInit, NotFound, SyaAssociativity
from core.sheerka.services.SheerkaConceptManager import SheerkaConceptManager, NoModificationFound, ForbiddenAttribute, \
UnknownAttribute, CannotRemoveMeta, ValueNotFound, ConceptIsReferenced, NoFirstTokenError
from parsers.BnfNodeParser import Sequence, StrMatch, ConceptExpression, OrderedChoice, Optional, ZeroOrMore, OneOrMore, \
@@ -320,13 +320,13 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
def test_i_can_modify_add_a_property(self):
sheerka, context, one, foo = self.init_concepts("one", Concept("foo", props={BuiltinConcepts.ISA: {"value"}}))
res = sheerka.modify_concept(context, foo, to_add={"props": {BuiltinConcepts.ISA: "value2",
res = sheerka.modify_concept(context, foo, to_add={"props": {BuiltinConcepts.ISA: {"value2"},
BuiltinConcepts.HASA: one}})
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.NEW_CONCEPT)
assert res.body.body.get_prop(BuiltinConcepts.ISA) == {"value", "value2"}
assert res.body.body.get_prop(BuiltinConcepts.HASA) == {sheerka.new("one")}
assert res.body.body.get_prop(BuiltinConcepts.ISA) == {"value2"}
assert res.body.body.get_prop(BuiltinConcepts.HASA) == sheerka.new("one")
def test_i_can_modify_remove_a_property(self):
sheerka, context, foo = self.init_concepts(
@@ -382,7 +382,7 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
to_add = {"meta": {"body": "metadata value"},
"variables": {"var_name": "default value"},
"props": {BuiltinConcepts.ISA: bar}}
"props": {BuiltinConcepts.ISA: {bar}}}
res = sheerka.modify_concept(context, foo, to_add)
new_concept = res.body.body
@@ -656,7 +656,7 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
to_add = {
"meta": {"body": "a body"},
"props": {BuiltinConcepts.ISA: "bar"},
"props": {BuiltinConcepts.ISA: {"bar"}},
"variables": {"c": "value"}
}
to_remove = {
@@ -826,6 +826,40 @@ class TestSheerkaConceptManager(TestUsingMemoryBasedSheerka):
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
assert sheerka.get_attr(foo, prop) == [bar, baz, qux]
def test_i_can_get_and_set_property(self):
sheerka, context, foo, prop, bar = self.init_concepts("foo", "property", "bar")
foo_instance = sheerka.new(foo)
res = sheerka.set_property(context, foo_instance, prop, bar)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
assert sheerka.get_property(foo_instance, prop) == bar
res = sheerka.set_property(context, foo_instance, BuiltinConcepts.ASSOCIATIVITY, SyaAssociativity.Left)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
assert sheerka.get_property(foo_instance, BuiltinConcepts.ASSOCIATIVITY) == SyaAssociativity.Left
res = sheerka.set_property(context, foo_instance, sheerka.new(BuiltinConcepts.ASSOCIATIVITY), SyaAssociativity.Right)
assert res.status
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
assert sheerka.get_property(foo_instance, BuiltinConcepts.ASSOCIATIVITY) == SyaAssociativity.Right
# by default, the concept itself is not modified
new_foo = sheerka.new(foo)
not_found = sheerka.get_property(new_foo, prop)
assert sheerka.isinstance(not_found, BuiltinConcepts.NOT_FOUND)
assert not_found.body == {"#concept": new_foo, "#prop": prop}
# I can modify the concept itself
another_foo_instance = sheerka.new(foo)
res = sheerka.set_property(context, another_foo_instance, prop, bar, all_concepts=True)
assert res.status
assert sheerka.get_property(another_foo_instance, prop) == bar
new_foo = sheerka.new(foo)
assert sheerka.get_property(new_foo, prop) == bar
def test_i_cannot_remove_a_concept_which_has_reference(self):
sheerka, context, one, twenty_one = self.init_test().with_concepts(
Concept("one"),
+6 -3
View File
@@ -8,7 +8,7 @@ from core.rule import Rule, ACTION_TYPE_EXEC
from core.sheerka.Sheerka import RECOGNIZED_BY_ID, RECOGNIZED_BY_NAME
from core.sheerka.services.SheerkaEvaluateRules import SheerkaEvaluateRules, LOW_PRIORITY_RULES, DISABLED_RULES
from core.sheerka.services.SheerkaExecute import ParserInput
from core.sheerka.services.SheerkaRuleManager import RuleCompiledPredicate, SheerkaRuleManager
from core.sheerka.services.SheerkaRuleManager import SheerkaRuleManager, CompiledCondition
from evaluators.PythonEvaluator import PythonEvaluator, Expando
from parsers.PythonParser import PythonParser
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -39,6 +39,7 @@ class TestSheerkaEvaluateRules(TestUsingMemoryBasedSheerka):
DISABLED_RULES: [r7]
}
@pytest.mark.skip("Not ready for that")
def test_i_can_evaluate_question_concept_rules(self):
sheerka, context, concept, r1, r2, r3, r4, r5, r6, r7, r8, r9 = self.init_test().with_concepts(
Concept("x equals y", body="x == y", pre="is_question()").def_var("x").def_var("y"),
@@ -82,8 +83,8 @@ class TestSheerkaEvaluateRules(TestUsingMemoryBasedSheerka):
# create fake compiled predicates
parser = PythonParser()
my_rule.compiled_predicates = [
RuleCompiledPredicate("my rule", None, PythonEvaluator.NAME, parser.parse(context, ParserInput(exp)), None)
my_rule.compiled_conditions = [
CompiledCondition(PythonEvaluator.NAME, parser.parse(context, ParserInput(exp)), set(), set(), None)
for exp in predicates]
my_rule.metadata.is_compiled = True
my_rule.metadata.is_enabled = True
@@ -94,6 +95,7 @@ class TestSheerkaEvaluateRules(TestUsingMemoryBasedSheerka):
True: [my_rule],
}
@pytest.mark.skip("Not ready for that")
def test_i_can_evaluate_rules_when_concepts_are_questions(self):
sheerka, context, isa, cat, crocodile, pet, r1, r2, r3 = self.init_test().with_concepts(
Concept("x is a y", body="isa(x,y)", pre="is_question()").def_var("x").def_var("y"),
@@ -188,6 +190,7 @@ class TestSheerkaEvaluateRules(TestUsingMemoryBasedSheerka):
res = service.evaluate_rules(context, [rule], {"__ret": ret}, set())
assert res == {True: [rule]}
@pytest.mark.skip("Not ready for that")
def test_i_can_evaluate_concept_rules_when_same_name(self):
sheerka, context, g1, g2, rule = self.init_test().with_concepts(
Concept("greetings", definition="hello a", definition_type=DEFINITION_TYPE_DEF).def_var("a"),
File diff suppressed because it is too large Load Diff