Implemented a first and basic version of a Rete rule engine
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
import ast
|
||||
|
||||
import pytest
|
||||
import core.utils
|
||||
|
||||
from core.builtin_concepts import ParserResultConcept, BuiltinConcepts, ReturnValueConcept
|
||||
from core.concept import Concept
|
||||
from core.rule import Rule
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from core.tokenizer import Token, TokenKind, Tokenizer
|
||||
from parsers.BaseNodeParser import ConceptNode, UnrecognizedTokensNode, RuleNode
|
||||
from parsers.BaseNodeParser import ConceptNode, UnrecognizedTokensNode, RuleNode, SourceCodeNode
|
||||
from parsers.PythonParser import PythonNode
|
||||
from parsers.PythonWithConceptsParser import PythonWithConceptsParser
|
||||
from parsers.UnrecognizedNodeParser import UnrecognizedNodeParser
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
from tests.parsers.parsers_utils import get_source_code_node
|
||||
|
||||
unrecognized_nodes_parser = UnrecognizedNodeParser()
|
||||
|
||||
@@ -73,7 +73,8 @@ class TestPythonWithConceptsParser(TestUsingMemoryBasedSheerka):
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert wrapper.source == "foo + 1"
|
||||
assert isinstance(return_value, PythonNode)
|
||||
assert return_value.source == "foo + 1"
|
||||
assert return_value.source == "__C__foo__C__ + 1"
|
||||
assert return_value.original_source == "foo + 1"
|
||||
assert return_value.get_dump(return_value.ast_) == to_str_ast("__C__foo__C__ + 1")
|
||||
assert return_value.objects["__C__foo__C__"] == foo
|
||||
|
||||
@@ -93,7 +94,8 @@ class TestPythonWithConceptsParser(TestUsingMemoryBasedSheerka):
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert wrapper.source == "foo + 1"
|
||||
assert isinstance(return_value, PythonNode)
|
||||
assert return_value.source == "foo + 1"
|
||||
assert return_value.source == "__C__foo__1001__C__ + 1"
|
||||
assert return_value.original_source == "foo + 1"
|
||||
assert return_value.get_dump(return_value.ast_) == to_str_ast("__C__foo__1001__C__ + 1")
|
||||
assert return_value.objects["__C__foo__1001__C__"] == foo
|
||||
|
||||
@@ -124,7 +126,8 @@ class TestPythonWithConceptsParser(TestUsingMemoryBasedSheerka):
|
||||
assert context.sheerka.isinstance(parser_result, BuiltinConcepts.PARSER_RESULT)
|
||||
assert parser_result.source == "func(foo, bar)"
|
||||
assert isinstance(return_value, PythonNode)
|
||||
assert return_value.source == "func(foo, bar)"
|
||||
assert return_value.source == "func(__C__foo__1001__C__, __C__bar__1002__C__)"
|
||||
assert return_value.original_source == "func(foo, bar)"
|
||||
assert return_value.get_dump(return_value.ast_) == to_str_ast("func(__C__foo__1001__C__, __C__bar__1002__C__)")
|
||||
assert return_value.objects["__C__foo__1001__C__"] == foo
|
||||
assert return_value.objects["__C__bar__1002__C__"] == bar
|
||||
@@ -144,7 +147,8 @@ class TestPythonWithConceptsParser(TestUsingMemoryBasedSheerka):
|
||||
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert wrapper.source == "r:|rule_id: + 1"
|
||||
assert isinstance(return_value, PythonNode)
|
||||
assert return_value.source == "r:|rule_id: + 1"
|
||||
assert return_value.source == "__R____rule_id__R__ + 1"
|
||||
assert return_value.original_source == "r:|rule_id: + 1"
|
||||
assert return_value.get_dump(return_value.ast_) == to_str_ast("__R____rule_id__R__ + 1")
|
||||
assert return_value.objects["__R____rule_id__R__"] == rule
|
||||
|
||||
@@ -193,3 +197,26 @@ class TestPythonWithConceptsParser(TestUsingMemoryBasedSheerka):
|
||||
|
||||
assert not result.status
|
||||
assert context.sheerka.isinstance(result.value, BuiltinConcepts.NOT_FOR_ME)
|
||||
|
||||
def test_i_can_parse_nodes_when_source_code_node(self):
|
||||
context = self.get_context()
|
||||
foo = Concept("foo")
|
||||
bar = Concept("bar")
|
||||
|
||||
nodes = [
|
||||
UnrecognizedTokensNode(0, 1, list(Tokenizer("not ", yield_eof=False))),
|
||||
get_source_code_node(2, "foo == 1", {"foo": foo}),
|
||||
UnrecognizedTokensNode(7, 9, list(Tokenizer(" and ", yield_eof=False))),
|
||||
get_source_code_node(10, "bar < 1", {"bar": bar}),
|
||||
]
|
||||
expected_ast = ast.parse('not __C__foo__C__ == 1 and __C__bar__C__ < 1', "<source>", 'eval')
|
||||
|
||||
parser = PythonWithConceptsParser()
|
||||
result = parser.parse_nodes(context, nodes)
|
||||
|
||||
result_python_node = result.value.value
|
||||
assert isinstance(result_python_node, PythonNode)
|
||||
assert result_python_node.source == 'not __C__foo__C__ == 1 and __C__bar__C__ < 1'
|
||||
assert result_python_node.ast_str == PythonNode.get_dump(expected_ast)
|
||||
assert result_python_node.original_source == "not foo == 1 and bar < 1"
|
||||
assert result_python_node.objects == {"__C__foo__C__": foo, "__C__bar__C__": bar}
|
||||
|
||||
Reference in New Issue
Block a user