anacl_travel.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,047 行 · 第 1/3 页
JAVA
1,047 行
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.doAskAll: query completed."); System.err.println(new Date() + " - anacl_travel.doAskAll: result of ask-all: "); System.err.println(content); } try { rtnMsg.setPerformative("tell"); rtnMsg.setParameter(":sender", msg.getParameter(":receiver")); rtnMsg.setParameter(":receiver", msg.getParameter(":sender")); rtnMsg.setParameter(":in-reply-to", msg.getParameter(":reply-with")); rtnMsg.setParameter(":reply-with", id); rtnMsg.setParameter(":language", msg.getParameter(":language")); rtnMsg.setParameter(":ontology", msg.getParameter(":ontology")); rtnMsg.setContent("("+ content + " )"); } catch (Exception ex) { ex.printStackTrace(); } } else { if (debug) System.err.println(new Date() + " - anacl_travel.doAskAll: No flight found."); try { rtnMsg.setPerformative("sorry"); rtnMsg.setParameter(":sender", msg.getParameter(":receiver")); rtnMsg.setParameter(":receiver", msg.getParameter(":sender")); rtnMsg.setParameter(":in-reply-to", msg.getParameter(":reply-with")); rtnMsg.setParameter(":reply-with", id); rtnMsg.setParameter(":language", msg.getParameter(":language")); rtnMsg.setParameter(":ontology", msg.getParameter(":ontology")); rtnMsg.setContent("(:errmsg \" No flight found.\")"); } catch (Exception ex) { ex.printStackTrace(); } } } Conversation c = null; try { c = km.sendMessage(rtnMsg); } 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.doAskAll: sending response message failed"); return false; } else { if (debug) System.err.println(new Date() + " < anacl_travel.doAskAll: sent response message successfully"); return true; } } /** * Handles tell performative. * * @param conversation reference to the Conversation * @param msg KQML message * @return true if message is interpreted successfully, otherwise false. * */ protected boolean doTell(Conversation conversation, KQML msg) { if (debug) System.err.println(new Date() + " > anacl_travel.doTell entered"); Conversation conv = (Conversation) conversation; Result res = conv.getResult(); if (handleBrokeredMSG(conv, msg, "tell")) return true; String carrier = null; String flight = null; String departure = null; String arrival = null; String deptTime = null; Vector flights = new Vector(); FlightInfo rec = null; Vector tokens = ParseContent.parse((String) msg.getContent()); 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.doTell parsing: carrier: " + carrier); if (rec != null) { if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing carrier: illegal sequence found." + " tell of ask-all corrupted. "); rec = null; continue; } rec = new FlightInfo(); rec.setCarrier(carrier); continue; } else if (((String) tokens.elementAt(i)).equalsIgnoreCase(":flight")) { flight = (String) tokens.elementAt(++i); if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing: flight: " + flight); if (rec == null || (rec.getCarrier() == null)) { if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing flight: illegal sequence found." + " tell of ask-all corrupted. "); rec = null; continue; } rec.setFlight(flight); continue; } else if (((String) tokens.elementAt(i)).equalsIgnoreCase(":departure")) { departure = (String) tokens.elementAt(++i); if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing: departure: " + departure); if (rec == null || (rec.getCarrier() == null) || (rec.getFlight() == null)) { if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing departure: illegal sequence found." + " tell of ask-all corrupted. "); rec = null; continue; } rec.setDeparture(departure); continue; } else if (((String) tokens.elementAt(i)).equalsIgnoreCase(":arrival")) { arrival = (String) tokens.elementAt(++i); if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing: arrival: " + arrival); if (rec == null || (rec.getCarrier() == null) || (rec.getFlight() == null) || (rec.getDeparture() == null)) { if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing departure: illegal sequence found." + " tell of ask-all corrupted. "); rec = null; continue; } rec.setArrival(arrival); continue; } else if (((String) tokens.elementAt(i)).equalsIgnoreCase(":deptTime")) { deptTime = (String) tokens.elementAt(++i); if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing: deptTime: " + deptTime); if (rec == null || (rec.getCarrier() == null) || (rec.getFlight() == null) || (rec.getDeparture() == null) || (rec.getArrival() == null)) { if (debug) System.err.println(new Date() + " - anacl_travel.doTell parsing departure: illegal sequence found." + " tell of ask-all corrupted. "); rec = null; continue; } rec.setDeptTime(deptTime); flights.addElement(rec); rec = null; continue; } } try { if (flights.size() != 0) { if (debug) { System.err.println(new Date() + " < anacl_travel.doTell: " + flights.size() + " flight(s) found."); System.err.println(new Date() + " < anacl_travel.doTell: last element of tell"); int last = flights.size() - 1; FlightInfo fInfo = (FlightInfo) flights.elementAt(last); System.err.println("\t - carrier: " + fInfo.getCarrier()); System.err.println("\t - flight: " + fInfo.getFlight()); System.err.println("\t - departure: " + fInfo.getDeparture()); System.err.println("\t - arrival: " + fInfo.getArrival()); System.err.println("\t - deptTime: " + fInfo.getDeptTime()); } res.setResult(new ReturnValue(true, flights)); res.complete(); if (debug) System.err.println(new Date() + " < anacl_travel.doTell completed. tell recived."); return true; } else { if (debug) res.setResult(new ReturnValue(false, "content of tell is blank.")); res.complete(); System.err.println(new Date() + " < anacl_travel.doTell failed. content of tell is blank."); return false; } } catch (ResultException ex) { System.err.println(ex.getMessage()); if (debug) System.err.println(new Date() + " < anacl_travel.doTell failed, cannot set result"); ex.printStackTrace(); return false; } } /** * Handles sorry performative. * * @param conversation reference to the Conversation * @param msg KQML message * @return true if message is interpreted successfully, otherwise false. * */ protected boolean doSorry(Conversation conversation, KQML msg) { if (debug) System.err.println(new Date() + " > anacl_travel.doSorry entered"); Conversation conv = (Conversation) conversation; Result res = conv.getResult(); if (handleBrokeredMSG(conv, msg, "sorry")) return true; try { Vector tokens = ParseContent.parse((String) msg.getContent()); String errmsg = null; for (int i=0; i < tokens.size(); i++) { if (((String) tokens.elementAt(i)).equalsIgnoreCase(":errmsg")) { errmsg = (String) tokens.elementAt(i + 1); break; } } if (debug) { System.err.println(new Date() + " - anacl_travel.doSorry errmsg: " + errmsg); System.err.println(new Date() + " - anacl_travel.doSorry Result: " + res); } res.setResult(new ReturnValue(false, errmsg)); res.complete(); if (debug) System.err.println(new Date() + " < anacl_travel.doSorry completed"); return true; } catch (ResultException ex) { System.err.println(ex.getMessage()); if (debug) System.err.println(new Date() + " < anacl_travel.doSorry failed: cannot set result"); ex.printStackTrace(); return false; } } /** * Handles error performative. * * @param conversation reference to the Conversation * @param msg KQML message * @return true if message is interpreted successfully, otherwise false. * */ protected boolean doError(Conversation conversation, KQML msg) { if (debug) System.err.println(new Date() + " > anacl_travel.doError entered"); Conversation conv = (Conversation) conversation; Result res = conv.getResult(); if (handleBrokeredMSG(conv, msg, "error")) return true; try { Vector tokens = ParseContent.parse((String) msg.getContent()); String errmsg = null; for (int i=0; i < tokens.size(); i++) { if (((String) tokens.elementAt(i)).equalsIgnoreCase(":errmsg")) { errmsg = (String) tokens.elementAt(i + 1); break; } } if (debug) { System.err.println(new Date() + " - anacl_travel.doError errmsg: " + errmsg); System.err.println(new Date() + " - anacl_travel.doError Result: " + res); } res.setResult(new ReturnValue(false, errmsg)); res.complete(); if (debug) System.err.println(new Date() + " < anacl_travel.doError completed"); return true; } catch (ResultException ex) { System.err.println(ex.getMessage()); if (debug) System.err.println(new Date() + " < anacl_travel.doError failed: cannot set result"); return false; } } /** * Handles performatives which not supported in this class. * * @param conversation reference to the Conversation * @param msg KQML message * @return true if message is interpreted successfully, otherwise false. * */ protected boolean doOtherPerformatives(Conversation conversation, KQML msg) { if (debug) { System.err.println(new Date() +
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?