def _date(agent, term, intention): date = datetime.datetime.now() if (agentspeak.unify(term.args[0], date.year, intention.scope, intention.stack) and agentspeak.unify(term.args[1], date.month, intention.scope, intention.stack) and agentspeak.unify(term.args[2], date.day, intention.scope, intention.stack)): yield
def _time(agent, term, intention): time = datetime.datetime.now() if (agentspeak.unify(term.args[0], time.hour, intention.scope, intention.stack) and agentspeak.unify(term.args[1], time.minute, intention.scope, intention.stack) and agentspeak.unify(term.args[2], time.second, intention.scope, intention.stack)): yield
def _string2term(agent, term, intention): string = asp.grounded(term.args[0], intention.scope) result = asp.Literal(string) logger.success("[DBG] {} -> {}".format(string, result)) if asp.unify(term.args[-1], result, intention.scope, intention.stack): yield
def _getEnemyBase(agent, term, intention): enemyBase = self.map.allied_base; if(self.team == TEAM_ALLIED): enemyBase = self.map.axis_base; basePos = tuple((enemyBase.init.x, enemyBase.init.y, enemyBase.init.z,)); if asp.unify(term.args[-1], basePos, intention.scope, intention.stack): yield
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 #fahid 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 agentspeak.unify(intention.calling_term, frozen, calling_intention.scope, calling_intention.stack): raise RuntimeError("back unification failed") return True try: if instr.f(self, intention): #fahid.... note.... intention.instr = instr.success else: intention.instr = instr.failure if not intention.instr: raise AslError("plan failure") except AslError as err: log = agentspeak.Log(LOGGER) raise log.error("%s", err, loc=instr.loc, extra_locs=instr.extra_locs) except Exception as err: log = agentspeak.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 _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 make_patch(self, term, intention): klass = agentspeak.grounded(term.args[0], intention.scope) patch = Patch() patch.removed.add(klass) patch.added.add(klass) if agentspeak.unify(term.args[1], patch, intention.scope, intention.stack): yield
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 _findall(agent, term, intention): pattern = agentspeak.evaluate(term.args[0], intention.scope) query = agentspeak.runtime.TermQuery(term.args[1]) result = [] memo = {} for _ in query.execute(agent, intention): result.append(agentspeak.freeze(pattern, intention.scope, memo)) if agentspeak.unify(tuple(result), term.args[2], intention.scope, intention.stack): yield
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 _getEnemyBase(agent, term, intention): enemyBase = self.map.allied_base; if(self.team == TEAM_ALLIED): enemyBase = self.map.axis_base; enemyBaseCenter = Vector3D(enemyBase.end); enemyBaseCenter.add(enemyBase.init); enemyBaseCenter.x = math.floor(enemyBaseCenter.x*0.5); enemyBaseCenter.y = math.floor(enemyBaseCenter.y*0.5); enemyBaseCenter.z = math.floor(enemyBaseCenter.z*0.5); basePos = tuple((enemyBaseCenter.x, enemyBaseCenter.y, enemyBaseCenter.z,)); if asp.unify(term.args[-1], basePos, intention.scope, intention.stack): yield
def _getReunionPoint(agent, term, intention): [name, domain] = str(self.name).split("@"); nnum = int(name.split("_")[-1]); logger.success("[DBG] {} : {}".format(nnum,str(self.name))); ## enemyBase = self.map.allied_base; friendBase = self.map.axis_base; if(self.team == TEAM_ALLIED): enemyBase = self.map.axis_base; friendBase = self.map.allied_base; ## enemyBaseCenter = Vector3D(enemyBase.end); enemyBaseCenter.add(enemyBase.init); enemyBaseCenter.x *= 0.5; enemyBaseCenter.y *= 0.5; enemyBaseCenter.z *= 0.5; friendBaseCenter = Vector3D(friendBase.end); friendBaseCenter.add(friendBase.init); friendBaseCenter.x *= 0.5; friendBaseCenter.y *= 0.5; friendBaseCenter.z *= 0.5; ##logger.success("[DBG] {} {} {}".format(enemyBase.end,enemyBase.init,enemyBaseCenter)); #Direccion hacia base enemiga MapDir = Vector3D(enemyBaseCenter); MapDir.sub(friendBaseCenter); MapDir.normalize(); ##rotar 90 o -90 grados segĂșn numero patrulla meetingPoint = Vector3D(MapDir); ##logger.success("[DBG] meetingPoint {}".format(meetingPoint)); ##logger.success("[DBG] ANGLE {} {}".format(nnum,math.pi*0.5 + math.pi*nnum)); pcos = math.cos( math.pi*0.5 + math.pi*nnum); psin = math.sin( math.pi*0.5 + math.pi*nnum); meetingPoint.x = MapDir.x * pcos - MapDir.z * psin; meetingPoint.z = MapDir.x * psin + MapDir.z * pcos; ##logger.success("[DBG] meetingPoint {}".format(meetingPoint)); ## baseSpan = abs(friendBase.end.x - friendBase.init.x); ##logger.success("[DBG] BaseSPan {}".format(baseSpan)); meetingPoint.x = 0.95*baseSpan*meetingPoint.x + friendBaseCenter.x; meetingPoint.z = 0.95*baseSpan*meetingPoint.z + friendBaseCenter.z; ##logger.success("[DBG] meetingPoint {}".format(meetingPoint)); meetingPoint.x = math.floor(meetingPoint.x); meetingPoint.y = math.floor(meetingPoint.y); meetingPoint.z = math.floor(meetingPoint.z); ##logger.success("[DBG] Meeting Point {}".format(meetingPoint)); ## result = tuple((meetingPoint.x, meetingPoint.y, meetingPoint.z,)); ## if(False == self.map.can_walk(meetingPoint.x, meetingPoint.z)): result = tuple((friendBase.init.x, friendBase.init.y, friendBase.init.z,)); ## if asp.unify(term.args[-1], result, intention.scope, intention.stack): yield
def _concat(agent, term, intention): args = [ agentspeak.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 agentspeak.unify(term.args[-1], result, intention.scope, intention.stack): yield
def _countEnemiesInFov(agent, term, intention): enemyCount = 0 for s in self.fov_objects: if s.team == self.team: continue if s.team == TEAM_NONE: continue if s.health > 0: enemyCount += 1 logger.success("[DBG] seeing {} {} {} {} ".format( s.team, s.type, s.position, s.distance)) ## if asp.unify(term.args[-1], enemyCount, 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 _getSquadLeader(agent, term, intention): ## SQUAD_SIZE = 3 #soldados por escuadra LEADER_ROLE = "squadleader" [name, domain] = str(self.name).split("@"); [team, role , user, nnum] = name.split("_"); if len(name.split("_")) != 4: logger.success("ERROR: {} debe ser equipo_rol_usuario_num".format(name)); nnum = int(name.split("_")[-1]); #en funcion del numero de agente, asigna lider #nos hacemos un "Round-robin" leaderID = "nil"; squadNum = nnum % SQUAD_SIZE; leaderID = team+"_"+LEADER_ROLE+"_"+user+"_"+str(squadNum)+"@"+domain; #cadena a literal y "groundea" valor al primer parametro sqLeader = asp.Literal(leaderID); if asp.unify(term.args[-1], sqLeader, intention.scope, intention.stack): yield
def _getClosestPoint(agent, term, intention): agentPos = tuple((self.movement.position.x, self.movement.position.y, self.movement.position.z,)) backUpPos = asp.grounded(term.args[0], intention.scope) logger.success( "[{}] Recieved Positions: {}".format(self.jid.localpart, backUpPos) ) closestIndex = 0 minDist = 10000000 i = 0 while i < len(backUpPos): pos = backUpPos[i] x = (pos[0] - agentPos[0]) * (pos[0] - agentPos[0]) y = (pos[1] - agentPos[1]) * (pos[1] - agentPos[1]) z = (pos[2] - agentPos[2]) * (pos[2] - agentPos[2]) realDist = math.sqrt(x+y+z) if(realDist < minDist): minDist = realDist closestIndex = i i+=1 if asp.unify(term.args[-1], closestIndex, intention.scope, intention.stack): yield
def getDropPoint(agent,term,intention): dist = asp.grounded(term.args[0], intention.scope); tx = self.movement.position.x + self.movement.heading.x * dist; ty = self.movement.position.y; tz = self.movement.position.z + self.movement.heading.z * dist; #busca drop point valido tx = math.floor(tx); ty = math.floor(ty); tz = math.floor(tz); alpha = 0.0; while (alpha <= 2*math.pi and False == self.map.can_walk(tx, tz)): alpha += 0.05; tx = math.cos(alpha) * self.movement.heading.x * dist; tx += self.movement.position.x ; tz = math.sin(alpha) * self.movement.heading.z * dist; tz += self.movement.position.z; tx = math.floor(tx); tz = math.floor(tz); ## targetPos = tuple((tx,ty,tz)); if asp.unify(term.args[-1], targetPos, intention.scope, intention.stack): yield
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 get_issue(self, term, intention): for issue in self.env.issues: if agentspeak.unify(term.args[0], issue.as_term(), intention.scope, intention.stack): self.env.issues.remove(issue) yield
def _countEnemiesInFov(agent, term, intention): enemyCount = 0 ## if asp.unify(term.args[-1], enemyCount, intention.scope, intention.stack): yield
def _getSquadLeader(agent, term, intention): sqLeader = asp.Literal(str(self.name)); if asp.unify(term.args[-1], sqLeader, intention.scope, intention.stack): yield
def _my_name(agent, term, intention): if agentspeak.unify(term.args[0], Literal(agent.name), intention.scope, intention.stack): yield