Ejemplo n.º 1
0
def test_experta_skill_constructor():
    ke = get_KE_fixture()
    factory = ExpertaSkillFactory(ke)
    skill = factory.build(_condition=(ex.Fact(a=1), ex.Fact(b=2)),
                          _function=ke._._wrapped,
                          _name="_")
    assert isinstance(skill, Skill)
    r = factory.to_ex_rule(skill)
    assert r == ke._
Ejemplo n.º 2
0
    class KE(ex.KnowledgeEngine):
        activated = False

        def declare_fact(self, _a=1, _b=2):
            self.declare(ex.Fact(a=_a))
            self.declare(ex.Fact(b=_b))
            return self

        @ex.Rule(ex.Fact(a=1), ex.Fact(b=2))
        def _(self):
            self.activated = True
Ejemplo n.º 3
0
def test_experta_add_fact():
    """
    .. todo::
        - Add more complicated test of nested dicts/facts
    """
    wm = ExpertaWorkingMemory(get_KE_fixture())
    ke2 = get_KE_fixture()
    ke2.declare(ex.Fact(a=1))
    ke2.declare(ex.Fact(b=2))
    for i, fact in ke2.facts.items():
        wm.add_fact(i, fact)

    assert len(wm.facts) == 3
Ejemplo n.º 4
0
def apply_diff_to_fact(fact: ex.Fact, diff: dict) -> ex.Fact:
    if jsondiff.symbols.replace in diff:
        return ex.Fact(**diff[jsondiff.symbols.replace])

    new_fact = {}
    for k in fact:
        if (jsondiff.symbols.delete in diff
                and k in diff[jsondiff.symbols.delete]):
            continue
        new_fact[k] = fact[k]

    for k in diff:
        if k is not jsondiff.symbols.delete:
            new_fact[k] = diff[k]

    return ex.Fact(**new_fact)
Ejemplo n.º 5
0
 def r2c(c):
     if type(c) is tuple:
         return tuple(r2c(_) for _ in c)
     if isinstance(c, tuple):
         r = tuple(r2c(_) for _ in c)
         # print('==>', c.__class__, ' with args ', r, flush=True)
         return c.__class__(*r)
     if isinstance(c, dict):
         return ex.Fact(**c)
     if callable(c):
         return c
     assert False
Ejemplo n.º 6
0
def test_apply_diff_to_fact():
    s1 = {'a': 1, 'b': 1, 'c': 1}
    s2 = {'a': 1, 'b': 2, 'd': 1}
    diff = jsondiff.diff(s1, s2)

    f = ex.Fact(**s1)
    f2 = apply_diff_to_fact(f, diff)

    assert 'a' in f2
    assert 'b' in f2
    assert 'c' not in f2
    assert 'd' in f2
    assert f2['a'] == 1
    assert f2['b'] == 2
    assert f2['d'] == 1
Ejemplo n.º 7
0
 def add_fact(self, key: object, fact: dict) -> None:
     f = ex.Fact(**fact)
     self.ke.declare(f)
     self.lookup[key] = f
Ejemplo n.º 8
0
 def build(self, _dict: dict) -> ex.Fact:
     return ex.Fact(**dict)
Ejemplo n.º 9
0
 def declare_fact(self, _a=1, _b=2):
     self.declare(ex.Fact(a=_a))
     self.declare(ex.Fact(b=_b))
     return self