personalassist.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 456 行

JAVA
456
字号
/* PersonalAssist.java * $Id: PersonalAssist.java,v 2.4 1997-12-07 14:59:50+09 ht Exp $ * $Name: alpha5_1-release $ *  */package com.ibm.acl.kqmlsample.travel;import com.ibm.acl.kqml.*;import com.ibm.acl.facilitator.FacilitatorProperties;import java.lang.*;import java.io.*;import java.net.*;import java.util.*;/** * PersonalAssist Sample * * @version     $Revision: 2.4 $ $Date: 1997-12-07 14:59:50+09 $ * @author      Osamu Furusawa * @author      Hajime Tsuchitani */public class PersonalAssist {  private static final String rcsid = "$Id: PersonalAssist.java,v 2.4 1997-12-07 14:59:50+09 ht Exp $ $Name: alpha5_1-release $";  /**   * This agnet has the reference of KQMLManager, this agnet is   * KQML-Speakable.   */  private KQMLManager kManager;    /**   * agent name   */  private String agentName;    /**   * local hostname (ktp format)   */  private String localHost;    /**   * facilitator address(ktp format)    */  private String facilitator;    /**   *  lifecycleID   */  private String lifecycleID;    /**   *  yellowpageID   */  private String yellowpageID;      /**   * frame   */  private PersonalAssistFrame frame;    /**   * frame   */  private PersonalSelectWindow selectWindow;    /**   * port number   */  private int port;      /**   * TravelBroker Address   */  private String travelBroker;      /**   * Constructor   *   * @param port socket port number.   */  public PersonalAssist(int port){        this.port = port;        FacilitatorProperties fp;    try {      fp = new FacilitatorProperties();    } catch (IOException ex) {      ex.printStackTrace();      return;    }    facilitator = fp.getOneURL("ktp");    if (facilitator == null) {      print(" no ktp facilitator found, system exit!");      System.exit(1);    }    agentName = new String("PersonalAssist");        print("port" + port);        try {      InetAddress home = InetAddress.getLocalHost();      StringBuffer buf = new StringBuffer("ktp://");      buf.append(home.getHostName() + ":");      buf.append(Integer.toString(port));      localHost = new String(buf.toString());    } catch (UnknownHostException e) {      System.err.println("Unknown HostName");      //            System.exit(1);    }        if ( -1 != port ) {      kManager = new KQMLManager(agentName, "ktp", port);      frame = new PersonalAssistFrame(this);      frame.show();            // scenario - (35)      setRegisterL();            // scenario - (2-1)      setAskallY();          }  }    public KQMLManager getKQMLManager() {    return kManager;  }    public String getTravelBroker() {    return travelBroker;  }    public String getLocalHost() {    return localHost;  }    /**   * showFlightSchedule called by PersonalAssitFrame.   *   */  public void showFlightSchedule(FlightInfo fInfo, int expiredTime) {        print("time schedule ");    print("carrier       " + fInfo.getCarrier());    //print("flight        " + fInfo.);    print("departure     " + fInfo.getDeparture());    print("arrival       " + fInfo.getArrival());    print("deptTime      " + fInfo.getDeptTime());    print("ExpiredTime   " + expiredTime );        if ( fInfo.getArrival().equals("(NRT) Tokyo, Japan") ) {      fInfo.setArrival("NRT");    } else if ( fInfo.getArrival().equals("(CDG) Paris, France") ) {      fInfo.setArrival("CDG");    } else if ( fInfo.getArrival().equals("(JFK) New York, U.S.") ) {      fInfo.setArrival("JFK");    }        if ( fInfo.getDeparture().equals("(NRT) Tokyo, Japan") ) {      fInfo.setDeparture("NRT");    } else if ( fInfo.getDeparture().equals("(CDG) Paris, France") ) {      fInfo.setDeparture("CDG");    } else if ( fInfo.getDeparture().equals("(JFK) New York, U.S.") ) {      fInfo.setDeparture("JFK");    }        // scenario (2-3)    Vector airLineCandidates = setBrkallAskallT(fInfo, expiredTime);    if (airLineCandidates != null) {      selectWindow = new PersonalSelectWindow(this, airLineCandidates);    } else {      System.err.println("no result scenario(2-3)");    }  }      /**   * PersonalAssistant register on LifeCycle   * scenario (35)   */  private void setRegisterL() {        KQML kqml = new KQML();    String rw = new String(kManager.getInitialID());        kqml.setPerformative("register");    kqml.setParameter(":name", agentName);    kqml.setParameter(":sender", localHost);    kqml.setParameter(":receiver", facilitator);    kqml.setParameter(":reply-with", rw);    kqml.setParameter(":language", "anACL");    kqml.setParameter(":ontology", "lifecycle");    kqml.setContent("(:class PersonalAssistant)");        Conversation conv = null;    try {      conv =	(Conversation) kManager.sendMessage(kqml);    } catch (SendFailedException ex) {      ex.printStackTrace();    } catch (InvalidFormatException ex) {      ex.printStackTrace();    } catch (InvalidMessageSequenceException ex) {      ex.printStackTrace();    } catch (JKQMLException ex) {      ex.printStackTrace();    }         conv.getResult().waitFor(45000);    Result res = conv.getResult();    if ( res.isAvailable()) {      ReturnValue rtn = (ReturnValue)res.getResult();      if (rtn.isOK()) {	lifecycleID = new String((String)rtn.getReturnValue());	print("register: " + lifecycleID);      } else {	System.err.println("setRegisterL() :Error in registered process");	System.err.println((String)rtn.getReturnValue());	//		System.exit(-1);      }    } else {      System.err.println("setRegisterL() :Error Result isn't available");      //	    System.exit(-1);    }  }      /**   * PersonalAssit ask-all YP on Yellowpages   * scenario (2-1)   */  private void setAskallY() {        KQML kqml = new KQML();    String rw = new String(kManager.getInitialID());        kqml.setPerformative("ask-all");    kqml.setParameter(":name", agentName);    kqml.setParameter(":sender", localHost);    kqml.setParameter(":receiver", facilitator);    kqml.setParameter(":reply-with", rw);    kqml.setParameter(":language", "anACL");    kqml.setParameter(":ontology", "yellowpages");    String content = new String(        	              	"(:category travel"		+"\n"+                	       	":subCategory travel-broker )"                       		);        kqml.setContent(content);        Conversation conv = null;    try {      conv =	(Conversation) kManager.sendMessage(kqml);    } catch (SendFailedException ex) {      ex.printStackTrace();    } catch (InvalidFormatException ex) {      ex.printStackTrace();    } catch (InvalidMessageSequenceException ex) {      ex.printStackTrace();    } catch (JKQMLException ex) {      ex.printStackTrace();    } 	        conv.getResult().waitFor(45000);    Result res = conv.getResult();        if ( res.isAvailable() ) {      ReturnValue rtn = (ReturnValue)res.getResult();      if ( rtn.isOK() ) {	AgentInfo agentinfo = new AgentInfo();	Vector agentInfos = (Vector)rtn.getReturnValue();	Enumeration enum = agentInfos.elements();	while (enum.hasMoreElements()) {	  agentinfo = (AgentInfo)enum.nextElement();	  print("Name      : " + agentinfo.getAgentName() );	  print("URL       : " + agentinfo.getURL() );	  print("ClassName : " + agentinfo.getClassName() );	  travelBroker = new String(agentinfo.getURL());	}      } else {	System.err.println("setAskallY() :Error in ask-all performative");	System.err.println((String)rtn.getReturnValue());	//	    	System.exit(-1);      }    } else {      System.err.println("setAskallY() :Error Result isn't available");    }  }      /**   * PersonalAssistr broker-all/ask-all on Travel   * scenario (2-3)   */  private Vector setBrkallAskallT(FlightInfo fInfo, int expiredTime) {        KQML kqml = new KQML();    String rw = new String(kManager.getInitialID());    String rw2 = new String(kManager.getInitialID());        kqml.setPerformative("broker-all");    kqml.setParameter(":name", agentName);    kqml.setParameter(":sender", localHost);    kqml.setParameter(":receiver", travelBroker);    kqml.setParameter(":reply-with", rw);    kqml.setParameter(":language", "KQML");    kqml.setParameter(":ontology", "travel-broker");        KQML kqml_i = new KQML();    kqml_i.setPerformative("ask-all");    kqml_i.setParameter(":name", agentName);    kqml_i.setParameter(":sender", localHost);    kqml_i.setParameter(":receiver", travelBroker);    kqml_i.setParameter(":reply-with", rw2);    kqml_i.setParameter(":language", "anACL");    kqml_i.setParameter(":ontology", "travel");        String content;    int expirationTime = expiredTime - 10; // 10 seconds for comunication with Travel broker    if (fInfo.getCarrier().equalsIgnoreCase("any")) {      content = new String(				   "( :departure " + fInfo.getDeparture()  +"\n"+			   ":arrival "   + fInfo.getArrival()    +"\n"+			   ":deptTime \""  + fInfo.getDeptTime() + "\"\n" +			   ":expirationTime " + new Integer(expirationTime) + ")"   			   );    } else {      content = new String(				   "( :carrier " + fInfo.getCarrier()  +"\n"+			   ":departure " + fInfo.getDeparture()  +"\n"+			   ":arrival "   + fInfo.getArrival()    +"\n"+			   ":deptTime \""  + fInfo.getDeptTime() + "\"\n" + 			   ":expirationTime " + new Integer(expirationTime) + ")"   			   );    }        kqml_i.setContent(content);    kqml.setContent(kqml_i);        print("query string :" + kqml.toString());        Conversation conv = null;    try {      conv =	(Conversation) kManager.sendMessage(kqml);    } catch (SendFailedException ex) {      ex.printStackTrace();    } catch (InvalidFormatException ex) {      ex.printStackTrace();    } catch (InvalidMessageSequenceException ex) {      ex.printStackTrace();    } catch (JKQMLException ex) {      ex.printStackTrace();    } 	    Result res = conv.getResult();    ReturnValue rtn;        Vector candidates = new Vector();                res.waitFor(expiredTime * 1000);    if ( res.isAvailable() ) {      if (null != ( rtn = (ReturnValue)res.getSubscribedResult() ) ) {	if (rtn.getReturnValue() instanceof Vector) {	  	  Vector flights = (Vector)rtn.getReturnValue();	  Enumeration enum = flights.elements();	  while (enum.hasMoreElements()) {	    FlightInfo resultFInfo = (FlightInfo) enum.nextElement();	    candidates.addElement(resultFInfo);	    print("Carrier   : " + resultFInfo.getCarrier() );	    print("Flight    : " + resultFInfo.getFlight() );	    print("Departure : " + resultFInfo.getDeparture() );	    print("Arrival   : " + resultFInfo.getArrival() );	    print("DeptTime  : " + resultFInfo.getDeptTime() );	  }	}      }    } else {      System.err.println("setBrkallAskallT() :Error Result isn't available");    }    return candidates;  }        public void startSubscribe(FlightInfo finfo) {  }        /**   * print for debug.   *   * @param msg print message   */  private void print(String msg) {    if (KQMLManager.debug) {      System.err.println("PersonalAssist." + msg);    }  }      /**   * main routine   *   * @param args[] port number. The default number is 8001.   */  public static void main(String args[]) {        int port = 8101;    String usage =       new String("Usage: PersonalAssist [-trace] [-port <port#>] [-help]");     int argc = args.length;    for (int i = 0; i < argc; i++) {      try {	if (args[i].equals("-trace")) {	  KQMLManager.debug = true;	} else if (args[i].equals("-port")) {	  port = Integer.parseInt(args[++i]);	} else if (args[i].equals("-help")) {	  System.err.println(usage);	  System.exit(0);	} else {	  System.err.println(usage);	  System.exit(1);	}      } catch (ArrayIndexOutOfBoundsException e) {	System.err.println(usage);	System.exit(1);      } catch (NumberFormatException nfe) {	System.err.println(usage);	System.exit(1);      }    }        PersonalAssist app = new PersonalAssist(port);      }}

⌨️ 快捷键说明

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