def test_unifies(): assert not agentspeak.unifies(True, 1) assert agentspeak.unifies(3, 3.0) assert agentspeak.unifies(True, True) assert agentspeak.unifies(5, agentspeak.Wildcard()) assert agentspeak.unifies((1, 2), (1, agentspeak.Wildcard())) assert agentspeak.unifies(("hello", 2), ("hello", agentspeak.Var())) assert agentspeak.unifies(agentspeak.Var(), agentspeak.Literal("beep")) assert agentspeak.unifies(agentspeak.Literal("boop"), agentspeak.Var()) X = agentspeak.Var() assert agentspeak.unifies(agentspeak.Literal("foo", (X, X)), agentspeak.Literal("foo", (1, 1))) assert not agentspeak.unifies(agentspeak.Literal("bar", (X, X)), agentspeak.Literal("bar", (1, 2)))
def test_unifies_annotated(): X = agentspeak.Var() Y = agentspeak.Var() foo_a = agentspeak.Literal("foo", (), ("a",)) foo_ab = agentspeak.Literal("foo", (), ("a", "b")) foo_XY = agentspeak.Literal("foo", (), (X, Y)) foo_XX = agentspeak.Literal("foo", (), (X, X)) assert agentspeak.unifies_annotated(foo_a, foo_ab) assert agentspeak.unifies_annotated(foo_a, foo_XY) assert agentspeak.unifies_annotated(foo_a, foo_XX) assert agentspeak.unifies_annotated(foo_ab, foo_XY) assert agentspeak.unifies_annotated(foo_XY, foo_ab) assert agentspeak.unifies_annotated(foo_XX, foo_ab) assert agentspeak.unifies_annotated(foo_XX, foo_ab) assert not agentspeak.unifies_annotated(foo_ab, foo_XX)
def test_unifies_annotated(self): X = agentspeak.Var() Y = agentspeak.Var() foo_a = agentspeak.Literal("foo", (), ("a", )) foo_ab = agentspeak.Literal("foo", (), ("a", "b")) foo_XY = agentspeak.Literal("foo", (), (X, Y)) foo_XX = agentspeak.Literal("foo", (), (X, X)) self.assertTrue(agentspeak.unifies_annotated(foo_a, foo_ab)) self.assertTrue(agentspeak.unifies_annotated(foo_a, foo_XY)) self.assertTrue(agentspeak.unifies_annotated(foo_a, foo_XX)) self.assertTrue(agentspeak.unifies_annotated(foo_ab, foo_XY)) self.assertTrue(agentspeak.unifies_annotated(foo_XY, foo_ab)) self.assertTrue(agentspeak.unifies_annotated(foo_XX, foo_ab)) self.assertTrue(agentspeak.unifies_annotated(foo_XX, foo_ab)) self.assertFalse(agentspeak.unifies_annotated(foo_ab, foo_XX)) self.assertFalse(agentspeak.unifies_annotated(foo_XY, foo_a))
def test_concat_strings(): env = agentspeak.runtime.Environment() agent = agentspeak.runtime.Agent(env, "agent") intention = agentspeak.runtime.Intention() X = agentspeak.Var() term = agentspeak.Literal(".concat", ("hello", " ", "world", X)) next(agentspeak.stdlib._concat(agent, term, intention)) assert X.grounded(intention.scope) == "hello world"
def test_concat_lists(): env = agentspeak.runtime.Environment() agent = agentspeak.runtime.Agent(env, "agent") intention = agentspeak.runtime.Intention() X = agentspeak.Var() term = agentspeak.Literal(".concat", ((1, 2), (3, ), X)) next(agentspeak.stdlib._concat(agent, term, intention)) assert X.grounded(intention.scope) == (1, 2, 3)
def visit_variable(self, ast_variable): try: return self.variables[ast_variable.name] except KeyError: if ast_variable.name == "_": var = agentspeak.Wildcard() else: var = agentspeak.Var() self.variables[ast_variable.name] = var return var
def test_unifies(self): self.assertFalse(agentspeak.unifies(True, 1)) self.assertTrue(agentspeak.unifies(3, 3.0)) self.assertTrue(agentspeak.unifies(True, True)) self.assertTrue(agentspeak.unifies(5, agentspeak.Wildcard())) self.assertTrue(agentspeak.unifies((1, 2), (1, agentspeak.Wildcard()))) self.assertTrue( agentspeak.unifies(("hello", 2), ("hello", agentspeak.Var()))) self.assertTrue( agentspeak.unifies(agentspeak.Var(), agentspeak.Literal("beep"))) self.assertTrue( agentspeak.unifies(agentspeak.Literal("boop"), agentspeak.Var())) X = agentspeak.Var() self.assertTrue( agentspeak.unifies(agentspeak.Literal("foo", (X, X)), agentspeak.Literal("foo", (1, 1)))) self.assertFalse( agentspeak.unifies(agentspeak.Literal("bar", (X, X)), agentspeak.Literal("bar", (1, 2))))
def test_request_action(self): agent = agentspeak.mapc2017.Agent() with open(os.path.join(os.path.dirname(__file__), "request-action.xml")) as xml: agent.message_received(etree.parse(xml).getroot()) term = agentspeak.Literal("money", (50000, )) intention = agentspeak.runtime.Intention() self.assertTrue(agent.test_belief(term, intention)) term = agentspeak.Literal( "shop", ("shop1", 48.8217, 2.33207, 4, agentspeak.Var())) intention = agentspeak.runtime.Intention() self.assertTrue(agent.test_belief(term, intention)) agent.dump()