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)
def _member(agent, term, intention): choicepoint = object() for member in pyson.evaluate(term.args[1], intention.scope): intention.stack.append(choicepoint) if pyson.unify(term.args[0], member, intention.scope, intention.stack): yield pyson.reroll(intention.scope, intention.stack, choicepoint)
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
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
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