def test_symbolpredicateunifier_with_subfields(self): spu = SymbolPredicateUnifier() class CT(ComplexTerm): a = IntegerField b = StringField(index=True) c = (IntegerField(index=True),ConstantField) @spu.register class P(Predicate): d = CT.Field(index=True) e = CT.Field() expected=set([hashable_path(P.d), hashable_path(P.d.b), hashable_path(P.d.c.arg1), hashable_path(P.e.b), hashable_path(P.e.c.arg1)]) self.assertEqual(spu.predicates, (P,)) self.assertEqual(set([hashable_path(p) for p in spu.indexes]), set(expected)) ct_func=Function("ct",[Number(1),String("aaa"), Function("",[Number(1),Function("const",[])])]) p1=Function("p",[ct_func,ct_func]) fb=spu.unify(symbols=[p1],raise_on_empty=True) self.assertEqual(len(fb),1) self.assertEqual(set([hashable_path(p) for p in fb.indexes]), expected)
def test_symbolpredicateunifier_symbols(self): class Afact(Predicate): num1=IntegerField() num2=IntegerField() str1=StringField() class Bfact(Predicate): num1=IntegerField() str1=StringField() class Cfact(Predicate): num1=IntegerField() af1 = Afact(1,10,"bbb") af2 = Afact(2,20,"aaa") af3 = Afact(3,20,"aaa") bf1 = Bfact(1,"aaa") bf2 = Bfact(2,"bbb") cf1 = Cfact(1) raws = [ Function("afact",[Number(1), Number(10), String("bbb")]), Function("afact",[Number(2), Number(20), String("aaa")]), Function("afact",[Number(3), Number(20), String("aaa")]), Function("bfact",[Number(1),String("aaa")]), Function("bfact",[Number(2),String("bbb")]), Function("cfact",[Number(1)]) ] spu = SymbolPredicateUnifier(predicates=[Afact,Bfact,Cfact]) # Test the different ways that facts can be added fb = spu.unify(symbols=raws) self.assertFalse(fb._delayed_init) self.assertEqual(set(fb.predicates), set([Afact,Bfact,Cfact])) s_af_all = fb.query(Afact) self.assertEqual(set(s_af_all.all()), set([af1,af2,af3])) fb = spu.unify(symbols=raws, delayed_init=True) self.assertTrue(fb._delayed_init) self.assertEqual(set(fb.predicates), set([Afact,Bfact,Cfact])) s_af_all = fb.query(Afact) self.assertEqual(set(s_af_all.all()), set([af1,af2,af3])) fb = FactBase() fb.add([af1,af2,af3]) #### self.assertEqual(fb.add([af1,af2,af3]),3) s_af_all = fb.query(Afact) self.assertEqual(set(s_af_all.all()), set([af1,af2,af3])) fb = FactBase() fb.add(af1) fb.add(af2) fb.add(af3) #### self.assertEqual(fb.add(af1),1) #### self.assertEqual(fb.add(af2),1) #### self.assertEqual(fb.add(af3),1) s_af_all = fb.query(Afact) self.assertEqual(set(s_af_all.all()), set([af1,af2,af3])) # Test that adding symbols can handle symbols that don't unify fb = spu.unify(symbols=raws) s_af_all = fb.query(Afact) self.assertEqual(set(s_af_all.all()), set([af1,af2,af3])) return # Test the specification of indexes class MyFactBase3(FactBase): predicates = [Afact, Bfact] spu = SymbolPredicateUnifier(predicates=[Afact,Bfact,Cfact], indexes=[Afact.num1, Bfact.num1]) fb = spu.unify(symbols=raws) s = fb.query(Afact).where(Afact.num1 == 1) self.assertEqual(s.get_unique(), af1) s = fb.query(Bfact).where(Bfact.num1 == 1) self.assertEqual(s.get_unique(), bf1)