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)
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_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() 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_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_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_attention_value(self): a = Node("test2") self.assertEqual(a.av, {'lti': 0, 'sti': 0, 'vlti': False}) # test set av a.av = { "sti": 10, "lti": 1, "vlti": True } self.assertEqual(a.av, {'sti': 10, 'lti': 1, 'vlti': True}) def test_af_bindlink(self): atom = af_bindlink(self.atomspace, self.bindlink_atom) # The SetLink is empty. ??? Should it be. self._check_result_setlink(atom, 0)
class AtomSpaceTest(TestCase): def setUp(self): self.space = AtomSpace() initialize_opencog(self.space) def tearDown(self): finalize_opencog() del self.space 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_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_attention_value(self): a = Node("test2") self.assertEqual(a.av, {'lti': 0, 'sti': 0, 'vlti': False}) # test set av a.av = {"sti": 10, "lti": 1, "vlti": True} self.assertEqual(a.av, {'sti': 10, 'lti': 1, 'vlti': True}) def test_af_bindlink(self): atom = af_bindlink(self.atomspace, self.bindlink_atom) # The SetLink is empty. ??? Should it be. self._check_result_setlink(atom, 0)