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
def test_unifies_annotated(self): X = pyson.Var() Y = pyson.Var() foo_a = pyson.Literal("foo", (), ("a", )) foo_ab = pyson.Literal("foo", (), ("a", "b")) foo_XY = pyson.Literal("foo", (), (X, Y)) foo_XX = pyson.Literal("foo", (), (X, X)) self.assertTrue(pyson.unifies_annotated(foo_a, foo_ab)) self.assertTrue(pyson.unifies_annotated(foo_a, foo_XY)) self.assertTrue(pyson.unifies_annotated(foo_a, foo_XX)) self.assertTrue(pyson.unifies_annotated(foo_ab, foo_XY)) self.assertTrue(pyson.unifies_annotated(foo_XY, foo_ab)) self.assertTrue(pyson.unifies_annotated(foo_XX, foo_ab)) self.assertTrue(pyson.unifies_annotated(foo_XX, foo_ab)) self.assertFalse(pyson.unifies_annotated(foo_ab, foo_XX)) self.assertFalse(pyson.unifies_annotated(foo_XY, foo_a))
def call(self, trigger, goal_type, term, calling_intention, delayed=False): # Modify beliefs. 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 # Freeze with caller scope. frozen = pyson.freeze(term, calling_intention.scope, {}) if not isinstance(frozen, pyson.Literal): raise PysonError("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 pyson.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 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