Exemplo n.º 1
0
    def execute(self, agent, intention):
        # Boolean constants.
        term = pyson.evaluate(self.term, intention.scope)
        if term is True:
            yield
            return
        elif term is False:
            return

        try:
            group = term.literal_group()
        except AttributeError:
            raise PysonError(
                "expected boolean or literal in query context, got: '%s'" %
                term)

        # Query on the belief base.
        for belief in agent.beliefs[group]:
            for _ in pyson.unify_annotated(term, belief, intention.scope,
                                           intention.stack):
                yield

        choicepoint = object()

        # Follow rules.
        for rule in agent.rules[group]:
            rule = copy.deepcopy(rule)

            intention.stack.append(choicepoint)

            if pyson.unify(term, rule.head, intention.scope, intention.stack):
                for _ in rule.query.execute(agent, intention):
                    yield

            pyson.reroll(intention.scope, intention.stack, choicepoint)
Exemplo n.º 2
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
Exemplo n.º 3
0
 def execute(self, agent, intention):
     return pyson.unify_annotated(self.left, self.right, intention.scope,
                                  intention.stack)