def _date(agent, term, intention): date = datetime.datetime.now() if (pyson.unify(term.args[0], date.year, intention.scope, intention.stack) and pyson.unify(term.args[1], date.month, intention.scope, intention.stack) and pyson.unify(term.args[2], date.day, intention.scope, intention.stack)): yield
def _time(agent, term, intention): time = datetime.datetime.now() if (pyson.unify(term.args[0], time.hour, intention.scope, intention.stack) and pyson.unify(term.args[1], time.minute, intention.scope, intention.stack) and pyson.unify(term.args[2], time.second, intention.scope, intention.stack)): yield
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 bridgeStatus(self, term, intention): node1 = pyson.grounded(term.args[0], intention.scope) node2 = pyson.grounded(term.args[1], intention.scope) result = G.get_edge_data(node1, node2)["bridge"]["open"] if pyson.unify(term.args[2], pyson.Literal("open", (result, )), intention.scope, intention.stack): yield
def make_patch(self, term, intention): klass = pyson.grounded(term.args[0], intention.scope) patch = Patch() patch.removed.add(klass) patch.added.add(klass) if pyson.unify(term.args[1], patch, intention.scope, intention.stack): yield
def _concat(agent, term, intention): args = [pyson.grounded(arg, intention.scope) for arg in term.args[:-1]] if all(isinstance(arg, (tuple, list)) for arg in args): result = tuple(el for arg in args for el in arg) else: result = "".join(str(arg) for arg in args) if pyson.unify(term.args[-1], result, intention.scope, intention.stack): yield
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 _range_2(agent, term, intention): choicepoint = object() for i in range(int(pyson.grounded(term.args[0], intention.scope))): intention.stack.append(choicepoint) if pyson.unify(term.args[1], i, intention.scope, intention.stack): yield pyson.reroll(intention.scope, intention.stack, choicepoint)
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
def getDetour(self, term, intention): target = pyson.grounded(term.args[0], intention.scope) position = agentStates[self.name]["node"] edgeData = G.get_edge_data(position, target) length = edgeData["length"] edgeData["length"] = 10000 path = nx.shortest_path(G, position, target, "length")[1:] edgeData["length"] = length if pyson.unify(term.args[1], tuple(path), intention.scope, intention.stack): yield
def position(agent, term, intention): """Return the agent position. Args: agent (:obj:`jason_malmo.agent.Agent`): Target agent. term: Pyson associated context. intention: Pyson associated context. """ pos = agent.get_position() if pyson.unify(tuple(pos), term.args[0], intention.scope, intention.stack): yield
def _count(agent, term, intention): query = pyson.runtime.TermQuery(term.args[0]) choicepoint = object() count = 0 intention.stack.append(choicepoint) for _ in query.execute(agent, intention): count += 1 pyson.reroll(intention.scope, intention.stack, choicepoint) if pyson.unify(count, term.args[1], intention.scope, intention.stack): yield
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 floor_grid(agent, term, intention): """Return the 3x3 grid of blocks under the agent. Args: agent (:obj:`jason_malmo.agent.Agent`): Target agent. term: Pyson associated context. intention: Pyson associated context. """ observations = agent.get_observations() grid = observations.get(u'floor3x3', 0) if pyson.unify(tuple(grid), term.args[0], intention.scope, intention.stack): yield
def _substring(agent, term, intention): needle = pyson_str(pyson.grounded(term.args[0], intention.scope)) haystack = pyson_str(pyson.grounded(term.args[1], intention.scope)) choicepoint = object() pos = haystack.find(needle) while pos != -1: intention.stack.append(choicepoint) if pyson.unify(term.args[2], pos, intention.scope, intention.stack): yield pyson.reroll(intention.scope, intention.stack, choicepoint) pos = haystack.find(needle, pos + 1)
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
def nextSteps(self, term, intention): position = pyson.grounded(term.args[0], intention.scope) destination = pyson.grounded(term.args[1], intention.scope) results = [] resetWeights() for edge in G.edges(position): road = G[edge[0]][edge[1]] road["w"] = 1000 for neighbor in G.neighbors(position): path_length = nx.shortest_path_length(G, neighbor, destination, "w") + G.get_edge_data( position, neighbor)["length"] results.append(pyson.Literal("road", (neighbor, path_length))) results.sort(key=lambda x: x.args[1]) if pyson.unify(term.args[2], tuple(results), intention.scope, intention.stack): yield
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
def getTraffic(self, term, intention): target = pyson.grounded(term.args[0], intention.scope) position = agentStates[self.name]["node"] destination = agentStates[self.name]["destination"] road = G[position][target] state = agentStates[self.name] resetWeights() for edge in G.edges(position): # update information for all incident roads state["traffic"][edge] = G[edge[0]][ edge[1]]["length"] / calculateRoadProgress( trafficData[(edge[0], edge[1])]) state["traffic"][( edge[1], edge[0])] = G[edge[1]][edge[0]]["length"] / calculateRoadProgress( trafficData[(edge[1], edge[0])]) for edge, w in state["traffic"].items(): # use agent's known traffic info G[edge[0]][edge[1]]["w"] = w path_length = nx.shortest_path_length(G, target, destination, "w") + road["w"] if pyson.unify(term.args[1], path_length, intention.scope, intention.stack): yield
def _my_name(agent, term, intention): if pyson.unify(term.args[0], agent.name, intention.scope, intention.stack): yield
def distance(self, term, intention): n1 = pyson.grounded(term.args[0], intention.scope) n2 = pyson.grounded(term.args[1], intention.scope) if pyson.unify(term.args[2], nx.shortest_path_length(G, n1, n2), intention.scope, intention.stack): yield
def _wrapped_get_value(agent, term, intention): if pyson.unify(agent.get_observations_value(value), term.args[0], intention.scope, intention.stack): yield
def sense_location(agent, term, intention): (x, y) = locations[agent] pyson.unify(x, term.args[0], intention.scope, intention.stack) pyson.unify(y, term.args[1], intention.scope, intention.stack) yield
def get_issue(self, term, intention): for issue in self.env.issues: if pyson.unify(term.args[0], issue.as_term(), intention.scope, intention.stack): self.env.issues.remove(issue) yield