def __init__(self, expression="", context=None): AssociativeSet.__init__(self, expression) if context: self.context = context context.connect(self)
def test_parse_nested_expression(self): root = AssociativeSet('*') parse_text = "foo bar (baz berz)" parse(parse_text, root) # Expected: foo, bar, (baz berz), baz, berz, outer expression, inner expression (parenthetical) # Plus outer ordinals: 0, 1, 2 (which should be shared w/ inner ordinals) and 5 relations to # support them, and the 'position' category. # self.assertEqual( len(root.contents), 15) self.assertTrue("foo" in root.contents) self.assertTrue("bar" in root.contents) relations = root.comprehend("baz", "berz") self.assertTrue( len(relations) > 0, "Should get a relation back for the inner expression") inner_expression = relations[0] self.assertNotEqual(inner_expression.name, '*', "Shouldn't get root back as our match") self.assertTrue("baz" in inner_expression, "Can't see first item in nested expression.") self.assertTrue("berz" in inner_expression, "Can't see second item in nested expression.")
def test_parse_nested_expression(self): root = AssociativeSet('*') parse_text = "foo bar (baz berz)" parse(parse_text, root) # Expected: foo, bar, (baz berz), baz, berz, outer expression, inner expression (parenthetical) # Plus outer ordinals: 0, 1, 2 (which should be shared w/ inner ordinals) and 5 relations to # support them, and the 'position' category. # self.assertEqual( len(root.contents), 15) self.assertTrue( "foo" in root.contents ) self.assertTrue( "bar" in root.contents ) relations = root.comprehend("baz", "berz") self.assertTrue(len(relations) > 0, "Should get a relation back for the inner expression") inner_expression = relations[0] self.assertNotEqual( inner_expression.name, '*', "Shouldn't get root back as our match") self.assertTrue("baz" in inner_expression, "Can't see first item in nested expression.") self.assertTrue("berz" in inner_expression, "Can't see second item in nested expression.")
def test_categorization_of_atom(self): expression = ['foo'] context = AssociativeSet("*") categorized = categorize(expression, context) self.assertEqual( AssociativeSet("__relation__0").name, categorized.name) # We expect a category related to the context we've created and our expression, 'foo' self.assertTrue("*" in categorized.contents) self.assertTrue("foo" in categorized.contents)
def test_parse_ordering_of_expression(self): root = AssociativeSet('*') expression_result = parse("foo bar", root) self.assertEqual(len(root.comprehend("foo", "0")), 1) # Should get ONE hit. position_relation = root.comprehend("foo", "0")[0] self.assertTrue("position" in position_relation.contents) position_tags = [x for x in position_relation.contents.values() if "position" in x.contents] self.assertTrue(position_tags, "Should be able to see the position relation marked as such.")
def test_generate_simple_dump(self): filename = "/nml/nextwheel/tests/test.dot" root = AssociativeSet('*') # parse_text = "foo bar (baz berz)" parse_text = "foo bar" parse(parse_text, root) # Expected: foo, bar, (baz berz), baz, berz, outer expression, inner expression (parenthetical) CategoryInspector(root).dump_to_file(filename) dumpfile = open(filename, "r") self.assertNotEqual(dumpfile, None, "Can't find the file?") content = dumpfile.readlines() self.assertTrue(len(content) > 0, "No lines read from dot file?") # We don't want duplicate relations. Let's only allow them once. # In this case our context is named '*' and our test category is 'bar'. lines_with_bar_and_star = [ line for line in content if "bar" in line and "*" in line ] self.assertTrue( len(lines_with_bar_and_star) == 1, "'bar' and '*' should appear only ONCE")
def test_parse_atom(self): root = AssociativeSet('.') expression = "foo" parsed = parse(expression, root) self.assertTrue(isinstance(parsed, AssociativeSet)) self.assertTrue(expression in parsed.contents, "Not seeing 'foo' in the contents.")
def test_parse_ordering_of_expression(self): root = AssociativeSet('*') expression_result = parse("foo bar", root) self.assertEqual(len(root.comprehend("foo", "0")), 1) # Should get ONE hit. position_relation = root.comprehend("foo", "0")[0] self.assertTrue("position" in position_relation.contents) position_tags = [ x for x in position_relation.contents.values() if "position" in x.contents ] self.assertTrue( position_tags, "Should be able to see the position relation marked as such.")
def test_parse_simple_expression(self): root = AssociativeSet('.') expression = "foo bar" # Note that this is the result of the expression being # *parsed*, not *parsed AND evaluated*. If it was being # evaluated there would be another category that represented # the result of the interaction. expression_result = parse(expression, root) # Expected in root: foo, bar, the relation between them, and the literal <<relation>> category. # Also the ordinal values (positions) for each element of the expression, and a relation for each. # And the 'position' category. # self.assertEqual( len(root.contents), 9) self.assertTrue("foo" in root.contents) self.assertTrue("bar" in root.contents) self.assertTrue("__relation__0" in root.contents)
def test_single_level_match(self): """ Test to see if one graph describes another (tree subset) Note that the root nodes will not be the same. This is a test to see if the subject's contents contain the given pattern. """ pattern = AssociativeSet("pattern") subject = AssociativeSet("subject") # Give the subject structure subject.connect(AssociativeSet("string")) subject.connect(AssociativeSet("blarf")) subject.connect(AssociativeSet("fizzbinn")) self.assertFalse(pattern.describes(subject), "The pattern shouldn't describe the subject yet!") # Now give the pattern part of that structure. pattern.connect(AssociativeSet("string")) self.assertTrue(pattern.describes(subject), "The pattern should describe the subject.")
def test_simplest_match(self): setA = AssociativeSet("foo") setB = AssociativeSet("foo") self.assertTrue(setA.matches(setB))
def test_creating_categorized_expression(self): context = AssociativeSet("") categories = [Category("foo", context)] foo = categorized_expression(categories, context) self.assertEqual(foo.name, "__relation__0")
from associative_tools import AssociativeSet # This should be a proper unit test. def expect(expected, msg=""): if not expected: print "Fail: %s" % msg else: print ".", s = AssociativeSet("*") # * essentially means "root" or "this namespace" s.associate("foo", "bar") expect("foo" in s.comprehend("bar")[0], "Can't associate properly?") expect("bar" in s.comprehend("foo")[0], "Can't associate properly a second time?") s = AssociativeSet("*") s.associate("foo", "bar", "baz") expect(set(["bar", "baz"]).issubset(s.comprehend("foo")[0].terms), "Multiple associations aren't working.") expect(set(["baz"]).issubset(s.comprehend("foo", "bar")[0].terms), "Multiple comprehensions aren't working.") s = AssociativeSet("*") s.associate("foo", "bar", "baz")