diff --git a/bin/build.xml b/bin/build.xml index 9a9fe68..252b39c 100644 --- a/bin/build.xml +++ b/bin/build.xml @@ -5,7 +5,7 @@ This file was generated by Jason 1.3.6a http://jason.sf.net - March 20, 2012 - 16:30:55 + March 22, 2012 - 19:21:46 --> to ", To, "<", Arrival, "> for ", Fares). \ No newline at end of file ++journey(From, To, Departure, Arrival, fare(FName, FPrice))[query(Query), source(percept)] + : true + <- .print("Discard travel : From ", From,"<", Departure, "> to ", To, "<", Arrival, "> for ", Fares). + \ No newline at end of file diff --git a/src/java/es/upm/dit/gsi/sojason/Web40Model.java b/src/java/es/upm/dit/gsi/sojason/Web40Model.java index bcdcadc..25fd755 100644 --- a/src/java/es/upm/dit/gsi/sojason/Web40Model.java +++ b/src/java/es/upm/dit/gsi/sojason/Web40Model.java @@ -82,6 +82,7 @@ public class Web40Model extends SOModel{ */ public boolean findTravel (String agName, Collection params) { + logger.info("Entering findTravel..."); try{ String[] strParams = CollectionUtils.toStringArray(params); Collection serviceData = renfeScrapper.call(strParams); @@ -91,6 +92,7 @@ public class Web40Model extends SOModel{ this.setDataInbox(agName, serviceData); } catch (Exception e){ return false; } + logger.info("Scraping completed successfully"); return true; } diff --git a/src/java/es/upm/dit/gsi/sojason/beans/Journey.java b/src/java/es/upm/dit/gsi/sojason/beans/Journey.java index 003bd86..0630c21 100644 --- a/src/java/es/upm/dit/gsi/sojason/beans/Journey.java +++ b/src/java/es/upm/dit/gsi/sojason/beans/Journey.java @@ -5,6 +5,7 @@ package es.upm.dit.gsi.sojason.beans; import jason.asSyntax.Literal; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -12,7 +13,13 @@ import java.util.Map; import es.upm.dit.gsi.jason.utils.NotationUtils; /** - * @author miguel + * + * Project: SOJA + * Package: es.upm.dit.gsi.sojason.beans + * Class: Journey + * + * @author Miguel Coronado (miguelcb@dit.upm.es) + * @version Mar 22, 2012 * */ public class Journey implements Perceptable{ @@ -37,7 +44,10 @@ public class Journey implements Perceptable{ /** The fee map that contains the different available fee */ private Map fares; - + + /** The query id. It is an annotation, it is not mandatory */ + private String queryid = null; + /** * @return the departureTime */ @@ -122,6 +132,20 @@ public class Journey implements Perceptable{ this.fares = fares; } + /** + * @return the queryid + */ + public String getQueryid() { + return queryid; + } + + /** + * @param queryid the queryid to set + */ + public void setQueryid(String queryid) { + this.queryid = queryid; + } + /** Textual representation of the journey. Use for debuging purposes inly.*/ public String toString() { @@ -144,41 +168,42 @@ public class Journey implements Perceptable{ } /** - * 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)] + *

Generates a beliefs representation of the Journey. + * In the current implementation of {@link #toPercepts()}, the journey + * is represented by several beliefs, all of them with the same value + * in the from, to, departure and arrival time fields, but different + * fares. The exact representation is presented below:

* - * @return + *
    + *
  • journey(madrid, ciudad_real, time(10,15), time(11,5), fare(turista, 22.5))
  • + *
  • journey(madrid, ciudad_real, time(10,15), time(11,5), fare(preferente, 35))
  • + *
+ * + *

Notice that in the current implementation the Duration + * attribute is skiped.

+ * + * @return The list with the percepts or an empty list if no fare is given */ public List 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 ret = new LinkedList(); -// ret.add(Literal.parseLiteral(percept)); -// -// return ret; - - if (this.fares.size() == 0){ return null; } + if (this.fares.size() == 0){ return new LinkedList(); } + return unfoldPercepts(); + //return foldPercepts(); + } + + /** + *

Generates a beliefs representation of the Journey. + * The journey is represented by a single belief, that contains all the + * fares as an array. The exact representation is presented below

+ * + *
    + *
  • journey(madrid, ciudad_real, time(10,15), time(11,5), [fare(turista, 22.5), fare(preferente, 35)])
  • + *
+ * + * @return List with literals that represent the journey. With a folded + * percept only one percept will be included in the list. + */ + @SuppressWarnings("unused") + private List foldPercepts() { String percept = "journey("; percept = percept.concat(NotationUtils.compact(this.origin)); @@ -208,10 +233,98 @@ public class Journey implements Perceptable{ percept = percept.substring(0, percept.lastIndexOf(",")); percept = percept.concat("])"); + // queryid annotation + percept = percept.concat("[query("); + percept = percept.concat(this.getQueryid()); + percept = percept.concat(")]"); + + LinkedList ret = new LinkedList(); ret.add(Literal.parseLiteral(percept)); return ret; } + + /** + *

Generates a beliefs representation of the Journey. + * The journey is represented by several beliefs, all of them with the + * same value in the from, to, departure and arrival time fields, but + * different fares. The exact representation is presented below:

+ * + *
    + *
  • journey(madrid, ciudad_real, time(10,15), time(11,5), fare(turista, 22.5))
  • + *
  • journey(madrid, ciudad_real, time(10,15), time(11,5), fare(preferente, 35))
  • + *
+ * + * TODO: when it is unfloded, it might be usefull if it includes, as + * annotation, an id that represents the journey, to easily join the + * literals. + * + * @return List with literals that represent the journey. All of the + * literals will have the same, from, to, departure and arrival + * time value, but different fares. + */ + private List unfoldPercepts() { + LinkedList ret = new LinkedList();// returning list + + 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()) { + String perceptFare = percept.concat("fare("); + perceptFare = perceptFare.concat(NotationUtils.compact(fareName)); + perceptFare = perceptFare.concat(", "); + perceptFare = perceptFare.concat(fares.get(fareName)); + perceptFare = perceptFare.concat(") "); + perceptFare = perceptFare.concat(")"); // close journey + + // queryid annotation + perceptFare = perceptFare.concat("[query("); + perceptFare = perceptFare.concat(this.getQueryid()); + perceptFare = perceptFare.concat(")]"); + + ret.add(Literal.parseLiteral(perceptFare)); + } + + return ret; + } + + /** Try toPercepts method */ + public static void main(String[]args) { + Journey j = new Journey(); + j.setArrivalTime("10.00"); + j.setDepartureTime("8:50"); + j.setDestination("ciudad real"); + j.setDuration(""); + j.setOrigin("madrid"); + j.setQueryid("123456"); + + Map fares = new HashMap(); + fares.put("turista", "22.5"); + fares.put("preferente", "38.5"); + fares.put("turista niño", "16.0"); + fares.put("preferente niño", "28.5"); + j.setFares(fares); + + System.out.println(j.toPercepts()); +// System.out.println(j.foldPercepts()); + } + } diff --git a/src/java/es/upm/dit/gsi/sojason/services/nlu/NLUConnector.java b/src/java/es/upm/dit/gsi/sojason/services/nlu/NLUConnector.java index dc82ad1..5cbb61f 100644 --- a/src/java/es/upm/dit/gsi/sojason/services/nlu/NLUConnector.java +++ b/src/java/es/upm/dit/gsi/sojason/services/nlu/NLUConnector.java @@ -6,7 +6,6 @@ import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_DATES_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_DATES_RETURN_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_DOMAINS_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_FROM_NODENAME; -import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_TO_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_LOCATIONS_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_MAX_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_MIN_NODENAME; @@ -15,6 +14,7 @@ import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_PRICE_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_TIME_DEPART_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_TIME_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_TIME_RETURN_NODENAME; +import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_TO_NODENAME; import static es.upm.dit.gsi.sojason.services.nlu.NLUModel.JSON_TRAVEL_NODENAME; import jason.asSyntax.Literal; @@ -32,7 +32,6 @@ import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; import es.upm.dit.gsi.jason.utils.NotationUtils; -import es.upm.dit.gsi.sojason.Web40Model; import es.upm.dit.gsi.sojason.beans.NLUTravel; import es.upm.dit.gsi.sojason.services.WebServiceConnector; diff --git a/src/java/es/upm/dit/gsi/sojason/services/travel/RenfeScrapper.java b/src/java/es/upm/dit/gsi/sojason/services/travel/RenfeScrapper.java index 05cb220..764938a 100644 --- a/src/java/es/upm/dit/gsi/sojason/services/travel/RenfeScrapper.java +++ b/src/java/es/upm/dit/gsi/sojason/services/travel/RenfeScrapper.java @@ -46,15 +46,30 @@ public class RenfeScrapper implements WebServiceConnector { return null; } try { - List schedule = getSchedule ( params[0].toString(), - params[1].toString(), + String queryid = params[0].toString(); + List schedule = getSchedule ( params[1].toString(), params[2].toString(), params[3].toString(), - params[4].toString()); + params[4].toString(), + params[5].toString()); + System.out.println(">>>" + schedule); // prepare response Collection res = new LinkedList(); for (Perceptable travel : schedule){ + System.out.println(">>>" + travel); + if(travel instanceof Journey){ + ((Journey)travel).setQueryid(queryid); // add the query id + } + System.out.println(">>>" + travel.toPercepts()); + + + List list = travel.toPercepts(); + for(Literal l : list){ + System.out.println("Anota" + l.getAnnots()); + } + + res.addAll(travel.toPercepts()); } return res; @@ -67,7 +82,7 @@ public class RenfeScrapper implements WebServiceConnector { } public boolean validateParams(String... params) { - if(params.length != 5){ + if(params.length != 6){ return false; } return true;