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__disagreement__2(self): literal1 = Literal.parse('a') literal2 = Literal.parse('c') program = Program.parse('~h <- b. h <- a.') result = disagree(literal1, literal2, program.rules) assert_that(result).is_false()
def disagree(literal1: Literal, literal2: Literal, rules: Iterable[Rule]) -> bool: if literal1.get_complement() == literal2: return True index = as_index(rules, RuleType.STRICT) index.setdefault(literal1, set()).add(literal1.as_fact()) index.setdefault(literal2, set()).add(literal2.as_fact()) return bool(is_contradictory(index))
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 query( self, literal: Literal, mode: RuleType = RuleType.DEFEASIBLE ) -> Tuple[Answer, Optional[Warrant]]: if self._answers is None: self._answers = {} answers = self._answers.setdefault(mode, {}) if literal not in answers: if literal not in self.get_literals(mode): answers.setdefault(literal, (Answer.UNKNOWN, None)) else: warrant = self._get_warrant(literal, mode) if warrant is not None: answers.setdefault(literal, (Answer.YES, warrant)) else: complement = literal.get_complement() warrant = self._get_warrant(complement, mode) if warrant is not None: answers.setdefault(literal, (Answer.NO, warrant)) else: answers.setdefault(literal, (Answer.UNDECIDED, None)) return self._answers.get(mode, {}).get(literal, (Answer.UNKNOWN, None))
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__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 test__get_derivations__not_flies_tina__strictly(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 = set() result = i.get_derivations(Literal.parse('~flies(tina)'), RuleType.STRICT) assert_that(result).is_equal_to(expected)
def visit_literal(self, node: Node, children: List) -> Literal: try: return Literal(children[0], children[1]) except IndexError: return Literal(False, children[0])
def test__disagreement__0(self): literal1 = Literal.parse('a') literal2 = Literal.parse('~a') result = disagree(literal1, literal2, {}) assert_that(result).is_true()