VariableNode("$var"), # The pattern to be grounded. InheritanceLink( VariableNode("$var"), ConceptNode("animal")), # The grounding to be returned. VariableNode("$var") # Bindlink needs a handle, not an atom. ).h # Remember the starting atomspace size. This test should not # change the atomspace. starting_size = atomspace.size() # Run bindlink. result = stub_bindlink(atomspace, bindlink_handle) assert_true(result is not None and result.value() > 0) # Check the ending atomspace size, it should be the same. ending_size = atomspace.size() assert_equals(ending_size, starting_size) # Remember the starting atomspace size. starting_size = atomspace.size() # Run bindlink. result = bindlink(atomspace, bindlink_handle) assert_true(result is not None and result.value() > 0)
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)
VariableNode("$var"), # The pattern to be grounded. InheritanceLink( VariableNode("$var"), ConceptNode("animal")), # The grounding to be returned. VariableNode("$var") # Bindlink needs a handle, not an atom. ) # Remember the starting atomspace size. This test should not # change the atomspace. starting_size = atomspace.size() # Run bindlink. result = stub_bindlink(atomspace, bindlink_atom) assert_true(result is not None) # Check the ending atomspace size, it should be the same. ending_size = atomspace.size() assert_equals(ending_size, starting_size) # Remember the starting atomspace size. starting_size = atomspace.size() # Run bindlink. result = bindlink(atomspace, bindlink_atom) assert_true(result is not None)
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 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() 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 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() 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 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 BindlinkTest(TestCase): bindlink_handle = None 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 tearDown(self): finalize_opencog() del self.atomspace def test_stub_bindlink(self): # Remember the starting atomspace size. This test should not # change the atomspace. starting_size = self.atomspace.size() # Run bindlink. result = stub_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should be the same. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size) def test_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink should have three items in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 3) self.assertEquals(atom.type, types.SetLink) def test_single_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = single_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink should have one item in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 1) self.assertEquals(atom.type, types.SetLink) def test_pln_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = pln_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink is empty. ??? Should it be. atom = self.atomspace[result] self.assertEquals(atom.arity, 0) self.assertEquals(atom.type, types.SetLink) def test_satisfy(self): satisfaction_handle = 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"))))).h result = satisfaction_link(self.atomspace, satisfaction_handle) self.assertTrue(result is not None and result.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))
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)
class BindlinkTest(TestCase): bindlink_handle = None 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 bound (VariableNode "$var") (ImplicationLink ;; The pattern to be searched for (InheritanceLink (VariableNode "$var") (ConceptNode "animal") ) ;; The value to be returned. (VariableNode "$var") ) ) ''' self.bindlink_handle = scheme_eval_h(self.atomspace, bind_link_query) def tearDown(self): del self.atomspace def test_stub_bindlink(self): # Remember the starting atomspace size. This test should not # change the atomspace. starting_size = self.atomspace.size() # Run bindlink. result = stub_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should be the same. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size) def test_validate_bindlink(self): # Remember the starting atomspace size. This test should not # change the atomspace. starting_size = self.atomspace.size() # Run bindlink. result = validate_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should be the same. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size) # This should return the BindLink which has two items in it, i.e. # the VariableNode and ImplicationLink. atom = self.atomspace[result] self.assertEquals(atom.arity, 2) self.assertEquals(atom.type, types.BindLink) def test_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one ListLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The ListLink should have three items in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 3) self.assertEquals(atom.type, types.ListLink) def test_single_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = single_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one ListLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The ListLink should have one item in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 1) self.assertEquals(atom.type, types.ListLink) def test_crisp_logic_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = crisp_logic_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one ListLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The ListLink should have three items in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 3) self.assertEquals(atom.type, types.ListLink) def test_pln_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = pln_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one ListLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The ListLink is empty. ??? Should it be. atom = self.atomspace[result] self.assertEquals(atom.arity, 0) self.assertEquals(atom.type, types.ListLink)
class BindlinkTest(TestCase): bindlink_handle = None 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 tearDown(self): finalize_opencog() del self.atomspace def test_stub_bindlink(self): # Remember the starting atomspace size. This test should not # change the atomspace. starting_size = self.atomspace.size() # Run bindlink. result = stub_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should be the same. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size) def test_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink should have three items in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 3) self.assertEquals(atom.type, types.SetLink) def test_single_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = single_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink should have one item in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 1) self.assertEquals(atom.type, types.SetLink) def test_crisp_logic_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = crisp_logic_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink should have three items in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 3) self.assertEquals(atom.type, types.SetLink) def test_pln_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = pln_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink is empty. ??? Should it be. atom = self.atomspace[result] self.assertEquals(atom.arity, 0) self.assertEquals(atom.type, types.SetLink) def test_satisfy(self): # XXX TODO implement a test here... pass 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) pass 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))
class BindlinkTest(TestCase): bindlink_handle = None 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 tearDown(self): del self.atomspace def test_stub_bindlink(self): # Remember the starting atomspace size. This test should not # change the atomspace. starting_size = self.atomspace.size() # Run bindlink. result = stub_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should be the same. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size) def test_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink should have three items in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 3) self.assertEquals(atom.type, types.SetLink) def test_single_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = single_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink should have one item in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 1) self.assertEquals(atom.type, types.SetLink) def test_crisp_logic_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = crisp_logic_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink should have three items in it. atom = self.atomspace[result] self.assertEquals(atom.arity, 3) self.assertEquals(atom.type, types.SetLink) def test_pln_bindlink(self): # Remember the starting atomspace size. starting_size = self.atomspace.size() # Run bindlink. result = pln_bindlink(self.atomspace, self.bindlink_handle) self.assertTrue(result is not None and result.value() > 0) # Check the ending atomspace size, it should have added one SetLink. ending_size = self.atomspace.size() self.assertEquals(ending_size, starting_size + 1) # The SetLink is empty. ??? Should it be. atom = self.atomspace[result] self.assertEquals(atom.arity, 0) self.assertEquals(atom.type, types.SetLink) def test_satisfy(self): # XXX TODO implement a test here... pass