Exemplo n.º 1
0
def _abolish(agent, term, intention):
    memo = {}
    pattern = agentspeak.freeze(term.args[0], intention.scope, memo)
    group = agent.beliefs[pattern.literal_group()]

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

    yield
Exemplo n.º 2
0
    def test_unifies_annotated(self):
        X = agentspeak.Var()
        Y = agentspeak.Var()
        foo_a = agentspeak.Literal("foo", (), ("a", ))
        foo_ab = agentspeak.Literal("foo", (), ("a", "b"))
        foo_XY = agentspeak.Literal("foo", (), (X, Y))
        foo_XX = agentspeak.Literal("foo", (), (X, X))

        self.assertTrue(agentspeak.unifies_annotated(foo_a, foo_ab))
        self.assertTrue(agentspeak.unifies_annotated(foo_a, foo_XY))
        self.assertTrue(agentspeak.unifies_annotated(foo_a, foo_XX))
        self.assertTrue(agentspeak.unifies_annotated(foo_ab, foo_XY))
        self.assertTrue(agentspeak.unifies_annotated(foo_XY, foo_ab))
        self.assertTrue(agentspeak.unifies_annotated(foo_XX, foo_ab))
        self.assertTrue(agentspeak.unifies_annotated(foo_XX, foo_ab))

        self.assertFalse(agentspeak.unifies_annotated(foo_ab, foo_XX))
        self.assertFalse(agentspeak.unifies_annotated(foo_XY, foo_a))
Exemplo n.º 3
0
def test_unifies_annotated():
    X = agentspeak.Var()
    Y = agentspeak.Var()
    foo_a = agentspeak.Literal("foo", (), ("a",))
    foo_ab = agentspeak.Literal("foo", (), ("a", "b"))
    foo_XY = agentspeak.Literal("foo", (), (X, Y))
    foo_XX = agentspeak.Literal("foo", (), (X, X))

    assert agentspeak.unifies_annotated(foo_a, foo_ab)
    assert agentspeak.unifies_annotated(foo_a, foo_XY)
    assert agentspeak.unifies_annotated(foo_a, foo_XX)
    assert agentspeak.unifies_annotated(foo_ab, foo_XY)
    assert agentspeak.unifies_annotated(foo_XY, foo_ab)
    assert agentspeak.unifies_annotated(foo_XX, foo_ab)
    assert agentspeak.unifies_annotated(foo_XX, foo_ab)

    assert not agentspeak.unifies_annotated(foo_ab, foo_XX)
Exemplo n.º 4
0
    def call(self, trigger, goal_type, term, calling_intention, delayed=False):
        # Modify beliefs.
        if goal_type == agentspeak.GoalType.belief:
            if trigger == agentspeak.Trigger.addition:
                self.add_belief(term, calling_intention.scope)
            else:
                found = self.remove_belief(term, calling_intention)
                if not found:
                    return True

        # Freeze with caller scope.
        frozen = agentspeak.freeze(term, calling_intention.scope, {})

        if not isinstance(frozen, agentspeak.Literal):
            raise AslError("expected literal")

        # Wake up waiting intentions.
        for intention_stack in self.intentions:
            if not intention_stack:
                continue
            intention = intention_stack[-1]

            if not intention.waiter or not intention.waiter.event:
                continue
            event = intention.waiter.event

            if event.trigger != trigger or event.goal_type != goal_type:
                continue

            if agentspeak.unifies_annotated(event.head, frozen):
                intention.waiter = None
        # fahid..... log plan trace here.....
        self.get_belief_base()
        applicable_plans = self.plans[(trigger, goal_type, frozen.functor,
                                       len(frozen.args))]
        applicable_plans_trace = []
        for plan in applicable_plans:
            """
            if type(plan.context) is list:
                print (">>>> list")
            else:
                print (">>>>> ", type(plan.context))
                if type(plan.context) is AndQuery:
                    print(".....")
                    print(plan.context.left)
                    print(plan.context.right)
                elif type(plan.context) is OrQuery:
                    print("////")
                    print(plan.context.left)

                    print(plan.context.right)
            """
            applicable_plans_trace.append({
                "IDENTIFIER":
                str(plan.head),
                "CONTEXT":
                flatten_context(
                    plan.context
                ),  #str(plan.context) if type(plan.context) is not type (NotQuery("")) else "not " + str(plan.context.query),
                "CODE_LINE":
                "",
                "CODE_FILE":
                ""
            })
        if len(applicable_plans_trace) > 0:
            Logger.log_plan_trace(agent=self.name,
                                  payload=applicable_plans_trace,
                                  bb_payload=self.get_belief_base())

        choicepoint = object()
        intention = Intention()
        # Find matching plan.
        counter = -1
        for plan in applicable_plans:
            counter += 1
            for _ in agentspeak.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)
                                # Fahid: log selected plan....
                                Logger.log_plan_selection(
                                    agent=self.name,
                                    payload=applicable_plans_trace[counter],
                                    bb_payload=self.get_belief_base())
                                return True

                    new_intention_stack = collections.deque()
                    new_intention_stack.append(intention)
                    self.intentions.append(new_intention_stack)
                    # Fahid: Log selected plan.....
                    Logger.log_plan_selection(
                        agent=self.name,
                        payload=applicable_plans_trace[counter],
                        bb_payload=self.get_belief_base())
                    return True

        #fahid: log not found...
        if goal_type == agentspeak.GoalType.achievement:
            raise AslError("no applicable plan for %s%s%s/%d" %
                           (trigger.value, goal_type.value, frozen.functor,
                            len(frozen.args)))
        elif goal_type == agentspeak.GoalType.test:
            return self.test_belief(term, calling_intention)

        return True
Exemplo n.º 5
0
    def call(self, trigger, goal_type, term, calling_intention, delayed=False):
        # Modify beliefs.
        if goal_type == agentspeak.GoalType.belief:
            if trigger == agentspeak.Trigger.addition:
                self.add_belief(term, calling_intention.scope)
            else:
                found = self.remove_belief(term, calling_intention)
                if not found:
                    return True

        # Freeze with caller scope.
        frozen = agentspeak.freeze(term, calling_intention.scope, {})

        if not isinstance(frozen, agentspeak.Literal):
            raise AslError("expected literal")

        # Wake up waiting intentions.
        for intention_stack in self.intentions:
            if not intention_stack:
                continue
            intention = intention_stack[-1]

            if not intention.waiter or not intention.waiter.event:
                continue
            event = intention.waiter.event

            if event.trigger != trigger or event.goal_type != goal_type:
                continue

            if agentspeak.unifies_annotated(event.head, frozen):
                intention.waiter = None

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

        # Find matching plan.
        for plan in applicable_plans:
            for _ in agentspeak.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 == agentspeak.GoalType.achievement:
            raise AslError("no applicable plan for %s%s%s/%d" %
                           (trigger.value, goal_type.value, frozen.functor,
                            len(frozen.args)))
        elif goal_type == agentspeak.GoalType.test:
            return self.test_belief(term, calling_intention)

        return True