1
0
mirror of https://github.com/balkian/SOJA.git synced 2025-12-17 14:08:17 +00:00

Added socket.io PoC

This commit is contained in:
J.Fernando Sánchez
2012-03-22 19:51:10 +01:00
parent 8a517c87a0
commit e725d7a733
64 changed files with 522 additions and 63 deletions

View File

@@ -57,7 +57,11 @@ public class SOEnvironment extends Environment {
result = this.model.sendNlu(agName, terms);
}
else if (functor.equals("findTravel")) {
result = this.model.findTravel(agName, terms);
this.model.findTravel(agName, terms);
return true;
}
else if (functor.equals("sendSocket")) {
result = this.model.sendSocket(agName, terms);
return true;
}
else {

View File

@@ -9,6 +9,7 @@ import java.util.logging.Logger;
import es.upm.dit.gsi.jason.utils.CollectionUtils;
import es.upm.dit.gsi.sojason.services.nlu.NLUConnector;
import es.upm.dit.gsi.sojason.services.socketio.SocketIOConnector;
import es.upm.dit.gsi.sojason.services.travel.RenfeScrapper;
/**
@@ -26,12 +27,15 @@ public class Web40Model extends SOModel{
/** */
public final static String NLU_SERVICE_URL = "http://46.4.52.82:3333/nlu";
public final static String SOCKET_SERVICE_URL = "http://localhost:3000";
/** */
private NLUConnector nluConnector;
/** */
private RenfeScrapper renfeScrapper;
/** */
private Logger logger = Logger.getLogger("Web40SOJason." + Web40Model.class.getName());
private SocketIOConnector socketConnector;
/** Constructor
* @throws IOException */
@@ -39,6 +43,7 @@ public class Web40Model extends SOModel{
super();
this.nluConnector = new NLUConnector(NLU_SERVICE_URL);
this.renfeScrapper = new RenfeScrapper();
this.socketConnector = new SocketIOConnector(SOCKET_SERVICE_URL);
}
/**
@@ -95,4 +100,33 @@ public class Web40Model extends SOModel{
}
/**
*
* @param agName
* @param terms
* @return
*/
public boolean sendSocket (String agName, Collection<Term> params) {
logger.info("Entering sendSocket...");
try{
String[] strParams = CollectionUtils.toStringArray(params);
boolean serviceData = socketConnector.call(this,agName,strParams);
if(serviceData == false){
logger.info("Could not complete action socketIO: false!");
return false;
}
// put data into mailbox
// this.setDataInbox(agName, serviceData);
}
catch (Exception e){
logger.info("Could not complete action SocketIO (error):" + e.getCause() + "_"+e.getCause()+"_"+e.getMessage());
return false;
}
logger.info("SocketConnector call completed successfully");
return true;
}
}

View File

@@ -13,28 +13,28 @@ import es.upm.dit.gsi.jason.utils.NotationUtils;
/**
* @author miguel
*
*
*/
public class Journey implements Perceptable{
public class Journey implements Perceptable {
/** The departure time of the journey */
private String departureTime;
/** The arrival time of the journey */
private String arrivalTime;
/**
* The duration of the journey. This is not simply the difference of the
/**
* The duration of the journey. This is not simply the difference of the
* departure and arrival time because of timezone considerations.
*/
private String duration;
/** The origin */
private String origin;
/** The destination */
private String destination;
/** The fee map that contains the different available fee */
private Map<String, String> fares;
@@ -46,7 +46,8 @@ public class Journey implements Perceptable{
}
/**
* @param departureTime the departureTime to set
* @param departureTime
* the departureTime to set
*/
public void setDepartureTime(String departureTime) {
this.departureTime = departureTime;
@@ -60,7 +61,8 @@ public class Journey implements Perceptable{
}
/**
* @param arrivalTime the arrivalTime to set
* @param arrivalTime
* the arrivalTime to set
*/
public void setArrivalTime(String arrivalTime) {
this.arrivalTime = arrivalTime;
@@ -74,7 +76,8 @@ public class Journey implements Perceptable{
}
/**
* @param duration the duration to set
* @param duration
* the duration to set
*/
public void setDuration(String duration) {
this.duration = duration;
@@ -88,7 +91,8 @@ public class Journey implements Perceptable{
}
/**
* @param oringin the oringin to set
* @param oringin
* the oringin to set
*/
public void setOrigin(String origin) {
this.origin = origin;
@@ -102,7 +106,8 @@ public class Journey implements Perceptable{
}
/**
* @param destination the destination to set
* @param destination
* the destination to set
*/
public void setDestination(String destination) {
this.destination = destination;
@@ -116,15 +121,16 @@ public class Journey implements Perceptable{
}
/**
* @param fares the fares to set
* @param fares
* the fares to set
*/
public void setFares(Map<String, String> fares) {
this.fares = fares;
}
/** Textual representation of the journey. Use for debuging purposes inly.*/
/** Textual representation of the journey. Use for debuging purposes inly. */
public String toString() {
String toString = "From: ";
toString = toString.concat(origin);
toString = toString.concat(" (");
@@ -136,69 +142,72 @@ 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;
}
/**
* journey(madrid, ciudad_real, 10.15, 11.5, [fare(turista, 22.5), fare(preferente, 35)]
* journey(madrid, ciudad_real, time(10,15), time(11,5), [fare(turista, 22.5), fare(preferente, 35)]
* journey(madrid, ciudad_real, 10.15, 11.5, [fare(turista, 22.5),
* fare(preferente, 35)] journey(madrid, ciudad_real, time(10,15),
* time(11,5), [fare(turista, 22.5), fare(preferente, 35)]
*
* @return
*/
public List<Literal> toPercepts() {
// if (this.fares.size() == 0){ return null; }
//
// String percept = "journey(";
// percept = percept.concat(this.origin);
// percept = percept.concat(", ");
// percept = percept.concat(this.destination);
// percept = percept.concat(", ");
// percept = percept.concat(this.departureTime);
// percept = percept.concat(", ");
// percept = percept.concat(this.arrivalTime);
//
// percept = percept.concat(", [");
// for(String fareName : fares.keySet()) {
// percept = percept.concat("fare(");
// percept = percept.concat(fareName);
// percept = percept.concat(", ");
// percept = percept.concat(fares.get(fareName));
// percept = percept.concat("), ");
// }
// percept = percept.substring(0, percept.lastIndexOf(","));
// percept = percept.concat("])");
//
// LinkedList<Literal> ret = new LinkedList<Literal>();
// ret.add(Literal.parseLiteral(percept));
//
// return ret;
if (this.fares.size() == 0){ return null; }
// if (this.fares.size() == 0){ return null; }
//
// String percept = "journey(";
// percept = percept.concat(this.origin);
// percept = percept.concat(", ");
// percept = percept.concat(this.destination);
// percept = percept.concat(", ");
// percept = percept.concat(this.departureTime);
// percept = percept.concat(", ");
// percept = percept.concat(this.arrivalTime);
//
// percept = percept.concat(", [");
// for(String fareName : fares.keySet()) {
// percept = percept.concat("fare(");
// percept = percept.concat(fareName);
// percept = percept.concat(", ");
// percept = percept.concat(fares.get(fareName));
// percept = percept.concat("), ");
// }
// percept = percept.substring(0, percept.lastIndexOf(","));
// percept = percept.concat("])");
//
// LinkedList<Literal> ret = new LinkedList<Literal>();
// ret.add(Literal.parseLiteral(percept));
//
// return ret;
if (this.fares.size() == 0) {
return null;
}
String percept = "journey(";
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(", ");
@@ -207,10 +216,10 @@ public class Journey implements Perceptable{
}
percept = percept.substring(0, percept.lastIndexOf(","));
percept = percept.concat("])");
LinkedList<Literal> ret = new LinkedList<Literal>();
ret.add(Literal.parseLiteral(percept));
return ret;
}

View File

@@ -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 <code>Dictionary</code>
* 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 <code>Literal</code>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<Literal> checkForErrors(String... params);
}

View File

@@ -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:
*
* <ul>
* <li>The first parameter is que query id. It is an alphanumeric string
* which normally will contain numbers, but other non-digit characters
* are permitted. <b>No alphanumeric values are not allowed</b></li>
* <li></li>
* </ul>
*
*/
public boolean validateParams(String... params) {
if (params.length != 2){
return false;
}
// TODO: check other things
return true;
}
}