Example #1
0
    def add_belief(self, term, scope):
        term = term.grounded(scope)

        if term.functor is None:
            raise PysonError("expected belief literal")

        self.beliefs[(term.functor, len(term.args))].add(term)
Example #2
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)
Example #3
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
Example #4
0
    def step(self):
        while self.intentions and not self.intentions[0]:
            self.intentions.popleft()

        for intention_stack in self.intentions:
            intention = intention_stack[-1]

            # Wait until.
            if intention.wait_until:
                if intention.wait_until < self.env.time():
                    intention.wait_until = None
                else:
                    continue

            break
        else:
            return False

        instr = intention.instr

        if not instr:
            intention_stack.pop()
            if not intention_stack:
                self.intentions.remove(intention_stack)
            elif intention.calling_term:
                frozen = intention.head_term.freeze(intention.scope, {})
                calling_intention = intention_stack[-1]
                if not pyson.unify(intention.calling_term, frozen,
                                   calling_intention.scope,
                                   calling_intention.stack):
                    raise RuntimeError("back unification failed")
            return True

        try:
            if instr.f(self, intention):
                intention.instr = instr.success
            else:
                intention.instr = instr.failure
                if not intention.instr:
                    raise PysonError("plan failure")
        except PysonError as err:
            log = pyson.Log(LOGGER)
            raise log.error("%s @ %s",
                            err,
                            instr.f,
                            loc=instr.loc,
                            extra_locs=instr.extra_locs)
        except:
            log = pyson.Log(LOGGER)
            raise log.exception("python exception @ %s",
                                instr.f,
                                loc=instr.loc,
                                extra_locs=instr.extra_locs)

        return True
Example #5
0
    def test_belief(self, term, intention):
        term = pyson.evaluate(term, intention.scope)

        if not isinstance(term, pyson.Literal):
            raise PysonError("expected belief literal, got: '%s'" % term)

        query = TermQuery(term)

        try:
            next(query.execute(self, intention))
            return True
        except StopIteration:
            return False
Example #6
0
    def step(self):
        while self.intentions and not self.intentions[0]:
            self.intentions.popleft()

        for intention_stack in self.intentions:
            intention = intention_stack[-1]

            # Suspended / waiting.
            if intention.waiter is not None:
                if intention.waiter.poll(self.env):
                    intention.waiter = None
                else:
                    continue

            break
        else:
            return False

        instr = intention.instr

        if not instr:
            intention_stack.pop()
            if not intention_stack:
                self.intentions.remove(intention_stack)
            elif intention.calling_term:
                frozen = intention.head_term.freeze(intention.scope, {})
                calling_intention = intention_stack[-1]
                if not pyson.unify(intention.calling_term, frozen, calling_intention.scope, calling_intention.stack):
                    raise RuntimeError("back unification failed")
            return True

        try:
            if instr.f(self, intention):
                intention.instr = instr.success
            else:
                intention.instr = instr.failure
                if not intention.instr:
                    raise PysonError("plan failure")
        except PysonError as err:
            log = pyson.Log(LOGGER)
            raise log.error("%s", err, loc=instr.loc, extra_locs=instr.extra_locs)
        except Exception as err:
            log = pyson.Log(LOGGER)
            raise log.exception("agent %r raised python exception: %r", self.name, err,
                                loc=instr.loc, extra_locs=instr.extra_locs)

        return True
Example #7
0
    def remove_belief(self, term, intention):
        term = pyson.evaluate(term, intention.scope)

        try:
            group = term.literal_group()
        except AttributeError:
            raise PysonError("expected belief literal, got: '%s'" % term)

        choicepoint = object()

        relevant_beliefs = self.beliefs[group]

        for belief in relevant_beliefs:
            intention.stack.append(choicepoint)

            if pyson.unify(term, belief, intention.scope, intention.stack):
                relevant_beliefs.remove(belief)
                return True

            pyson.reroll(intention.scope, intention.stack, choicepoint)

        return False