class SimpleAgent(): def __init__(self): self.a = AtomSpace() self.nodes = {} # Initialize Scheme scheme_preload = [ "opencog/atomspace/core_types.scm", "opencog/scm/utilities.scm" ] scheme.__init__(self.a) for scheme_file in scheme_preload: load_scm(self.a, scheme_file) initialize_opencog(self.a) #add 3 nodes with integer values self.nodes[0] = self.a.add(types.ConceptNode, "0") self.nodes[1] = self.a.add(types.ConceptNode, "1") self.nodes[2] = self.a.add(types.ConceptNode, "2") def performAction(self): #randomly select a link from those available and add the nodes fnode = self.a.add_node(types.GroundedSchemaNode, "py: sendValue") current_link = self.a.add_link(types.ExecutionOutputLink, [ fnode, self.a.add_link(types.ListLink, [self.nodes[randint(0,2)]])]) scheme_eval(self.a, '(cog-execute! (cog-atom %d))'%(current_link.handle_uuid())) def remove(self): # make sure this is called by the time script exits finalize_opencog() del self.a
class TestSpaceServer: def setUp(self): self._atomspace = AtomSpace() self.space_server = SpaceServer(self._atomspace) self.time_server = TimeServer(self._atomspace, self.space_server) self.space_server.set_time_server(self.time_server) def tearDown(self): del self.space_server del self._atomspace def test_addMap(self): map_atom = self.space_server.add_map(123456, "testmap", 1) assert self._atomspace.get_name(map_atom) == "testmap" def test_getMap(self): map_atom = self.space_server.add_map(123456, "testmap", 1) mapinstance = self.space_server.get_map(map_atom) assert mapinstance.get_map_name() == "testmap" def test_addAndRemoveMapInfo(self): map_atom = self.space_server.add_map(123456, "testmap", 1) object_atom = self._atomspace.add_node(types.StructureNode,"object111") assert self.space_server.add_map_info(object_atom, map_atom, False, False, 123456, 4, 5, 6) == True map_instance = self.space_server.get_map(map_atom) assert map_instance.get_block_location(object_atom) == (4, 5, 6) self.space_server.remove_map_info(object_atom, map_atom, 234567) assert map_instance.get_block_location(object_atom) == None
class TestTimeServer: def setUp(self): self._atomspace = AtomSpace() self._spaceserver = SpaceServer(self._atomspace) self._timeserver = TimeServer(self._atomspace, self._spaceserver) self._spaceserver.set_time_server(self._timeserver) def tearDown(self): del self._spaceserver del self._atomspace def test_addTimeInfo(self): objnode = self._atomspace.add_node(types.StructureNode, "object111") at_time_link = self._timeserver.add_time_info(objnode, 123456) assert objnode in self._atomspace.get_outgoing(at_time_link)
def setUp(self): self.atomspace = AtomSpace() scheme.__init__(self.atomspace) for scheme_file in scheme_preload: load_scm(self.atomspace, scheme_file) # Define several animals and something of a different type as well scheme_animals = \ ''' (InheritanceLink (ConceptNode "Frog") (ConceptNode "animal")) (InheritanceLink (ConceptNode "Zebra") (ConceptNode "animal")) (InheritanceLink (ConceptNode "Deer") (ConceptNode "animal")) (InheritanceLink (ConceptNode "Spaceship") (ConceptNode "machine")) ''' scheme_eval_h(self.atomspace, scheme_animals) # Define a graph search query bind_link_query = \ ''' (BindLink ;; The variable to be grounded (VariableNode "$var") (ImplicationLink ;; The pattern to be grounded (InheritanceLink (VariableNode "$var") (ConceptNode "animal") ) ;; The grounding to be returned. (VariableNode "$var") ) ) ''' self.bindlink_handle = scheme_eval_h(self.atomspace, bind_link_query)
def setUp(self): self.atomspace= AtomSpace() __init__(self.atomspace) data=["opencog/scm/config.scm", "opencog/scm/core_types.scm", "spacetime/spacetime_types.scm", "opencog/nlp/types/nlp_types.scm", "opencog/dynamics/attention/attention_types.scm", "opencog/embodiment/embodiment_types.scm", "opencog/reasoning/pln/pln_types.scm", "opencog/scm/apply.scm", "opencog/scm/file-utils.scm", "opencog/scm/persistence.scm", #"opencog/scm/repl-shell.scm", "opencog/scm/utilities.scm", "opencog/scm/av-tv.scm", "opencog/nlp/scm/type-definitions.scm", "opencog/nlp/scm/config.scm", "opencog/nlp/scm/file-utils.scm", "opencog/nlp/scm/nlp-utils.scm", "opencog/nlp/scm/disjunct-list.scm", "opencog/nlp/scm/processing-utils.scm", ] for item in data: status=load_scm(self.atomspace, item) self.hobbsAgent=HobbsAgent()
def setUp(self): self.atomspace = AtomSpace() # Get the config file name in a manner not dependent on the # starting working directory. full_path = os.path.realpath(__file__) config_file_name = os.path.dirname(full_path) + "/bindlink_test.conf" # Initialize Python initialize_opencog(self.atomspace, config_file_name) # Define several animals and something of a different type as well InheritanceLink( ConceptNode("Frog"), ConceptNode("animal")) InheritanceLink( ConceptNode("Zebra"), ConceptNode("animal")) InheritanceLink( ConceptNode("Deer"), ConceptNode("animal")) InheritanceLink( ConceptNode("Spaceship"), ConceptNode("machine")) # Define a graph search query self.bindlink_handle = \ BindLink( # The variable node to be grounded. VariableNode("$var"), # The pattern to be grounded. ImplicationLink( InheritanceLink( VariableNode("$var"), ConceptNode("animal") ), # The grounding to be returned. VariableNode("$var") ) # bindlink needs a handle ).h
def setUp(self): self.atomspace= AtomSpace() scheme_eval(self.atomspace, "(add-to-load-path \"/usr/local/share/opencog/scm\")") scheme_eval(self.atomspace, "(use-modules (opencog))") scheme_eval(self.atomspace, "(use-modules (opencog atom-types))") scheme_eval(self.atomspace, "(use-modules (opencog query))") data=["opencog/scm/config.scm", "opencog/scm/core_types.scm", "opencog/scm/apply.scm", "opencog/scm/file-utils.scm", "opencog/scm/utilities.scm", "opencog/scm/av-tv.scm", "opencog/nlp/scm/type-definitions.scm", "opencog/nlp/scm/config.scm", "opencog/nlp/scm/file-utils.scm", "opencog/nlp/scm/nlp-utils.scm", "opencog/nlp/scm/disjunct-list.scm", "opencog/nlp/scm/processing-utils.scm", ] for item in data: status=load_scm(self.atomspace, item) # print "load status=", status, " item=", item self.hobbsAgent=HobbsAgent()
def setUp(self): self.atomspace = AtomSpace() resolution = 1 floor_height = -255 agent_height = 1.6 self.testmap = Octree3DMapManager.init_new_map(self.atomspace, "testmap", resolution, floor_height, agent_height)
def setUp(self): self.a = AtomSpace() self.x1 = self.a.add(t.ConceptNode,"test1") self.x2 = self.a.add(t.ConceptNode,"test2") self.l1 = self.a.add(t.Link, out=[self.x1,self.x2]) self.l2 = self.a.add(t.Link, out=[self.l1,self.x2]) print 'l1', self.l1
def setUp(self): self.atomspace= AtomSpace() scheme_eval(self.atomspace, "(add-to-load-path \"/usr/local/share/opencog/scm\")") scheme_eval(self.atomspace, "(use-modules (opencog))") scheme_eval(self.atomspace, "(use-modules (opencog query))") scheme_eval(self.atomspace, "(use-modules (opencog nlp))") self.hobbsAgent=HobbsAgent()
class TestTimeServer: def setUp(self): self._atomspace = AtomSpace() self._spaceserver = SpaceServer(self._atomspace) self._timeserver = TimeServer(self._atomspace, self._spaceserver) self._spaceserver.set_time_server(self._timeserver) def tearDown(self): del self._spaceserver del self._atomspace def test_addTimeInfo(self): objnode = self._atomspace.add_node(types.StructureNode, "object111") at_time_link = self._timeserver.add_time_info(objnode, 123456) assert objnode in at_time_link.out
def setUp(self): self.atomspace = AtomSpace() print "setUp - atomspace = ", self.atomspace # Get the config file name in a manner not dependent on the # starting working directory. full_path = os.path.realpath(__file__) config_file_name = os.path.dirname(full_path) + "/bindlink_test.conf" # Initialize Python initialize_opencog(self.atomspace, config_file_name) # Define several animals and something of a different type as well InheritanceLink(ConceptNode("Frog"), ConceptNode("animal")) InheritanceLink(ConceptNode("Zebra"), ConceptNode("animal")) InheritanceLink(ConceptNode("Deer"), ConceptNode("animal")) InheritanceLink(ConceptNode("Spaceship"), ConceptNode("machine")) # Define a graph search query self.bindlink_handle = \ BindLink( # The variable node to be grounded. VariableNode("$var"), # The pattern to be grounded. ImplicationLink( InheritanceLink( VariableNode("$var"), ConceptNode("animal") ), # The grounding to be returned. VariableNode("$var") ) # bindlink needs a handle ).h
def test_load_scm_file(): # load a scm file a = AtomSpace() load_scm_file(a, "./test/scm/test_load_scm_file.scm") #load_scm_file(a, "./air.scm") # output atomspace to file "diff_log" output_atomspace(a, "py_atomspace.log") # compare output with output of atomspace loaded with cogserver if rough_compare_files( "py_atomspace.log", "./test/log/server_atomspace.log") and rough_compare_files( "./test/log/server_atomspace.log", "py_atomspace.log"): log.info("test passed!") else: log.info("test failed!")
def load_atomspace(): """ loads atomspace with knowledge bases and annotation scheme functions found in scm directory. :return: atomspace instance """ atomspace = AtomSpace() logger.info("Loading Atoms") print("loading atoms") scheme_eval(atomspace, '(primitive-load "{}")'.format(config.OPENCOG_DEPS_PATH)) print("initial atoms:" + scheme_eval(atomspace, "(count-all)").decode("utf-8")) atomspace = load_datasets(atomspace) atomspace = apply_pln(atomspace) print("after datasets:" +scheme_eval(atomspace, "(count-all)").decode("utf-8")) print("done") logger.info("Atoms loaded!") return atomspace
def __init__(self): self.a = AtomSpace() self.nodes = {} # Initialize Scheme scheme_preload = [ "opencog/atomspace/core_types.scm", "opencog/scm/utilities.scm" ] scheme.__init__(self.a) for scheme_file in scheme_preload: load_scm(self.a, scheme_file) initialize_opencog(self.a) #add 3 nodes with integer values self.nodes[0] = self.a.add(types.ConceptNode, "0") self.nodes[1] = self.a.add(types.ConceptNode, "1") self.nodes[2] = self.a.add(types.ConceptNode, "2")
def test_cartpole(): env = gym.make("CartPole-v1") # Set main atomspace atomspace = AtomSpace() set_default_atomspace(atomspace) # Wrap environment wrapped_env = CartPoleWrapper(env, atomspace) # Instantiate CartPoleAgent, and tune parameters cpa = FixedCartPoleAgent(wrapped_env, atomspace) cpa.delta = 1.0e-16 # Run control loop while not cpa.control_cycle(): time.sleep(0.1)
def run(self): atomspace = AtomSpace() scheme_clear=\ """ (clear) """ scheme_code=\ """ (load-scm-from-file "/home/mahlet/webCodes/moses_result.scm") """ scheme_eval(atomspace, scheme_clear) scheme_eval(atomspace, scheme_code) #start the REST API IP_ADDRESS = '0.0.0.0' PORT = 5000 api = RESTAPI(atomspace) api.run(host=IP_ADDRESS, port=PORT)
def do_test(prep, test, description, op_time_adjustment): """Runs tests and prints the test name and performance""" if not args.columns: print description if (test != None): total_time = 0 total_ops = 0 for i in xrange(test_iterations): # Prep the test atomspace = AtomSpace() prep_result = prep(atomspace) # Time the test start = time.time() ops = test(atomspace, prep_result) finish = time.time() # Add the time and ops for this iteration total_time += finish - start total_ops += ops average_time = total_time / test_iterations average_ops = total_ops / test_iterations adjusted_time = average_time - (ops * op_time_adjustment) # Print timing results if args.verbose: time_string = "{0:.03f}".format(adjusted_time) print " Total: {0: >12}s".format(time_string) test_ops = "{0:,d}".format(int(average_ops)) print " Ops: {0: >12}".format(test_ops) op_time = "{0:.03f}".format(adjusted_time * 1000000 / ops) ops_sec = "{0:,d}".format(int(ops / adjusted_time)) if args.columns: print "{0: <40} {1: >12}µs {2: >15}".format( description, op_time, ops_sec) else: print " Op time: {0: >12}µs".format(op_time) print " Ops/sec: {0: >12}".format(ops_sec) print
def main(): path_query = 'query.scm' path_data = 'data.scm' atomspace = AtomSpace() initialize_opencog(atomspace) storage = Storage() index = Index(storage) with tmp_atomspace() as tmp: imp = ImplicationLink(VariableNode("C"), VariableNode("B")) # GetLink returns only bindings for variables pat = BindLink(imp, imp) index.add_pattern(pat) with tmp_atomspace() as tmp: base = open(path_data).read() data = scheme_eval_h(tmp, base) index.add_data(data) query = open(path_query).read() q = scheme_eval_h(tmp, query) result = index.query(tmp, q) print(result)
class UtilitiesTest(TestCase): def setUp(self): self.atomspace = AtomSpace() def tearDown(self): del self.atomspace def test_initialize_finalize(self): initialize_opencog(self.atomspace) finalize_opencog() def test_fast_load(self): gen_atoms(self.atomspace) with tempfile.TemporaryDirectory() as tmpdirname: tmp_file = os.path.join(tmpdirname, 'tmp.scm') write_sorted_file(tmp_file, self.atomspace) new_space = AtomSpace() load_file(tmp_file, new_space) self.assertTrue(len(new_space) == len(self.atomspace)) # files should be binary equal new_tmp = os.path.join(tmpdirname, 'tmp1.scm') write_sorted_file(new_tmp, new_space) self.assertTrue(filecmp.cmp(tmp_file, new_tmp, shallow=False), "files are not equal") checklist = """(ListLink(ConceptNode "vfjv\\"jnvfé") (ConceptNode "conceptIR~~gF\\",KV") (ConceptNode "вверху плыли редкие облачка"))""" with open(tmp_file, 'wt') as f: f.write(checklist) new_space1 = AtomSpace() load_file(tmp_file, new_space1) self.assertTrue(len(new_space1) == 4) def test_is_closed(self): A = self.atomspace.add_node(types.ConceptNode, 'A') B = self.atomspace.add_node(types.ConceptNode, 'B') X = self.atomspace.add_node(types.VariableNode, '$X') AB = self.atomspace.add_link(types.InheritanceLink, [A, B]) AX = self.atomspace.add_link(types.InheritanceLink, [A, X]) self.assertTrue(is_closed(AB)) self.assertFalse(is_closed(AX)) def test_get_free_variables(self): A = self.atomspace.add_node(types.ConceptNode, 'A') X = self.atomspace.add_node(types.VariableNode, '$X') AX = self.atomspace.add_link(types.InheritanceLink, [A, X]) self.assertEqual(get_free_variables(AX), [X])
def __init__(self, atomspace=None): if not atomspace: atomspace = AtomSpace() self.a = self.atomspace = atomspace #hmmmm.. is this needed? not sure where it came from __init__(self.atomspace) # To run this script outside of the cogserver required # adding the 'add_type' python binding in cython, which i have not yet # requested to be pulled to the project repo. # See https://github.com/opencog/agi-bio/tree/master/bioscience for # instructions on how to add the custom bio atom types and use config to # load when the cogserver starts up # if not is_defined('GeneNode'): # types.GeneNode = add_type(types.ConceptNode, 'GeneNode') # if not is_defined('ProteinNode'): # types.ProteinNode = add_type(types.ConceptNode, 'ProteinNode') # geneset (dicrect) members cache self.set_members_dict = {} # geneset members including descendents cache self.set_members_with_descendents_dict = {} # member genesets cache self.member_sets_dict = {} # subset relationship truth value dictionary cache self.subset_values = {} # dict of importance score for a generated relationship link self.relationship_importance_score = {} #dict of category ancestors self.category_ancestors = {} self.scheme_loaded = False
class TreeTest(TestCase): def setUp(self): self.a = AtomSpace() self.x1 = self.a.add(t.ConceptNode,"test1") self.x2 = self.a.add(t.ConceptNode,"test2") self.l1 = self.a.add(t.Link, out=[self.x1,self.x2]) self.l2 = self.a.add(t.Link, out=[self.l1,self.x2]) print 'l1', self.l1 def tearDown(self): del self.a def test_atom_tree(self): node_tree = tree.tree_from_atom(self.x1) self.assertEquals(node_tree.is_leaf(), True) 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_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_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_is_variable(self): var_tree = tree.Var(1) self.assertEquals(var_tree.is_variable(),True) node_tree = tree.T(self.x1) self.assertEquals(node_tree.is_variable(),False) 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_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]) # Test whether find_conj can be used to find atoms for Psi Rules. That is not actually done in the code, but could be useful as an alternative approach. # (This may be obsolete; an even better approach would be to use find_matching_conjunctions) def test_find_conj2(self): a = self.a conj = ( a.add(t.AtTimeLink, out=[a.add(t.TimeNode, '11210347010'), a.add(t.EvaluationLink, out=[a.add(t.PredicateNode, 'increased'), a.add(t.ListLink, out=[a.add(t.EvaluationLink, out=[a.add(t.PredicateNode, 'EnergyDemandGoal'), a.add(t.ListLink, out=[])])])])]), a.add(t.AtTimeLink, out=[a.add(t.TimeNode, '11210347000'), a.add(t.EvaluationLink, out=[a.add(t.PredicateNode, 'actionDone'), a.add(t.ListLink, out=[a.add(t.ExecutionLink, out=[a.add(t.GroundedSchemaNode, 'eat'), a.add(t.ListLink, out=[a.add(t.AccessoryNode, 'id_-54646')])])])])]), a.add(t.SequentialAndLink, out=[a.add(t.TimeNode, '11210347000'), a.add(t.TimeNode, '11210347010')]) ) conj = tuple(map(tree.tree_from_atom, conj)) res = tree.find_conj(conj,a.get_atoms_by_type(t.Atom)) def test_find_conj3(self): a = self.a t1 = tree.atom_from_tree(tree.new_var(), a) t2 = tree.atom_from_tree(tree.new_var(), a) action = tree.atom_from_tree(tree.new_var(), a) goal = tree.atom_from_tree(tree.new_var(), a) conj = ( a.add(t.AtTimeLink, out=[t1, a.add(t.EvaluationLink, out=[a.add(t.PredicateNode, 'actionDone'), action])]), a.add(t.AtTimeLink, out=[t2, a.add(t.EvaluationLink, out=[a.add(t.PredicateNode, 'increased'), a.add(t.ListLink, out=[a.add(t.EvaluationLink, out=[goal, a.add(t.ListLink, out=[])])])])]), a.add(t.SequentialAndLink, out=[a.add(t.TimeNode, '11210347000'), a.add(t.TimeNode, '11210347010')]) ) conj = tuple(map(tree.tree_from_atom, conj)) res = tree.find_conj(conj,a.get_atoms_by_type(t.Atom)) def test_apply_rule(self): atoms = [self.l1, self.l2] # This is supposed to look up all Atoms of (exactly) type 'Link', and return their first outgoing atom link_template = tree.T('Link', 1, 2) first = tree.Var(1) result_trees = tree.apply_rule(link_template, first, atoms) result_correct = map(tree.tree_from_atom, [self.x1, self.l1]) self.assertEquals(result_trees, result_correct) def test_standardize_apart(self): var1, var2 = tree.Var(1), tree.Var(2) tr1 = tree.T('ListLink', var1, var2) tr2 = tree.standardize_apart(tr1) print tr1 print tr2 self.assertNotEquals(tree.unify(tr1, tr2, {}), None) var1_new, var2_new = tr2.args self.assertNotEquals(var1_new, var2_new) assert var1_new not in [var1, var2] assert var2_new not in [var1, var2] def test_canonical_trees(self): conj = ( tree.T('ListLink', 1, 2), tree.T('ListLink', 2, 3) ) canon = tree.canonical_trees(conj) print canon
class AtomSpaceTest(TestCase): def setUp(self): self.space = AtomSpace() def tearDown(self): del self.space def test_add_node(self): a1 = self.space.add_node(types.Node, "test") self.assertTrue(a1) # duplicates resolve to same handle a2 = self.space.add_node(types.Node, "test") self.assertEquals(a1, a2) # Should fail when intentionally adding bad type # self.assertRaises(RuntimeError, self.space.add_node(types.Link, "test")) caught = False try: self.space.add_node(types.Link, "test") except RuntimeError: caught = True self.assertEquals(caught, True) # test adding with a truthvalue a3 = self.space.add_node(types.Node, "test_w_tv", TruthValue(0.5, 100)) self.assertEquals(self.space.size(), 2) # test adding with prefixed node a1 = self.space.add_node(types.Node, "test", prefixed=True) a2 = self.space.add_node(types.Node, "test", prefixed=True) self.assertNotEqual(a1, a2) self.assertEquals(self.space.size(), 4) a3 = self.space.add_node(types.Node, "test", TruthValue(0.5, 100), prefixed=True) self.assertNotEqual(a1, a3) self.assertEquals(self.space.size(), 5) # tests with bad parameters # test with not a proper truthvalue self.assertRaises(TypeError, self.space.add_node, types.Node, "test", 0, True) # test with bad type self.assertRaises(TypeError, self.space.add_node, "ConceptNode", "test", TruthValue(0.5, 100)) def test_add_link(self): n1 = self.space.add_node(types.Node, "test1") n2 = self.space.add_node(types.Node, "test2") l1 = self.space.add_link(types.Link, [n1, n2]) self.assertTrue(l1 is not None) l2 = self.space.add_link(types.Link, [n1, n2]) self.assertTrue(l2 is not None) self.assertTrue(l2 == l1) n3 = self.space.add_node(types.Node, "test3") l3 = self.space.add_link(types.Link, [n1, n3], TruthValue(0.5, 100)) self.assertTrue(l3 is not None) # Test with a handle instead of atom l4 = self.space.add_link(types.Link, [n2.h, n3], TruthValue(0.5, 100)) self.assertTrue(l4 is not None) # Should fail when adding an intentionally bad type caught = False try: l1 = self.space.add_link(types.Node, [n1, n3]) except RuntimeError: caught = True self.assertEquals(caught, True) def test_is_valid(self): h1 = self.space.add_node(types.Node, "test1") # check with Handle object self.assertTrue(self.space.is_valid(h1.h)) # check with raw UUID self.assertTrue(self.space.is_valid(h1.h.value())) # check with bad UUID self.assertFalse(self.space.is_valid(2919)) # check with bad type self.assertRaises(TypeError, self.space.is_valid, "test") def test_truth_value(self): # check attributes come back as assigned tv = TruthValue(0.5, 100) self.assertEqual(tv.mean, 0.5) self.assertEqual(tv.count, 100) # test confidence self.assertAlmostEqual(tv.confidence, 0.1111, places=4) # test string representation self.assertEqual(str(tv), "(stv 0.500000 0.111111)") # check equality tv2 = TruthValue(0.5, 100) tv3 = TruthValue(0.6, 100) self.assertTrue(tv == tv2) self.assertFalse(tv == tv3) def test_get_by_name_and_type(self): n1 = self.space.add_node(types.Node, "test") n2 = self.space.add_node(types.ConceptNode, "test") n3 = self.space.add_node(types.PredicateNode, "test") # test recursive subtypes result = self.space.get_atoms_by_name(types.Node, "test") self.assertTrue(n1 in result) self.assertTrue(n2 in result) self.assertTrue(n3 in result) # test non-recursive subtype result = self.space.get_atoms_by_name(types.Node, "test", subtype=False) self.assertTrue(n1 in result) self.assertTrue(n2 not in result) self.assertTrue(n3 not in result) # test empty result = self.space.get_atoms_by_name(types.AnchorNode, "test", subtype=False) self.assertEqual(len(result), 0) def test_get_by_type(self): h1 = self.space.add_node(types.Node, "test1") h2 = self.space.add_node(types.ConceptNode, "test2") h3 = self.space.add_node(types.PredicateNode, "test3") # test recursive subtypes result = self.space.get_atoms_by_type(types.Node) self.assertTrue(h1 in result) self.assertTrue(h2 in result) self.assertTrue(h3 in result) # links l1 = self.space.add_link(types.InheritanceLink, [h1, h2]) result = self.space.get_atoms_by_type(types.Link) self.assertTrue(l1 in result) # test non-recursive subtype result = self.space.get_atoms_by_type(types.Node, subtype=False) self.assertTrue(h1 in result) self.assertTrue(h2 not in result) self.assertTrue(h3 not in result) # test empty result = self.space.get_atoms_by_type(types.AnchorNode, subtype=False) self.assertEqual(len(result), 0) def test_get_by_av(self): h1 = self.space.add_node(types.ConceptNode, "test1") h2 = self.space.add_node(types.ConceptNode, "test2") h3 = self.space.add_link(types.InheritanceLink, [h1, h2]) h4 = self.space.add_node(types.ConceptNode, "test4") h5 = self.space.add_node(types.ConceptNode, "test5") self.space.set_av(h=h1.h, sti=10) self.space.set_av(h=h2.h, sti=5) self.space.set_av(h=h3.h, sti=4) self.space.set_av(h=h4.h, sti=1) result = self.space.get_atoms_by_av(4, 10) assert len(result) == 3 assert set(result) == set([h1, h2, h3]) assert h4 not in result result = self.space.get_atoms_in_attentional_focus() assert len(result) == 4 assert set(result) == set([h1, h2, h3, h4]) def test_get_by_target_type(self): h1 = self.space.add_node(types.Node, "test1") h2 = self.space.add_node(types.ConceptNode, "test2") h3 = self.space.add_node(types.PredicateNode, "test3") # test it doesn't apply to Nodes result = self.space.get_atoms_by_target_type(types.Node, types.Node) self.assertTrue(h1 not in result) # links l1 = self.space.add_link(types.InheritanceLink, [h1.h, h2.h]) result = self.space.get_atoms_by_target_type(types.Link, types.ConceptNode, target_subtype=False) self.assertTrue(l1 in result) # test recursive target subtype result = self.space.get_atoms_by_target_type(types.Link, types.Node, target_subtype=True) self.assertTrue(l1 in result) def test_get_by_target_atom(self): h1 = self.space.add_node(types.Node, "test1") h2 = self.space.add_node(types.ConceptNode, "test2") h3 = self.space.add_node(types.PredicateNode, "test3") # test it doesn't apply to Nodes result = self.space.get_atoms_by_target_atom(types.Node, h1) self.assertTrue(h1 not in result) # links l1 = self.space.add_link(types.InheritanceLink, [h1, h2]) result = self.space.get_atoms_by_target_atom(types.Link, h1) self.assertTrue(l1 in result) result = self.space.get_atoms_by_target_atom(types.Link, h3) self.assertTrue(l1 not in result) def test_include_incoming_outgoing(self): frog = self.space.add_node(types.ConceptNode, "Frog") thing = self.space.add_node(types.ConceptNode, "Thing") animal = self.space.add_node(types.ConceptNode, "Animal") self.space.add_node(types.ConceptNode, "SeparateThing") self.space.add_link(types.InheritanceLink, [frog, animal]) self.space.add_link(types.InheritanceLink, [animal, thing]) assert len(self.space.include_incoming(self.space.get_atoms_by_name(types.ConceptNode, "Frog"))) == 2 assert len(self.space.include_incoming(self.space.get_atoms_by_type(types.ConceptNode))) == 6 assert len(self.space.include_outgoing(self.space.get_atoms_by_type(types.InheritanceLink))) == 5 assert len(self.space.include_outgoing( self.space.include_incoming(self.space.get_atoms_by_name(types.ConceptNode, "Frog")))) == 3 def test_remove(self): h1 = self.space.add_node(types.Node, "test1") h2 = self.space.add_node(types.ConceptNode, "test2") h3 = self.space.add_node(types.PredicateNode, "test3") self.assertTrue(h1 in self.space) self.assertTrue(h2 in self.space) self.assertTrue(h3 in self.space) self.space.remove(h1) self.assertTrue(h1 not in self.space) self.assertTrue(h2 in self.space) self.assertTrue(h3 in self.space) l = self.space.add_link(types.SimilarityLink, [h2, h3]) self.space.remove(h2, True) # won't remove it unless recursive is True self.assertTrue(h2 not in self.space) self.assertTrue(l not in self.space) def test_clear(self): h1 = self.space.add_node(types.Node, "test1") h2 = self.space.add_node(types.ConceptNode, "test2") h3 = self.space.add_node(types.PredicateNode, "test3") self.space.clear() self.assertEquals(self.space.size(), 0) self.assertEquals(self.space.size(), 0) self.assertEquals(len(self.space), 0) def test_container_methods(self): self.assertEquals(len(self.space), 0) h = Handle(100) self.assertRaises(KeyError, self.space.__getitem__, "blah") self.assertRaises(IndexError, self.space.__getitem__, h) a1 = self.space.add_node(types.Node, "test1") a2 = self.space.add_node(types.ConceptNode, "test2") a3 = self.space.add_node(types.PredicateNode, "test3") h1 = a1.h h2 = a2.h self.assertEquals(a1, self.space[h1]) self.assertTrue(h1 in self.space) self.assertTrue(a1 in self.space) self.assertTrue(h2 in self.space) self.assertTrue(len(self.space), 3)
def setUp(self): self.space = AtomSpace() initialize_opencog(self.space)
class AnaphoraUnitTester(TestCase): def setUp(self): self.atomspace= AtomSpace() scheme_eval(self.atomspace, "(use-modules (opencog))") scheme_eval(self.atomspace, "(use-modules (opencog nlp))") scheme_eval(self.atomspace, "(use-modules (opencog nlp oc))") self.hobbsAgent=HobbsAgent() def tearDown(self): del self.atomspace del self.hobbsAgent def getWord(self,name,type=types.WordInstanceNode): word = self.atomspace.add_node(type, name) return word def compare(self,list_1,list_2): if len(list_1)==len(list_2): for i in range(len(list_1)): if list_1[i]!=list_2[i].name: return False return True else: return False def test_bfs(self): ''' Testing the bfs function ''' self.hobbsAgent.initilization(self.atomspace) self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/bfs.scm")) self.hobbsAgent.run(self.atomspace) self.assertTrue(self.compare(['a','b','c','d','e','f','g'],self.hobbsAgent.bfs(self.getWord('a')))) self.atomspace.clear() def test_getWords(self): ''' Testing the getWords function ''' self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/getWords.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.compare(['a','b','c','d','e','f','g','h','j'],self.hobbsAgent.getWords())) self.atomspace.clear() def test_propose(self): ''' Testing the propose function ''' def filter_1(): print("Testing filter #1...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#4.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#5.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#6.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() def filter_2(): print("Testing filter #2...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#2/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#2/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() def filter_3(): print("Testing filter #3...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() def filter_4(): print("Testing filter #4...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() def filter_5(): print("Testing filter #5...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() def filter_6(): print("Testing filter #6...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() def filter_7(): print("Testing filter #7...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() def filter_8(): print("Testing filter #8...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() def filter_9(): print("Testing filter #9...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() def filter_10(): print("Testing filter #10...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#10/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),10)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#10/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),10)) self.atomspace.clear() def filter_11(): print("Testing filter #11...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#11/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),11)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#11/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),11)) self.atomspace.clear() def filter_12(): print("Testing filter #12...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#12/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent',types.ParseNode),12)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#12/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),12)) self.atomspace.clear() def filter_13(): print("Testing filter #13...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#13/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),13)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#13/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),13)) self.atomspace.clear() def filter_14(): print("Testing filter #14...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#14/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),14)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#14/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),14)) self.atomspace.clear() def filter_15(): print("Testing filter #15...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#15/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),15)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#15/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),15)) self.atomspace.clear() def filter_16(): print("Testing filter #16...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#16/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),16)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#16/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),16)) self.atomspace.clear() def filter_17(): print("Testing filter #17...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#17/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),17)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#17/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),17)) self.atomspace.clear() def filter_18(): print("Testing filter #18...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#18/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),18)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#18/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),18)) self.atomspace.clear() filter_1() filter_2() filter_3() filter_4() filter_5() filter_6() filter_7() filter_8() filter_9() filter_10() filter_11() filter_12() filter_13() filter_14() filter_15() filter_16() filter_17() filter_18() def test_pleonastic_if(self): self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#4.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#5.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() def test_conjunctions(self): self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/conjunction.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('waitresses'))) self.atomspace.clear()
class AtomSpaceTest(TestCase): def setUp(self): self.space = AtomSpace() initialize_opencog(self.space) def tearDown(self): finalize_opencog() del self.space def test_add_node(self): # Test long form atomspace node addition. # Test node add self.space.add_node(types.Node, "node" ) # Test with not a proper truthvalue self.assertRaises(TypeError, self.space.add_node, types.Node, "test", 0, True) # Test with bad type self.assertRaises(TypeError, self.space.add_node, "ConceptNode", "test", TruthValue(0.5, 0.8)) # From here on out we'll use the more compact type constructors a1 = Node("test") self.assertTrue(a1) # duplicates resolve to same atom a2 = Node("test") self.assertEquals(a1, a2) # Should fail when intentionally adding bad type caught = False try: self.space.add_node(types.Link, "test") except RuntimeError: caught = True self.assertEquals(caught, True) # Test adding with a truthvalue a3 = Node("test_w_tv").truth_value(0.5, 0.8) self.assertEquals(self.space.size(), 3) def test_add_link(self): n1 = Node("test1") n2 = Node("test2") l1 = Link(n1, n2) self.assertTrue(l1 is not None) l2 = Link(n1, n2) self.assertTrue(l2 is not None) self.assertTrue(l2 == l1) n3 = Node("test3") l3 = Link(n1, n3).truth_value(0.5, 0.8) self.assertTrue(l3 is not None) # Should fail when adding an intentionally bad type caught = False try: l1 = self.space.add_link(types.Node, [n1, n3]) except RuntimeError: caught = True self.assertEquals(caught, True) def test_is_valid(self): a1 = Node("test1") # check with Atom object self.assertTrue(self.space.is_valid(a1)) # check with bad type self.assertRaises(TypeError, self.space.is_valid, "test") def test_truth_value(self): # check attributes come back as assigned tv = TruthValue(0.5, 0.8) self.assertEqual(tv.mean, 0.5) self.assertAlmostEqual(tv.confidence, 0.8, places=4) # test string representation self.assertEqual(str(tv), "(stv 0.500000 0.800000)") # check equality tv2 = TruthValue(0.5, 0.8) tv3 = TruthValue(0.6, 0.8) self.assertTrue(tv == tv2) self.assertFalse(tv == tv3) # check truth_value function of atom atom = Node("atom with tv") default_tv = atom.tv atom.truth_value(0.75, 0.9) new_tv = atom.tv self.assertFalse(new_tv == default_tv) self.assertEqual(new_tv.mean, 0.75) self.assertAlmostEqual(new_tv.confidence, 0.9, places=4) def test_attention_value(self): node = Node("test") # check values come back as assigned node.sti = 1 node.lti = 2 node.vlti = 3 assert node.sti == 1 assert node.lti == 2 assert node.vlti == 3 # Check increment and decrement for vlti node.decrement_vlti() assert node.vlti == 2 node.increment_vlti() assert node.vlti == 3 # Check dictionary setting and getting of av property. node.av = {"sti": 4, "lti": 5, "vlti": 6} assert node.sti == 4 assert node.lti == 5 assert node.vlti == 6 assert node.av == {"sti": 4, "lti": 5, "vlti": 6} def test_get_by_type(self): a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") # test recursive subtypes result = self.space.get_atoms_by_type(types.Node) self.assertTrue(a1 in result) self.assertTrue(a2 in result) self.assertTrue(a3 in result) # links l1 = InheritanceLink(a1, a2) result = self.space.get_atoms_by_type(types.Link) self.assertTrue(l1 in result) # test non-recursive subtype result = self.space.get_atoms_by_type(types.Node, subtype=False) self.assertTrue(a1 in result) self.assertTrue(a2 not in result) self.assertTrue(a3 not in result) # test empty result = self.space.get_atoms_by_type(types.AnchorNode, subtype=False) self.assertEqual(len(result), 0) def test_get_by_av(self): a1 = ConceptNode("test1") a2 = ConceptNode("test2") a3 = InheritanceLink(a1, a2) a4 = ConceptNode("test4") a5 = ConceptNode("test5") a1.sti = 10 a2.sti = 5 a3.sti = 4 a4.sti = 1 #ImportanceIndex is Asynchronus give it some time sleep(1) result = self.space.get_atoms_by_av(4, 10) print "The atoms-by-av result is ", result assert len(result) == 3 assert set(result) == set([a1, a2, a3]) assert a4 not in result result = self.space.get_atoms_in_attentional_focus() assert len(result) == 4 assert set(result) == set([a1, a2, a3, a4]) def test_incoming_by_type(self): a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") # test no incoming Node for a1 result = a1.incoming_by_type(types.Node) self.assertTrue(a1 not in result) # now check links l1 = InheritanceLink(a1, a2) result = a1.incoming_by_type(types.InheritanceLink) self.assertTrue(l1 in result) result = a2.incoming_by_type(types.InheritanceLink) self.assertTrue(l1 in result) result = a3.incoming_by_type(types.InheritanceLink) self.assertTrue(l1 not in result) def test_include_incoming_outgoing(self): frog = ConceptNode("Frog") thing = ConceptNode("Thing") animal = ConceptNode("Animal") ConceptNode("SeparateThing") InheritanceLink(frog, animal) InheritanceLink(animal, thing) assert len(self.space.include_incoming([ConceptNode("Frog")])) == 2 assert len(self.space.include_outgoing(self.space.include_incoming([ConceptNode("Frog")]))) == 3 assert len(self.space.include_incoming(self.space.get_atoms_by_type(types.ConceptNode))) == 6 assert len(self.space.include_outgoing(self.space.get_atoms_by_type(types.InheritanceLink))) == 5 def test_remove(self): a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") self.assertTrue(a1 in self.space) self.assertTrue(a2 in self.space) self.assertTrue(a3 in self.space) self.space.remove(a1) self.assertTrue(a1 not in self.space) self.assertTrue(a2 in self.space) self.assertTrue(a3 in self.space) l = SimilarityLink(a2, a3) self.space.remove(a2, True) # won't remove it unless recursive is True self.assertTrue(a2 not in self.space) self.assertTrue(l not in self.space) def test_clear(self): a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") self.space.clear() self.assertEquals(self.space.size(), 0) self.assertEquals(len(self.space), 0) def test_container_methods(self): self.assertEquals(len(self.space), 0) a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") self.assertTrue(a1 in self.space) self.assertTrue(a2 in self.space) self.assertTrue(a3 in self.space) self.assertEquals(len(self.space), 3) def test_get_predicates(self): dog = ConceptNode("dog") mammal = ConceptNode("mammal") canine = ConceptNode("canine") animal = ConceptNode("animal") dog_mammal = ListLink(dog, mammal) dog_canine = ListLink(dog, canine) dog_animal = ListLink(dog, animal) isA = PredicateNode("IsA") dogIsAMammal = EvaluationLink(isA, dog_mammal) dogIsACanine = EvaluationLink(isA, dog_canine) dogIsAAnimal = EvaluationLink(isA, dog_animal) dog_predicates = self.space.get_predicates(dog) self.assertEquals(len(dog_predicates), 3) count = 0 for dogIs in self.space.xget_predicates(dog): count += 1 self.assertEquals(count, 3) def test_get_predicates_for(self): dog = ConceptNode("dog") mammal = ConceptNode("mammal") canine = ConceptNode("canine") animal = ConceptNode("animal") dog_mammal = ListLink(dog, mammal) dog_canine = ListLink(dog, canine) dog_animal = ListLink(dog, animal) isA = PredicateNode("IsA") dogIsAMammal = EvaluationLink(isA, dog_mammal) dogIsACanine = EvaluationLink(isA, dog_canine) dogIsAAnimal = EvaluationLink(isA, dog_animal) human = ConceptNode("human") dog_human = ListLink(dog, human) loves = PredicateNode("loves") dogLovesHumans = EvaluationLink(loves, dog_human) dog_predicates = self.space.get_predicates_for(dog, isA) self.assertEquals(len(dog_predicates), 3) dog_predicates = self.space.get_predicates_for(dog, loves) self.assertEquals(len(dog_predicates), 1) count = 0 for dogIsA in self.space.xget_predicates_for(dog, isA): count += 1 self.assertEquals(count, 3) count = 0 for dogLoves in self.space.xget_predicates_for(dog, loves): count += 1 self.assertEquals(count, 1)
class RulesTest(TestCase): def setUp(self): self.atomspace = AtomSpace() self.chainer = Chainer(self.atomspace) def tearDown(self): del self.atomspace del self.chainer def _inh_animal_breathe(self): '''InheritanceLink animal breathe''' default_av = {'sti':1} self.animal = self.atomspace.add_node(types.ConceptNode, "animal") self.breathe = self.atomspace.add_node(types.ConceptNode, "breathe") self.inh_animal_breathe = self.atomspace.add_link(types.InheritanceLink, [self.animal, self.breathe]) self.animal.tv = TruthValue(0.1, 1) self.breathe.tv = TruthValue(0.1, 1) self.inh_animal_breathe.tv = TruthValue(1, 1) atoms = [] atoms.append( self.animal ) atoms.append( self.breathe ) atoms.append( self.inh_animal_breathe ) for atom in atoms: atom.av = default_av return atoms # def _apply_rule(self, rule, def test_standardize_apart_input_output(self): rule = rules.InversionRule(self.chainer, types.InheritanceLink) (input, output) = rule.standardize_apart_input_output(self.chainer) def test_InversionRule(self): rule = rules.InversionRule(self.chainer, types.InheritanceLink) self._inh_animal_breathe() result = self.chainer._apply_forward(rule) print result def test_InversionRule_backward(self): rule = rules.InversionRule(self.chainer, types.InheritanceLink) self._inh_animal_breathe() self.inh_breathe_animal = self.atomspace.add_link(types.InheritanceLink, [self.breathe, self.animal]) self.inh_breathe_animal.av = {'sti':1} result = self.chainer._apply_backward(rule) print result def disabled_test_rules_generically(self): '''See what happens if you give a rule the generic inputs. This makes sure that the rule and formula don't have any basic code errors, but doesn't check that they do the right thing.''' def apply_rule(rule): generic_inputs = rule.inputs generic_outpus = rule.outputs # Take the generic required input atoms and give them boring TVs for atom in generic_inputs: atom.av = {'sti':1} atom.tv = TruthValue(1, 1) status = self.chainer._apply_forward(rule) self.assertNotEquals(status, None) return None for rule in self.chainer.rules: apply_rule(rule)
def add_to_atomspace(atoms: set[Atom] | list[Atom], atomspace: AtomSpace) -> None: """Add all atoms to the atomspace.""" for atom in atoms: atomspace.add_atom(atom)
a.add_node(t.ConceptNode, 'plan_action_list'), T('ListLink', actions)) ps_a = atom_from_tree(ps, a) pal_a = atom_from_tree(pal, a) print ps print pal ps_a.tv = TruthValue(1.0, confidence_to_count(1.0)) print ps_a.tv return result_atoms if __name__ == '__main__': a = AtomSpace() log.use_stdout(True) atom_from_tree(T('EvaluationLink',a.add_node(t.PredicateNode,'A')), a).tv = TruthValue(1, 1) #atom_from_tree(T('EvaluationLink',1), a).tv = TruthValue(1, 1) c = Chainer(a) #search(T('EvaluationLink',a.add_node(t.PredicateNode,'B'))) #fc(a) #c.bc(T('EvaluationLink',a.add_node(t.PredicateNode,'A'))) # global rules # A = T('EvaluationLink',a.add_node(t.PredicateNode,'A')) # B = T('EvaluationLink',a.add_node(t.PredicateNode,'B'))
from unittest import TestCase from opencog.atomspace import AtomSpace, TruthValue, Atom, Handle from opencog.atomspace import types, is_a, get_type, get_type_name from opencog.scheme_wrapper import load_scm, scheme_eval, scheme_eval_h, __init__ # We are poking atoms into this from the scm files, so we want # them to still be there, later. shared_space = AtomSpace() __init__(shared_space) class SchemeTest(TestCase): def setUp(self): global shared_space self.space = shared_space def tearDown(self): pass # Load several different scheme files, containing atom type # declarations, and utilities. They should load just fine. # These don't actually put any atoms into the atomspace. def test_a_load_core_types(self): # These relative paths are horridly ugly. # There must be a better way ... status = load_scm(self.space, "build/opencog/atomspace/core_types.scm") self.assertTrue(status)
The if-then is implemented via a matching clause with the pattern matcher. When a match is seen, the matcher moves on to the next clause. """ from opencog.atomspace import AtomSpace, TruthValue, types, get_type_name from opencog.bindlink import satisfaction_link from opencog.type_constructors import * from opencog.logger import Logger, log # Logging will be written to opencog.log in the current directory. log.set_level('DEBUG') log.info("Starting the stop-go demo") # The atomspace where everything will live. atomspace = AtomSpace() set_type_ctor_atomspace(atomspace) # The callback counts the number fo red and green lights. # It returns a TruthValue of TRUE for green lights and FALSE for the # red lights. FALSE is interpreted as a mismatch (failure to satisfy) # by the pattner matcher, and thus, the pattern matcher will backtrack # and sarch for a different solution. Since the example below contains # no variables, it will just backtrack to the start, and then report # non-satisfiability (which is what we want, when we get a red light). green = 0 red = 0 def stop_go(atom):
class AtomSpaceTest(TestCase): def setUp(self): self.space = AtomSpace() initialize_opencog(self.space) def tearDown(self): finalize_opencog() del self.space def test_add_node(self): # Test long form atomspace node addition. # Test node add self.space.add_node(types.Node, "node") # Test with not a proper truthvalue self.assertRaises(TypeError, self.space.add_node, types.Node, "test", 0, True) # Test with bad type self.assertRaises(TypeError, self.space.add_node, "ConceptNode", "test", TruthValue(0.5, 0.8)) # From here on out we'll use the more compact type constructors a1 = Node("test") self.assertTrue(a1) # duplicates resolve to same atom a2 = Node("test") self.assertEquals(a1, a2) # Should fail when intentionally adding bad type caught = False try: self.space.add_node(types.Link, "test") except RuntimeError: caught = True self.assertEquals(caught, True) # Test adding with a truthvalue a3 = Node("test_w_tv").truth_value(0.5, 0.8) self.assertEquals(self.space.size(), 3) def test_add_link(self): n1 = Node("test1") n2 = Node("test2") l1 = Link(n1, n2) self.assertTrue(l1 is not None) l2 = Link(n1, n2) self.assertTrue(l2 is not None) self.assertTrue(l2 == l1) n3 = Node("test3") l3 = Link(n1, n3).truth_value(0.5, 0.8) self.assertTrue(l3 is not None) # Should fail when adding an intentionally bad type caught = False try: l1 = self.space.add_link(types.Node, [n1, n3]) except RuntimeError: caught = True self.assertEquals(caught, True) def test_is_valid(self): a1 = Node("test1") # check with Atom object self.assertTrue(self.space.is_valid(a1)) # check with raw UUID self.assertTrue(self.space.is_valid(a1.value())) # check with bad UUID self.assertFalse(self.space.is_valid(2919)) # check with bad type self.assertRaises(TypeError, self.space.is_valid, "test") def test_truth_value(self): # check attributes come back as assigned tv = TruthValue(0.5, 0.8) self.assertEqual(tv.mean, 0.5) self.assertAlmostEqual(tv.confidence, 0.8, places=4) # test string representation self.assertEqual(str(tv), "(stv 0.500000 0.800000)") # check equality tv2 = TruthValue(0.5, 0.8) tv3 = TruthValue(0.6, 0.8) self.assertTrue(tv == tv2) self.assertFalse(tv == tv3) # check truth_value function of atom atom = Node("atom with tv") default_tv = atom.tv atom.truth_value(0.75, 0.9) new_tv = atom.tv self.assertFalse(new_tv == default_tv) self.assertEqual(new_tv.mean, 0.75) self.assertAlmostEqual(new_tv.confidence, 0.9, places=4) def test_attention_value(self): node = Node("test") # check values come back as assigned node.sti = 1 node.lti = 2 node.vlti = 3 assert node.sti == 1 assert node.lti == 2 assert node.vlti == 3 # Check increment and decrement for vlti node.decrement_vlti() assert node.vlti == 2 node.increment_vlti() assert node.vlti == 3 # Check dictionary setting and getting of av property. node.av = {"sti": 4, "lti": 5, "vlti": 6} assert node.sti == 4 assert node.lti == 5 assert node.vlti == 6 assert node.av == {"sti": 4, "lti": 5, "vlti": 6} def test_get_by_type(self): a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") # test recursive subtypes result = self.space.get_atoms_by_type(types.Node) self.assertTrue(a1 in result) self.assertTrue(a2 in result) self.assertTrue(a3 in result) # links l1 = InheritanceLink(a1, a2) result = self.space.get_atoms_by_type(types.Link) self.assertTrue(l1 in result) # test non-recursive subtype result = self.space.get_atoms_by_type(types.Node, subtype=False) self.assertTrue(a1 in result) self.assertTrue(a2 not in result) self.assertTrue(a3 not in result) # test empty result = self.space.get_atoms_by_type(types.AnchorNode, subtype=False) self.assertEqual(len(result), 0) def test_get_by_av(self): a1 = ConceptNode("test1") a2 = ConceptNode("test2") a3 = InheritanceLink(a1, a2) a4 = ConceptNode("test4") a5 = ConceptNode("test5") a1.sti = 10 a2.sti = 5 a3.sti = 4 a4.sti = 1 result = self.space.get_atoms_by_av(4, 10) assert len(result) == 3 assert set(result) == set([a1, a2, a3]) assert a4 not in result result = self.space.get_atoms_in_attentional_focus() assert len(result) == 4 assert set(result) == set([a1, a2, a3, a4]) def test_incoming_by_type(self): a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") # test no incoming Node for a1 result = a1.incoming_by_type(types.Node) self.assertTrue(a1 not in result) # now check links l1 = InheritanceLink(a1, a2) result = a1.incoming_by_type(types.Link) self.assertTrue(l1 in result) result = a2.incoming_by_type(types.Link) self.assertTrue(l1 in result) result = a3.incoming_by_type(types.Link) self.assertTrue(l1 not in result) def test_include_incoming_outgoing(self): frog = ConceptNode("Frog") thing = ConceptNode("Thing") animal = ConceptNode("Animal") ConceptNode("SeparateThing") InheritanceLink(frog, animal) InheritanceLink(animal, thing) assert len(self.space.include_incoming([ConceptNode("Frog")])) == 2 assert len( self.space.include_outgoing( self.space.include_incoming([ConceptNode("Frog")]))) == 3 assert len( self.space.include_incoming( self.space.get_atoms_by_type(types.ConceptNode))) == 6 assert len( self.space.include_outgoing( self.space.get_atoms_by_type(types.InheritanceLink))) == 5 def test_remove(self): a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") self.assertTrue(a1 in self.space) self.assertTrue(a2 in self.space) self.assertTrue(a3 in self.space) self.space.remove(a1) self.assertTrue(a1 not in self.space) self.assertTrue(a2 in self.space) self.assertTrue(a3 in self.space) l = SimilarityLink(a2, a3) self.space.remove(a2, True) # won't remove it unless recursive is True self.assertTrue(a2 not in self.space) self.assertTrue(l not in self.space) def test_clear(self): a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") self.space.clear() self.assertEquals(self.space.size(), 0) self.assertEquals(len(self.space), 0) def test_container_methods(self): self.assertEquals(len(self.space), 0) a1 = Node("test1") a2 = ConceptNode("test2") a3 = PredicateNode("test3") self.assertTrue(a1 in self.space) self.assertTrue(a2 in self.space) self.assertTrue(a3 in self.space) self.assertEquals(len(self.space), 3) def test_get_predicates(self): dog = ConceptNode("dog") mammal = ConceptNode("mammal") canine = ConceptNode("canine") animal = ConceptNode("animal") dog_mammal = ListLink(dog, mammal) dog_canine = ListLink(dog, canine) dog_animal = ListLink(dog, animal) isA = PredicateNode("IsA") dogIsAMammal = EvaluationLink(isA, dog_mammal) dogIsACanine = EvaluationLink(isA, dog_canine) dogIsAAnimal = EvaluationLink(isA, dog_animal) dog_predicates = self.space.get_predicates(dog) self.assertEquals(len(dog_predicates), 3) count = 0 for dogIs in self.space.xget_predicates(dog): count += 1 self.assertEquals(count, 3) def test_get_predicates_for(self): dog = ConceptNode("dog") mammal = ConceptNode("mammal") canine = ConceptNode("canine") animal = ConceptNode("animal") dog_mammal = ListLink(dog, mammal) dog_canine = ListLink(dog, canine) dog_animal = ListLink(dog, animal) isA = PredicateNode("IsA") dogIsAMammal = EvaluationLink(isA, dog_mammal) dogIsACanine = EvaluationLink(isA, dog_canine) dogIsAAnimal = EvaluationLink(isA, dog_animal) human = ConceptNode("human") dog_human = ListLink(dog, human) loves = PredicateNode("loves") dogLovesHumans = EvaluationLink(loves, dog_human) dog_predicates = self.space.get_predicates_for(dog, isA) self.assertEquals(len(dog_predicates), 3) dog_predicates = self.space.get_predicates_for(dog, loves) self.assertEquals(len(dog_predicates), 1) count = 0 for dogIsA in self.space.xget_predicates_for(dog, isA): count += 1 self.assertEquals(count, 3) count = 0 for dogLoves in self.space.xget_predicates_for(dog, loves): count += 1 self.assertEquals(count, 1)
if is_node: log.debug("%s -> %s"%(atom_stack[i].name, uni_node_id)) tree.add_edge(atom_stack[i].name, uni_node_id) else: log.debug("%s -> %s"%(atom_stack[i].name, uni_link_id)) tree.add_edge(atom_stack[i].name, uni_link_id) ## set the 'order' attribute tree.get_node_attr(atom_stack[i].name).setdefault('order',-1) tree.get_node_attr(atom_stack[i].name)['order'] += 1 order = tree.get_node_attr(atom_stack[i].name)['order'] if is_node: tree.set_edge_attr(atom_stack[i].name, uni_node_id, order = order) else: tree.set_edge_attr(atom_stack[i].name, uni_link_id, order = order) break ## deal with the last segment if tree.number_of_nodes() > 0: add_tree_to_atomspace(a, tree, root) tree.clear() log.info("loaded scm file sucessfully!" ) except IOError: log.error("failed to read file %s "%filename ) raise IOError else: f.close() if __name__ == "__main__": a = AtomSpace() load_scm_file(a, "./examples/test_load_scm_file.scm") a.print_list()
(PredicateNode "eat" (av 0 0 0) (stv 1.000000 0.000000)) ; [4] (ConceptNode "bat-man" (av 0 0 0) (stv 1.000000 0.000000)) ; [443] (ConceptNode "steak" (av 0 0 0) (stv 1.000000 0.000000)) ; [5] ) ; [655] ] """ print "--------Start fourth example--------" # Interaction Information algorithm takes very long time - about O(n*(2^n)) - # if we not give any limit. If you want to check what is going now then you can # enable below logger option. # log.use_stdout(True) # log.set_level("DEBUG") a = AtomSpace() initialize_opencog(a) high_tv = TruthValue(0.7, 0.8) low_tv = TruthValue(0.3, 0.8) # 1. Define a super-class information bat = ConceptNode("bat") man = ConceptNode("man") eat = PredicateNode("eat") steak = ConceptNode("steak") flies = ConceptNode("flies") has = PredicateNode("has") claws = ConceptNode("claws")
#! /usr/bin/env python # # create_atoms_simple.py # """ A simple way of creating atoms in the AtomSpace. See also create_stoms_by_type.py for an alternate API for atoms. """ from opencog.atomspace import AtomSpace, TruthValue from opencog.atomspace import types from opencog.type_constructors import * a = AtomSpace() # Tell the type constructors which atomspace to use. set_default_atomspace(a) # Create a truth value asserting true and mostly confident. TV = TruthValue(1, 0.8) # Add three nodes A = ConceptNode('Apple', TV) B = ConceptNode('Berry', TruthValue(0.5, 0.75)) C = ConceptNode('Comestible', TV) # Add three inhertance links, asserting that apples are berries # and that berries are edible. AB = InheritanceLink(A, B, tv=TV) BC = InheritanceLink(B, C, tv=TV) AC = InheritanceLink(A, C)
T('ListLink', actions)) ps_a = atom_from_tree(ps, a) pal_a = atom_from_tree(pal, a) print ps print pal ps_a.tv = TruthValue(1.0, confidence_to_count(1.0)) print ps_a.tv return result_atoms if __name__ == '__main__': a = AtomSpace() log.use_stdout(True) atom_from_tree(T('EvaluationLink', a.add_node(t.PredicateNode, 'A')), a).tv = TruthValue(1, 1) #atom_from_tree(T('EvaluationLink',1), a).tv = TruthValue(1, 1) c = Chainer(a) #search(T('EvaluationLink',a.add_node(t.PredicateNode,'B'))) #fc(a) #c.bc(T('EvaluationLink',a.add_node(t.PredicateNode,'A'))) # global rules # A = T('EvaluationLink',a.add_node(t.PredicateNode,'A'))
def setUp(self): self.atomspace = AtomSpace() self.test_entity_recorder = EntityRecorder.init_new_entity_recorder()
class BindlinkTest(unittest.TestCase): bindlink_atom = None getlink_atom = None atomspace = AtomSpace() starting_size = 0 def setUp(self): print ("setUp - atomspace = ", self.atomspace) # Clear atoms from previous test self.atomspace.clear() # Initialize Python initialize_opencog(self.atomspace) set_type_ctor_atomspace(self.atomspace) # Define several animals and something of a different type as well InheritanceLink( ConceptNode("Frog"), ConceptNode("animal")) InheritanceLink( ConceptNode("Zebra"), ConceptNode("animal")) InheritanceLink( ConceptNode("Deer"), ConceptNode("animal")) InheritanceLink( ConceptNode("Spaceship"), ConceptNode("machine")) # Define a graph search query self.bindlink_atom = \ BindLink( # The variable node to be grounded. VariableNode("$var"), # The pattern to be grounded. InheritanceLink( VariableNode("$var"), ConceptNode("animal") ), # The grounding to be returned. VariableNode("$var") # bindlink needs a handle ) # Define a pattern to be grounded self.getlink_atom = \ GetLink( InheritanceLink( VariableNode("$var"), ConceptNode("animal") ) ) # Remember the starting atomspace size. self.starting_size = self.atomspace.size() def tearDown(self): print ("tearDown - atomspace = ", self.atomspace) # Can't do this; finalize can be called only once, ever, and # then never again. The second call will never follow through. # Also, cannot create and delete atomspaces here; this will # confuse the PythonEval singletonInstance. # finalize_opencog() # del self.atomspace def _check_result_setlink(self, atom, expected_arity): # Check if the atom is a SetLink self.assertTrue(atom is not None) self.assertEquals(atom.type, types.SetLink) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, self.starting_size + 1) # The SetLink should have expected_arity items in it. self.assertEquals(atom.arity, expected_arity) def test_bindlink(self): atom = execute_atom(self.atomspace, self.bindlink_atom) print("Bindlink found: " + str(atom)) self._check_result_setlink(atom, 3) def test_satisfying_set(self): atom = execute_atom(self.atomspace, self.getlink_atom) self._check_result_setlink(atom, 3) def test_satisfy(self): satisfaction_atom = SatisfactionLink( VariableList(), # no variables SequentialAndLink( EvaluationLink( GroundedPredicateNode("py: test_functions.stop_go"), ListLink( ConceptNode("green light") ) ), EvaluationLink( GroundedPredicateNode("py: test_functions.stop_go"), ListLink( ConceptNode("green light") ) ), EvaluationLink( GroundedPredicateNode("py: test_functions.stop_go"), ListLink( ConceptNode("red light") ) ), EvaluationLink( GroundedPredicateNode("py: test_functions.stop_go"), ListLink( ConceptNode("traffic ticket") ) ) ) ) tv = evaluate_atom(self.atomspace, satisfaction_atom) self.assertTrue(tv is not None and tv.mean <= 0.5) self.assertEquals(green_count(), 2) self.assertEquals(red_count(), 1) def test_execute_atom(self): result = execute_atom(self.atomspace, ExecutionOutputLink( GroundedSchemaNode("py: test_functions.add_link"), ListLink( ConceptNode("one"), ConceptNode("two") ) ) ) list_link = ListLink( ConceptNode("one"), ConceptNode("two") ) self.assertEquals(result, list_link) def test_evaluate_atom(self): result = evaluate_atom(self.atomspace, EvaluationLink( GroundedPredicateNode("py: test_functions.bogus_tv"), ListLink( ConceptNode("one"), ConceptNode("two") ) ) ) self.assertEquals(result, TruthValue(0.6, 0.234)) def test_execute_atom_no_return_value(self): result = execute_atom(self.atomspace, PutLink(DeleteLink(VariableNode("X")), ConceptNode("deleteme"))) self.assertEquals(result, None)
def setUp(self): self.space = AtomSpace()
class TestEntityRecorder(unittest.TestCase): def setUp(self): self.atomspace = AtomSpace() self.test_entity_recorder = EntityRecorder.init_new_entity_recorder() def tearDown(self): del self.test_entity_recorder del self.atomspace def testAddandRemoveNoneBlockEntity_NormalEntity_AllGetFunctionsWork(self): test_pos = (17, 28, 39) timestamp = 12345 test_handle1 = self.atomspace.add_node(types.EntityNode, "entity").h entity_is_self = False entity_is_avatar = False self.test_entity_recorder.add_none_block_entity( test_handle1, test_pos, entity_is_self, entity_is_avatar, timestamp) test_handle2 = self.test_entity_recorder.get_entity(test_pos) self.assertEqual(test_handle1, test_handle2) self.assertEqual( test_pos, self.test_entity_recorder.get_last_appeared_location(test_handle1)) self.test_entity_recorder.remove_none_block_entity(test_handle1) self.assertTrue( self.test_entity_recorder.get_entity(test_pos).is_undefined()) #preserve record self.assertEqual( test_pos, self.test_entity_recorder.get_last_appeared_location(test_handle1)) def testUpdateEntityLocation_MultipleLocation_LastLocationIsPos2(self): test_pos1 = (17, 28, 39) timestamp1 = 12345 test_handle1 = self.atomspace.add_node(types.EntityNode, "entity").h entity_is_self = False entity_is_avatar = False self.test_entity_recorder.add_none_block_entity( test_handle1, test_pos1, entity_is_self, entity_is_avatar, timestamp1) test_pos2 = (17, 28, 40) timestamp2 = 12346 self.test_entity_recorder.update_none_block_entity_location( test_handle1, test_pos2, timestamp2) last_location = self.test_entity_recorder.get_last_appeared_location( test_handle1) self.assertEqual(test_pos2, last_location) def testAddEntity_PressureTest(self): count = 10000 timestamp = 12345 while count != 0: print count count -= 1 test_pos = (count, count, count) test_handle1 = self.atomspace.add_node(types.EntityNode, "entity" + str(count)).h entity_is_self = False entity_is_avatar = False self.test_entity_recorder.add_none_block_entity( test_handle1, test_pos, entity_is_self, entity_is_avatar, timestamp) test_handle2 = self.test_entity_recorder.get_entity(test_pos) self.assertEqual(test_handle1, test_handle2) self.assertEqual( test_pos, self.test_entity_recorder.get_last_appeared_location( test_handle1)) self.test_entity_recorder.remove_none_block_entity(test_handle1) self.assertTrue( self.test_entity_recorder.get_entity(test_pos).is_undefined()) #preserve record self.assertEqual( test_pos, self.test_entity_recorder.get_last_appeared_location( test_handle1))
def assert_equals(object_one, object_two): if (object_one != object_two): print("assert_equals FAILED: one ", object_one, ", two ", object_two) def assert_true(test): if (not test): print("assert_true FAILED: test ", test) def add_link(atom_one, atom_two): return ListLink(atom_one, atom_two) atomspace = AtomSpace() # Initialize Python initialize_opencog(atomspace) # Define several animals and something of a different type as well InheritanceLink(ConceptNode("Frog"), ConceptNode("animal")) InheritanceLink(ConceptNode("Zebra"), ConceptNode("animal")) InheritanceLink(ConceptNode("Deer"), ConceptNode("animal")) InheritanceLink(ConceptNode("Spaceship"), ConceptNode("machine")).truth_value(0.5, 0.5) # Define a graph search query bindlink_atom = \ BindLink( # The variable node to be grounded.
def setUp(self): self.atomspace = AtomSpace() self.chainer = Chainer(self.atomspace)
class AnaphoraUnitTester(TestCase): def setUp(self): self.atomspace= AtomSpace() __init__(self.atomspace) data=["opencog/scm/config.scm", "opencog/scm/core_types.scm", "spacetime/spacetime_types.scm", "opencog/nlp/types/nlp_types.scm", "opencog/dynamics/attention/attention_types.scm", "opencog/embodiment/embodiment_types.scm", "opencog/reasoning/pln/pln_types.scm", "opencog/scm/apply.scm", "opencog/scm/file-utils.scm", "opencog/scm/persistence.scm", #"opencog/scm/repl-shell.scm", "opencog/scm/utilities.scm", "opencog/scm/av-tv.scm", "opencog/nlp/scm/type-definitions.scm", "opencog/nlp/scm/config.scm", "opencog/nlp/scm/file-utils.scm", "opencog/nlp/scm/nlp-utils.scm", "opencog/nlp/scm/disjunct-list.scm", "opencog/nlp/scm/processing-utils.scm", ] for item in data: status=load_scm(self.atomspace, item) self.hobbsAgent=HobbsAgent() def tearDown(self): del self.atomspace del self.hobbsAgent def getWord(self,name,type=types.WordInstanceNode): rv=self.atomspace.get_atoms_by_name(type,name) return rv[0] def compare(self,list_1,list_2): if len(list_1)==len(list_2): for i in range(len(list_1)): if list_1[i]!=list_2[i].name: return False return True else: return False @unittest.skip("Unit test disabled, see: https://github.com/opencog/opencog/issues/1280") def test_bfs(self): ''' Testing the bfs function ''' self.hobbsAgent.initilization(self.atomspace) self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/bfs.scm")) self.hobbsAgent.run(self.atomspace) self.assertTrue(self.compare(['a','b','c','d','e','f','g'],self.hobbsAgent.bfs(self.getWord('a')))) self.atomspace.clear() #@unittest.skip("debugging skipping") def test_getWords(self): ''' Testing the getWords function ''' self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/getWords.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.compare(['a','b','c','d','e','f','g','h','j'],self.hobbsAgent.getWords())) self.atomspace.clear() @unittest.skip("Unit test disabled, see: https://github.com/opencog/opencog/issues/1280") def test_propose(self): ''' Testing the propose function ''' def filter_1(): print("Testing filter #1...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#4.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#5.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#6.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() def filter_2(): print("Testing filter #2...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#2/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#2/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() def filter_3(): print("Testing filter #3...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() def filter_4(): print("Testing filter #4...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() def filter_5(): print("Testing filter #5...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() def filter_6(): print("Testing filter #6...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() def filter_7(): print("Testing filter #7...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() def filter_8(): print("Testing filter #8...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() def filter_9(): print("Testing filter #9...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() def filter_10(): print("Testing filter #10...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#10/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),10)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#10/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),10)) self.atomspace.clear() def filter_11(): print("Testing filter #11...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#11/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),11)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#11/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),11)) self.atomspace.clear() def filter_12(): print("Testing filter #12...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#12/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent',types.ParseNode),12)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#12/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),12)) self.atomspace.clear() def filter_13(): print("Testing filter #13...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#13/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),13)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#13/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),13)) self.atomspace.clear() def filter_14(): print("Testing filter #14...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#14/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),14)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#14/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),14)) self.atomspace.clear() def filter_15(): print("Testing filter #15...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#15/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),15)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#15/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),15)) self.atomspace.clear() def filter_16(): print("Testing filter #16...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#16/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),16)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#16/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),16)) self.atomspace.clear() def filter_17(): print("Testing filter #17...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#17/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),17)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#17/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),17)) self.atomspace.clear() def filter_18(): print("Testing filter #18...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#18/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),18)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#18/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),18)) self.atomspace.clear() filter_1() filter_2() filter_3() filter_4() filter_5() filter_6() filter_7() filter_8() filter_9() filter_10() filter_11() filter_12() filter_13() filter_14() filter_15() filter_16() filter_17() filter_18() #@unittest.skip("debugging skipping") def test_pleonastic_if(self): self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#4.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#5.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() #@unittest.skip("debugging skipping") def test_conjunctions(self): self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/conjunction.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('waitresses'))) self.atomspace.clear()
from opencog.type_constructors import * __author__ = 'Curtis Faith' def assert_equals(object_one, object_two): if (object_one != object_two): print "assert_equals FAILED: one ", object_one, ", two ", object_two def assert_true(test): if (not test): print "assert_true FAILED: test ", test def add_link(atom_one, atom_two): return ListLink(atom_one, atom_two) atomspace = AtomSpace() # Get the config file name in a manner not dependent on the # starting working directory. full_path = os.path.realpath(__file__) config_file_name = os.path.dirname(full_path) + "/bindlink_test.conf" print config_file_name # Initialize Python initialize_opencog(atomspace, config_file_name) # Define several animals and something of a different type as well InheritanceLink( ConceptNode("Frog"), ConceptNode("animal")) InheritanceLink( ConceptNode("Zebra"), ConceptNode("animal")) InheritanceLink( ConceptNode("Deer"), ConceptNode("animal")) InheritanceLink( ConceptNode("Spaceship"), ConceptNode("machine")).truth_value(0.5,0.5)
class AnaphoraUnitTester(TestCase): def setUp(self): self.atomspace= AtomSpace() scheme_eval(self.atomspace, "(add-to-load-path \"/usr/local/share/opencog/scm\")") scheme_eval(self.atomspace, "(use-modules (opencog))") scheme_eval(self.atomspace, "(use-modules (opencog atom-types))") scheme_eval(self.atomspace, "(use-modules (opencog query))") data=["opencog/scm/config.scm", "opencog/scm/core_types.scm", "opencog/scm/apply.scm", "opencog/scm/file-utils.scm", "opencog/scm/utilities.scm", "opencog/scm/av-tv.scm", "opencog/nlp/scm/type-definitions.scm", "opencog/nlp/scm/config.scm", "opencog/nlp/scm/file-utils.scm", "opencog/nlp/scm/nlp-utils.scm", "opencog/nlp/scm/disjunct-list.scm", "opencog/nlp/scm/processing-utils.scm", ] for item in data: status=load_scm(self.atomspace, item) # print "load status=", status, " item=", item self.hobbsAgent=HobbsAgent() def tearDown(self): del self.atomspace del self.hobbsAgent def getWord(self,name,type=types.WordInstanceNode): rv=self.atomspace.get_atoms_by_name(type,name) return rv[0] def compare(self,list_1,list_2): if len(list_1)==len(list_2): for i in range(len(list_1)): if list_1[i]!=list_2[i].name: return False return True else: return False def test_bfs(self): ''' Testing the bfs function ''' self.hobbsAgent.initilization(self.atomspace) self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/bfs.scm")) self.hobbsAgent.run(self.atomspace) self.assertTrue(self.compare(['a','b','c','d','e','f','g'],self.hobbsAgent.bfs(self.getWord('a')))) self.atomspace.clear() #@unittest.skip("debugging skipping") def test_getWords(self): ''' Testing the getWords function ''' self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/getWords.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.compare(['a','b','c','d','e','f','g','h','j'],self.hobbsAgent.getWords())) self.atomspace.clear() def test_propose(self): ''' Testing the propose function ''' def filter_1(): print("Testing filter #1...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#4.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#5.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#1/data_#6.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() def filter_2(): print("Testing filter #2...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#2/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#2/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'))) self.atomspace.clear() def filter_3(): print("Testing filter #3...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#3/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),3)) self.atomspace.clear() def filter_4(): print("Testing filter #4...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#4/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),4)) self.atomspace.clear() def filter_5(): print("Testing filter #5...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#5/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),5)) self.atomspace.clear() def filter_6(): print("Testing filter #6...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#6/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),6)) self.atomspace.clear() def filter_7(): print("Testing filter #7...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#7/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),7)) self.atomspace.clear() def filter_8(): print("Testing filter #8...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#8/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),8)) self.atomspace.clear() def filter_9(): print("Testing filter #9...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#9/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),9)) self.atomspace.clear() def filter_10(): print("Testing filter #10...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#10/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),10)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#10/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),10)) self.atomspace.clear() def filter_11(): print("Testing filter #11...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#11/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),11)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#11/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),11)) self.atomspace.clear() def filter_12(): print("Testing filter #12...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#12/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent',types.ParseNode),12)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#12/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),12)) self.atomspace.clear() def filter_13(): print("Testing filter #13...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#13/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),13)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#13/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),13)) self.atomspace.clear() def filter_14(): print("Testing filter #14...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#14/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),14)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#14/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),14)) self.atomspace.clear() def filter_15(): print("Testing filter #15...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#15/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),15)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#15/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),15)) self.atomspace.clear() def filter_16(): print("Testing filter #16...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#16/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),16)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#16/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),16)) self.atomspace.clear() def filter_17(): print("Testing filter #17...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#17/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),17)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#17/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),17)) self.atomspace.clear() def filter_18(): print("Testing filter #18...") self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#18/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.propose(self.getWord('antecedent'),18)) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/propose/filter-#18/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.propose(self.getWord('antecedent'),18)) self.atomspace.clear() filter_1() filter_2() filter_3() filter_4() filter_5() filter_6() filter_7() filter_8() filter_9() filter_10() filter_11() filter_12() filter_13() filter_14() filter_15() filter_16() filter_17() filter_18() #@unittest.skip("debugging skipping") def test_pleonastic_if(self): self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#1.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#2.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#3.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertTrue(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#4.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/pleonastic_it/data_#5.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('it'))) self.atomspace.clear() #@unittest.skip("debugging skipping") def test_conjunctions(self): self.assertTrue(load_scm(self.atomspace, "tests/nlp/anaphora/data/conjunction.scm")) self.hobbsAgent.initilization(self.atomspace) self.assertFalse(self.hobbsAgent.pleonastic_it(self.getWord('waitresses'))) self.atomspace.clear()
vehicle=> SimilarityLink (stv 0.100000 0.900000) person=> SimilarityLink (stv 0.800000 0.900000) ----- (ConceptNode "car-man-2") ; [537] vehicle=> SimilarityLink (stv 0.100000 0.900000) person=> SimilarityLink (stv 0.800000 0.900000) ----- (ConceptNode "car-man-3") ; [538] vehicle=> SimilarityLink (stv 0.100000 0.900000) person=> SimilarityLink (stv 0.800000 0.900000) """ print "--------Start third example--------" a = AtomSpace() initialize_opencog(a) # Make custom concept network. """ Make test nodes. """ # Nodes will be blended: car = ConceptNode("car") man = ConceptNode("man") # A. Car is metal. (Not duplicated) metal = ConceptNode("metal") # B. Car moves, man moves. (Duplicated, not conflicted) move = ConceptNode("move")
def setUp(self): self.atomspace = AtomSpace() resolution = 1 self.testmap = OctomapOcTree.init_new_map("testmap", resolution)
""" Data definitions for smokes_example.py The full definitions are in this Scheme file: https://github.com/opencog/test-datasets/blob/master/pln/tuffy/smokes/smokes.scm They are redefined in Python format here for testing, due to this bug that prevents importing Scheme files without a running cogserver: https://github.com/opencog/opencog/issues/530 """ __author__ = 'Cosmo Harrigan' from opencog.atomspace import AtomSpace, TruthValue, types atomspace = AtomSpace() # Basic variable definitions X = atomspace.add_node(types.VariableNode, "$X") lX = atomspace.add_link(types.ListLink, [X]) Y = atomspace.add_node(types.VariableNode, "$Y") lY = atomspace.add_link(types.ListLink, [Y]) X_Y = atomspace.add_link(types.ListLink, [X, Y]) full_confidence = TruthValue().confidence_to_count(1) crisp_true = TruthValue(1, full_confidence) # Anna smokes. Anna = atomspace.add_node(types.ConceptNode, "Anna") smokes = atomspace.add_node(types.PredicateNode, "smokes")
from opencog.atomspace import AtomSpace, TruthValue, Atom from opencog.atomspace import types as t a = AtomSpace() TV = TruthValue(1, 1) A = a.add_node(t.ConceptNode, 'A', TV) B = a.add_node(t.ConceptNode, 'B', TruthValue(0.5, 1)) C = a.add_node(t.ConceptNode, 'C', TV) AB = a.add_link(t.InheritanceLink, [A, B], TV) BC = a.add_link(t.InheritanceLink, [B, C], TV) AC = a.add_link(t.InheritanceLink, [A, C]) a.print_list() import logic from tree import * target = tree_from_atom(AC) chainer = logic.Chainer(a) results = chainer.bc(target) print '\n---------------------------\n' print results print AC
class ForwardChainerTest(TestCase): def setUp(self): self.atomspace = AtomSpace() self.chainer = Chainer(self.atomspace) def tearDown(self): del self.atomspace del self.chainer def _simple_atoms_1(self): atoms = [] atoms.append(self.atomspace.add_node(types.ConceptNode, "animal")) atoms.append(self.atomspace.add_node(types.ConceptNode, "breathe")) atoms.append( self.atomspace.add_link(types.InheritanceLink, [atoms[0], atoms[1]])) return atoms def test__selectOne(self): atoms = self._simple_atoms_1() atom = self.chainer._selectOne(atoms) self.assertNotEquals(atom, None) self.assertEqual(atom, atoms[0]) atom = self.chainer._selectOne(atoms) self.assertTrue(atom in atoms) def test_get_attentional_focus(self): atoms = self._simple_atoms_1() print atoms[2] contents = get_attentional_focus(self.atomspace) print contents self.assertEqual(len(contents), 1) def test__select_one_matching(self): atoms = self._simple_atoms_1() template = atoms[2] result = self.chainer._select_one_matching(template) self.assertEqual(result, atoms[2]) print get_attentional_focus(self.atomspace) template = self.atomspace.add_node(types.VariableNode, "$v1") result = self.chainer._select_one_matching(template) self.assertNotEqual(result, None) def test__find_inputs_recursive(self): def apply(generic_inputs, generic_outputs): inputs = [] outputs = [] empty_substitution = {} status = self.chainer._find_inputs_recursive( inputs, outputs, generic_inputs, generic_outputs, empty_substitution) return (inputs, outputs) atoms = self._simple_atoms_1() # test it on lots of simple made up rules that include the edge cases # [] => [] generic_inputs = [] generic_outputs = [] (inputs, outputs) = apply(generic_inputs, generic_outputs) self.assertEquals(inputs, []) self.assertEquals(outputs, []) # [animal] => [] generic_inputs = [atoms[0]] generic_outputs = [] (inputs, outputs) = apply(generic_inputs, generic_outputs) self.assertEquals(inputs, [atoms[0]]) self.assertEquals(outputs, []) v1 = self.atomspace.add_node(types.VariableNode, "$v1") # [v1] => [] generic_inputs = [v1] generic_outputs = [] (inputs, outputs) = apply(generic_inputs, generic_outputs) self.assertEquals(len(inputs), 1) self.assertEquals(outputs, []) #from nose.tools import set_trace; set_trace() # [v1] => [v1] generic_inputs = [v1] generic_outputs = [v1] (inputs, outputs) = apply(generic_inputs, generic_outputs) print str(inputs[0]) print str(outputs[0]) self.assertEquals(len(inputs), 1) self.assertEquals(len(outputs), 1)
class AtomTest(TestCase): def setUp(self): self.space = AtomSpace() def test_creation(self): a = self.space.add_node(types.Node, "test1") self.assertEqual(a.name, "test1") self.assertEqual(a.tv, TruthValue(1.0, 0.0)) # default is true, no confidence def test_w_truthvalue(self): tv = TruthValue(0.5, 100) a = self.space.add_node(types.Node, "test2", tv) self.assertEqual(a.tv, tv) # test set tv a.tv = TruthValue(0.1, 10) self.assertEqual(a.tv, TruthValue(0.1, 10)) def test_w_attention_value(self): a = self.space.add_node(types.Node, "test2") self.assertEqual(a.av, {'lti': 0, 'sti': 0, 'vlti': False}) # test set av a.av = { "sti": 10, "lti": 1, "vlti": True } self.assertEqual(a.av, {'sti': 10, 'lti': 1, 'vlti': True}) def test_out(self): # test get out a1 = self.space.add_node(types.Node, "test2") self.assertEqual(a1.out, []) tv = TruthValue(0.5, 100) a2 = self.space.add_node(types.Node, "test3", tv) l = self.space.add_link(types.Link, [a1, a2]) self.assertEqual(l.out, [a1, a2]) # ensure out is considered immutable self.assertRaises(AttributeError, setattr, l, "out", [a1]) def test_arity(self): a1 = self.space.add_node(types.Node, "test2") self.assertEqual(a1.arity, 0) tv = TruthValue(0.5, 100) a2 = self.space.add_node(types.Node, "test3", tv) l = self.space.add_link(types.Link, [a1, a2]) self.assertEqual(l.arity, 2) # ensure arity is considered immutable self.assertRaises(AttributeError, setattr, l, "arity", 4) def test_is_source(self): # any out item is a source for unordered links # only the fist item is a source of ordered links a1 = self.space.add_node(types.Node, "test1") a2 = self.space.add_node(types.Node, "test2") l_ordered = self.space.add_link(types.OrderedLink, [a1, a2]) l_unordered = self.space.add_link(types.UnorderedLink, [a1, a2]) self.assertEqual(l_ordered.is_source(a1), True) self.assertEqual(l_ordered.is_source(a2), False) self.assertEqual(l_unordered.is_source(a1), True) self.assertEqual(l_unordered.is_source(a2), True) def test_type(self): # test get out a = self.space.add_node(types.Node, "test2") self.assertEqual(a.type, 1) self.assertEqual(a.t, 1) a2 = self.space.add_node(types.Node, "test3") l = self.space.add_link(types.Link, [a, a2]) self.assertEqual(l.type, 2) self.assertEqual(l.t, 2) # ensure type is considered immutable self.assertRaises(AttributeError, setattr, l, "type", 5) self.assertRaises(AttributeError, setattr, a, "type", 5) self.assertEqual(l.type_name, "Link") self.assertEqual(a.type_name, "Node") def test_strings(self): # set up a link and atoms tv = TruthValue(0.5, 100) a1 = self.space.add_node(types.Node, "test1", tv) a2 = self.space.add_node(types.Node, "test2") a2.av = {"sti": 10, "lti": 1, "vlti": True} a2.tv = TruthValue(0.1, 10) l = self.space.add_link(types.Link, [a1, a2]) # test string representation a1_expected = "(Node \"test1\") ; [{0}]\n".format(str(a1.h.value())) a1_expected_long = \ "(Node \"test1\" (av 0 0 0) (stv 0.500000 0.111111)) ; [{0}]\n"\ .format(str(a1.h.value())) a2_expected = "(Node \"test2\") ; [{0}]\n".format(str(a2.h.value())) a2_expected_long = \ "(Node \"test2\" (av 10 1 1) (stv 0.100000 0.012346)) ; [{0}]\n"\ .format(str(a2.h.value())) l_expected = \ "(Link (stv 1.000000 0.000000)\n {0} {1}) ; [{2}]\n"\ .format(a1_expected, a2_expected, str(l.h.value())) l_expected_long = \ "(Link (av 0 0 0) (stv 1.000000 0.000000)\n {0} {1}) ; [{2}]\n"\ .format(a1_expected_long, a2_expected_long, str(l.h.value())) self.assertEqual(str(a2), a2_expected) self.assertEqual(a2.long_string(), a2_expected_long) self.assertEqual(str(l), l_expected) self.assertEqual(l.long_string(), l_expected_long)
from opencog.scheme_wrapper import load_scm, scheme_eval, scheme_eval_h, __init__ from pln.chainers import Chainer from pln.rules.temporal_rules import create_temporal_rules __author__ = 'Sebastian Ruder' num_steps = 100 print_starting_contents = True coreTypes = "opencog/scm/core_types.scm" utilities = "opencog/scm/utilities.scm" timeLinks = "opencog/spacetime/spacetime_types.scm" data = "tests/python/test_pln/scm_disabled/temporal/temporalToyExample.scm" # data = "opencog/python/pln_old/examples/temporal/temporal-r2l-input.scm" # initialize atomspace atomspace = AtomSpace() __init__(atomspace) for item in [coreTypes, utilities, timeLinks, data]: load_scm(atomspace, item) # initialize chainer chainer = Chainer(atomspace, stimulateAtoms=False, allow_output_with_variables=True, delete_temporary_variables=True) for rule in create_temporal_rules(chainer): chainer.add_rule(rule) if print_starting_contents: print('AtomSpace starting contents:') atomspace.print_list()
class TreeTest(TestCase): def setUp(self): self.a = AtomSpace() self.x1 = self.a.add(t.ConceptNode, "test1") self.x2 = self.a.add(t.ConceptNode, "test2") self.l1 = self.a.add(t.Link, out=[self.x1, self.x2]) self.l2 = self.a.add(t.Link, out=[self.l1, self.x2]) print 'l1', self.l1 def tearDown(self): del self.a def test_atom_tree(self): node_tree = tree.tree_from_atom(self.x1) self.assertEquals(node_tree.is_leaf(), True) 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_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_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_is_variable(self): var_tree = tree.Var(1) self.assertEquals(var_tree.is_variable(), True) node_tree = tree.T(self.x1) self.assertEquals(node_tree.is_variable(), False) 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_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]) # Test whether find_conj can be used to find atoms for Psi Rules. That is not actually done in the code, but could be useful as an alternative approach. # (This may be obsolete; an even better approach would be to use find_matching_conjunctions) def test_find_conj2(self): a = self.a conj = (a.add( t.AtTimeLink, out=[ a.add(t.TimeNode, '11210347010'), a.add(t.EvaluationLink, out=[ a.add(t.PredicateNode, 'increased'), a.add(t.ListLink, out=[ a.add(t.EvaluationLink, out=[ a.add(t.PredicateNode, 'EnergyDemandGoal'), a.add(t.ListLink, out=[]) ]) ]) ]) ]), a.add( t.AtTimeLink, out=[ a.add(t.TimeNode, '11210347000'), a.add( t.EvaluationLink, out=[ a.add(t.PredicateNode, 'actionDone'), a.add( t.ListLink, out=[ a.add( t.ExecutionLink, out=[ a.add(t.GroundedSchemaNode, 'eat'), a.add(t.ListLink, out=[ a.add( t.AccessoryNode, 'id_-54646') ]) ]) ]) ]) ]), a.add(t.SequentialAndLink, out=[ a.add(t.TimeNode, '11210347000'), a.add(t.TimeNode, '11210347010') ])) conj = tuple(map(tree.tree_from_atom, conj)) res = tree.find_conj(conj, a.get_atoms_by_type(t.Atom)) def test_find_conj3(self): a = self.a t1 = tree.atom_from_tree(tree.new_var(), a) t2 = tree.atom_from_tree(tree.new_var(), a) action = tree.atom_from_tree(tree.new_var(), a) goal = tree.atom_from_tree(tree.new_var(), a) conj = (a.add( t.AtTimeLink, out=[ t1, a.add(t.EvaluationLink, out=[a.add(t.PredicateNode, 'actionDone'), action]) ]), a.add(t.AtTimeLink, out=[ t2, a.add(t.EvaluationLink, out=[ a.add(t.PredicateNode, 'increased'), a.add(t.ListLink, out=[ a.add(t.EvaluationLink, out=[ goal, a.add(t.ListLink, out=[]) ]) ]) ]) ]), a.add(t.SequentialAndLink, out=[ a.add(t.TimeNode, '11210347000'), a.add(t.TimeNode, '11210347010') ])) conj = tuple(map(tree.tree_from_atom, conj)) res = tree.find_conj(conj, a.get_atoms_by_type(t.Atom)) def test_apply_rule(self): atoms = [self.l1, self.l2] # This is supposed to look up all Atoms of (exactly) type 'Link', and return their first outgoing atom link_template = tree.T('Link', 1, 2) first = tree.Var(1) result_trees = tree.apply_rule(link_template, first, atoms) result_correct = map(tree.tree_from_atom, [self.x1, self.l1]) self.assertEquals(result_trees, result_correct) def test_standardize_apart(self): var1, var2 = tree.Var(1), tree.Var(2) tr1 = tree.T('ListLink', var1, var2) tr2 = tree.standardize_apart(tr1) print tr1 print tr2 self.assertNotEquals(tree.unify(tr1, tr2, {}), None) var1_new, var2_new = tr2.args self.assertNotEquals(var1_new, var2_new) assert var1_new not in [var1, var2] assert var2_new not in [var1, var2] def test_canonical_trees(self): conj = (tree.T('ListLink', 1, 2), tree.T('ListLink', 2, 3)) canon = tree.canonical_trees(conj) print canon
concept_nodes = [n for n in concept_nodes if n.type_name in ['ConceptNode', 'PredicateNode']] concept_nodes = map(Tree, concept_nodes) print len(concept_nodes),'concepts' for A in concept_nodes: for B in concept_nodes: target = T('SubsetLink', A, B) print target results = chainer.bc(target) print results if __name__ == '__main__': atomspace = AtomSpace() # jade.ATOMS ENTITY = atomspace.add_node('VariableNode', '$ENTITY') BLOCK = atomspace.add_node('VariableNode', '$BLOCK') COLOR = atomspace.add_node('VariableNode', '$COLOR') atomspace.add_link('ForAllLink', [atomspace.add_link('ListLink', [ENTITY, BLOCK, COLOR]), atomspace.add_link('ImplicationLink', [atomspace.add_link('AndLink', [atomspace.add_link('EvaluationLink', [atomspace.add_node('PredicateNode', 'part-of'), atomspace.add_link('ListLink', [BLOCK, ENTITY])]), atomspace.add_link('EvaluationLink', [atomspace.add_node('PredicateNode', 'color'),
def fetch_cogscms(atomspace: AtomSpace) -> set[Atom]: """Fetch all cognitive schematics from an given atomspace.""" pit = get_type("BackPredictiveImplicationScopeLink") return set(atomspace.get_atoms_by_type(pit))