fares) {
this.fares = fares;
@@ -146,9 +152,9 @@ public class Journey implements Perceptable{
this.queryid = queryid;
}
- /** Textual representation of the journey. Use for debuging purposes inly.*/
+ /** Textual representation of the journey. Use for debuging purposes only. */
public String toString() {
-
+
String toString = "From: ";
toString = toString.concat(origin);
toString = toString.concat(" (");
@@ -160,13 +166,13 @@ public class Journey implements Perceptable{
toString = toString.concat(") in ");
toString = toString.concat(duration);
toString = toString.concat(" for ");
- if(fares != null)
+ if (fares != null)
toString = toString.concat(fares.toString());
else
toString += null;
return toString;
}
-
+
/**
* Generates a beliefs representation of the Journey.
* In the current implementation of {@link #toPercepts()}, the journey
@@ -209,21 +215,21 @@ public class Journey implements Perceptable{
percept = percept.concat(NotationUtils.compact(this.origin));
percept = percept.concat(", ");
percept = percept.concat(NotationUtils.compact(this.destination));
-
+
percept = percept.concat(", time(");
String digits[] = this.departureTime.split("[\\x2E\\x3A]"); // [.:]
percept = percept.concat(digits[0]);
percept = percept.concat(", ");
percept = percept.concat(digits[1]);
-
+
percept = percept.concat("), time(");
digits = this.arrivalTime.split("[\\x2E\\x3A]"); // [.:]
percept = percept.concat(digits[0]);
percept = percept.concat(", ");
percept = percept.concat(digits[1]);
-
+
percept = percept.concat("), [");
- for(String fareName : fares.keySet()) {
+ for (String fareName : fares.keySet()) {
percept = percept.concat("fare(");
percept = percept.concat(NotationUtils.compact(fareName));
percept = percept.concat(", ");
@@ -238,10 +244,9 @@ public class Journey implements Perceptable{
percept = percept.concat(this.getQueryid());
percept = percept.concat(")]");
-
LinkedList ret = new LinkedList();
ret.add(Literal.parseLiteral(percept));
-
+
return ret;
}
diff --git a/src/java/es/upm/dit/gsi/sojason/services/AsyncWebServiceConnector.java b/src/java/es/upm/dit/gsi/sojason/services/AsyncWebServiceConnector.java
new file mode 100644
index 0000000..c5eb7ed
--- /dev/null
+++ b/src/java/es/upm/dit/gsi/sojason/services/AsyncWebServiceConnector.java
@@ -0,0 +1,57 @@
+/**
+ *
+ */
+package es.upm.dit.gsi.sojason.services;
+
+import es.upm.dit.gsi.sojason.SOModel;
+
+/**
+ * This interface defines a standard way to connect to a web service in
+ * the definition of an external action in Jason.
+ *
+ * Project: Web40SOJason
+ * Package: es.upm.dit.gsi.sojason.services
+ * Class: WebServiceConnector
+ *
+ * @author Miguel Coronado (miguelcb@dit.upm.es)
+ * @version Feb 27, 2012
+ *
+ */
+public interface AsyncWebServiceConnector {
+
+ /**
+ * This calls the service including in the request the parameters given.
+ * The URL of the service, the method to use and any other particularity
+ * of the transaction to connect to the service must be determined in the
+ * implementation of the method.
+ *
+ * @param params The list of parameters to include in the service
+ * request. Due to this is not a Dictionary
+ * the order of the parameters it is important and will be
+ * determined by the implementation of the extendee
+ * classes.
+ * @return Whether the call was successful or not.
+ */
+ public boolean call(SOModel model, String agname, String... params);
+
+
+ /**
+ * This validates the set of parameters provided. Typically, this method
+ * should use some regex exprsesions to check whether a parameter is valid
+ * or not, due to the nature of the parameter cannot be checked because of
+ * the type of the parameters has been unified to String.
+ *
+ * @param params The list of parameters to validate
+ */
+ public boolean validateParams(String... params);
+
+ /**
+ * This generates a set of error Literal
s that describes the
+ * errors committed when trying to call the given service with the set of
+ * parameters given.
+ *
+ * @param params The list of parameters
+ */
+// public Set checkForErrors(String... params);
+
+}
diff --git a/src/java/es/upm/dit/gsi/sojason/services/socketio/SocketIOConnector.java b/src/java/es/upm/dit/gsi/sojason/services/socketio/SocketIOConnector.java
new file mode 100644
index 0000000..2f7f5ce
--- /dev/null
+++ b/src/java/es/upm/dit/gsi/sojason/services/socketio/SocketIOConnector.java
@@ -0,0 +1,131 @@
+package es.upm.dit.gsi.sojason.services.socketio;
+
+import io.socket.IOAcknowledge;
+import io.socket.IOCallback;
+import io.socket.SocketIO;
+import io.socket.SocketIOException;
+
+import jason.asSyntax.Literal;
+
+import java.net.MalformedURLException;
+import java.util.logging.Logger;
+
+import net.sf.json.JSONException;
+import net.sf.json.JSONObject;
+import es.upm.dit.gsi.sojason.SOModel;
+import es.upm.dit.gsi.sojason.services.AsyncWebServiceConnector;
+
+/**
+ * Project: Web40SOJason
+ * Package: es.upm.dit.gsi.sojason.services.nlu
+ * Class: NLUConnector
+ *
+ * @author Miguel Coronado (miguelcb@dit.upm.es)
+ * @version Feb 27, 2012
+ *
+ */
+public class SocketIOConnector implements AsyncWebServiceConnector{
+
+ /** The url of the service */
+ private String serviceUrl;
+ /** */
+ private Logger logger = Logger.getLogger("Web40SOJason." + SocketIOConnector.class.getName());
+
+ private static SocketIO socket;
+ static IOCallback callback = new IOCallback() {
+
+ public void onMessage(JSONObject json, IOAcknowledge ack) {
+ System.out.println("Message:" + json.toString());
+ }
+
+ @Override
+ public void onMessage(String data, IOAcknowledge ack) {
+ System.out.println("Message:" + data);
+ }
+
+ @Override
+ public void onError(SocketIOException socketIOException) {
+ System.out.println("Error");
+ socketIOException.printStackTrace();
+ }
+
+ @Override
+ public void onDisconnect() {
+
+ }
+
+ @Override
+ public void onConnect() {
+ }
+
+ @Override
+ public void on(String event, IOAcknowledge ack, Object... args) {
+
+ }
+
+ @Override
+ public void onMessage(org.json.JSONObject arg0, IOAcknowledge arg1) {
+ // TODO Auto-generated method stub
+
+ }
+ };
+
+
+ //TODO Remove try-catch
+ /** Constructor */
+ public SocketIOConnector(String serviceUrl) {
+ this.serviceUrl = serviceUrl;
+
+ try {
+ socket = new SocketIO();
+ socket.connect(serviceUrl, callback);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ *
+ */
+ public boolean call(final SOModel model, final String agName, String... params) {
+ logger.info("Calling SocketIOConnector with"+params[0]);
+ try {
+ socket.emit("test", new IOAcknowledge() {
+ @Override
+ public void ack(Object... args) {
+ logger.info("Package ack'ed");
+ Literal lit = Literal.parseLiteral("success(yes)");
+ model.setDataInbox(agName, lit);
+ logger.info("DataInbox set");
+ }
+ }, new JSONObject().put("Hello", "Planeta"));
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * This validates the parameters received. The
+ * {@link SocketIOConnector#call(String...)} method expects to receive two
+ * parameters of the nature and characteristics described below:
+ *
+ *
+ * - The first parameter is que query id. It is an alphanumeric string
+ * which normally will contain numbers, but other non-digit characters
+ * are permitted. No alphanumeric values are not allowed
+ *
+ *
+ *
+ */
+ public boolean validateParams(String... params) {
+ if (params.length != 2){
+ return false;
+ }
+ // TODO: check other things
+ return true;
+ }
+
+}
diff --git a/testSocket.mas2j b/testSocket.mas2j
new file mode 100644
index 0000000..9c89ece
--- /dev/null
+++ b/testSocket.mas2j
@@ -0,0 +1,16 @@
+/*
+ Jason Project
+
+ -- create on March 19, 2012
+*/
+
+MAS web40sojason {
+ infrastructure: Centralised
+
+ environment: es.upm.dit.gsi.sojason.SOEnvironment
+
+ agents:
+ testAgent;
+
+ aslSourcePath: "src/asl";
+}
\ No newline at end of file