Пример #1
0
 def setUp(self):
   self.v, self.a = VariableFactory(), FamilyMemberFactory()
   self.kb = KnowledgeBase()
   v, a = self.v, self.a
   self.father = RuleBasedPredicate('father', (FamilyMember, FamilyMember))
   self.mother = RuleBasedPredicate('mother', (FamilyMember, FamilyMember))
   self.kb.add_all([self.mother, self.father])
Пример #2
0
def __define_algorithmetic_predicates():
  from trimlogic.term import VariableFactory
  from trimlogic.predicate import CutPredicate, FailPredicate, RuleBasedPredicate
  global fail, cut, neg, eql
  v = VariableFactory()
  cut = CutPredicate()
  fail = FailPredicate()
  neg = RuleBasedPredicate('neg')
  neg.add_rule( Head=( v.Goal, ),
               Body=( v.Goal, cut, fail ) )
  neg.add_rule( Head=( v.Goal, ) )
  eql = RuleBasedPredicate('eql')
  eql.add_rule( Head=( v.X, v.X ) )
Пример #3
0
 def testAncestor(self):
   v, a = self.v, self.a
   self.loadFamilyTree1()
   ancestor = RuleBasedPredicate('ancestor', (FamilyMember, FamilyMember))
   self.kb.add(ancestor)
   positive_tuples = [[a.jane, a.ann],
                               [a.tim, a.tom],
                               [a.tipsy, a.billy],
                               [a.jannet, a.billy],
                               [a.joan, a.jane],
                               [a.rebecca, a.ann],
                               [a.frank, a.jane],
                               [a.jan, a.ann],
                               [a.jill, a.ann]]
   negative_tuples = [[a.tim, a.ann],
                               [a.ann, a.billy],
                               [a.jane, a.frank],
                               [a.tom, a.debrah],
                               [a.tim, a.tammy],
                               [a.tom, a.george],
                               [a.jane, a.joan]]
   foil(ancestor, positive_tuples, negative_tuples, self.kb, ordering=None)
   self.assertFollows(ancestor(a.bob, a.jane))
   self.assertNotFollows(ancestor(a.bob, a.ian))
   self.kb.remove(ancestor)
   self.print_rules(ancestor)
Пример #4
0
 def testFindRecursiveRules(self):
   from trimlogic.predicate import Rule, MutableRule
   from trimlogic.foil import will_halt
   v, a = self.v, self.a
   self.loadFamilyTree1()
   mother = self.mother
   predicate = RuleBasedPredicate('ancestor', (FamilyMember, FamilyMember))
   predicate.rules.append( Rule( predicate, (v.X, v.Y), (mother(v.X, v.Y),) ) )
   current_rule = MutableRule( predicate, (v.X, v.Y), (mother(v.Z, v.Y),) )
   predicate.rules.append(current_rule)
   recursive_literal = predicate(v.X, v.Z)
   ordering = find_partial_ordering_of_terms(current_rule)
   self.assertTrue(will_halt(predicate, recursive_literal, [v.X, v.Y], ordering))
Пример #5
0
 def testGrandparent(self):
   import sys
   v, a = self.v, self.a
   self.loadFamilyTree1()
   grandfather = RuleBasedPredicate('grandfather', (FamilyMember, FamilyMember))
   grandfather.arity = 2
   self.kb.add(grandfather)
   positive_tuples = [[a.frank, a.sean],
                               [a.tom, a.billy],
                               [a.george, a.jan],
                               [a.bob, a.jane],
                               [a.sean, a.ann],
                               [a.frank, a.sean]]
   negative_tuples = [[a.jannet, a.tim], 
                               [a.jane, a.alan],
                               [a.rebecca, a.sean],
                               [a.jan, a.ann]]
   foil(grandfather, positive_tuples, negative_tuples, self.kb, ordering=None)
   self.assertFollows(grandfather(a.george, a.tom))
   self.assertFollows(grandfather(a.alan, a.jane))
   self.assertNotFollows(grandfather(a.tipsy, a.billy))
   self.assertNotFollows(grandfather(a.bob, a.ian))
   self.kb.remove(grandfather)
   self.print_rules(grandfather)
Пример #6
0
 def testGrandparent(self):
     import sys
     v, a = self.v, self.a
     self.loadFamilyTree1()
     grandfather = RuleBasedPredicate('grandfather',
                                      (FamilyMember, FamilyMember))
     grandfather.arity = 2
     self.kb.add(grandfather)
     positive_tuples = [[a.frank, a.sean], [a.tom, a.billy],
                        [a.george, a.jan], [a.bob, a.jane], [a.sean, a.ann],
                        [a.frank, a.sean]]
     negative_tuples = [[a.jannet, a.tim], [a.jane, a.alan],
                        [a.rebecca, a.sean], [a.jan, a.ann]]
     foil(grandfather,
          positive_tuples,
          negative_tuples,
          self.kb,
          ordering=None)
     self.assertFollows(grandfather(a.george, a.tom))
     self.assertFollows(grandfather(a.alan, a.jane))
     self.assertNotFollows(grandfather(a.tipsy, a.billy))
     self.assertNotFollows(grandfather(a.bob, a.ian))
     self.kb.remove(grandfather)
     self.print_rules(grandfather)
Пример #7
0
 def testMemberPredicate(self):
     print ""
     v, a = VariableFactory(), AtomFactory()
     kb = KnowledgeBase()
     kb.add(components)
     member = RuleBasedPredicate("member", (Term, dot))
     kb.add(member)
     positive_tuples = ((1, plist([1, 2, 3, 4])), (4, plist([1, 2, 3, 4])),
                        (5, plist([5, 4, 3, 2])), (2, plist([1, 2, 3, 4])),
                        (3, plist([1, 2, 3, 4])))
     negative_tuples = ((1, plist([2, 3, 4, 5])), (2, plist([1, 3, 4, 5])),
                        (3, plist([1, 2, 4, 5])), (4, plist([1, 2, 3, 5])),
                        (5, plist([1, 2, 3,
                                   4])), (plist([1, 2]), plist([1, 2, 3,
                                                                4])))
     foil(member, positive_tuples, negative_tuples, kb, ordering=None)
     self.assertFollows(member(11, plist([2, 3, 5, 7, 11, 13])))
     self.assertNotFollows(member(12, plist([2, 3, 5, 7, 11, 13])))
     self.print_rules(member)
Пример #8
0
def __define_list_predicates():
  import operator
  from trimlogic.term import VariableFactory, Term
  from trimlogic.predicate import RuleBasedPredicate, ListPredicate
  global dot, car, cdr, cons, append, reverse, components
  
  v = VariableFactory()
  dot = ListPredicate('.')
  
  car = RuleBasedPredicate('car')
  car.add_rule( Head=( dot(v.H, v.T), v.H ) )
  
  cdr = RuleBasedPredicate('cdr')
  cdr.add_rule( Head=( dot(v.H, v.T), v.T ) )
  
  cons = RuleBasedPredicate('cons')
  cons.add_rule( Head=( v.H, dot(v.H1, v.T), dot(v.H, dot(v.H1, v.T)) ) )
  cons.add_rule( Head=( v.H, [], dot(v.H, []) ) )

  components = RuleBasedPredicate('components', (dot, Term, Term))
  components.add_rule( Head=( v.X, v.H, v.T ), Body=( car(v.X, v.H), cdr(v.X, v.T ) ) )
  components.param_orderings = {(0, 2):operator.gt}
  
  append = RuleBasedPredicate('append')
  append.add_rule( Head=( [], v.L, v.L ) )
  append.add_rule( Head=( dot(v.X, v.A), v.B, dot(v.X, v.C) ), 
                  Body=( append(v.A, v.B, v.C), ) )
  
  reverse = RuleBasedPredicate('reverse')
  reverse.add_rule( Head=( [], [] ) )
  reverse.add_rule( Head=( dot(v.H, v.T), v.Rev ), 
                   Body=( reverse(v.T, v.Trev), append(v.Trev, plist([v.H]), v.Rev) ) )
Пример #9
0
 def __init__(self):
   RuleBasedPredicate.__init__(self, 'is_number')