def _broadcast(agent, term, intention): # Illocutionary force. ilf = pyson.grounded(term.args[0], intention.scope) if not pyson.is_atom(ilf): return if ilf.functor == "tell": goal_type = pyson.GoalType.belief elif ilf.functor == "achieve": goal_type = pyson.GoalType.achievement else: raise pyson.PysonError("unknown illocutionary force: %s" % ilf) # Prepare message. message = pyson.freeze(term.args[1], intention.scope, {}) tagged_message = message.with_annotation( pyson.Literal("source", (pyson.Literal(agent.name), ))) # Broadcast. for receiver in agent.env.agents.values(): if receiver == agent: continue receiver.call(pyson.Trigger.addition, goal_type, tagged_message, pyson.runtime.Intention()) yield
def _send(agent, term, intention): # Find the receivers: By a string, atom or list of strings or atoms. receivers = pyson.grounded(term.args[0], intention.scope) if not pyson.is_list(receivers): receivers = [receivers] receiving_agents = [] for receiver in receivers: if pyson.is_atom(receiver): receiving_agents.append(agent.env.agents[receiver.functor]) else: receiving_agents.append(agent.env.agents[receiver]) # Illocutionary force. ilf = pyson.grounded(term.args[1], intention.scope) if not pyson.is_atom(ilf): return if ilf.functor == "tell": goal_type = pyson.GoalType.belief trigger = pyson.Trigger.addition elif ilf.functor == "untell": goal_type = pyson.GoalType.belief trigger = pyson.Trigger.removal elif ilf.functor == "achieve": goal_type = pyson.GoalType.achievement trigger = pyson.Trigger.addition else: raise pyson.PysonError("unknown illocutionary force: %s" % ilf) # TODO: unachieve, askOne, askAll, tellHow, untellHow, askHow # Prepare message. message = pyson.freeze(term.args[2], intention.scope, {}) tagged_message = message.with_annotation( pyson.Literal("source", (pyson.Literal(agent.name), ))) # Broadcast. for receiver in receiving_agents: receiver.call(trigger, goal_type, tagged_message, pyson.runtime.Intention()) yield