def test_find_conj(self): conj = (tree.tree_from_atom(self.l1), tree.tree_from_atom(self.l2)) matches = tree.find_conj(conj, self.a.get_atoms_by_type(t.Atom)) self.assertEquals(len(matches), 1) if len(matches) == 1: first = matches[0] self.assertEquals(first.subst, {}) self.assertEquals(first.atoms, [self.l1, self.l2])
def test_link_tree(self): l_tree = tree.tree_from_atom(self.l1) self.assertEquals(l_tree.is_leaf(), False) # should be something like ('Link', 17, 18) x = l_tree.to_tuple() self.assertEquals(len(x), 3)
def test_link_tree(self): l_tree = tree.tree_from_atom(self.l1) self.assertEquals(l_tree.is_leaf(), False) # should be something like ('Link', 17, 18) x = l_tree.to_tuple() self.assertEquals(len(x), 3 )
def test_link_to_link_tree(self): l_tree = tree.tree_from_atom(self.l2) self.assertEquals(l_tree.is_leaf(), False) # should be something like ('Link', ('Link', 13, 14), 14) x = l_tree.to_tuple() self.assertEquals(len(x), 3) self.assertEquals(len(x[1]), 3) self.assertEquals(x[1][2], x[2])
def test_unify(self): T = tree.T V = tree.Var x1_template = T(self.x1) x1_tree = tree.tree_from_atom(self.x1) s = tree.unify(x1_template, x1_tree, {}) self.assertEquals(s, {}) x2_template = T(self.x2) s = tree.unify(x2_template, x1_tree, {}) self.assertEquals(s, None) all_template = V(1) l2_tree = tree.tree_from_atom(self.l2) s = tree.unify(all_template, l2_tree, {}) s_correct = {all_template : l2_tree} self.assertEquals(s, s_correct) t1 = V(1) t2 = V(2) s = tree.unify(t1, t2, {}) self.assertEquals(s, {V(1):V(2)}) t1 = V(1) t2 = V(2) s_correct = {V(1):V(2)} s = tree.unify(t1, t2, s_correct) self.assertEquals(s, s_correct) t1 = T('blah',V(1)) t2 = T('blah',V(2)) s = tree.unify(t1, t2, {}) self.assertEquals(s, {V(1):V(2)}) t1 = T('blah',V(1), V(2)) t2 = T('blah',V(3), V(4)) s = tree.unify(t1, t2, {}) self.assertEquals(s, {V(1):V(3), V(2):V(4)}) t1 = T('blah',V(1), V(1)) t2 = T('blah',V(2), V(2)) s = tree.unify(t1, t2, {}) self.assertEquals(s, {V(1):V(2)})
def test_unify(self): T = tree.T V = tree.Var x1_template = T(self.x1) x1_tree = tree.tree_from_atom(self.x1) s = tree.unify(x1_template, x1_tree, {}) self.assertEquals(s, {}) x2_template = T(self.x2) s = tree.unify(x2_template, x1_tree, {}) self.assertEquals(s, None) all_template = V(1) l2_tree = tree.tree_from_atom(self.l2) s = tree.unify(all_template, l2_tree, {}) s_correct = {all_template: l2_tree} self.assertEquals(s, s_correct) t1 = V(1) t2 = V(2) s = tree.unify(t1, t2, {}) self.assertEquals(s, {V(1): V(2)}) t1 = V(1) t2 = V(2) s_correct = {V(1): V(2)} s = tree.unify(t1, t2, s_correct) self.assertEquals(s, s_correct) t1 = T('blah', V(1)) t2 = T('blah', V(2)) s = tree.unify(t1, t2, {}) self.assertEquals(s, {V(1): V(2)}) t1 = T('blah', V(1), V(2)) t2 = T('blah', V(3), V(4)) s = tree.unify(t1, t2, {}) self.assertEquals(s, {V(1): V(3), V(2): V(4)}) t1 = T('blah', V(1), V(1)) t2 = T('blah', V(2), V(2)) s = tree.unify(t1, t2, {}) self.assertEquals(s, {V(1): V(2)})
def test_compare(self): l_tree1 = tree.tree_from_atom(self.l1) l_tree = tree.tree_from_atom(self.l2) self.assertEquals(l_tree1 > l_tree, False) self.assertEquals(l_tree1 < l_tree, True)
def test_atom_tree(self): node_tree = tree.tree_from_atom(self.x1) self.assertEquals(node_tree.is_leaf(), True)
def run_pln_example(a, f): a.clear() testdir = '../tests/reasoning/pln/targets/' datadirs = ['../tests/reasoning/pln/scm/', '../opencog/'] fname = testdir+f config = ConfigParser.ConfigParser() read_files = config.read(fname) if not read_files: raise Exception("no such file:",fname) def get(field): return config.get('PLN_TEST',field) print f def load_axioms(fname): for d in datadirs: kf = d+fname+'.scm' try: tmp = open(kf,'r') scheme_wrapper.load_scm(a, kf) print kf return except IOError: continue raise IOError("missing data file: "+kf) data_files = get('load').replace(' ','').split(',') for fname in data_files: load_axioms(fname) scm_target = '(cog-handle %s)' % (get('target'),) print scm_target handle_str = scheme_wrapper.scheme_eval(scm_target) try: h = int(handle_str) except ValueError: print handle_str raise Exception("Scheme error in target") nsteps = int(get('max_steps')) target = Atom(Handle(h), a) print target import logic import tree c = logic.Chainer(a) target_tr = tree.tree_from_atom(target) # hack - won't work if the Scheme target is some variable that doesn't contain "Demand" if "Demand" in scm_target: res = logic.do_planning(a, target_tr,c) else: res = c.bc(target_tr, nsteps) print map(str, res) if len(res): print 'PASSED' passed.append(f) else: print 'FAILED' failed.append(f)
def run_pln_example(a, f): a.clear() testdir = '../tests/reasoning/pln/targets/' datadirs = ['../tests/reasoning/pln/scm/', '../opencog/'] fname = testdir + f config = ConfigParser.ConfigParser() read_files = config.read(fname) if not read_files: raise Exception("no such file:", fname) def get(field): return config.get('PLN_TEST', field) print f def load_axioms(fname): for d in datadirs: kf = d + fname + '.scm' try: tmp = open(kf, 'r') scheme_wrapper.load_scm(a, kf) print kf return except IOError: continue raise IOError("missing data file: " + kf) data_files = get('load').replace(' ', '').split(',') for fname in data_files: load_axioms(fname) scm_target = '(cog-handle %s)' % (get('target'), ) print scm_target handle_str = scheme_wrapper.scheme_eval(scm_target) try: h = int(handle_str) except ValueError: print handle_str raise Exception("Scheme error in target") nsteps = int(get('max_steps')) target = Atom(Handle(h), a) print target import logic import tree # hack - won't work if the Scheme target is some variable that doesn't contain "Demand" if "Demand" in scm_target: # superhack - doesn't care which target you say target_tr = tree.tree_from_atom(target) res = logic.do_planning(a, target_tr) else: c = logic.Chainer(a) target_tr = tree.tree_from_atom(target) res = c.bc(target_tr, nsteps) if len(res): print 'PASSED' passed.append(f) else: print 'FAILED' failed.append(f)
def test_coerce_tree(self): node_tree = tree.tree_from_atom(self.x1) print str(node_tree) self.assertEquals(tree.coerce_tree(node_tree), node_tree) self.assertEquals(tree.coerce_tree(self.x1), node_tree) self.assertEquals(tree.coerce_tree("tree").op, "tree")
def test_coerce_tree(self): node_tree = tree.tree_from_atom(self.x1) print str(node_tree) self.assertEquals(tree.coerce_tree(node_tree),node_tree) self.assertEquals(tree.coerce_tree(self.x1),node_tree) self.assertEquals(tree.coerce_tree("tree").op,"tree")
nsteps = int(get('max_steps')) target = Atom(Handle(h), a) try: rule_names = get_list('allowed_rules') except ConfigParser.NoOptionError, e: rule_names = [] print target import logic import tree c = logic.Chainer(a, allowed_rule_names=rule_names, use_fc=True) target_tr = tree.tree_from_atom(target) # hack - won't work if the Scheme target is some variable that doesn't contain "Demand" if "Demand" in scm_target: res = logic.do_planning(a, target_tr, c) else: res = c.bc(target_tr, nsteps) print map(str, res) if len(res): print 'PASSED' passed.append(f) else: print 'FAILED' failed.append(f)