def _instances(self, rule, index, binding, results, possibilities): """Return all instances of the given RULE without evaluating builtins. Assumes self.head_index returns rules with empty bodies. """ if index >= len(rule.body): results.add(rule.plug(binding)) return lit = rule.body[index] self._print_call(lit, binding, 0) # if already ground or a builtin, go to the next literal if (lit.is_ground() or lit.is_builtin()): self._instances(rule, index + 1, binding, results, possibilities) return # Otherwise, find instances in this theory if lit.tablename() in possibilities: options = possibilities[lit.tablename()] else: options = self.head_index(lit.tablename(), lit.plug(binding)) for data in options: self._print_note(lit, binding, 0, "Trying: %s" % repr(data)) undo = unify.match_atoms(lit, binding, self.head(data)) if undo is None: # no unifier continue self._print_exit(lit, binding, 0) # recurse on the rest of the literals in the rule self._instances(rule, index + 1, binding, results, possibilities) if undo is not None: unify.undo_all(undo) self._print_redo(lit, binding, 0) self._print_fail(lit, binding, 0)
def check(self, atom1, atom2): atom1 = compile.parse1(atom1) atom2 = compile.parse1(atom2) unifier = unify.BiUnifier() changes = unify.match_atoms(atom1, unifier, atom2) self.assertIsNotNone(changes) self.assertEqual(atom1.plug(unifier), atom2)
def test_sequence(self): atom1 = compile.parse1('p(x, y)') atom2 = compile.parse1('p(1, 2)') unifier = unify.BiUnifier() changes = unify.match_atoms(atom1, unifier, atom2) self.assertIsNotNone(changes) atom3 = compile.parse1('q(y, z)') atom4 = compile.parse1('q(2, 3)') changes = unify.match_atoms(atom3, unifier, atom4) self.assertIsNotNone(changes) atom5 = compile.parse1('r(x, y, z, z)') atom6 = compile.parse1('r(1, 2, 3, 3)') changes = unify.match_atoms(atom5, unifier, atom6) self.assertIsNotNone(changes) self.assertEqual(atom1.plug(unifier), atom2) self.assertEqual(atom3.plug(unifier), atom4) self.assertEqual(atom5.plug(unifier), atom6)
def cherr(self, atom1, atom2): atom1 = compile.parse1(atom1) atom2 = compile.parse1(atom2) unifier = unify.BiUnifier() self.assertIsNone(unify.match_atoms(atom1, unifier, atom2))