Commit ca3bc24a by serfrape

Allow sending messages with variables.

parent 85f38e39
...@@ -67,8 +67,8 @@ class BDIAgent(Agent): ...@@ -67,8 +67,8 @@ class BDIAgent(Agent):
ilf_type = ilf.functor ilf_type = ilf.functor
mdata = {"performative": "BDI", mdata = {"performative": "BDI",
"ilf_type": ilf_type, } "ilf_type": ilf_type, }
body = json.dumps({"functor": str(term.args[2].functor), body = pyson.pyson_str(pyson.freeze(
"args": str(term.args[2].args)}) term.args[2], intention.scope, {}))
msg = Message(to=receiver, body=body, metadata=mdata) msg = Message(to=receiver, body=body, metadata=mdata)
self.agent.submit(self.send(msg)) self.agent.submit(self.send(msg))
yield yield
...@@ -193,7 +193,6 @@ class BDIAgent(Agent): ...@@ -193,7 +193,6 @@ class BDIAgent(Agent):
if self.agent.bdi_enabled: if self.agent.bdi_enabled:
msg = await self.receive(timeout=0) msg = await self.receive(timeout=0)
if msg: if msg:
received = json.loads(msg.body)
mdata = msg.metadata mdata = msg.metadata
ilf_type = mdata["ilf_type"] ilf_type = mdata["ilf_type"]
if ilf_type == "tell": if ilf_type == "tell":
...@@ -208,15 +207,13 @@ class BDIAgent(Agent): ...@@ -208,15 +207,13 @@ class BDIAgent(Agent):
else: else:
raise pyson.PysonError( raise pyson.PysonError(
"unknown illocutionary force: %s" % ilf_type) "unknown illocutionary force: %s" % ilf_type)
intention = pyson.runtime.Intention() intention = pyson.runtime.Intention()
args = literal_eval(received["args"]) functor, args = await parse_literal(msg.body)
if args != tuple(): message = pyson.Literal(functor, args)
message = pyson.Literal(
received["functor"], args)
else:
message = pyson.Literal(received["functor"])
message = pyson.freeze(message, intention.scope, {}) message = pyson.freeze(message, intention.scope, {})
tagged_message = message.with_annotation( tagged_message = message.with_annotation(
pyson.Literal("source", (pyson.Literal(str(msg.sender)), ))) pyson.Literal("source", (pyson.Literal(str(msg.sender)), )))
self.agent.bdi_intention_buffer.append((trigger, goal_type, self.agent.bdi_intention_buffer.append((trigger, goal_type,
...@@ -225,15 +222,30 @@ class BDIAgent(Agent): ...@@ -225,15 +222,30 @@ class BDIAgent(Agent):
# tagged_message, intention) # tagged_message, intention)
if self.agent.bdi_intention_buffer: if self.agent.bdi_intention_buffer:
temp_intentions = deque(self.agent.bdi_intention_buffer) temp_intentions = deque(self.agent.bdi_intention_buffer)
for i in temp_intentions: for trigger, goal_type, tagged_message, intention in temp_intentions:
self.agent.bdi_agent.call(i[0], i[1], i[2], i[3]) self.agent.bdi_agent.call(
trigger, goal_type, tagged_message, intention)
self.agent.bdi_agent.step() self.agent.bdi_agent.step()
self.agent.bdi_intention_buffer.popleft() self.agent.bdi_intention_buffer.popleft()
else: else:
self.agent.bdi_agent.step() self.agent.bdi_agent.step()
async def on_end(self):
""" async def parse_literal(msg):
Coroutine called after the behaviour is done or killed. functor = msg.split("(")[0]
""" if "(" in msg:
pass args = msg.split("(")[1]
args = args.split(")")[0]
args = literal_eval(args)
if not isinstance(args, tuple):
args = (args,)
new_args = []
for arg in args:
if isinstance(arg, list):
arg = tuple(arg)
new_args.append(arg)
args = tuple(new_args)
else:
args = None
return functor, args
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment