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) ### FIXME TODO -- re-enable this test after the prefixing code is ### ### # 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_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.assertEquals(len(self.space), 3) def test_get_predicates(self): dog = self.space.add_node(types.ConceptNode, "dog") mammal = self.space.add_node(types.ConceptNode, "mammal") canine = self.space.add_node(types.ConceptNode, "canine") animal = self.space.add_node(types.ConceptNode, "animal") dog_mammal = self.space.add_link(types.ListLink, [dog, mammal]) dog_canine = self.space.add_link(types.ListLink, [dog, canine]) dog_animal = self.space.add_link(types.ListLink, [dog, animal]) isA = self.space.add_node(types.PredicateNode, "IsA") dogIsAMammal = self.space.add_link(types.EvaluationLink, [isA, dog_mammal]) dogIsACanine = self.space.add_link(types.EvaluationLink, [isA, dog_canine]) dogIsAAnimal = self.space.add_link(types.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 = self.space.add_node(types.ConceptNode, "dog") mammal = self.space.add_node(types.ConceptNode, "mammal") canine = self.space.add_node(types.ConceptNode, "canine") animal = self.space.add_node(types.ConceptNode, "animal") dog_mammal = self.space.add_link(types.ListLink, [dog, mammal]) dog_canine = self.space.add_link(types.ListLink, [dog, canine]) dog_animal = self.space.add_link(types.ListLink, [dog, animal]) isA = self.space.add_node(types.PredicateNode, "IsA") dogIsAMammal = self.space.add_link(types.EvaluationLink, [isA, dog_mammal]) dogIsACanine = self.space.add_link(types.EvaluationLink, [isA, dog_canine]) dogIsAAnimal = self.space.add_link(types.EvaluationLink, [isA, dog_animal]) human = self.space.add_node(types.ConceptNode, "human") dog_human = self.space.add_link(types.ListLink, [dog, human]) loves = self.space.add_node(types.PredicateNode, "loves") dogLovesHumans = self.space.add_link(types.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 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)
class AtomTest(TestCase): def setUp(self): self.space = AtomSpace() initialize_opencog(self.space) def tearDown(self): finalize_opencog() del self.space def test_create_child_atomspace(self): """ Test that parent atomspace will not be deleted before child """ a = opencog.atomspace.AtomSpace() b = opencog.atomspace.create_child_atomspace(a) del a def test_creation(self): a = 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 = 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_out(self): # test get out a1 = Node("test2") self.assertEqual(a1.out, []) tv = TruthValue(0.5, 100) a2 = Node("test3", tv) l = 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 = Node("test2") self.assertEqual(a1.arity, 0) tv = TruthValue(0.5, 100) a2 = Node("test3", tv) l = Link(a1, a2) self.assertEqual(l.arity, 2) # ensure arity is considered immutable self.assertRaises(AttributeError, setattr, l, "arity", 4) def test_type(self): # test get out a = Node("test2") a2 = Node("test3") l = Link(a, a2) # 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_create_child_atomspace(self): test = ConceptNode("test") b = create_child_atomspace(self.space) test2 = b.add_node(types.ConceptNode, 'test2') self.assertTrue(test in b.get_atoms_by_type(types.ConceptNode)) self.assertTrue(test2 in b.get_atoms_by_type(types.ConceptNode)) self.assertTrue(test2 not in self.space.get_atoms_by_type(types.ConceptNode)) def test_strings(self): # set up a link and atoms tv = TruthValue(0.5, 0.8) a1 = Node("test1", tv) a2 = Node("test2") a2.tv = TruthValue(0.1, 0.3) l = Link(a1, a2) space_uuid = 0 # test string representation a1_expected = "(Node \"test1\") ; [{0}]\n".format(space_uuid) a1_expected_long = \ "(Node \"test1\" (stv 0.500000 0.800000)) ; [{0}]\n"\ .format(space_uuid) a2_expected = "(Node \"test2\") ; [{0}]\n".format(space_uuid) a2_expected_long = \ "(Node \"test2\" (stv 0.100000 0.300000)) ; [{0}]\n"\ .format(space_uuid) l_expected = \ "(Link\n {0} {1}) ; [{2}]\n"\ .format(a1_expected, a2_expected, space_uuid) l_expected_long = \ "(Link\n {0} {1}) ; [{2}]\n"\ .format(a1_expected_long, a2_expected_long, space_uuid)
EXTRA_DATA = True atomspace = AtomSpace() __init__(atomspace) data = ["opencog/atomspace/core_types.scm", "opencog/scm/utilities.scm", "opencog/python/pln/examples/tuffy/smokes/smokes.scm"] if EXTRA_DATA: data.append("opencog/python/pln/examples/tuffy/smokes/extra-data.scm") for item in data: load_scm(atomspace, item) atoms = atomspace.get_atoms_by_type(types.Atom) for atom in atoms: print(atom) MAX_STEPS = 500 chainer = InferenceAgent() chainer.create_chainer(atomspace=atomspace, stimulate_atoms=False) answer = False outputs_produced = 0 for i in range(0, MAX_STEPS): result = chainer.run(atomspace) output = None input = None
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
input = None rule = None if result is not None: atomspace_string = "" (rule, input, output) = result outputs_produced += 1 print("\n----- [Output # {0}] -----".format(outputs_produced)) for j in output: print("-- Output:\n{0}".format(j)) print("-- using production rule: {0}".format(rule.name)) print("\n-- based on this input:\n{0}".format(input)) clear_atomspace() # visualization filtering for atom in atomspace.get_atoms_by_type(types.Atom): # links that shouldn't be visualized are skipped if atom.type in not_visualized_links: continue if atom not in [types.ConceptNode, types.PredicateNode]: out = [atom1 for atom1 in atomspace.get_outgoing(atom.h)] # links involving automatically generated VariableNodes are # skipped if out and "$pln_var_" in out[0].name: continue # check if result is found if atom.is_a(types.EvaluationLink): if out[0].is_a(types.PredicateNode)\ and "breathe" in out[0].name\ and out[1].is_a(types.ListLink)\
class AtomTest(TestCase): def setUp(self): self.space = AtomSpace() initialize_opencog(self.space) def tearDown(self): finalize_opencog() del self.space def test_create_child_atomspace(self): """ Test that parent atomspace will not be deleted before child """ a = opencog.atomspace.AtomSpace() b = opencog.atomspace.create_child_atomspace(a) del a def test_creation(self): a = 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 = 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_out(self): # test get out a1 = Node("test2") self.assertEqual(a1.out, []) tv = TruthValue(0.5, 100) a2 = Node("test3", tv) l = 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 = Node("test2") self.assertEqual(a1.arity, 0) tv = TruthValue(0.5, 100) a2 = Node("test3", tv) l = Link(a1, a2) self.assertEqual(l.arity, 2) # ensure arity is considered immutable self.assertRaises(AttributeError, setattr, l, "arity", 4) def test_type(self): # test get out a = Node("test2") a2 = Node("test3") l = Link(a, a2) # 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_create_child_atomspace(self): test = ConceptNode("test") b = create_child_atomspace(self.space) test2 = b.add_node(types.ConceptNode, 'test2') self.assertTrue(test in b.get_atoms_by_type(types.ConceptNode)) self.assertTrue(test2 in b.get_atoms_by_type(types.ConceptNode)) self.assertTrue( test2 not in self.space.get_atoms_by_type(types.ConceptNode)) def test_strings(self): # set up a link and atoms tv = TruthValue(0.5, 0.8) a1 = Node("test1", tv) a2 = Node("test2") a2.tv = TruthValue(0.1, 0.3) l = Link(a1, a2) space_uuid = 0 # test string representation a1_expected = "(Node \"test1\") ; [{0}]\n".format(space_uuid) a1_expected_long = \ "(Node \"test1\" (stv 0.500000 0.800000)) ; [{0}]\n"\ .format(space_uuid) a2_expected = "(Node \"test2\") ; [{0}]\n".format(space_uuid) a2_expected_long = \ "(Node \"test2\" (stv 0.100000 0.300000)) ; [{0}]\n"\ .format(space_uuid) l_expected = \ "(Link\n {0} {1}) ; [{2}]\n"\ .format(a1_expected, a2_expected, space_uuid) l_expected_long = \ "(Link\n {0} {1}) ; [{2}]\n"\ .format(a1_expected_long, a2_expected_long, space_uuid)
# """ 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) print("The atomspace contains:\n\n", a.get_atoms_by_type(types.Atom))
from opencog.type_constructors import * a = AtomSpace() # Tell the type constructors which atomspace to use. set_type_ctor_atomspace(a) # Create a truth value asserting true and mostly confident. TV = TruthValue(1, 0.8) # Add three nodes # A = a.add_node(types.ConceptNode, 'Apple', TV) # B = a.add_node(types.ConceptNode, 'Berry', TruthValue(0.5,1)) # C = a.add_node(types.ConceptNode, 'Comestible', TV) 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 = a.add_link(types.InheritanceLink, [A, B], TV) # BC = a.add_link(types.InheritanceLink, [B, C], TV) # AC = a.add_link(types.InheritanceLink, [A, C]) AB = InheritanceLink(A, B, TV) BC = InheritanceLink(B, C, TV) AC = InheritanceLink(A, C) print "The atomspace contains:\n\n", a.get_atoms_by_type(types.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 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_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_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_context_mgr_tmp(self): a = ConceptNode('a') with tmp_atomspace() as tmp_as: b = ConceptNode('b') self.assertTrue(a in self.space) # verify that current default atomspace is tmp_as self.assertFalse(b in self.space) c = ConceptNode('c') # verify that current default atomspace is self.space self.assertTrue(c in self.space)
class LogicTest(TestCase): def setUp(self): self.atomspace = AtomSpace() self.logic = Logic(self.atomspace) def tearDown(self): del self.atomspace del self.logic def _simple_atoms(self): '''InheritanceLink animal breathe''' 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]) atoms = [] atoms.append( self.animal ) atoms.append( self.breathe ) atoms.append( self.inh_animal_breathe ) return atoms def _what_breathes(self): '''InheritanceLink $v1 breathe''' self.v1 = self.atomspace.add_node(types.VariableNode, "$v1") template = self.atomspace.add_link(types.InheritanceLink, [self.v1, self.breathe]) return template def test_variable_stuff(self): var1 = self.logic.new_variable() self.assertTrue( var1.is_a(types.VariableNode) ) var2 = self.logic.new_variable() self.assertNotEqual( var1, var2 ) self.assertTrue(self.logic.is_variable(var1)) def test_find(self): atoms = self._simple_atoms() template = self.inh_animal_breathe result = self.logic.find(template, self.atomspace.get_atoms_by_type(types.Atom)) self.assertEquals( type(result), list ) self.assertEquals( result, [self.inh_animal_breathe] ) template = self._what_breathes() result = self.logic.find(template, self.atomspace.get_atoms_by_type(types.Atom)) # it should give you the inheritancelink and the template link itself! self.assertEquals( result, [self.inh_animal_breathe, template] ) def test_substitute(self): atoms = self._simple_atoms() template = self._what_breathes() cat = self.atomspace.add_node(types.ConceptNode, "cat") substitution = {self.v1: cat} # substitute $v1->cat in (InheritanceLink $v1 breathe) # producing (InheritanceLink cat breathe) intended_result = self.atomspace.add_link(types.InheritanceLink, [cat, self.breathe]) result = self.logic.substitute(substitution, template) self.assertEqual( result, intended_result)
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) # fails when adding with a link type a1 = self.space.add_node(types.Link,"test") self.assertEquals(a1,None) # 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) # fails when adding with a node type l1 = self.space.add_link(1,[n1,n3]) self.assertEquals(l1,None) 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),"[0.500000,100.000000=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.SemeNode,"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.SemeNode,subtype=False) self.assertEqual(len(result),0) 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_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) # won't remove it unless recursive is True self.assertTrue(h2 in self.space) 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)
print("-- Output:\n{0}".format(output[0])) print("-- Rule:\n{0}".format(rule)) print("\n-- Input:\n{0}".format(input)) # print('\n--- Trails:') # trails = test_agent.get_trails() # i = 0 # for item in trails: # i += 1 # print("[%s]\n%s" % (i, item)) # # print('--- History:') # history = test_agent.get_history() # pprint(vars(history)) # # with open('pln_log.txt', 'w') as logfile: # all_atoms = atomspace.get_atoms_by_type(t=types.Atom) # print('; Number of atoms in atomspace after inference: %d' % len(all_atoms), file=logfile) # for atom in all_atoms: # print(atom, file=logfile) #a = raw_input("press any key") print("******") #print(test_agent.get_trails()) #pprint(vars(test_agent.get_history())) gsn = atomspace.get_atoms_by_type(types.ExecutionLink) print(gsn)
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 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_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_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, False) # won't remove it unless recursive is True self.assertTrue(h2 in self.space) 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)
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)
from opencog.scheme_wrapper import load_scm, scheme_eval, scheme_eval_h, __init__ from pln.examples.interactive_agent import InteractiveAgent __author__ = 'Sebastian Ruder' atomspace = AtomSpace() __init__(atomspace) coreTypes = "opencog/atomspace/core_types.scm" utilities = "opencog/scm/utilities.scm" data = "opencog/python/pln_old/examples/backward_chaining/criminal.scm" for item in [coreTypes, utilities, data]: load_scm(atomspace, item) atoms = atomspace.get_atoms_by_type(types.Atom) for atom in atoms: print(atom) MAX_STEPS = 500 chainer = BackwardAgent() chainer.create_chainer(atomspace=atomspace) result_found = False outputs_produced = 0 for i in range(MAX_STEPS): result = chainer.run(atomspace) output = None input = None
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() 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_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_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_context_mgr_tmp(self): a = ConceptNode('a') with tmp_atomspace() as tmp_as: b = ConceptNode('b') self.assertTrue(a in self.space) # verify that current default atomspace is tmp_as self.assertFalse(b in self.space) c = ConceptNode('c') # verify that current default atomspace is self.space self.assertTrue(c in self.space)
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))
def to_csv(file_dir, main_nodes=False): start = time.time() if main_nodes: main_nodes = [i["geneName"] for i in main_nodes] path = os.path.join(RESULT_DIR, file_dir) input_file = os.path.join(path, "result.scm") atomspace = AtomSpace() modules = "(use-modules (opencog) (opencog bioscience) (opencog persist-file) (opencog exec))" scheme_eval(atomspace, modules) scheme_eval(atomspace, "(load-file \"{}\")".format(input_file)) df_columns = [ "Bio-entity", "Type", "Name", "Source db", "Member_of", "Has_members", "Inherits_from", "Has_inherits", "Interaction/type", "Cell Location" ] df1 = pd.DataFrame([], columns=df_columns) df2 = pd.DataFrame([], columns=df_columns) summary = dict() cross_an = dict() main_input = dict() # Find all Biological entities bio_types = scheme_eval(atomspace, "(cog-get-types)").decode("utf-8") bio_types = bio_types.replace("(", "").replace(")", "").split(" ") bio_types = [i for i in bio_types if "Node" in i] molecules = [ "Uniprot", "Gene", "Chebi", "Pubchem", "Drubank", "Refseq", "Enst" ] atoms = [] atoms_count = [{}] for t in bio_types: try: atom = atomspace.get_atoms_by_type(getattr(types, t)) if len(atom) > 0 and not t in [ "Node", "PredicateNode", "ConceptNode" ]: atoms_count[0].update({t.replace("Node", ""): len(atom)}) atoms = atoms + atom except: continue for i, val in enumerate(atoms): txt_name = find_txt_name(val, atomspace) if txt_name: node = val.name node_type = val.type_name.replace("Node", "") source = generate_url(node, node_type) count = {} member_of, has_members, count_mem = find_inherits_or_members( val, atomspace, linktype="member") count.update(count_mem) inherits_from, has_inherits, count_inh = find_inherits_or_members( val, atomspace) count.update(count_inh) location = find_locations(val, atomspace) if node_type in molecules: interacts_with, count_int = find_interactions(val, atomspace) count.update(count_int) if main_nodes and node in main_nodes: main_input[node] = [count] else: cross_an[node] = [count] df1.loc[len(df1)] = [ node, node_type, txt_name, source, member_of, has_members, inherits_from, has_inherits, interacts_with, location ] else: interacts_with = None df2.loc[len(df2)] = [ node, node_type, txt_name, source, member_of, has_members, inherits_from, has_inherits, interacts_with, location ] summary["A Reference Databases"] = "mozi.ai/datasets/" summary["Cross Annotations"] = cross_an summary["Input Genes"] = main_input summary["Total count"] = {"Count": atoms_count} with open(os.path.join(path, "summary.json"), "w") as j: json.dump(summary, j) df1 = filter_df(df1) if len(df1) > 0: df1.to_csv(os.path.join(path, "result1.csv"), index=False) df2 = filter_df(df2) if len(df2) > 0: df2.to_csv(os.path.join(path, "result2.csv"), index=False) end = time.time() return "Time to parse atomese to csv and generate summary: {}".format( end - start)