def test_term_no_match_already_bound_var(self): t1 = Term('actor', Term('alice')) p1 = Term('actor', Var('?A')) u = {u'?A': Term('bob')} with self.assertRaises(ValueError): p1.match(t1, u) self.assertEqual(u, {u'?A': Term('bob')})
def test_match_all_with_unique_binding(self): # Find all pairs of actors. Because the actors must be different, there are only 2 matches. self.assertMatchAll([a(t('actor', t('?A'))), a(t('actor', t('?B')))], [{ '?A': Term('alice'), '?B': Term('bob') }, { '?A': Term('bob'), '?B': Term('alice') }]) # Find all pairs of drinks. Since there is only one, and we can't return (gin,gin), # there will be no matches. self.assertMatchAll([a(t('drink', t('?A'))), a(t('drink', t('?B')))], [])
def word(self): if self.scanner.on_type('variable'): return self.var() self.scanner.check_type('word', 'punct', 'operator') constructor = self.scanner.token self.scanner.scan() return Term(constructor)
def test_term_match_bind_var(self): t1 = Term('actor', Term('alice')) p1 = Term('actor', Var('?A')) e = {} u = p1.match(t1, e) self.assertEqual(u, {u'?A': Term('alice')}) self.assertEqual(e, {})
def term(self): if self.scanner.on_type('variable'): return self.var() self.scanner.check_type('word') constructor = self.scanner.token self.scanner.scan() subterms = [] if self.scanner.consume('('): subterms.append(self.term()) while self.scanner.consume(','): subterms.append(self.term()) self.scanner.expect(')') return Term(constructor, *subterms)
def test_match_all(self): # Find all actors who are Cody. Since there is no such actor, this will return no matches. self.assertMatchAll([a(t('actor', t('cody')))], []) # Find all actors who are Alice. This will return one match, but no bindings. self.assertMatchAll([a(t('actor', t('alice')))], [{}]) # Find all drinks. This will return one match, with ?D bound to the result. self.assertMatchAll( [a(t("drink", t("?D")))], [{ '?D': Term('gin') }] # there was a match, in which ?D was bound ) # Find all actors. self.assertMatchAll([a(t('actor', t('?C')))], [{ '?C': Term('alice') }, { '?C': Term('bob') }]) # Find all actors who are holding the revolver. self.assertMatchAll( [a(t('actor', t('?C'))), a(t('holding', t('?C'), t('revolver')))], [{ '?C': t('bob') }]) # Find all actors who are holding a weapon. self.assertMatchAll([ a(t('actor', t('?C'))), a(t('weapon', t('?W'))), a(t('holding', t('?C'), t('?W'))) ], [{ '?W': t('knife'), '?C': t('alice') }, { '?W': t('revolver'), '?C': t('bob') }])
def t(s, *args): if s.startswith('?'): return Var(s) else: return Term(s, *args)
def test_term_subst(self): t = Term('actor', Var('?A')) r = t.subst({u'?A': Term('alice')}) self.assertEqual(r, Term('actor', Term('alice')))
def test_term_match_already_bound_var(self): t1 = Term('actor', Term('alice')) p1 = Term('actor', Var('?A')) u = p1.match(t1, {u'?A': Term('alice')}) self.assertEqual(u, {u'?A': Term('alice')})
def test_term_no_match_ground(self): t1 = Term('actor', Term('alice')) p1 = Term('actor', Term('bob')) with self.assertRaises(ValueError): p1.match(t1, {})
def test_term_match_ground(self): t1 = Term('actor', Term('alice')) p1 = Term('actor', Term('alice')) u = p1.match(t1, {}) self.assertEqual(u, {})
def test_term_basic_properties(self): t1 = Term('alice') t2 = Term('actor', t1) v1 = Var('?A') t3 = Term('actor', v1) self.assertTrue(t1.is_atom()) self.assertFalse(t2.is_atom()) self.assertFalse(v1.is_atom()) self.assertFalse(t3.is_atom()) self.assertTrue(t1.is_ground()) self.assertTrue(t2.is_ground()) self.assertFalse(v1.is_ground()) self.assertFalse(t3.is_ground()) self.assertEqual(t2, Term('actor', Term('alice')))