def load_prelude(): for line in open( os.path.dirname(os.path.abspath(__file__)) + "/prelude.w", "r"): line = line.strip() # Comment stripper. Fragile! line = line[:line.find('#')] if '#' in line else line # Ignore blank lines if line: evaluate( parse(line, ROOT), ROOT )
def test_evaluate_simple_expression(self): root = Category('*') expression = "foo bar baz" evaluate(parse(expression, root), root) # Don't forget that at this point there are a bunch of meta-categories. connections = root.comprehend("baz", "foo", "bar") self.assertEqual(len(connections), 1) # Should just have the expression.
def load_file(filename): try: for line in open( os.path.dirname(os.path.abspath(__file__)) + "/%s.w" % filename, "r"): line = line.strip() # Comment stripper. Fragile! line = line[:line.find('#')] if '#' in line else line # Ignore blank lines if line: evaluate( parse( expand_arrows(line), ROOT), ROOT ) except: print "Can't load that file for some damned reason or another."
def test_evaluate_different_atoms(self): rootA = Category('*') rootB = Category('*') expressionA = "foo" expressionB = "(foo)" evaluate(parse(expressionA, rootA), rootA) evaluate(parse(expressionB, rootB), rootB) category_delta = len(rootB.contents) - len(rootA.contents) self.assertTrue(category_delta > 0, "Second expression should generate different numbers of categories")
def test_simple_interaction(self): root = Category('*') expression = "foo bar" evaluate(parse(expression, root), root) self.assertTrue(root.has("foo")) self.assertTrue(isinstance(root.contents["foo"], Category)) foo = root.contents["foo"] bar = root.contents["bar"] self.assertTrue(bar.related(set([foo])), "no double relation?")
def test_simple_interaction(self): root = Category('*') expression = "foo bar" evaluate(parse(expression, root), root) self.assertTrue( root.has("foo") ) self.assertTrue( isinstance( root.contents["foo"], Category) ) foo = root.contents["foo"] bar = root.contents["bar"] self.assertTrue(bar.related(set([foo])), "no double relation?")
def test_getting_a_thingy_between_two_other_thingies(self): root = Category('*') expression = "apple tasty red" evaluate(parse(expression, root), root) apple = root.contents["apple"] tasty = root.contents["tasty"] red = root.contents["red"] related = root.comprehend("apple", "red") self.assertTrue(len(related) > 0, "We should have received some results back.") self.assertTrue( root.comprehend("apple", "red")[0].related( set([tasty]) ), "Can't find a relationship?")
def test_getting_a_thingy_between_two_other_thingies2(self): root = Category('*') expression = "apple tasty red" evaluate(parse(expression, root), root) apple = root.contents["apple"] tasty = root.contents["tasty"] red = root.contents["red"] relation = root.comprehend("apple", "red") self.assertTrue(len(relation) > 0, "We should have received some results back.") # TODO: Make this test less crude and more direct. self.assertTrue( tasty in relation[0].contents.values(), "Can't find a relationship?")
def test_getting_a_thingy_between_two_other_thingies(self): root = Category('*') expression = "apple tasty red" evaluate(parse(expression, root), root) apple = root.contents["apple"] tasty = root.contents["tasty"] red = root.contents["red"] related = root.comprehend("apple", "red") self.assertTrue( len(related) > 0, "We should have received some results back.") self.assertTrue( root.comprehend("apple", "red")[0].related(set([tasty])), "Can't find a relationship?")
def test_getting_a_thingy_between_two_other_thingies2(self): root = Category('*') expression = "apple tasty red" evaluate(parse(expression, root), root) apple = root.contents["apple"] tasty = root.contents["tasty"] red = root.contents["red"] relation = root.comprehend("apple", "red") self.assertTrue( len(relation) > 0, "We should have received some results back.") # TODO: Make this test less crude and more direct. self.assertTrue(tasty in relation[0].contents.values(), "Can't find a relationship?")
def test_another_interaction(self): root = Category('*') expression = "apple tasty red" evaluate(parse(expression, root), root) apple = root.contents["apple"] tasty = root.contents["tasty"] red = root.contents["red"] # TODO: Make #related not suck. self.assertTrue(apple.related(set([tasty])), "no relation?") self.assertTrue(apple.related(set([red])), "no relation?") self.assertTrue(tasty.related(set([red])), "no relation?") self.assertTrue(tasty.related(set([apple])), "no relation?") self.assertTrue(red.related(set([tasty])), "no relation?") self.assertTrue(red.related(set([apple])), "no relation?")
def test_another_interaction(self): root = Category('*') expression = "apple tasty red" evaluate(parse(expression, root), root) apple = root.contents["apple"] tasty = root.contents["tasty"] red = root.contents["red"] # TODO: Make #related not suck. self.assertTrue(apple.related( set([tasty]) ), "no relation?") self.assertTrue(apple.related( set([red]) ), "no relation?") self.assertTrue(tasty.related( set([red]) ), "no relation?") self.assertTrue(tasty.related( set([apple]) ), "no relation?") self.assertTrue(red.related( set([tasty]) ) , "no relation?") self.assertTrue(red.related( set([apple]) ), "no relation?")
def test_simultaneous_transitions(self): root = Category('*') setup_expressions = [ "foo A", "bar B", "transition (pattern (A)) (action B)", "transition (pattern (Y)) (action Z)", ] # Setup the world for expression in setup_expressions: evaluate(parse(expression, root), root) expression = "foo bar" # After evaluation the following relationships should exist: # foo B # bar Z # foo bar expected_relationships = ["foo B", "bar Z", "foo bar"] relationships = list( chain.from_iterable( [root.comprehend(*r.split()) for r in expected_relationships])) # Verify those relationships do not yet exist. self.assertFalse(any(relationships), "None of those relationships should exist yet.") evaluate(parse(expression, root), root) # Now, ALL of those relationships should exist. relationships = list( chain.from_iterable( [root.comprehend(*r.split()) for r in expected_relationships])) self.assertTrue(all(relationships), "All of those relationships should exist.")
def test_simultaneous_transitions(self): root = Category('*') setup_expressions = [ "foo A", "bar B", "transition (pattern (A)) (action B)", "transition (pattern (Y)) (action Z)", ] # Setup the world for expression in setup_expressions: evaluate(parse(expression, root), root) expression = "foo bar" # After evaluation the following relationships should exist: # foo B # bar Z # foo bar expected_relationships = [ "foo B", "bar Z", "foo bar" ] relationships = list(chain.from_iterable([ root.comprehend(*r.split()) for r in expected_relationships ])) # Verify those relationships do not yet exist. self.assertFalse(any(relationships), "None of those relationships should exist yet.") evaluate(parse(expression, root), root) # Now, ALL of those relationships should exist. relationships = list(chain.from_iterable([ root.comprehend(*r.split()) for r in expected_relationships ])) self.assertTrue(all(relationships), "All of those relationships should exist.")
def test_evaluate_atom(self): root = Category('*') root.create("relation") # Housekeeping expression = "foo" parsed = parse(expression, root) evaluate(parsed, root) # After an evaluation we should be able to see a relation which represents the result # of this particular evaluation. It will relate to a timestamp and the given expression. # In this way you should be able to see a history of evaluations. self.assertTrue( "foo" in root.contents.keys() ) self.assertTrue( root.has("foo") ) self.assertTrue( isinstance( root.contents["foo"], Category) ) foo = root.contents["foo"] self.assertFalse(foo.has("relation")) # Should not connect directly to <<relation>>
from interpreter.tools import parse, evaluate, expand_arrows from common import * import os def load_prelude(): for line in open(os.path.dirname(os.path.abspath(__file__)) + "/prelude.w", "r"): line = line.strip() # Comment stripper. Fragile! line = line[:line.find('#')] if '#' in line else line # Ignore blank lines if line: evaluate( parse( expand_arrows(line), ROOT), ROOT ) load_prelude() tokenized_lines = [] line_number = 1 for line in fileinput.input(): line = line.strip() # Comment stripper. Fragile! line = line[:line.find('#')] if '#' in line else line # Ignore blank lines if line: evaluate( parse(line, ROOT), ROOT )
if command in ('reset',): ROOT = Category("*") ROOT.add(ROOT) load_prelude() print "* was reset" continue if command.split() and command.split()[0] == "load": args = command.split() if len(args) != 2: print "This didn't make sense for loading: ", args else: load_file(args[1]) continue # Ignore blank lines if line: evaluate( parse( expand_arrows(line), ROOT), ROOT )