anacl_travel.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,047 行 · 第 1/3 页

JAVA
1,047
字号
          " - anacl_travel.doOtherPerformatives : "	  + "unknow performative: " + msg.getPerformative()	  + " recieved. message is ignored.");    }    return false;  }  Interpreter getKqmlInterpreter(Conversation conv) {    KQML msg = new KQML();    msg.setParameter(":language", "KQML");    msg.setParameter(":ontology", "kqml-ontology");        if (debug) {	System.err.println(new Date() +           " - anacl_travel.getKqmlInterpreter : "	  + "befor calling lookupInterpreterFor.");    }    Interpreter kqmlinterpreter =       conv.getInterpreterPool().lookupInterpreterFor(msg);    if (debug) {	System.err.println(new Date() +           " - anacl_travel.getKqmlInterpreter : "	  + "got Interpreter: " + kqmlinterpreter);    }    return kqmlinterpreter;  }    String getAirPort(String airport) {    if (airport.equalsIgnoreCase("NRT")) {      return new String("\"(NRT) Tokyo, Japan (Narita)\"");    } else if (airport.equalsIgnoreCase("CDG")) {      return new String("\"(CDG) Paris, France (Charles De Gaulle)\"");    } else if (airport.equalsIgnoreCase("SFO")) {      return new String("\"(SFO) San Francisco, CA, USA\"");    } else if (airport.equalsIgnoreCase("JFK")) {      return new String("\"(JFK) New York, NY, USA (Kennedy Intl)\"");    } else if (airport.equalsIgnoreCase("LHR")) {      return new String("\"(LHR) London, England (Heathrow)\"");    } else if (airport.equalsIgnoreCase("HKG")) {      return new String("\"(HKG) Hong Kong, Hong Kong\"");    } else if (airport.equalsIgnoreCase("HNL")) {      return new String("\"(HNL) Honolulu, HI, USA\"");    } else {      return airport;    }//    return airport;  }  String getTime(String time) {    if (time.endsWith("PM")) {      return "\"" + time.concat("12:15") + "\"";    } else if (time.endsWith("AM")) {      return "\"" + time.concat("10:15") + "\"";    } else {      return time;    }//    return time;  }  boolean handleBrokeredMSG(Conversation conv, KQML msg, String perf) {			        idgen = conv.getIDGenerator();    String id = idgen.getID();    // get KQML interpreter    kqml_kqml_ontology kqmli = (kqml_kqml_ontology) getKqmlInterpreter(conv);    if (kqmli == null) {      System.err.println(new Date() + 	" > anacl_travel.handleBrokeredMSG. Could not found KQML Interpreter."	+ " return false.");      return false;    }    if (debug)      System.err.println(new Date() + 	" > anacl_travel.handleBrokeredMSG. getKQMLInterpreter: " + kqmli);    // check whether this tell must be forwarded to some one?    // some one asked brokering?    KQML brokerMsg;    if ((brokerMsg = (KQML) 	  kqmli.getBrokeredTable().get(msg.getParameter(":in-reply-to")))	!= null)     {      // some one asked brokering       if (debug)	System.err.println(new Date() + 	  " > anacl_travel.handleBrokeredMSG. MUST forward this performative as"	  + " response of Broker performative");      KQML fwdMsg = new KQML();      KQML innerMsg = new KQML();      try {	innerMsg.setPerformative(perf);	innerMsg.setParameter(":receiver", brokerMsg.getParameter(":sender"));	innerMsg.setParameter(":language", msg.getParameter(":language"));	innerMsg.setParameter(":ontology", msg.getParameter(":ontology"));	innerMsg.setContent(msg.getContent());	fwdMsg.setPerformative("forward");	fwdMsg.setParameter(":to", brokerMsg.getParameter(":sender"));	fwdMsg.setParameter(":sender", msg.getParameter(":receiver"));	fwdMsg.setParameter(":receiver", brokerMsg.getParameter(":sender"));	fwdMsg.setParameter(":in-reply-to", brokerMsg.getParameter(":reply-with"));	fwdMsg.setParameter(":reply-with", id); 	fwdMsg.setParameter(":language", "KQML");	fwdMsg.setParameter(":ontology", "kqml-ontology");	fwdMsg.setContent(innerMsg);      } catch (Exception ex) {	ex.printStackTrace();      }      Conversation c = null;      try {	c = km.sendMessage(fwdMsg);      } catch (SendFailedException ex) {	ex.printStackTrace();      } catch (InvalidFormatException ex) {	ex.printStackTrace();      } catch (InvalidMessageSequenceException ex) {	ex.printStackTrace();      } catch (JKQMLException ex) {	ex.printStackTrace();      }       if (c == null) {	System.err.println(new Date() + 	  " < anacl_travel.handleBrokeredMSG: sending response message failed");	return false;      } else {	if (debug)	  System.err.println(new Date() + 	    " < anacl_travel.handleBrokeredMSG: sent response message successfully");	return true;      }    }    if (debug)      System.err.println(new Date() + 	 " < anacl_travel.handleBrokeredMSG: This msg is not the response " +	 "of broker-xxx performative.");    return false;  }  FlightInfo parseAskContent(KQML msg) {    Vector tokens = ParseContent.parse((String) msg.getContent());    String carrier = null;    String flight = null;    String departure = null;    String arrival = null;    String deptTime = null;    for (int i=0; i < tokens.size(); i++) {      if (((String) tokens.elementAt(i)).equalsIgnoreCase(":carrier")) {	carrier = (String) tokens.elementAt(++i);	if (debug)	  System.err.println(new Date() + 	    " - anacl_travel.parseAskContent parsing: carrier: " 		+ carrier);	continue;      } else if (((String) tokens.elementAt(i)).equalsIgnoreCase(":flight")) {	flight = (String) tokens.elementAt(++i);	if (debug)	  System.err.println(new Date() + 	    " - anacl_travel.parseAskContent parsing: flight: " 		+ flight);	continue;      } else if (((String) tokens.elementAt(i)).equalsIgnoreCase(":departure")) {	departure = (String) tokens.elementAt(++i);	if (debug)	  System.err.println(new Date() + 	    " - anacl_travel.parseAskContent parsing: departure: " + departure);	continue;      } else if (((String) tokens.elementAt(i)).equalsIgnoreCase(":arrival")) {	arrival = (String) tokens.elementAt(++i);	if (debug)	  System.err.println(new Date() + 	    " - anacl_travel.parseAskContent parsing: arrival: " + arrival);	continue;      } else if (((String) tokens.elementAt(i)).equalsIgnoreCase(":deptTime")) {	deptTime = (String) tokens.elementAt(++i);	if (debug)	  System.err.println(new Date() + 	    " - anacl_travel.parseAskContent parsing: deptTime: " + deptTime);	continue;      }    }    return new FlightInfo(carrier, flight, departure, arrival, deptTime);  }  boolean handleSubscribedMSG(Conversation conv, KQML msg, FlightInfo rec) {    // subscribed ask-all support    kqml_kqml_ontology kqmli = (kqml_kqml_ontology) getKqmlInterpreter(conv);    if (kqmli == null) {      System.err.println(new Date() + 	" > anacl_travel.handleSubscribedMSG. Could not found KQML Interpreter."	+ " return false.");      return false;    }    Vector subscribeLog = kqmli.getSubscribeLog();    if (subscribeLog == null) {      System.err.println(new Date() +         " > anacl_travel.handleSubscribedMSG. No subscribeLog found.");      // return true, because nobody made subscription.      return true;    }    // somebody made subscription! handle it!    int subLogSize = subscribeLog.size();    if (debug)      System.err.println(new Date() + 	" > anacl_travel.handleSubscribedMSG. subscribeLog size: " + subLogSize);    for (int i = 0; i < subLogSize; i++) {      // check whether subscribe's content is ask-all?      KQML subMsg = (KQML) subscribeLog.elementAt(i);      Object subContent = subMsg.getContent();      if (subContent instanceof KQML) {	KQML subContentMsg = (KQML) subContent;	if (subContentMsg.getPerformative().equalsIgnoreCase("ask-all")) {	  String lang = msg.getParameter(":language"); // AnACL expected	  String onto = msg.getParameter(":ontology"); // YellowPage expected	  if (!((subContentMsg.getParameter(":language").equalsIgnoreCase(lang))		&&		(subContentMsg.getParameter(":ontology").equalsIgnoreCase(onto))))	  {	    if (debug)	      System.err.println(new Date() + 		" > anacl_travel.handleSubscribedMSG. lang and ontology pair "		+ "of ask-all of subscribe not match ");	    continue;	  }	      	  //	  // (subscribe (ask-all ...)) handling Starts!!	  //	  FlightInfo askContent = parseAskContent(subContentMsg);	  String carrier = askContent.getCarrier();	  String flight = askContent.getFlight();	  String departure = askContent.getDeparture();	  String arrival = askContent.getArrival();	  String deptTime = askContent.getDeptTime();	  if ((carrier == null) && (flight == null) && (departure == null) &&	      (arrival == null))	  {	    if (debug)	      System.err.println(new Date() + 		" > anacl_travel.handleSubscribedMSG. " +		" no valid query specified in content of ask-all of subscribe.");	    continue;	  }	  if ((rec.getCarrier().equals(carrier) || (carrier == null)) &&	      (rec.getFlight().equals(flight) || (flight == null)) &&	      (rec.getDeparture().equals(departure) || (departure == null)) &&	      (rec.getArrival().equals(arrival) || (arrival == null)))	  {	    // match found!	    // send insert record...	    StringBuffer content = new StringBuffer();	    content.append(" :carrier ");	    content.append(rec.getCarrier());	    content.append(" :flight ");	    content.append(rec.getFlight());	    content.append(" :departure ");	    content.append(getAirPort(rec.getDeparture()));	    content.append(" :arrival ");	    content.append(getAirPort(rec.getArrival()));	    content.append(" :deptTime ");	    content.append(getTime(rec.getDeptTime()));	    content.append("\n");	    if (debug) { 	      System.err.println(new Date() + 		" - anacl_travel.handleSubscribedMSG: query completed.");	      System.err.println(new Date() +		" - anacl_travel.handleSubscribedMSG: result of ask-all of subscribe: ");	      System.err.println(content);	    }	    KQML tellMsg = new KQML();	    try {	      String id = idgen.getID();	      tellMsg.setPerformative("tell");	      tellMsg.setParameter(":sender", subMsg.getParameter(":receiver"));	      tellMsg.setParameter(":receiver", subMsg.getParameter(":sender"));	      tellMsg.setParameter(":in-reply-to", subContentMsg.getParameter(":reply-with"));	      tellMsg.setParameter(":reply-with", id);	      tellMsg.setParameter(":language", subContentMsg.getParameter(":language"));	      tellMsg.setParameter(":ontology", subContentMsg.getParameter(":ontology"));	      tellMsg.setContent("("+ content + " )");	    } catch (Exception ex) {	      ex.printStackTrace();	    }	    Conversation c = null;	    try {	      c = km.sendMessage(tellMsg);	    } catch (SendFailedException ex) {	      ex.printStackTrace();	    } catch (InvalidFormatException ex) {	      ex.printStackTrace();	    } catch (InvalidMessageSequenceException ex) {	      ex.printStackTrace();	    } catch (JKQMLException ex) {	      ex.printStackTrace();	    } 	    if (c == null) { 	      System.err.println(new Date() + 		" < anacl_travel.handleSubscribedMSG(): sending tell message" +		" as response of (subscribe (ask-if ))failed");	      continue;	    } else {	      if (debug)		System.err.println(new Date() + 		  " < anacl_travel.handleSubscribedMSG(): sending tell message"		  + " as response of (subscribe (ask-if )) succeeded");	      continue;	    }	  } // match found!	} else {	  // no (subscribe (ask-all ....)) found, continue...	  if (debug)	    System.err.println(new Date() + 	      " > anacl_travel.handleSubscribedMSG. " +	      " subscribeLog.ellementAt(" + i + 	      ") is not (subscribe (ask-all ....)), continue loop...");	  continue;	}      } else {	// content of subscribe is not KQML message, so continue ...	if (debug)	  System.err.println(new Date() + 	    " > anacl_travel.handleSubscribedMSG. " +	    " content of subscribeLog.ellementAt("	    + i + ") is not KQML message, continue loop...");	continue;      }    } // for...subscribeLog    return true;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?