Пример #1
0
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
Пример #2
0
def _abolish(agent, term, intention):
    memo = {}
    pattern = pyson.freeze(term.args[0], intention.scope, memo)
    group = agent.beliefs[pattern.literal_group()]

    for old_belief in list(group):
        if pyson.unifies_annotated(old_belief, pattern):
            group.remove(old_belief)

    yield
Пример #3
0
def _findall(agent, term, intention):
    pattern = pyson.evaluate(term.args[0], intention.scope)
    query = pyson.runtime.TermQuery(term.args[1])
    result = []

    memo = {}
    for _ in query.execute(agent, intention):
        result.append(pyson.freeze(pattern, intention.scope, memo))

    if pyson.unify(tuple(result), term.args[2], intention.scope,
                   intention.stack):
        yield
Пример #4
0
    def call(self, trigger, goal_type, term, calling_intention, delayed=False):
        if goal_type == pyson.GoalType.belief:
            if trigger == pyson.Trigger.addition:
                self.add_belief(term, calling_intention.scope)
            else:
                found = self.remove_belief(term, calling_intention)
                if not found:
                    return True

        frozen = pyson.freeze(term, calling_intention.scope, {})

        if not isinstance(frozen, pyson.Literal):
            raise PysonError("expected literal")

        applicable_plans = self.plans[(trigger, goal_type, frozen.functor,
                                       len(frozen.args))]
        choicepoint = object()
        intention = Intention()

        for plan in applicable_plans:
            for _ in pyson.unify_annotated(plan.head, frozen, intention.scope,
                                           intention.stack):
                for _ in plan.context.execute(self, intention):
                    intention.head_term = frozen
                    intention.instr = plan.body
                    intention.calling_term = term

                    if not delayed and self.intentions:
                        for intention_stack in self.intentions:
                            if intention_stack[-1] == calling_intention:
                                intention_stack.append(intention)
                                return True

                    new_intention_stack = collections.deque()
                    new_intention_stack.append(intention)
                    self.intentions.append(new_intention_stack)
                    return True

        if goal_type == pyson.GoalType.achievement:
            raise PysonError("no applicable plan for %s%s%s/%d" %
                             (trigger.value, goal_type.value, frozen.functor,
                              len(frozen.args)))
        elif goal_type == pyson.GoalType.test:
            return self.test_belief(term, calling_intention)

        return True
Пример #5
0
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
Пример #6
0
def _print(agent, term, intention, _color_map={}, _current_color=[0]):
    if agent in _color_map:
        color = _color_map[agent]
    else:
        color = COLORS[_current_color[0]]
        _current_color[0] = (_current_color[0] + 1) % len(COLORS)
        _color_map[agent] = color

    memo = {}
    text = " ".join(
        pyson_str(pyson.freeze(t, intention.scope, memo)) for t in term.args)

    with colorama.colorama_text():
        print(color[0],
              color[1],
              agent.name,
              colorama.Fore.RESET,
              colorama.Back.RESET,
              " ",
              text,
              sep="")

    yield