def execute(self, agent, intention): choicepoint = object() intention.stack.append(choicepoint) success = any(True for _ in self.query.execute(agent, intention)) agentspeak.reroll(intention.scope, intention.stack, choicepoint) if not success: yield
def _range_2(agent, term, intention): choicepoint = object() for i in range(int(agentspeak.grounded(term.args[0], intention.scope))): intention.stack.append(choicepoint) if agentspeak.unify(term.args[1], i, intention.scope, intention.stack): yield agentspeak.reroll(intention.scope, intention.stack, choicepoint)
def _member(agent, term, intention): choicepoint = object() for member in agentspeak.evaluate(term.args[1], intention.scope): intention.stack.append(choicepoint) if agentspeak.unify(term.args[0], member, intention.scope, intention.stack): yield agentspeak.reroll(intention.scope, intention.stack, choicepoint)
def _count(agent, term, intention): query = agentspeak.runtime.TermQuery(term.args[0]) choicepoint = object() count = 0 intention.stack.append(choicepoint) for _ in query.execute(agent, intention): count += 1 agentspeak.reroll(intention.scope, intention.stack, choicepoint) if agentspeak.unify(count, term.args[1], intention.scope, intention.stack): yield
def _substring(agent, term, intention): needle = asl_str(agentspeak.grounded(term.args[0], intention.scope)) haystack = asl_str(agentspeak.grounded(term.args[1], intention.scope)) choicepoint = object() pos = haystack.find(needle) while pos != -1: intention.stack.append(choicepoint) if agentspeak.unify(term.args[2], pos, intention.scope, intention.stack): yield agentspeak.reroll(intention.scope, intention.stack, choicepoint) pos = haystack.find(needle, pos + 1)
def remove_belief(self, term, intention): term = agentspeak.evaluate(term, intention.scope) try: group = term.literal_group() except AttributeError: raise AslError("expected belief literal, got: '%s'" % term) choicepoint = object() relevant_beliefs = self.beliefs[group] for belief in relevant_beliefs: intention.stack.append(choicepoint) if agentspeak.unify(term, belief, intention.scope, intention.stack): relevant_beliefs.remove(belief) return True agentspeak.reroll(intention.scope, intention.stack, choicepoint) return False
def execute(self, agent, intention): # Boolean constants. term = agentspeak.evaluate(self.term, intention.scope) if term is True: yield return elif term is False: return try: group = term.literal_group() except AttributeError: raise AslError( "expected boolean or literal in query context, got: '%s'" % term) # Query on the belief base. for belief in agent.beliefs[group]: for _ in agentspeak.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 agentspeak.unify(term, rule.head, intention.scope, intention.stack): for _ in rule.query.execute(agent, intention): yield agentspeak.reroll(intention.scope, intention.stack, choicepoint)
def pop_choicepoint(agent, intention): choicepoint = intention.choicepoint_stack.pop() agentspeak.reroll(intention.scope, intention.stack, choicepoint) return True