1
0
mirror of https://github.com/balkian/SOJA.git synced 2024-12-27 20:28:12 +00:00

merge fix

This commit is contained in:
Miguel Coronado 2012-03-22 19:57:42 +01:00
parent 8a517c87a0
commit 9bc4304a4a
10 changed files with 191 additions and 45 deletions

View File

@ -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
-->
<project name ="web40sojason"

View File

@ -11,6 +11,8 @@ contact(userAgent).
my_service(travel).
my_service(train).
fare(turista).
/************** Plans *****************/
/* Introduce myself to the user agent */
@ -35,14 +37,14 @@ my_service(train).
@findTravel2
+!find(travel, Query) : not canFindTravel(Query) & delay(Query)
<- -delay(Query);
.print("Not enought data. Lets ask!").
.print("Not enought data. TODO:Lets ask!").
@findTravel3
+!find(travel, Query) : canFindTravel(Query)
<- ?location(to, To);
?location(from, From);
?date(departure, Day, Month, Year);
findTravel(From, To, Day, Month, Year);
findTravel(Query,From, To, Day, Month, Year);
.print("ok").
@findTravelFailureRety
@ -53,8 +55,23 @@ my_service(train).
<- .print("Problema al encontrar viajes:", Msg);
!findTravel(Query).
+journey(From, To, Departure, Arrival, fare(FName, FPrice))[query(Query), source(percept)]
: fare(FName)
<- +journey(From, To, Departure, Arrival, fare(FName, FPrice))[query(Query)]. //note it down
+journey(From, To, Departure, Arrival, fare(FName, FPrice))[query(Query), source(percept)]
: ~fare(FName)
<- true. // skip this
+journey(From, To, Departure, Arrival, fare(FName, FPrice))[query(Query), source(percept)]
: not fare(_) & not ~fare(FName) // no restrictions, so note them all down
<- +journey(From, To, Departure, Arrival, fare(FName, FPrice))[query(Query)].
/* log results */
@log_the_journey
+journey(From, To, Departure, Arrival, Fares) : true
<- .print("Travel found: From ", From,"<", Departure, "> to ", To, "<", Arrival, "> for ", Fares).
+journey(From, To, Departure, Arrival, fare(FName, FPrice))[query(Query), source(percept)]
: true
<- .print("Discard travel : From ", From,"<", Departure, "> to ", To, "<", Arrival, "> for ", Fares).

View File

@ -82,6 +82,7 @@ public class Web40Model extends SOModel{
*/
public boolean findTravel (String agName, Collection<Term> params) {
logger.info("Entering findTravel...");
try{
String[] strParams = CollectionUtils.toStringArray(params);
Collection<Literal> 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;
}

View File

@ -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<String, String> 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)]
* <p>Generates a beliefs representation of the </code>Journey</code>.
* 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:</p>
*
* @return
* <ul>
* <li>journey(madrid, ciudad_real, time(10,15), time(11,5), fare(turista, 22.5))</li>
* <li>journey(madrid, ciudad_real, time(10,15), time(11,5), fare(preferente, 35))</li>
* </ul>
*
* <p>Notice that in the current implementation the <code>Duration</code>
* attribute is skiped.</p>
*
* @return The list with the percepts or an empty list if no fare is given
*/
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 new LinkedList<Literal>(); }
return unfoldPercepts();
//return foldPercepts();
}
/**
* <p>Generates a beliefs representation of the </code>Journey</code>.
* The journey is represented by a single belief, that contains all the
* fares as an array. The exact representation is presented below</p>
*
* <ul>
* <li>journey(madrid, ciudad_real, time(10,15), time(11,5), [fare(turista, 22.5), fare(preferente, 35)])</li>
* </ul>
*
* @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<Literal> 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<Literal> ret = new LinkedList<Literal>();
ret.add(Literal.parseLiteral(percept));
return ret;
}
/**
* <p>Generates a beliefs representation of the </code>Journey</code>.
* 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:</p>
*
* <ul>
* <li>journey(madrid, ciudad_real, time(10,15), time(11,5), fare(turista, 22.5))</li>
* <li>journey(madrid, ciudad_real, time(10,15), time(11,5), fare(preferente, 35))</li>
* </ul>
*
* 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<Literal> unfoldPercepts() {
LinkedList<Literal> ret = new LinkedList<Literal>();// 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<String, String> fares = new HashMap<String, String>();
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());
}
}

View File

@ -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;

View File

@ -46,15 +46,30 @@ public class RenfeScrapper implements WebServiceConnector {
return null;
}
try {
List<Perceptable> schedule = getSchedule ( params[0].toString(),
params[1].toString(),
String queryid = params[0].toString();
List<Perceptable> 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<Literal> res = new LinkedList<Literal>();
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<Literal> 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;