def test__is_strictly_more_specific__3(self): p = Program.parse(""" bird(X) <- chicken(X). bird(X) <- penguin(X). ~flies(X) <- penguin(X). chicken(tina). penguin(tweety). scared(tina). flies(X) -< bird(X). flies(X) -< chicken(X), scared(X). nests_in_trees(X) -< flies(X). ~flies(X) -< chicken(X). """) i = Interpreter(p) d1 = Derivation([ Rule.parse('flies(tina) -< chicken(tina), scared(tina).'), Rule.parse('chicken(tina).'), Rule.parse('scared(tina).'), ], i) s1 = d1.get_structure() d2 = Derivation([ Rule.parse('~flies(tina) -< chicken(tina).'), Rule.parse('chicken(tina).'), ], i) s2 = d2.get_structure() result = s1.is_strictly_more_specific_than(s2) assert_that(result).is_true()
def test__get_structure__flies_tina_2__not_strict(self): p = Program.parse(""" bird(X) <- chicken(X). bird(X) <- penguin(X). ~flies(X) <- penguin(X). chicken(tina). penguin(tweety). scared(tina). flies(X) -< bird(X). flies(X) -< chicken(X), scared(X). nests_in_trees(X) -< flies(X). ~flies(X) -< chicken(X). """) i = Interpreter(p) d = Derivation([ Rule.parse('flies(tina) -< chicken(tina), scared(tina).'), Rule.parse('chicken(tina).'), Rule.parse('scared(tina).'), ], i) expected = Structure( {Rule.parse('flies(tina) -< chicken(tina), scared(tina).')}, Literal.parse('flies(tina)'), d) result = d.get_structure() assert_that(result).is_equal_to(expected) assert_that(result.is_strict()).is_false()
def test__is_counter_argument_of__2(self): p = Program.parse(""" bird(X) <- chicken(X). bird(X) <- penguin(X). ~flies(X) <- penguin(X). chicken(tina). penguin(tweety). scared(tina). flies(X) -< bird(X). flies(X) -< chicken(X), scared(X). nests_in_trees(X) -< flies(X). ~flies(X) -< chicken(X). """) i = Interpreter(p) d1 = Derivation([ Rule.parse('~flies(tina) -< chicken(tina).'), Rule.parse('chicken(tina).'), ], i) s1 = d1.get_structure() d2 = Derivation([ Rule.parse('flies(tina) -< bird(tina).'), Rule.parse('bird(tina) <- chicken(tina).'), Rule.parse('chicken(tina).'), ], i) s2 = d2.get_structure() result = s2.is_counter_argument_of(s1, s1) assert_that(result).is_true()
def test__get_derivations__3(self): p = Program.parse(""" a <- b, c. b <- d, e. b <- c. c <- f, g. d <- h. e. f. g. h. """) i = Interpreter(p) expected = { Derivation([ Rule.parse('b <- c.'), Rule.parse('c <- f, g.'), Rule.parse('f.'), Rule.parse('g.') ], i), Derivation([ Rule.parse('b <- d, e.'), Rule.parse('d <- h.'), Rule.parse('h.'), Rule.parse('e.') ], i), } result = i.get_derivations(Literal.parse('b')) assert_that(result).is_equal_to(expected)
def test__is_subargument_of__min_min(self): p = Program.parse(""" bird(X) <- chicken(X). bird(X) <- penguin(X). ~flies(X) <- penguin(X). chicken(tina). penguin(tweety). scared(tina). flies(X) -< bird(X). flies(X) -< chicken(X), scared(X). nests_in_trees(X) -< flies(X). ~flies(X) -< chicken(X). """) i = Interpreter(p) d1 = Derivation([ Rule.parse('bird(tina) <- chicken(tina).'), Rule.parse('chicken(tina).') ], i) s1 = Structure(set(), Literal.parse('bird(tina)'), d1) d2 = Derivation([ Rule.parse('flies(tina) -< bird(tina).'), Rule.parse('bird(tina) <- chicken(tina).'), Rule.parse('chicken(tina).'), ], i) s2 = Structure({Rule.parse('flies(tina) -< bird(tina).')}, Literal.parse('flies(tina)'), d2) expected = True result = s1.is_subargument_of(s1) assert_that(result).is_equal_to(expected)
def test__get_derivations__flies_tina__defeasibly(self): p = Program.parse(""" bird(X) <- chicken(X). bird(X) <- penguin(X). ~flies(X) <- penguin(X). chicken(tina). penguin(tweety). scared(tina). flies(X) -< bird(X). flies(X) -< chicken(X), scared(X). nests_in_trees(X) -< flies(X). ~flies(X) -< chicken(X). """) i = Interpreter(p) expected = { Derivation([ Rule.parse('flies(tina) -< chicken(tina), scared(tina).'), Rule.parse('chicken(tina).'), Rule.parse('scared(tina).') ], i), Derivation([ Rule.parse('flies(tina) -< bird(tina).'), Rule.parse('bird(tina) <- chicken(tina).'), Rule.parse('chicken(tina).') ], i), } result = i.get_derivations(Literal.parse('flies(tina)'), RuleType.DEFEASIBLE) assert_that(result).is_equal_to(expected)
def notify(self, ground: List['Literal'], subs: 'Substitutions', parent: Union[Alfa, 'Beta']): from depysible.domain.definitions import Rule payload = (ground, subs) if payload not in self.memory: self.memory.append(payload) lit = self.rule.head.substitutes(subs) # if self.rule.type is RuleType.STRICT: # fact = Rule(lit, self.rule.type, []) # if fact not in self.agenda: # self.agenda.append(fact) rule = Rule(lit, self.rule.type, ground) if rule not in self.agenda: self.agenda.append(rule) self.root.notify(lit)
def visit_strict(self, node: Node, children: List) -> Rule: try: return Rule(children[0], RuleType.STRICT, children[1]) except IndexError: return Rule(children[0], RuleType.STRICT, [])
def visit_defeasible(self, node: Node, children: List) -> Rule: try: return Rule(children[0], RuleType.DEFEASIBLE, children[1]) except IndexError: return Rule(children[0], RuleType.DEFEASIBLE, [])