def test_parse_single_fact(self): s = 'sokrates.' exp = Program() exp.rules = [(('term', 'sokrates', []), [])] act = parser.parse(s) self.assertEqual(act.rules, exp.rules) self.assertEqual(act.queries, exp.queries)
def test_parse_single_query(self): s = '?- human(X).' exp = Program() exp.queries = [('term', 'human', [('var', 'X')])] act = parser.parse(s) self.assertEqual(act.rules, exp.rules) self.assertEqual(act.queries, exp.queries)
def test_solve_nested_clause2(self): src = ''' male(zeus). male(chronos). female(rhea). female(athena). parent(zeus, chronos). parent(zeus, rhea). parent(athena, zeus). father(X, Y) :- parent(X, Y), male(Y). grandfather(X, Z) :- parent(X, Y), father(Y, Z). ?- grandfather(X, Z). ''' db = parser.parse(src) gen = solve(db.rules, db.queries[0]) # Assertions exp = { 'X': ('var', 'X_0'), 'X_0': ('term', 'athena', []), 'X_1': ('term', 'zeus', []), 'Y_0': ('term', 'zeus', []), 'Y_1': ('var', 'Z_0'), 'Z': ('var', 'Z_0'), 'Z_0': ('term', 'chronos', []), } self.assertEqual(next(gen), exp) exp = None self.assertEqual(next(gen), exp)
def test_solve_double_clause2(self): src = ''' parent(zeus, chronos). parent(zeus, rhea). parent(athena, zeus). grandparent(X, Y) :- parent(X, Z), parent(Z, Y). ?- grandparent(X, Y). ''' db = parser.parse(src) gen = solve(db.rules, db.queries[0]) # Assertions exp = { 'X': ('var', 'X_0'), 'X_0': ('term', 'athena', []), 'Y': ('var', 'Y_0'), 'Y_0': ('term', 'chronos', []), 'Z_0': ('term', 'zeus', []) } self.assertEqual(next(gen), exp) exp = { 'X': ('var', 'X_0'), 'X_0': ('term', 'athena', []), 'Y': ('var', 'Y_0'), 'Y_0': ('term', 'rhea', []), 'Z_0': ('term', 'zeus', []) } self.assertEqual(next(gen), exp)
def test_parse_single_rule(self): s = 'mortal(X) :- human(X).' exp = Program() exp.rules = [(('term', 'mortal', [('var', 'X_0')]), [('term', 'human', [('var', 'X_0')])])] act = parser.parse(s) self.assertEqual(act.rules, exp.rules) self.assertEqual(act.queries, exp.queries)
def search(source): global rules prog = parser.parse(source) rules += prog.rules for query in prog.queries: vs = vars(query) for sol in solve(rules, query): sol = compress(sol) sol = restrict(sol, vs) yield prettify(sol)
def test_solve_fail(self): src = ''' human(sokrates). human(plato). god(zeus). ?- mortal(zeus). ''' db = parser.parse(src) gen = solve(db.rules, db.queries[0]) # Assertions exp = None self.assertEqual(next(gen), exp)
def test_parse_multiple_facts(self): s = ''' sokrates. human(plato). ''' exp = Program() exp.rules = [ (('term', 'sokrates', []), []), (('term', 'human', [('term', 'plato', [])]), []), ] act = parser.parse(s) self.assertEqual(act.rules, exp.rules) self.assertEqual(act.queries, exp.queries)
def test_solve_single_clause1(self): src = ''' human(sokrates). human(plato). god(zeus). mortal(X) :- human(X). ?- mortal(sokrates). ''' db = parser.parse(src) gen = solve(db.rules, db.queries[0]) # Assertions exp = {'X_0': ('term', 'sokrates', [])} self.assertEqual(next(gen), exp) exp = None self.assertEqual(next(gen), exp)
def test_solve_double_clause(self): src = ''' human(sokrates). deity(zeus). male(sokrates). male(zeus). god(X) :- male(X), deity(X). ?- god(X). ''' db = parser.parse(src) gen = solve(db.rules, db.queries[0]) # Assertions exp = {'X': ('var', 'X_0'), 'X_0': ('term', 'zeus', [])} self.assertEqual(next(gen), exp) exp = None self.assertEqual(next(gen), exp)
def test_solve_facts_list(self): src = ''' human(sokrates). human(plato). god(zeus). ?- human(X). ''' db = parser.parse(src) gen = solve(db.rules, db.queries[0]) # Assertions exp = {'X': ('term', 'sokrates', [])} self.assertEqual(next(gen), exp) exp = {'X': ('term', 'plato', [])} self.assertEqual(next(gen), exp) exp = None self.assertEqual(next(gen), exp)
def test_parse_empty(self): s = '' exp = Program() act = parser.parse(s) self.assertEqual(act.rules, exp.rules) self.assertEqual(act.queries, exp.queries)