def send_request(self, content=None): """ Sends an :class:`ACLMessage` to the coordinator to request a taxi. It uses the REQUEST_PROTOCOL and the REQUEST_PERFORMATIVE. If no content is set a default content with the passenger_id, origin and target coordinates is used. :param content: Optional content dictionary :type content: :class:`dict` """ if content is None or len(content) == 0: content = { "passenger_id": self.myAgent.agent_id, "origin": self.myAgent.current_pos, "dest": self.myAgent.dest } if not self.myAgent.dest: self.myAgent.dest = random_position() msg = ACLMessage() msg.addReceiver(coordinator_aid) msg.setProtocol(REQUEST_PROTOCOL) msg.setPerformative(REQUEST_PERFORMATIVE) msg.setContent(json.dumps(content)) self.myAgent.send(msg) self.logger.debug("Passenger {} asked for a taxi to {}.".format( self.myAgent.agent_id, self.myAgent.dest))
def move_to(self, dest): counter = 5 path = None while counter > 0 and path is None: logger.debug("Requesting path from {} to {}".format( self.current_pos, dest)) path, distance, duration = request_path(self.current_pos, dest) counter -= 1 if path is None: logger.error( "Taxi {} could not get a path to passenger {}.".format( self.agent_id, self.current_passenger.getName())) reply = ACLMessage() reply.addReceiver(self.current_passenger) reply.setProtocol(REQUEST_PROTOCOL) reply.setPerformative(CANCEL_PERFORMATIVE) reply.setContent("{}") logger.debug("Taxi {} sent cancel proposal to passenger {}".format( self.agent_id, self.current_passenger.getName())) self.send(reply) else: self.path = path self.dest = dest self.distances.append(distance) self.durations.append(duration)
def pick_up_passenger(self, passenger_id, origin, dest): """ Starts a TRAVEL_PROTOCOL to pick up a passenger and get him to his destiny. It automatically launches all the graphical process until the passenger is delivered. :param passenger_id: the id of the passenger :type passenger_id: :class:`str` :param origin: the coordinates of the current location of the passenger :type origin: :class:`list` :param dest: the coordinates of the target destiny of the passenger :type dest: :class:`list` """ self.logger.info("Taxi {} on route to passenger {}".format( self.myAgent.agent_id, passenger_id)) passenger_aid = build_aid(passenger_id) reply = ACLMessage() reply.addReceiver(passenger_aid) reply.setPerformative(INFORM_PERFORMATIVE) reply.setProtocol(TRAVEL_PROTOCOL) content = {"status": TAXI_MOVING_TO_PASSENGER} reply.setContent(json.dumps(content)) self.myAgent.status = TAXI_MOVING_TO_PASSENGER self.myAgent.current_passenger = passenger_aid self.myAgent.current_passenger_orig = origin self.myAgent.current_passenger_dest = dest self.myAgent.move_to(self.myAgent.current_passenger_orig) self.myAgent.send(reply) self.myAgent.num_assignments += 1
def create_agent(self, type_, number=1): msg = ACLMessage() msg.addReceiver(coordinator_aid) msg.setProtocol(CREATE_PROTOCOL) msg.setPerformative(REQUEST_PERFORMATIVE) content = {"type": type_, "number": number} msg.setContent(json.dumps(content)) self.send(msg)
def inform_passenger(self, status, data=None): if data is None: data = {} msg = ACLMessage() msg.addReceiver(self.current_passenger) msg.setProtocol(TRAVEL_PROTOCOL) msg.setPerformative(INFORM_PERFORMATIVE) content = {"status": status} for k, v in data.items(): content[k] = v msg.setContent(json.dumps(content)) self.send(msg)
def refuse_taxi(self, taxi_aid): """ Sends an ACLMessage to a taxi to refuse a travel proposal. It uses the REQUEST_PROTOCOL and the REFUSE_PERFORMATIVE. :param taxi_aid: The AgentID of the taxi :type taxi_aid: :class:`spade.AID.aid` """ reply = ACLMessage() reply.addReceiver(taxi_aid) reply.setProtocol(REQUEST_PROTOCOL) reply.setPerformative(REFUSE_PERFORMATIVE) content = { "passenger_id": self.myAgent.agent_id, "origin": self.myAgent.current_pos, "dest": self.myAgent.dest } reply.setContent(json.dumps(content)) self.myAgent.send(reply) self.logger.debug("Passenger {} refused proposal from taxi {}".format( self.myAgent.agent_id, taxi_aid.getName()))
def send_proposal(self, passenger_id, content=None): """ Send an :class:`ACLMessage` with a proposal to a passenger to pick up him. If the content is empty the proposal is sent without content. :param passenger_id: the id of the passenger :type passenger_id: :class:`str` :param content: the optional content of the message :type content: :class:`dict` """ if content is None: content = {} passenger_aid = build_aid(passenger_id) reply = ACLMessage() reply.addReceiver(passenger_aid) reply.setProtocol(REQUEST_PROTOCOL) reply.setPerformative(PROPOSE_PERFORMATIVE) reply.setContent(content) self.logger.debug("Taxi {} sent proposal to passenger {}".format( self.myAgent.agent_id, passenger_id)) self.myAgent.send(reply)
def accept_taxi(self, taxi_aid): """ Sends an :class:`ACLMessage` to a taxi to accept a travel proposal. It uses the REQUEST_PROTOCOL and the ACCEPT_PERFORMATIVE. :param taxi_aid: The AgentID of the taxi :type taxi_aid: :class:`spade.AID.aid` """ reply = ACLMessage() reply.addReceiver(taxi_aid) reply.setProtocol(REQUEST_PROTOCOL) reply.setPerformative(ACCEPT_PERFORMATIVE) content = { "passenger_id": self.myAgent.agent_id, "origin": self.myAgent.current_pos, "dest": self.myAgent.dest } reply.setContent(json.dumps(content)) self.myAgent.send(reply) self.myAgent.taxi_assigned = taxi_aid.getName() self.logger.debug("Passenger {} accepted proposal from taxi {}".format( self.myAgent.agent_id, taxi_aid.getName())) self.myAgent.status = PASSENGER_ASSIGNED