Ejemplo n.º 1
0
 async def prepare_response(self, request: ACLMessage) -> ACLMessage:
     content: ContentElement = self.agent.content_manager.extract_content(
         request)
     if isinstance(content, ReallocationRequest):
         await self.agent.acquire_lock()
         return request.create_reply(Performative.AGREE)
     return request.create_reply(Performative.NOT_UNDERSTOOD)
 async def prepare_requests(self) -> Sequence[ACLMessage]:
     request = ACLMessage(to=self._container_jid)
     request.performative = Performative.REQUEST
     request.protocol = 'Request'
     request.ontology = self.agent.ontology.name
     deallocation_request = DeallocationRequest(self._container_jid)
     self.agent.content_manager.fill_content(deallocation_request, request)
     return [request]
Ejemplo n.º 3
0
 async def prepare_response(self, request: ACLMessage) -> ACLMessage:
     content = self.agent.content_manager.extract_content(request)
     if isinstance(content, SelfDeallocationRequest):
         await self.agent.acquire_lock()
         if self.agent.has_container(content.container_id):
             return request.create_reply(Performative.AGREE)
         else:
             self.agent.release_lock()
             return request.create_reply(Performative.REFUSE)
     return request.create_reply(Performative.NOT_UNDERSTOOD)
Ejemplo n.º 4
0
 async def prepare_requests(self) -> Sequence[ACLMessage]:
     if self.agent.slot_id is None:
         raise Exception('Container is not allocated')
     request = ACLMessage(to=self.agent.slot_jid)
     request.protocol = 'Request'
     request.ontology = self.agent.ontology.name
     request.performative = Performative.REQUEST
     self.agent.content_manager.fill_content(
         SelfDeallocationRequest(self.agent.jid.localpart), request)
     return [request]
 def __createRequestMessage(agent: BaseAgent, action: Action,
                            domain: str) -> ACLMessage:
     msg: ACLMessage = ACLMessage(to=f'df_agent@{domain}')
     msg.performative = Performative.REQUEST
     msg.ontology = DFOntology.instance().name
     DFService.getContentManager().fill_content(action, msg)
     return msg
Ejemplo n.º 6
0
 async def handle_accept_proposal(self, accept: ACLMessage) -> ACLMessage:
     await self.agent.acquire_lock()
     if self.agent.is_full:
         self.agent.release_lock()
         return accept.create_reply(Performative.FAILURE)
     content = self.agent.content_manager.extract_content(accept)
     if isinstance(content, AllocationProposalAcceptance):
         try:
             await self.agent.add_container(
                 content.container_data.id,
                 content.container_data.departure_time, str(accept.sender))
             self.agent.release_lock()
             response = accept.create_reply(Performative.INFORM)
             self.agent.content_manager.fill_content(
                 AllocationConfirmation(self.agent.slot_id), response)
             return response
         except ValueError:
             pass
     self.agent.release_lock()
     return accept.create_reply(Performative.NOT_UNDERSTOOD)
Ejemplo n.º 7
0
 def _create_cfp(self, jid: str):
     cfp: ACLMessage = ACLMessage(to=jid)
     cfp.performative = Performative.CFP
     cfp.ontology = self.agent.ontology.name
     cfp.protocol = 'ContractNet'
     cfp.action = AllocationRequest.__key__
     container_data: ContentElement = ContainerData(
         self.agent.jid.localpart, self.agent.departure_time)
     content: ContentElement = AllocationRequest(container_data)
     self.agent.content_manager.fill_content(content, cfp)
     return cfp
Ejemplo n.º 8
0
 async def handle_cfp(self, cfp: ACLMessage) -> ACLMessage:
     await self.agent.acquire_lock()
     if self.agent.is_full:
         self.agent.release_lock()
         return cfp.create_reply(Performative.REFUSE)
     content = self.agent.content_manager.extract_content(cfp)
     if isinstance(content, AllocationRequest):
         if self.agent.has_container(content.container_data.id):
             self.agent.release_lock()
             return cfp.create_reply(Performative.REFUSE)
         try:
             td: float = self.agent.get_timedelta_from_forced_reallocation_to_departure(
                 content.container_data.departure_time)
             response: ACLMessage = cfp.create_reply(Performative.PROPOSE)
             allocation_proposal = AllocationProposal(
                 self.agent.slot_id, int(td))
             self.agent.content_manager.fill_content(
                 allocation_proposal, response)
             self.agent.release_lock()
             return response
         except ValueError:
             pass
     self.agent.release_lock()
     return cfp.create_reply(Performative.NOT_UNDERSTOOD)
Ejemplo n.º 9
0
    async def prepare_result_notification(self,
                                          request: ACLMessage) -> ACLMessage:
        self_deallocation_mt = Template()
        self_deallocation_mt.set_metadata('protocol', 'ContractNet')
        self_deallocation_mt.set_metadata('action',
                                          SelfDeallocationRequest.__key__)
        self_deallocation_behaviour = SelfDeallocationInitiator()
        self.agent.add_behaviour(self_deallocation_behaviour)

        await self_deallocation_behaviour.join()

        response = ACLMessage(to=str(request.sender))
        response.performative = Performative.INFORM
        response.action = DeallocationRequest.__key__
        response.protocol = 'Request'
        response.ontology = self.agent.ontology.name
        self.agent.release_lock()
        return response
Ejemplo n.º 10
0
 async def prepare_result_notification(self,
                                       request: ACLMessage) -> ACLMessage:
     content: SelfDeallocationRequest = self.agent.content_manager.extract_content(
         request)
     blocking_containers = self.agent.get_blocking_containers(
         content.container_id)
     for container_id, _, container_agent_jid in blocking_containers:
         await self.agent.remove_container(container_id)
         await self._reallocate_container(container_agent_jid)
     await self.agent.remove_container(content.container_id)
     self.agent.release_lock()
     response = ACLMessage(to=str(request.sender),
                           sender=str(self.agent.jid))
     response.protocol = 'Request'
     response.ontology = self.agent.ontology.name
     response.performative = Performative.INFORM
     response.action = SelfDeallocationRequest.__key__
     return response
    async def prepare_result_notification(self,
                                          request: ACLMessage) -> ACLMessage:
        content: ContainersDeallocationRequest = self.agent.content_manager.extract_content(
            request)

        deallocation_mt = Template()
        deallocation_mt.set_metadata('protocol', 'Request')
        deallocation_mt.set_metadata('action', DeallocationRequest.__key__)

        for container_jid in content.containers_jids:
            deallocation_behaviour = DeallocationInitiator(container_jid)
            self.agent.add_behaviour(deallocation_behaviour, deallocation_mt)
            await deallocation_behaviour.join()

        self.agent.log('Containers deallocated by Port Manager')
        response = ACLMessage(to=str(request.sender))
        response.performative = Performative.INFORM
        response.protocol = 'Request'
        response.ontology = self.agent.ontology.name
        response.action = ContainersDeallocationRequest.__key__
        return response
Ejemplo n.º 12
0
    async def prepare_result_notification(self,
                                          request: ACLMessage) -> ACLMessage:
        content: ReallocationRequest = self.agent.content_manager.extract_content(
            request)
        if content.slot_id == self.agent.slot_id:
            allocation_mt = Template()
            allocation_mt.set_metadata('protocol', 'ContractNet')
            allocation_mt.set_metadata('action', AllocationRequest.__key__)
            allocation_behaviour = AllocationInitiator(
                self.agent.available_slots_jids, False)

            self.agent.add_behaviour(allocation_behaviour, allocation_mt)
            await allocation_behaviour.join()

        response = ACLMessage(to=str(request.sender))
        response.performative = Performative.INFORM
        response.action = ReallocationRequest.__key__
        response.protocol = 'Request'
        response.ontology = self.agent.ontology.name
        self.agent.release_lock()
        return response
 def fill_content(self, content: ContentElement, msg: ACLMessage):
     msg.language = 'xml'
     if issubclass(type(content), Action):
         msg.set_metadata('action', content.__key__)
     msg.body = unparse({content.__key__: asdict(content)}, pretty=True)
 async def prepare_result_notification(self, request: ACLMessage) -> ACLMessage:
     await asyncio.sleep(random.randint(1, 4))
     response: ACLMessage = ACLMessage(to=str(request.sender))
     response.performative = Performative.INFORM
     return response
 async def prepare_response(self, request: ACLMessage) -> ACLMessage:
     print(f'{self.agent.name}: REQUEST received from {request.sender}')
     return request.create_reply(Performative.AGREE)
 def _prepare_request(self, receiver: str) -> ACLMessage:
     msg = ACLMessage(to=receiver)
     msg.performative = Performative.REQUEST
     return msg
@Singleton
class BookShopOntology(Ontology):
    def __init__(self):
        super().__init__('Book Shop Ontology')
        self.add(Author)
        self.add(Book)
        self.add(BookShelf)


# Create content manager and register ontology
content_manager = ContentManager()
book_shop_ontology = BookShopOntology.instance()
content_manager.register_ontology(book_shop_ontology)

# Test book serialization and deserialization
bookA: Book = Book('4.50 from Paddington', Author('Agatha', 'Christie'), 150)
bookB: Book = Book('The Doll', Author('Boleslaw', 'Prus'), 650)
book_shelve = BookShelf('1', [bookA, bookB])
print(f'Initial book shelve object:\n{book_shelve}\n')

msg: ACLMessage = ACLMessage(
    to='receiver@host',
    sender='sender@host',
)
msg.ontology = book_shop_ontology.name
content_manager.fill_content(book_shelve, msg)
print(f'Book shelve as message body:\n{msg.body}\n')

received_book = content_manager.extract_content(msg)
print(f'Received book shelve object:\n{received_book}\n')
Ejemplo n.º 18
0
 async def prepare_result_notification(self, request: ACLMessage) -> ACLMessage:
     print(f'{self.agent.name}: ACCEPT-PROPOSAL received from {request.sender}')
     await asyncio.sleep(random.randint(1, 4))
     return request.create_reply(Performative.INFORM)
Ejemplo n.º 19
0
 async def prepare_response(self, request: ACLMessage) -> ACLMessage:
     print(f'{self.agent.name}: REQUEST received from {request.sender}')
     msg = request.create_reply(Performative.PROPOSE)
     msg.body = str(random.randint(1, 10))
     return msg
Ejemplo n.º 20
0
 def _prepare_cfp(self, receiver: str) -> ACLMessage:
     msg = ACLMessage(to=receiver)
     msg.performative = Performative.CFP
     return msg