コード例 #1
0
ファイル: test_matcher.py プロジェクト: suparek/experta
def test_retematcher_changes_are_propagated(TestNode):
    from experta.engine import KnowledgeEngine
    from experta.fact import Fact
    from experta.matchers.rete import ReteMatcher
    from experta.matchers.rete.token import Token

    matcher = ReteMatcher(KnowledgeEngine())
    tn1 = TestNode()
    tn2 = TestNode()

    matcher.root_node.add_child(tn1, tn1.activate)
    matcher.root_node.add_child(tn2, tn2.activate)

    f1 = Fact(__factid__=1)
    f2 = Fact(__factid__=2)
    f3 = Fact(__factid__=3)
    f4 = Fact(__factid__=4)

    matcher.changes(adding=[f1, f2], deleting=[f3, f4])

    assert Token.valid(f1) in tn1.added
    assert Token.valid(f1) in tn2.added
    assert Token.valid(f2) in tn1.added
    assert Token.valid(f2) in tn2.added
    assert Token.invalid(f3) in tn1.added
    assert Token.invalid(f3) in tn2.added
    assert Token.invalid(f4) in tn1.added
    assert Token.invalid(f4) in tn2.added
コード例 #2
0
ファイル: test_fact.py プロジェクト: suparek/experta
def test_double_underscore_raise_on_declare():
    ke = KnowledgeEngine()
    ke.reset()

    ke.declare(Fact(__startwithdoubleunderscore__=True))

    with pytest.raises(KeyError):
        ke.declare(Fact(key__with__double__underscores=True))
コード例 #3
0
def test_conflictsetchange_valid_adds_to_memory():
    from experta.fact import Fact
    from experta.matchers.rete.nodes import ConflictSetNode
    from experta.matchers.rete.token import Token, TokenInfo
    from experta.rule import Rule

    csn = ConflictSetNode(Rule())

    f = Fact(test='data')
    f.__factid__ = 1

    csn.activate(Token.valid(f, {'mycontextdata': 'data'}))

    assert TokenInfo([f], {'mycontextdata': 'data'}) in csn.memory
コード例 #4
0
        def declare(self, r, fs):
            assume((r, frozenset(fs)) not in self.fss)
            self.fss.add((r, frozenset(fs)))

            fs = [Fact(i, __factid__=i) for i in fs]
            act = Activation(Rule(Fact(r)), facts=tuple(fs))

            # Update agenda
            self.strategy.update_agenda(self.agenda, [act], [])

            # Update model
            self.model |= set([act])

            return act
コード例 #5
0
ファイル: test_bus_node.py プロジェクト: suparek/experta
def test_busnode_remove(TestNode):
    from experta.matchers.rete.nodes import BusNode
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    bn = BusNode()
    tn1 = TestNode()
    tn2 = TestNode()

    bn.add_child(tn1, tn1.activate)
    bn.add_child(tn2, tn2.activate)

    bn.remove(Fact())

    assert tn1.added == [Token.invalid(Fact())]
    assert tn2.added == [Token.invalid(Fact())]
コード例 #6
0
ファイル: test_fact.py プロジェクト: suparek/experta
def test_fact_mix_positional_and_kw_index():
    f = Fact('x', 'y', 'z', a=1, b=2)

    assert f[0] == 'x'
    assert f[1] == 'y'
    assert f[2] == 'z'
    assert f['a'] == 1
    assert f['b'] == 2
コード例 #7
0
ファイル: test_fact.py プロジェクト: suparek/experta
def test_fact_setitem_do_raise_after_declare():
    f = Fact()
    ke = KnowledgeEngine()
    ke.reset()
    ke.declare(f)

    with pytest.raises(RuntimeError):
        f[0] = 1
コード例 #8
0
ファイル: test_matcher.py プロジェクト: suparek/experta
def test_retematcher_changes_return_activations_if_csn():
    from experta.engine import KnowledgeEngine
    from experta.fact import Fact
    from experta.rule import Rule
    from experta.activation import Activation
    from experta.matchers.rete.nodes import ConflictSetNode
    from experta.matchers.rete import ReteMatcher

    matcher = ReteMatcher(KnowledgeEngine())
    rule = Rule()
    csn = ConflictSetNode(rule)
    matcher.root_node.add_child(csn, csn.activate)

    added, removed = matcher.changes(
        adding=[Fact(__factid__=1), Fact(__factid__=2)])

    assert len(added) == 2
    assert all(isinstance(a, Activation) for a in added)
コード例 #9
0
def test_notnode_left_activate_valid_matching(TestNode):
    from experta.matchers.rete.nodes import NotNode
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    nn = NotNode(lambda l, r: True)
    nn.right_memory.append(Token.valid(Fact(test='data')).to_info())

    tn1 = TestNode()
    tn2 = TestNode()

    nn.add_child(tn1, tn1.activate)
    nn.add_child(tn2, tn2.activate)

    token = Token.valid(Fact(test='data'))
    nn.activate_left(token)

    assert not tn1.added
    assert not tn2.added
    assert nn.left_memory[token.to_info()] == 1
コード例 #10
0
ファイル: test_token.py プロジェクト: suparek/experta
def test_token_initialization_types():
    """
    Token.tag must be a Tag
    Token.data can be:
        - A Fact
        - An interable of Facts

    """
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    with pytest.raises(TypeError):
        Token(None, Fact())

    with pytest.raises(TypeError):
        Token(Token.TagType.VALID, None)

    with pytest.raises(TypeError):
        Token(Token.TagType.VALID, [Fact(), None])

    with pytest.raises(TypeError):
        Token(Token.TagType.VALID, [Fact()], [])

    # THIS MUST NOT RAISE
    Token(Token.TagType.VALID, Fact(), {})
    Token(Token.TagType.VALID, [Fact(), Fact()], {})
コード例 #11
0
def test_conflictsetchange_get_activations_data():
    from experta.matchers.rete.nodes import ConflictSetNode
    from experta.matchers.rete.token import Token
    from experta.rule import Rule
    from experta.fact import Fact

    rule = Rule()
    csn = ConflictSetNode(rule)

    f = Fact(first=1)
    f.__factid__ = 1

    csn.activate(Token.valid(f, {'data': 'test'}))

    added, removed = csn.get_activations()

    assert len(added) == 1
    assert len(removed) == 0

    assert list(added)[0].rule is rule
    assert f in list(added)[0].facts
    assert list(added)[0].context == {'data': 'test'}
コード例 #12
0
def test_featuretesternode_pass_fact_to_matcher():
    from experta.matchers.rete.nodes import FeatureTesterNode
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    fact = Fact(this_is_my_fact=True)

    def _matcher(f):
        nonlocal fact
        assert f is fact

    ftn = FeatureTesterNode(_matcher)

    ftn.activate(Token.valid(fact))
コード例 #13
0
def test_ordinarymatchnode_left_activate_invalid_remove_from_left_memory():
    from experta.matchers.rete.nodes import OrdinaryMatchNode
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    omn = OrdinaryMatchNode(lambda l, r: True)

    fact = Fact(test='data')
    token = Token.valid(fact)
    omn.left_memory.append(token.to_info())

    omn.activate_left(Token.invalid(fact))

    assert not omn.left_memory
コード例 #14
0
def test_ordinarymatchnode_left_activate_valid_store_in_left_memory():
    from experta.matchers.rete.nodes import OrdinaryMatchNode
    from experta.matchers.rete.token import Token, TokenInfo
    from experta.fact import Fact

    omn = OrdinaryMatchNode(lambda l, r: True)

    assert not omn.left_memory

    fact = Fact(test='data')
    token = Token.valid(fact)
    omn.activate_left(token)

    assert TokenInfo({fact}, {}) in omn.left_memory
コード例 #15
0
def test_featuretesternode_pass_when_callable_fail(TestNode):
    from experta.matchers.rete.nodes import FeatureTesterNode
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    ftn = FeatureTesterNode(lambda f: False)
    tn1 = TestNode()
    tn2 = TestNode()

    token = Token.valid(Fact(test=True))

    ftn.add_child(tn1, tn1.activate)
    ftn.add_child(tn2, tn2.activate)

    ftn.activate(token)

    assert tn1.added == tn2.added == []
コード例 #16
0
def test_featuretesternode_pass_when_callable_modify_context(TestNode):
    from experta.matchers.rete.nodes import FeatureTesterNode
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    ftn = FeatureTesterNode(lambda f: {'something': True})
    tn1 = TestNode()
    tn2 = TestNode()

    token = Token.valid(Fact(test=True), {'something': False})

    ftn.add_child(tn1, tn1.activate)
    ftn.add_child(tn2, tn2.activate)

    ftn.activate(token)

    newtoken = token.copy()
    newtoken.context['something'] = True

    assert tn1.added == tn2.added == []
コード例 #17
0
ファイル: test_token.py プロジェクト: suparek/experta
def test_token_initialization_conversions():
    """
    Token.data is modified in this way:
        - If is a Fact: Converted to a Set containing this Fact.
        - An interable of Facts: Converter to a set of Facts.

    """
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    t1 = Token(Token.TagType.VALID, Fact())
    assert t1.data == {Fact()}

    t2 = Token(Token.TagType.VALID, [Fact(a=1), Fact(b=2)])
    assert t2.data == {Fact(a=1), Fact(b=2)}
コード例 #18
0
ファイル: test_fact.py プロジェクト: suparek/experta
def test_fact_freeze_mutable_values():
    f = Fact([1, 2, 3])
    assert f[0] == (1, 2, 3)
コード例 #19
0
def execute():
    global first_base
    global second_base
    global third_base
    global image_baseball

    # Inference Starting
    engine.reset()
    engine.declare(Fact(outs=int(out.get())))
    engine.declare(Fact(strikes=int(strike.get())))
    engine.declare(Fact(balls=int(ball.get())))

    engine.declare(Fact(batter_order=int(order.get())))

    engine.declare(Fact(late_innings='yes' if int(inning.get()) > 6 else 'no'))

    engine.declare(Fact(our_scored=int(our_score.get())))
    engine.declare(Fact(rival_scored=int(rival_score.get())))

    engine.declare(Fact(batter_chased='no' if int(var.get()) == 1 else 'yes'))

    engine.declare(Fact(runner_1='yes' if first_base else 'no'))
    engine.declare(Fact(runner_2='yes' if second_base else 'no'))
    engine.declare(Fact(runner_3='yes' if third_base else 'no'))

    engine.declare(Fact(pitching_accuracy=int(pitcher_accu.get())))
    engine.run()

    # Get the result
    pitching, pitching_json = engine.printPitching()
    pitching_json = pitching_json[0]

    print(engine.facts)
    print("------------------------")
    print("Pitching Result:", pitching)
    # Update the GUI, Show the result

    # Text
    description.delete("1.0", "end")
    description.insert(END, pitching_json['description'])

    # Ball Position
    '''
    pitch_position = [[(30, 30), (45, 40), (125, 40), (210, 40), (220, 25)],
                      [(30, 120), (45, 120), (125, 125), (200, 120), (220, 120)],
                      [(35, 210), (45, 210), (125, 220), (210, 210), (220, 210)]]
    '''
    position = pitch_position[1][2]

    if pitching_json["zoom"] == "inside":
        if pitching_json["corner"] == "inside":
            if pitching_json["position"] == "high":
                position = pitch_position[0][3]
            elif pitching_json["position"] == "middle":
                position = pitch_position[1][3]
            elif pitching_json["position"] == "low":
                position = pitch_position[2][3]
        elif pitching_json["corner"] == "middle":
            if pitching_json["position"] == "high":
                position = pitch_position[0][2]
            elif pitching_json["position"] == "middle":
                position = pitch_position[1][2]
            elif pitching_json["position"] == "low":
                position = pitch_position[2][2]
        elif pitching_json["corner"] == "outside":
            if pitching_json["position"] == "high":
                position = pitch_position[0][1]
            elif pitching_json["position"] == "middle":
                position = pitch_position[1][1]
            elif pitching_json["position"] == "low":
                position = pitch_position[2][1]
    elif pitching_json["zoom"] == "outside":
        if pitching_json["corner"] == "inside":
            if pitching_json["position"] == "high":
                position = pitch_position[0][4]
            elif pitching_json["position"] == "middle":
                position = pitch_position[1][4]
            elif pitching_json["position"] == "low":
                position = pitch_position[2][4]
        elif pitching_json["corner"] == "middle":
            if pitching_json["position"] == "high":
                position = pitch_position[0][2]
            elif pitching_json["position"] == "middle":
                position = pitch_position[1][2]
            elif pitching_json["position"] == "low":
                position = pitch_position[2][2]
        elif pitching_json["corner"] == "outside":
            if pitching_json["position"] == "high":
                position = pitch_position[0][0]
            elif pitching_json["position"] == "middle":
                position = pitch_position[1][0]
            elif pitching_json["position"] == "low":
                position = pitch_position[2][0]

    image_zone.delete(image_baseball)
    image_baseball = image_zone.create_image(position, image=baseball2)
コード例 #20
0
ファイル: test_fact.py プロジェクト: suparek/experta
def test_fact_setitem_does_not_raise_before_declare():
    f = Fact()
    f[0] = 1

    assert f[0] == 1
コード例 #21
0
def test_ordinarymatchnode_left_activate_invalid_build_new_tokens(TestNode):
    from experta.matchers.rete.nodes import OrdinaryMatchNode
    from experta.matchers.rete.token import Token
    from experta.fact import Fact

    omn = OrdinaryMatchNode(lambda l, r: True)
    tn1 = TestNode()
    tn2 = TestNode()
    omn.add_child(tn1, tn1.activate)
    omn.add_child(tn2, tn2.activate)

    rt1 = Token.valid(Fact(rightdata='rightdata1'))
    rt2 = Token.valid(Fact(rightdata='rightdata2'))
    omn.right_memory.append(rt1.to_info())
    omn.right_memory.append(rt2.to_info())

    token = Token.invalid(Fact(leftdata='leftdata'))
    omn.activate_left(token)

    assert tn1.added == [
        Token.invalid(
            [Fact(leftdata='leftdata'),
             Fact(rightdata='rightdata1')]),
        Token.invalid(
            [Fact(leftdata='leftdata'),
             Fact(rightdata='rightdata2')])
    ]

    assert tn2.added == [
        Token.invalid(
            [Fact(leftdata='leftdata'),
             Fact(rightdata='rightdata1')]),
        Token.invalid(
            [Fact(leftdata='leftdata'),
             Fact(rightdata='rightdata2')])
    ]