/** @param message Converts Jason Message to a camel Message and add to the exchange */ public void agentMessaged(Message message) { Exchange exchange = endpoint.createExchange(); if (endpoint.getUriOption().contains("message")) { try { String ei = endpoint.getIlloc_force(); String es = endpoint.getSender(); String er = endpoint.getReceiver(); // create message header HashMap<String, Object> headerInfo = new HashMap<String, Object>(); String s = message.getSender(); String r = message.getReceiver(); String i = message.getIlForce(); headerInfo.put("sender", s); headerInfo.put("receiver", r); headerInfo.put("illoc_force", i); headerInfo.put("msg_id", message.getMsgId()); // Extract out any annotations and add as "annotations" header try { Literal lit = (Literal) message.getPropCont(); if (lit != null) { ListTerm lt = lit.getAnnots(); if (lt != null) headerInfo.put("annotations", StringUtils.join(lit.getAnnots(), ',')); } else headerInfo.put("annotations", ""); } catch (ClassCastException e) { } // COmpare the message content with uri options Matcher matcher = endpoint.getBodyMatcher(message.getPropCont().toString()); exchange.getIn().setHeaders(headerInfo); // send message to next processor in the route after checking route conditions if ((i.equals(ei) || ei == null) && (s.equals(es) || es == null) || (r.equals(er) || er == null)) processMatchedMessage(matcher, exchange, message.getPropCont().toString()); } catch (Exception e) { } finally { // log exception if an exception occurred and was not handled if (exchange.getException() != null) getExceptionHandler() .handleException("Error processing exchange", exchange, exchange.getException()); else { String ep = endpoint.getExchangePattern().toString(); if (ep.equals("InOut")) { /* if (exchange.getOut() != null) { Message m = new Message(); } */ } } } } }
/** * @param agentName * @param actionName * @param parameters * @param unifier * @return Maps internal Jason agent actions to camel messages */ public Object agentInternallyActed( String agentName, String actionName, List<Term> parameters, Unifier unifier) { Object actionsucceeded = true; Exchange exchange = endpoint.createExchange(); // Agent action can only be processed by an endpoint of type "action" if (endpoint.getUriOption().contains("action")) { try { List<String> paramsAsStrings = new ArrayList<String>(); for (Term t : parameters) { paramsAsStrings.add(t.toString()); } sendActionToCamel(agentName, actionName, paramsAsStrings, exchange, ""); } catch (Exception e) { } finally { // log exception if an exception occurred and was not handled if (exchange.getException() != null) { getExceptionHandler() .handleException("Error processing exchange", exchange, exchange.getException()); actionsucceeded = false; } else { String ep = endpoint.getExchangePattern().toString(); // Just send out camel message and inform success to agent if (ep.equals("InOnly")) actionsucceeded = true; // Unification is applicable to InOut exchanges only. This waits for the return message // from the exchange else if (ep.equals("InOut")) { if (exchange.hasOut()) { List<String[]> mappings = getResultHeaderSplitted(); for (String[] mapping : mappings) { int unPos = Integer.parseInt(mapping[1]); String headerVal = mapping[0]; // ArrayList<String> l = (ArrayList<String>)exchange.getIn().getHeader(headerVal); // Iterator<String> it = l.iterator(); String unVal = exchange.getIn().getHeader(headerVal).toString(); try { unifier.unifies(parameters.get(unPos), ASSyntax.parseTerm(unVal)); } catch (jason.asSyntax.parser.ParseException e) { System.out.println( "Error parsing result header from synchronous InOut action: " + unVal); return false; } } return true; } else actionsucceeded = false; } } } } return actionsucceeded; }
/** * @param jasonAction * @return Maps external Jason agent actions to camel messages */ public boolean agentActed(JasonAction jasonAction) { boolean actionsucceeded = true; Exchange exchange = endpoint.createExchange(); if (endpoint.getUriOption().contains("action")) { try { ActionExec action = jasonAction.getAction(); String agName = jasonAction.getAgName(); List<String> paramsAsStrings = new ArrayList<String>(); for (Term t : action.getActionTerm().getTerms()) paramsAsStrings.add(t.toString()); // extract annotations List<Term> ann = action.getActionTerm().getAnnots(); String annots = ""; if (ann != null) annots = ann.toString(); // StringUtils.join(ann, ','); exchange.getIn().setBody(action.getActionTerm().toString(), String.class); sendActionToCamel( agName, action.getActionTerm().getFunctor(), paramsAsStrings, exchange, annots); } catch (Exception e) { } finally { // log exception if an exception occurred and was not handled if (exchange.getException() != null) { getExceptionHandler() .handleException("Error processing exchange", exchange, exchange.getException()); actionsucceeded = false; } else { String ep = endpoint.getExchangePattern().toString(); // Just send out camel message and inform success to agent if (ep.equals("InOnly")) actionsucceeded = true; else if (ep.equals("InOut")) { if (exchange.getOut() != null) actionsucceeded = true; else actionsucceeded = false; } } } } return actionsucceeded; }