def setUp(self): # # db, store # db_url = 'sqlite:///foo.db' # setup compiler + environment self.db = LogicDB(db_url) self.parser = PrologParser(self.db) self.rt = PrologRuntime(self.db) self.db.clear_module(UNITTEST_MODULE)
logging.basicConfig(level=logging.INFO) if len(args) != 1: logging.error('exactly one input file needed.') parser.print_help() sys.exit(1) inputfn = args[0] outputfn = options.outputfn # # main # parser = PrologParser() with codecs.open(outputfn, 'w', 'utf8') as outf: outf.write('% prolog\n\n') outf.write("train_prefix('{self_address:L} ').\n\n") with codecs.open(inputfn, 'r', 'utf8') as f: parser.start(f, inputfn, module_name=MODULE_NAME) while parser.cur_sym != SYM_EOF: clauses = parser.clause(db=None) for clause in clauses:
class TestEmbeddings (unittest.TestCase): def setUp(self): # # db, store # db_url = 'sqlite:///foo.db' # setup compiler + environment self.db = LogicDB(db_url, echo=False) self.parser = PrologParser(self.db) self.rt = PrologRuntime(self.db) # self.rt.set_trace(True) self.db.clear_module(UNITTEST_MODULE) def tearDown(self): self.db.close() #@unittest.skip("temporarily disabled") def test_custom_builtins(self): global recorded_moves self.parser.compile_file('samples/hanoi2.pl', UNITTEST_MODULE) clause = self.parser.parse_line_clause_body('move(3,left,right,center)') logging.debug('clause: %s' % clause) # register our custom builtin recorded_moves = [] self.rt.register_builtin('record_move', record_move) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual (len(solutions), 1) self.assertEqual (len(recorded_moves), 7) #@unittest.skip("temporarily disabled") def test_custom_builtin_multiple_bindings(self): self.rt.register_builtin('multi_binder', multi_binder) clause = self.parser.parse_line_clause_body('multi_binder(X,Y)') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual (len(solutions), 4) def _custom_directive(self, db, module_name, clause, user_data): # logging.debug('custom_directive has been run') self.assertEqual (len(clause.head.args), 3) self.assertEqual (unicode(clause.head.args[0]), u'abc') self.assertEqual (clause.head.args[1].f, 42) self.assertEqual (clause.head.args[2].s, u'foo') self.directive_mark = True #@unittest.skip("temporarily disabled") def test_custom_directives(self): self.parser.register_directive('custom_directive', self._custom_directive, None) self.directive_mark = False # self.parser.compile_file('samples/dir.pl', UNITTEST_MODULE) clauses = self.parser.parse_line_clauses('custom_directive(abc, 42, \'foo\').') self.assertEqual (self.directive_mark, True)
if len(args) != 1: logging.error('exactly one input file needed.') parser.print_help() sys.exit(1) inputfn = args[0] outputfn = options.outputfn # # main # db = None parser = PrologParser(db, do_inline=False) with codecs.open(outputfn, 'w', 'utf8') as outf: outf.write('#!/usr/bin/env python\n') outf.write('# -*- coding: utf-8 -*-\n\n') outf.write('def get_data(k):\n') with codecs.open(inputfn, 'r', 'utf8') as f: parser.start(f, inputfn, module_name='foo') while parser.cur_sym != SYM_EOF: clauses = parser.clause()
class TestZamiaProlog(unittest.TestCase): def setUp(self): # # db, store # db_url = 'sqlite:///foo.db' # setup compiler + environment self.db = LogicDB(db_url) self.parser = PrologParser(self.db) self.rt = PrologRuntime(self.db) self.db.clear_module(UNITTEST_MODULE) def tearDown(self): self.db.close() # @unittest.skip("temporarily disabled") def test_parser(self): error_catched = False try: clause = self.parser.parse_line_clause_body( 'say_eoa(en, "Kids are the best') logging.debug('clause: %s' % clause) except PrologError as e: error_catched = True self.assertEqual(error_catched, True) # @unittest.skip("temporarily disabled") def test_parse_line_clauses(self): line = 'time_span(TE) :- date_time_stamp(+(D, 1.0)).' tree = self.parser.parse_line_clauses(line) logging.debug(unicode(tree[0].body)) self.assertEqual(tree[0].body.name, 'date_time_stamp') self.assertEqual(tree[0].head.name, 'time_span') line = 'time_span(tomorrow, TS, TE) :- context(currentTime, T), stamp_date_time(T, date(Y, M, D, H, Mn, S, "local")), date_time_stamp(date(Y, M, +(D, 1.0), 0.0, 0.0, 0.0, "local"), TS), date_time_stamp(date(Y, M, +(D, 1.0), 23.0, 59.0, 59.0, "local"), TE).' tree = self.parser.parse_line_clauses(line) logging.debug(unicode(tree[0].body)) self.assertEqual(tree[0].head.name, 'time_span') self.assertEqual(tree[0].body.name, 'and') self.assertEqual(len(tree[0].body.args), 4) # @unittest.skip("temporarily disabled") def test_kb1(self): self.assertEqual(len(self.db.lookup('party', 0)), 0) self.parser.compile_file('samples/kb1.pl', UNITTEST_MODULE) self.assertEqual(len(self.db.lookup('party', 0)), 1) clause = self.parser.parse_line_clause_body('woman(X)') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 3) clause = self.parser.parse_line_clause_body('party') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('woman(fred)') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 0) # @unittest.skip("temporarily disabled") def test_parse_to_string(self): line = u'time_span(c, X, Y) :- p1(c), p2(X, Y); p3(c); p4.' line2 = u'time_span(c, X, Y) :- or(and(p1(c), p2(X, Y)), p3(c), p4).' tree = self.parser.parse_line_clauses(line) logging.debug(unicode(tree[0].body)) self.assertEqual(unicode(tree[0]), line2) # @unittest.skip("temporarily disabled") def test_or(self): self.parser.compile_file('samples/or_test.pl', UNITTEST_MODULE) # self.rt.set_trace(True) solutions = self.rt.search_predicate('woman', ['X']) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 3) solutions = self.rt.search_predicate('human', ['X']) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 8) def test_or_toplevel(self): self.parser.compile_file('samples/or_test.pl', UNITTEST_MODULE) clause = self.parser.parse_line_clause_body( u'woman(mary); woman(jody)') logging.debug(u'clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) def test_or_bindings(self): clause = self.parser.parse_line_clause_body( u'S is "a", or(str_append(S, "b"), str_append(S, "c"))') logging.debug(u'clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 2) self.assertEqual(solutions[0]['S'].s, "ab") self.assertEqual(solutions[1]['S'].s, "ac") clause = self.parser.parse_line_clause_body(u'X is 42; X is 23') logging.debug(u'clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 2) def test_var_access(self): # set var X from python: clause = self.parser.parse_line_clause_body('Y is X*X') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {'X': NumberLiteral(3)}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) # access prolog result Y from python: self.assertEqual(solutions[0]['Y'].f, 9) def test_list_equality(self): clause = self.parser.parse_line_clause_body('[] is []') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('[1] is []') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body('909442800.0 is []') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body('[1,2,3] = [1,2,3]') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('[1,2,3] \\= [1,2,3,4,5]') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) def test_is(self): clause = self.parser.parse_line_clause_body( 'GENDER is "blubber", GENDER is wde:Male') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 0) # @unittest.skip("temporarily disabled") def test_list_eval(self): clause = self.parser.parse_line_clause_body( 'X is 23, Z is 42, Y is [X, U, Z].') solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) self.assertEqual(len(solutions[0]['Y'].l), 3) self.assertEqual(solutions[0]['Y'].l[0].f, 23.0) self.assertTrue(isinstance(solutions[0]['Y'].l[1], Variable)) self.assertEqual(solutions[0]['Y'].l[2].f, 42.0) def test_clauses_location(self): # this will trigger a runtime error since a(Y) is a predicate, # but format_str requires a literal arg clause = self.parser.parse_line_clause_body( 'X is format_str("%s", a(Y))') logging.debug('clause: %s' % clause) try: solutions = self.rt.search(clause, {}) self.fail("we should have seen a runtime error here") except PrologRuntimeError as e: self.assertEqual(e.location.line, 1) self.assertEqual(e.location.col, 29) def test_cut(self): self.parser.compile_file('samples/cut_test.pl', UNITTEST_MODULE) # self.rt.set_trace(True) clause = self.parser.parse_line_clause_body(u'bar(R, X)') logging.debug(u'clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 4) self.assertEqual(solutions[0]['R'].s, "one") self.assertEqual(solutions[1]['R'].s, "two") self.assertEqual(solutions[2]['R'].s, "many") self.assertEqual(solutions[3]['R'].s, "many") # @unittest.skip("temporarily disabled") def test_anon_var(self): clause = self.parser.parse_line_clause_body('_ is 23, _ is 42.') solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) def test_nonvar(self): clause = self.parser.parse_line_clause_body(u'S is "a", nonvar(S)') logging.debug(u'clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body(u'nonvar(S)') logging.debug(u'clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 0) def test_unify_pseudo(self): clause = self.parser.parse_line_clause_body( u'C is foo, assertz(mem(foo, bar)), if var(C:mem|bar) then C:mem|bar := 23 endif, X := C:mem|bar' ) logging.debug(u'clause: %s' % clause) # self.rt.set_trace(True) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) self.assertEqual(solutions[0]['X'].f, 23.0)
ListLiteral([ivr_in, ivr_out, ListLiteral([])]) ]) test_cnt += 1 clause = Clause(head=head) print unicode(clause) # import pdb; pdb.set_trace() misc.init_app('nlp_gens_conv') parser = PrologParser() with codecs.open(INPUT_FN, 'r', 'utf8') as f: parser.start(f, INPUT_FN, module_name=MODULE_NAME) while parser.cur_sym != SYM_EOF: clauses = parser.clause(db=None) for clause in clauses: if clause.head.name == 'nlp_gens': convert_nlp_gens(clause.head) elif clause.head.name == 'nlp_gen': convert_nlp_gen(clause.head) elif clause.head.name == 'answerz':
class TestBuiltins(unittest.TestCase): def setUp(self): # # db, store # db_url = 'sqlite:///foo.db' # setup compiler + environment self.db = LogicDB(db_url) self.parser = PrologParser(self.db) self.rt = PrologRuntime(self.db) self.db.clear_module(UNITTEST_MODULE) def tearDown(self): self.db.close() # @unittest.skip("temporarily disabled") def test_hanoi1(self): self.parser.compile_file('samples/hanoi1.pl', UNITTEST_MODULE) clause = self.parser.parse_line_clause_body( 'move(3,left,right,center)') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) # @unittest.skip("temporarily disabled") def test_lists(self): clause = self.parser.parse_line_clause_body('X is []') solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]['X'].l), 0) clause = self.parser.parse_line_clause_body( 'L is [1,2,3,4], X is list_sum(L), Y is list_max(L), Z is list_min(L), W is list_avg(L), V is list_len(L)' ) solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]['L'].l), 4) self.assertEqual(solutions[0]['L'].l[3].f, 4.0) self.assertEqual(solutions[0]['X'].f, 10.0) self.assertEqual(solutions[0]['Y'].f, 4.0) self.assertEqual(solutions[0]['Z'].f, 1.0) self.assertEqual(solutions[0]['W'].f, 2.5) self.assertEqual(solutions[0]['V'].f, 4.0) clause = self.parser.parse_line_clause_body( 'L is [1,2,3,4], list_contains(L, 2).') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body( 'L is [1,2,3,4], list_contains(L, 23).') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body( 'X is [1,2,3,4], list_nth(1, X, E).') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['E'].f, 2) clause = self.parser.parse_line_clause_body( 'X is [1,2,3,4], length(X, L).') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['L'].f, 4) clause = self.parser.parse_line_clause_body( 'X is [1,2,3,4], list_slice(1, 3, X, E).') solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]['E'].l), 2) self.assertEqual(solutions[0]['E'].l[0].f, 2.0) self.assertEqual(solutions[0]['E'].l[1].f, 3.0) clause = self.parser.parse_line_clause_body( 'X is [1,2,3,4], E is list_slice(1, 3, X).') solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]['E'].l), 2) self.assertEqual(solutions[0]['E'].l[0].f, 2.0) self.assertEqual(solutions[0]['E'].l[1].f, 3.0) clause = self.parser.parse_line_clause_body( 'X is [1,2,3,4], list_append(X, 5).') solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]['X'].l), 5) self.assertEqual(solutions[0]['X'].l[4].f, 5.0) clause = self.parser.parse_line_clause_body( 'X is ["1","2","3","4"], list_str_join("@", X, Y).') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['Y'].s, "1@2@3@4") clause = self.parser.parse_line_clause_body( 'X is ["1","2","3","4"], Y is list_join("@", X).') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['Y'].s, "1@2@3@4") # @unittest.skip("temporarily disabled") def test_list_findall(self): self.parser.compile_file('samples/kb1.pl', UNITTEST_MODULE) clause = self.parser.parse_line_clause_body( 'list_findall(X, woman(X), L)') solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]), 1) self.assertEqual(len(solutions[0]['L'].l), 3) # @unittest.skip("temporarily disabled") def test_strings(self): clause = self.parser.parse_line_clause_body( 'X is \'bar\', S is format_str(\'test %d %s foo\', 42, X)') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['S'].s, 'test 42 bar foo') clause = self.parser.parse_line_clause_body( 'X is \'foobar\', sub_string(X, 0, 2, _, Y)') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['Y'].s, 'fo') clause = self.parser.parse_line_clause_body( 'atom_chars(foo, X), atom_chars(Y, "bar").') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].s, 'foo') self.assertEqual(solutions[0]['Y'].name, 'bar') # @unittest.skip("temporarily disabled") def test_date_time(self): clause = self.parser.parse_line_clause_body('get_time(T)') solutions = self.rt.search(clause) self.assertGreater(solutions[0]['T'].s, '2017-04-30T23:39:29.092271') clause = self.parser.parse_line_clause_body( 'date_time_stamp(date(2017,2,14,1,2,3,\'local\'), TS), stamp_date_time(TS, date(Y,M,D,H,Mn,S,\'local\'))' ) solutions = self.rt.search(clause) self.assertEqual(solutions[0]['Y'].f, 2017) self.assertEqual(solutions[0]['M'].f, 2) self.assertEqual(solutions[0]['D'].f, 14) self.assertEqual(solutions[0]['H'].f, 1) self.assertEqual(solutions[0]['Mn'].f, 2) self.assertEqual(solutions[0]['S'].f, 3) clause = self.parser.parse_line_clause_body( 'date_time_stamp(date(2017,2,14,1,2,3,\'Europe/Berlin\'), TS), day_of_the_week(TS, WD)' ) solutions = self.rt.search(clause) self.assertEqual(solutions[0]['TS'].s, '2017-02-14T01:02:03+01:00') self.assertEqual(solutions[0]['WD'].f, 2) # @unittest.skip("temporarily disabled") def test_arith(self): clause = self.parser.parse_line_clause_body('X is -23') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, -23) clause = self.parser.parse_line_clause_body('X is +42') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, 42) clause = self.parser.parse_line_clause_body('X is 19 + 23') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, 42) clause = self.parser.parse_line_clause_body('X is 61 - 19') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, 42) clause = self.parser.parse_line_clause_body('X is 6*7') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, 42) clause = self.parser.parse_line_clause_body('X is 1764 / 42') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, 42) clause = self.parser.parse_line_clause_body('X is 85 mod 43') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, 42) clause = self.parser.parse_line_clause_body( 'X is 23, increment(X, 19)') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, 42) clause = self.parser.parse_line_clause_body( 'X is 42, decrement(X, 19)') solutions = self.rt.search(clause) self.assertEqual(solutions[0]['X'].f, 23) # @unittest.skip("temporarily disabled") def test_comp(self): clause = self.parser.parse_line_clause_body('3>1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('1>1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body('3<1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body('1<1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body('3=<1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body('1=<1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('3>=1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('1>=1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('3\\=1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('1\\=1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body('3=1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 0) clause = self.parser.parse_line_clause_body('1=1') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) # @unittest.skip("temporarily disabled") def test_between(self): clause = self.parser.parse_line_clause_body('between(1,100,42)') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('between(1,100,X)') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 100) # @unittest.skip("temporarily disabled") def test_dicts(self): clause = self.parser.parse_line_clause_body( 'dict_put(U, foo, 42), X is U, dict_put(X, bar, 23), dict_get(X, Y, Z), dict_get(X, foo, V)' ) solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]['U'].d), 1) self.assertEqual(len(solutions[0]['X'].d), 2) self.assertEqual(solutions[0]['Z'].f, 42) self.assertEqual(solutions[0]['V'].f, 42) self.assertEqual(solutions[1]['Z'].f, 23) self.assertEqual(solutions[1]['V'].f, 42) logging.debug(repr(solutions)) # @unittest.skip("temporarily disabled") def test_assertz(self): clause = self.parser.parse_line_clause_body( 'I is ias00001, assertz(frame (I, qIsFamiliar)), frame (ias00001, X)' ) solutions = self.rt.search(clause) logging.debug(repr(solutions)) self.assertEqual(len(solutions), 1) self.assertEqual(solutions[0]['X'].name, 'qIsFamiliar') # @unittest.skip("temporarily disabled") def test_retract(self): clause = self.parser.parse_line_clause_body( 'I is ias1, assertz(frame (I, a, x)), retract(frame (I, _, _)), assertz(frame (I, a, y)), frame(ias1, a, X)' ) solutions = self.rt.search(clause) logging.debug(repr(solutions)) self.assertEqual(len(solutions), 1) self.assertEqual(solutions[0]['X'].name, 'y') # @unittest.skip("temporarily disabled") def test_retract_db(self): clause = self.parser.parse_line_clause_body( 'I is ias1, assertz(frame (I, a, x))') solutions = self.rt.search(clause) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('frame(ias1, a, X)') s2s = self.rt.search(clause) self.assertEqual(len(s2s), 0) self.rt.apply_overlay(UNITTEST_MODULE, solutions[0]) clause = self.parser.parse_line_clause_body('frame(ias1, a, X)') s2s = self.rt.search(clause) self.assertEqual(len(s2s), 1) self.assertEqual(s2s[0]['X'].name, 'x') clause = self.parser.parse_line_clause_body( 'retract(frame (ias1, _, _)), frame(ias1, a, X)') s2s = self.rt.search(clause) self.assertEqual(len(s2s), 0) clause = self.parser.parse_line_clause_body( 'retract(frame (ias1, _, _))') solutions = self.rt.search(clause) self.rt.apply_overlay(UNITTEST_MODULE, solutions[0]) clause = self.parser.parse_line_clause_body('frame(ias1, a, X)') s2s = self.rt.search(clause) self.assertEqual(len(s2s), 0) # @unittest.skip("temporarily disabled") def test_setz(self): clause = self.parser.parse_line_clause_body( 'assertz(frame (ias1, a, x)), assertz(frame (ias1, a, y)), setz(frame (ias1, a, _), z), frame (ias1, a, X)' ) solutions = self.rt.search(clause) logging.debug(repr(solutions)) self.assertEqual(len(solutions), 1) self.assertEqual(solutions[0]['X'].name, 'z') # @unittest.skip("temporarily disabled") def test_setz_multi(self): # self.rt.set_trace(True) clause = self.parser.parse_line_clause_body( 'I is ias2, setz(ias (I, a, _), a), setz(ias (I, b, _), b), setz(ias (I, c, _), c), ias(I, X, Y). ' ) solutions = self.rt.search(clause) logging.debug(repr(solutions)) self.assertEqual(len(solutions), 3) self.assertEqual(solutions[0]['X'].name, 'a') self.assertEqual(solutions[0]['Y'].name, 'a') self.assertEqual(solutions[1]['X'].name, 'b') self.assertEqual(solutions[1]['Y'].name, 'b') self.assertEqual(solutions[2]['X'].name, 'c') self.assertEqual(solutions[2]['Y'].name, 'c') # @unittest.skip("temporarily disabled") def test_gensym(self): logging.debug('test_gensym...') clause = self.parser.parse_line_clause_body( 'gensym(foo, I), gensym(foo, J)') solutions = self.rt.search(clause) logging.debug(repr(solutions)) self.assertEqual(len(solutions), 1) self.assertNotEqual(solutions[0]['I'].name, solutions[0]['J'].name) logging.debug('test_gensym... done.') # @unittest.skip("temporarily disabled") def test_sets(self): clause = self.parser.parse_line_clause_body( 'set_add(S, 42), set_add(S, 23), set_add(S, 23), set_get(S, V)') solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]), 2) self.assertEqual(len(solutions[0]['S'].s), 2) logging.debug(repr(solutions)) # @unittest.skip("temporarily disabled") def test_set_findall(self): self.parser.compile_file('samples/kb1.pl', UNITTEST_MODULE) clause = self.parser.parse_line_clause_body( 'set_findall(X, woman(X), S)') solutions = self.rt.search(clause) self.assertEqual(len(solutions[0]), 1) self.assertEqual(len(solutions[0]['S'].s), 3) # @unittest.skip("temporarily disabled") def test_eval_functions(self): clause = self.parser.parse_line_clause_body( 'X is [23, 42], Y is [list_avg(X), list_sum(Z)]') solutions = self.rt.search(clause) logging.debug(repr(solutions)) self.assertEqual(len(solutions), 1) self.assertEqual(len(solutions[0]['Y'].l), 2) # @unittest.skip("temporarily disabled") def test_set(self): clause = self.parser.parse_line_clause_body( 'set(X, 23), set(X, 42), set(Y, 23), Z := Y * 2') solutions = self.rt.search(clause) logging.debug(repr(solutions)) self.assertEqual(len(solutions), 1) self.assertEqual(solutions[0]['X'].f, 42) self.assertEqual(solutions[0]['Y'].f, 23) self.assertEqual(solutions[0]['Z'].f, 46) # @unittest.skip("temporarily disabled") def test_set_pseudo(self): clause = self.parser.parse_line_clause_body( 'assertz(foo(bar, 23)), set(bar:foo, 42), foo(bar, X), Z := bar:foo' ) # self.rt.set_trace(True) solutions = self.rt.search(clause) logging.debug(repr(solutions)) self.assertEqual(len(solutions), 1) self.assertEqual(solutions[0]['X'].f, 42)
class TestNegation(unittest.TestCase): def setUp(self): # # db, store # db_url = 'sqlite:///foo.db' # setup compiler + environment self.db = LogicDB(db_url) self.parser = PrologParser(self.db) self.rt = PrologRuntime(self.db) self.db.clear_module(UNITTEST_MODULE) self.rt.set_trace(True) def tearDown(self): self.db.close() # @unittest.skip("temporarily disabled") def test_not_succ(self): clause = self.parser.parse_line_clause_body( 'X is 1, Y is 2, not(X is Y).') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) # @unittest.skip("temporarily disabled") def test_not_fail(self): clause = self.parser.parse_line_clause_body( 'X is 2, Y is 2, not(X is Y).') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 0) # @unittest.skip("temporarily disabled") def test_chancellors(self): self.parser.compile_file('samples/not_test.pl', UNITTEST_MODULE) clause = self.parser.parse_line_clause_body( 'was_chancellor(helmut_kohl).') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) # @unittest.skip("temporarily disabled") def test_double_negation(self): self.parser.compile_file('samples/not_test.pl', UNITTEST_MODULE) clause = self.parser.parse_line_clause_body( 'not(not(chancellor(helmut_kohl))).') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body( 'not(not(chancellor(angela_merkel))).') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1) clause = self.parser.parse_line_clause_body('not(not(chancellor(X))).') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 2) # @unittest.skip("temporarily disabled") def test_assertz_negation(self): clause = self.parser.parse_line_clause_body( 'assertz(foobar(a)), foobar(a), (not(foobar(b))).') logging.debug('clause: %s' % clause) solutions = self.rt.search(clause, {}) logging.debug('solutions: %s' % repr(solutions)) self.assertEqual(len(solutions), 1)
readline.read_history_file(histfile) # default history len is -1 (infinite), which may grow unruly readline.set_history_length(1000) except IOError: pass atexit.register(readline.write_history_file, histfile) # # main # db_url = config.get('db', 'url') # db_url = 'sqlite:///tmp/foo.db' db = LogicDB(db_url) parser = PrologParser() rt = PrologRuntime(db) while True: line = raw_input('?- ') if line == 'quit' or line == 'exit': break try: c = parser.parse_line_clause_body(line) logging.debug("Parse result: %s" % c) logging.debug("Searching for c:", c)