def assert_true(query, e=None): if e is None: e = Engine() terms, vars = e.parse(query) term, = terms e.run_query_in_current(term) return dict([(name, var.dereference(None)) for name, var in vars.iteritems()])
def assert_true(query, e=None): if e is None: e = Engine() terms, vars = e.parse(query) term, = terms e.run_query_in_current(term) return dict([(name, var.dereference(None)) for name, var in vars.iteritems()])
def entry_point(argv): if len(argv) != 8: print( "Usage: spyrolog PROGRAM SIMILARITIES QUERY1|QUERY2|...|QUERYN MAX_DEPTH LAMBDA_CUT1|LAMBDA_CUT2|...|LAMBDA_CUTN E-TNORM|P-TNORM MIN_WIDTH" ) return 1 program_fname = argv[1] sim_fname = argv[2] queries = argv[3] max_depth = int(argv[4]) lambda_cuts = [float(i) for i in argv[5].split('|')] entity_tnorm, predicate_tnorm = argv[6].split('|') min_width = int(argv[7]) sim = get_similarity_from_file(sim_fname, 0, entity_tnorm, predicate_tnorm) e = Engine(load_system=False, similarity=sim, max_depth=max_depth) e.modulewrapper.current_module = e.modulewrapper.user_module with open(program_fname) as f: sim.parse_rulescores(f.read(), e) query_idx = 0 for query in queries.split('|'): cut = lambda_cuts[query_idx] scores = [] depths = [] rules = [] unifications = [] sim.lambda_cut = cut sim.reset_threshold() sim.query_idx = query_idx collector = GetAllScoresContinuation(e, scores, sim, depths, rules, unifications, min_width) e.max_depth = max_depth goals, var_to_pos = e.parse(query) goal = goals[0] try: e.run_query_in_current(goal, collector) except UnificationFailed: info = get_max_info(scores, depths, rules, unifications) if info[0] > 0: print(info[0], info[1], '|'.join(info[3]), query + ''.join(info[2])) else: print(0.0, 0) query_idx += 1 return 0
def test_run(): e = Engine() e.add_rule(Callable.build("f", [Callable.build("a"), Callable.build("b")])) X = BindingVar() Y = BindingVar() c = Callable.build("f", [X, X]) e.add_rule(c) c2 = Callable.build(":-", [Callable.build("f", [X, Y]), Callable.build("f", [Y, X])]) e.add_rule(c2) hp = Heap() X = hp.newvar() c3 = Callable.build("f", [Callable.build("b"), X]) e.run_query_in_current(c3) assert X.dereference(hp).name()== "b" query = Callable.build("f", [Callable.build("b"), Callable.build("a")]) e.run_query_in_current(query)
def test_run(): e = Engine() e.add_rule(Callable.build("f", [Callable.build("a"), Callable.build("b")])) X = BindingVar() Y = BindingVar() c = Callable.build("f", [X, X]) e.add_rule(c) c2 = Callable.build( ":-", [Callable.build("f", [X, Y]), Callable.build("f", [Y, X])]) e.add_rule(c2) hp = Heap() X = hp.newvar() c3 = Callable.build("f", [Callable.build("b"), X]) e.run_query_in_current(c3) assert X.dereference(hp).name() == "b" query = Callable.build("f", [Callable.build("b"), Callable.build("a")]) e.run_query_in_current(query)
def test_numeral(): from prolog.interpreter.term import Callable, Atom, BindingVar from prolog.interpreter.continuation import Engine t = parse_file(""" numeral(null). % end of line comment numeral(succ(X)) :- numeral(X). % another one add_numeral(X, null, X). add_numeral(X, succ(Y), Z) :- add_numeral(succ(X), Y, Z). greater_than(succ(null), null). greater_than(succ(X), null) :- greater_than(X, null). greater_than(succ(X), succ(Y)) :- greater_than(X, Y). """) builder = TermBuilder() facts = builder.build(t) e = Engine() m = e.modulewrapper for fact in facts: print fact e.add_rule(fact) assert m.modules["user"].lookup(Signature.getsignature("add_numeral", 3)).rulechain.head.argument_at(1).name() == "null" four = Callable.build("succ", [Callable.build("succ", [Callable.build("succ", [Callable.build("succ", [Callable.build("null")])])])]) e.run_query_in_current(parse_query_term("numeral(succ(succ(null))).")) term = parse_query_term( """add_numeral(succ(succ(null)), succ(succ(null)), X).""") e.run_query_in_current(term) hp = Heap() var = BindingVar().dereference(hp) # does not raise var.unify(four, hp) term = parse_query_term( """greater_than(succ(succ(succ(null))), succ(succ(null))).""") e.run_query_in_current(term)
def test_numeral(): from prolog.interpreter.term import Callable, Atom, BindingVar from prolog.interpreter.continuation import Engine t = parse_file(""" numeral(null). % end of line comment numeral(succ(X)) :- numeral(X). % another one add_numeral(X, null, X). add_numeral(X, succ(Y), Z) :- add_numeral(succ(X), Y, Z). greater_than(succ(null), null). greater_than(succ(X), null) :- greater_than(X, null). greater_than(succ(X), succ(Y)) :- greater_than(X, Y). """) builder = TermBuilder() facts = builder.build(t) e = Engine() m = e.modulewrapper for fact in facts: print fact e.add_rule(fact) assert m.modules["user"].lookup(Signature.getsignature( "add_numeral", 3)).rulechain.head.argument_at(1).name() == "null" four = Callable.build("succ", [ Callable.build("succ", [ Callable.build("succ", [Callable.build("succ", [Callable.build("null")])]) ]) ]) e.run_query_in_current(parse_query_term("numeral(succ(succ(null))).")) term = parse_query_term( """add_numeral(succ(succ(null)), succ(succ(null)), X).""") e.run_query_in_current(term) hp = Heap() var = BindingVar().dereference(hp) # does not raise var.unify(four, hp) term = parse_query_term( """greater_than(succ(succ(succ(null))), succ(succ(null))).""") e.run_query_in_current(term)