def _handle_no_participant_name(self, aid): IBASGlobal.print_message( 'va handling no participant nid-name ' + str(aid), 2) self.__lock.acquire() self.name_responses = self.name_responses + 1 self.data[NID_DICT_KEY][aid.shortname] = "Unknown" self.__lock.release()
def view_node(self, nid, language): IBASGlobal.print_message( 'IA: View Node: ' + str(nid) + ' language ' + str(language), 1) self.node_rep = None aid = self.mts.ams.find_agent(nid) if aid is None: aid = self.aid pyroloc = str(aid.addresses[0]) self.add_behaviour( RequestInitiatorBehaviour( store=AID(pyroloc + 'ViewAgent'), request=str(nid) + ' ' + str(language), handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform_view, handle_inform_done=self._handle_inform_done_view, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel)) while self.node_rep == None: time.sleep(0.5) res = self.node_rep self.node_rep = None return res
def search_node_by_attribute(self, att_type, att_value): IBASGlobal.print_message( 'IA: SEARCH FOR ATT_TYPE ' + str(att_type) + ' AND VALUE WITH ' + str(att_value), 1) self.search_result = None if att_type != "" and att_value == "": request = 'att_type:' + att_type elif att_type == "" and att_value != "": request = 'att_value:' + att_value else: request = 'att_type_value:"' + att_type + '"#"' + att_value + '"' self.add_behaviour( RequestInitiatorBehaviour( store=AID('Search Agent'), request=request, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel)) while self.search_result is None: time.sleep(0.5) res = self.search_result self.search_result = None return res
def _handle_inform_name(self, nidname): IBASGlobal.print_message('va handling inform nid-name ' + str(nidname), 2) self.__lock.acquire() self.name_responses = self.name_responses + 1 self.data[NID_DICT_KEY][nidname[0]] = nidname[1] self.__lock.release()
def __add_node_sne(self, nid): try: url = "http://pc50002945:8045/sne/xml/getXmlNode?nid=" + str(nid) xml_file = urllib2.urlopen(url) xmldoc = minidom.parse(xml_file) self.iba_node = Node() self._parse_xml(xmldoc, self.iba_node) except: self.result.append('Error while parsing XML for node: ' + nid) return try: self.mts.ams.start_agent(IBA, str(self.iba_node.nid), node=self.iba_node) self.result.append(nid + " IBA created") except: self.result.append('Error while starting new IBA:' + self.iba_node.nid) return try: self.__index.nodes.put(self.iba_node) IBASGlobal.print_message('Added node to index ' + str(nid), 2) except: self.result.append('Error while adding new node to index:' + self.iba_node.nid)
def _perform_request(self, request): IBASGlobal.print_message('iba performing request '+request,2) if request == 'normal': res_node = copy.deepcopy(self.node) #res_node.statements = res_node.statements[:10] # limit the amount of statements #res_node.attributes = res_node.attributes[:10] # limit the amount of attributes self.result = res_node elif request == 'name': # return the first name of the node self.result = self.node.get_name(None) elif request.startswith('name:'): # return the name of the node in the given language self.result = self.node.get_name(request.split(':')[1]) elif request.startswith('nid-name:'): # return a tuple containing the nid of the node and it's name in a certain language self.result = (self.node.nid,self.node.get_name(request.split(':')[1])) elif request == 'nid-name': # return a tuple containing the nid of the node and the first name of the node self.result = (self.node.nid,self.node.names[0][0]) elif request.startswith('value-type:'): # t consists of the value of the attribute from which the type needs to be determined # and a language in which the view agent is currently working t = request.split(':') self.result = (self._get_value_type(),t[1],t[2]) IBASGlobal.print_message('iba value type-result '+str(self.result),2) return True
def _send_response(self): self.status = REQUEST_NODE self.name_requests = 0 self.name_responses = 0 self.value_type_requests = 0 self.value_type_responses = 0 self.data = {} self.data[NODE_KEY] = None self.data[NID_DICT_KEY] = {} # contains names of certain nids IBASGlobal.print_message('VA Sending agreed', 2)
def _perform_request(self, request): IBASGlobal.print_message('NMA: request ' + str(request), 2) self.result = [] if request.endswith('.zip'): self.__add_zip(request) else: self.__add_node(request) self.result.append('New node(s) added.') return True
def go_sleep(self,until=None,in_mem=False): """ when an agent goes to sleep it's RequestParticipantBehaviour needs to be removed since it can't be serialized """ self.remove_behaviour(self.rpb) self.rpb = None if until is None: self.suspend(in_mem) else: IBASGlobal.print_message('suspending '+self.shortname+' until '+str(until),1) self.suspend_until(until,in_mem)
def action(self): if datetime.now() >= self.agent.move_time: target = self.agent.mts.ams.get_move_target() if target is not None: IBASGlobal.print_message(self.agent.shortname+' moving to '+str(target),1) self.agent.remove_behaviour(self.agent.rpb) self.agent.rpb = None self.agent.move(target) else: # if an agent is not going to move it can go to sleep self.agent.allow_sleep=True
def delete_node(self, nid): try: index_present = len(os.listdir('index')) > 0 if index_present: reader = PyLucene.IndexReader.open("index") term = PyLucene.Term(COLUMN_NID, nid) if reader.termDocs(term) != None: reader.deleteDocuments(term) reader.close() except: IBASGlobal.print_message( "Error while deleting document from Lucene with nid " + str(nid), 0)
def _perform_request(self, request): request = request.split(' ') if self.status == REQUEST_NODE: IBASGlobal.print_message( 'VA: trying to contact ' + str(request[0]), 2) self.add_behaviour( RequestInitiatorBehaviour( store=AID(str(request[0])), request="normal", handle_no_participant=self._handle_no_participant, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, check_cancel=self._check_cancel)) self.status = REQUEST_INFO elif self.status == REQUEST_INFO: if self.data[NODE_KEY]: for att in self.data[NODE_KEY].attributes: self._handle_attribute(att, request[1]) for stat in self.data[NODE_KEY].statements: restricted = False for att in stat.attributes: if str(att.type) == RESTRICTED_TO: restricted = True if restricted: self.data[NID_DICT_KEY][stat.predicate] = 'Restricted' self.data[NID_DICT_KEY][stat.object] = 'Restricted' else: self._request_nid_name(stat.predicate, request[1]) self._request_nid_name(stat.object, request[1]) for att in stat.attributes: self._handle_attribute(att, request[1]) for name in self.data[NODE_KEY].names: self._request_nid_name(name[1], request[1]) self.status = REQUEST_WAIT elif self.status == REQUEST_WAIT: if self.name_responses >= self.name_requests and \ self.value_type_responses >= self.value_type_requests: return True return False
def search_node_by_name(self, name): IBASGlobal.print_message('IA: SEARCH FOR ' + str(name) + ' INITIATED', 1) self.search_result = None self.add_behaviour( RequestInitiatorBehaviour( store=AID('Search Agent'), request='name:' + name, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel)) while self.search_result is None: time.sleep(0.5) res = self.search_result self.search_result = None return res
def add_node(self, path): IBASGlobal.print_message('IA: Add Node: ' + str(path), 1) self.node_rep = None self.add_behaviour( RequestInitiatorBehaviour( store=AID('Node Manager Agent'), request=path, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform_view, handle_inform_done=self._handle_inform_done_view, handle_failure=self._handle_failure, cancel=self._cancel, check_cancel=self._check_cancel)) while self.node_rep == None: time.sleep(0.5) res = self.node_rep self.node_rep = None return res
def _request_nid_name(self, nid, language): IBASGlobal.print_message('VA requesting nid-name ' + str(nid), 2) try: nid = str(nid) except: return self.__lock.acquire() if self.data[NID_DICT_KEY].has_key(nid): self.__lock.release() return self.data[NID_DICT_KEY][nid] = None self.name_requests = self.name_requests + 1 self.__lock.release() self.add_behaviour( RequestInitiatorBehaviour( store=AID(str(nid)), request='nid-name:' + str(language), handle_no_participant=self._handle_no_participant_name, handle_agree=self._handle_agree, handle_inform=self._handle_inform_name, handle_inform_done=self._handle_inform_done_name, check_cancel=self._check_cancel)) return
def _perform_request(self, request): self.request_result=[] request = request.split(':') if request[0] == "name": IBASGlobal.print_message('SA: SEARCHING FOR NAME '+str(request[1]),2) self.request_result = self.__index.search_node_by_name(request[1]) elif request[0] == "att_type": IBASGlobal.print_message('SA: SEARCHING FOR ATT_TYPE '+str(request[1]),2) self.request_result = self.__index.search_node_by_attribute(request[1],"") elif request[0] == "att_value": IBASGlobal.print_message('SA: SEARCHING FOR ATT_VALUE '+str(request[1]),2) self.request_result = self.__index.search_node_by_attribute("",request[1]) elif request[0] == "att_type_value": IBASGlobal.print_message('SA: SEARCHING FOR ATT_TYPE AND ATT_VALUE '+str(request[1]),2) type_value = request[1].split('@') self.request_result = self.__index.search_node_by_attribute(type_value[0],type_value[1]) return True
def get_nid_name(self, nid, language): IBASGlobal.print_message( 'IA: GET NAME FOR ' + str(nid) + ' language' + str(language), 2) self.search_result = None self.add_behaviour( RequestInitiatorBehaviour( store=AID(str(nid)), request='name:' + language, handle_response=self._handle_response, handle_agree=self._handle_agree, handle_refuse=self._handle_refuse, handle_inform=self._handle_inform, handle_inform_done=self._handle_inform_done, handle_failure=self._handle_failure, handle_no_participant=self._handle_no_participant, cancel=self._cancel, check_cancel=self._check_cancel)) while self.search_result is None: time.sleep(0.1) res = self.search_result self.search_result = None return res
def run(self, args): try: IBASGlobal.verbose_level = int(args[0]) except: IBASGlobal.verbose_level = 0 index = NodeIndex() index.start() self.start_agent(SearchAgent, 'Search Agent', index=index) self.start_agent(InterfaceAgent, 'Interface Agent') self.start_agent(NodeManagerAgent, 'Node Manager Agent', index=index) self.start_agent(LoggerAgent, 'Logger Agent') self.start_agent(ViewAgent, str(Platform.mts.pyrouri) + 'ViewAgent') #data_dir = "nodegathering/n1/" data_dir = "nodegathering/n2/" #data_dir = "nodegathering/n3/" #data_dir = "nodegathering/n4/" #data_dir = "nodegathering/n5/" if len(os.listdir('storage')) == 0: IBASGlobal.print_message( 'No stored agents. Parsing XML files again.', 0) self.start_agent(StatusAgent, 'Status Agent') files = os.listdir(data_dir) for file in files: try: xmldoc = minidom.parse(data_dir + file) iba_node = Node() self._parse_xml(xmldoc, iba_node) name = self.create_agent(IBA, str(iba_node.nid), node=iba_node) #name = self.start_agent(IBA, str(iba_node.nid), node=iba_node) #time.sleep(0.2) index.index_node(iba_node) except: IBASGlobal.print_message( "## error while creating iba from:" + file + "##", 0) self.invoke_all_agents() IBASGlobal.print_message('########### IBAS STARTED #############', 0)
def _cancel(self): IBASGlobal.print_message('VA cancelling view request', 2)
def _handle_inform_done_name(self, node): IBASGlobal.print_message('va handling inform-done-name', 2)
def _handle_failure(self): IBASGlobal.print_message('IA: handling failure', 2)
def _handle_no_participant(self, participant): self.search_result = participant.shortname IBASGlobal.print_message('IA: handling no participant', 2)
def _handle_inform(self, node): IBASGlobal.print_message('VA handle inform', 2) self.data[NODE_KEY] = node
def _handle_no_participant(self, aid): IBASGlobal.print_message('VA handle no participant', 2) self.data[NODE_KEY] = None
def _handle_agree(self): IBASGlobal.print_message('VA handling agree', 2)
def _handle_response(self): IBASGlobal.print_message('VA handling response', 2)
def _send_result(self): IBASGlobal.print_message('VA: Node representation created', 2) return self.data
def _cancel(self): IBASGlobal.print_message('IA: cancelling search', 2)
def _handle_inform_done(self, content): IBASGlobal.print_message('IA: NO AGENTS FOUND', 2) self.search_result = "no agents found"
def _handle_inform(self, content): IBASGlobal.print_message('IA: FOUND ' + str(content), 2) self.search_result = content