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

Added reverse conversion and draft documentation

This commit is contained in:
J.Fernando Sánchez 2012-04-22 05:23:09 +02:00
parent b23bbd0451
commit 09102f4cdc

View File

@ -4,11 +4,19 @@
package es.upm.dit.gsi.jason.utils;
import jason.asSyntax.Literal;
import jason.asSyntax.NumberTermImpl;
import jason.asSyntax.Term;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import net.sf.json.JSON;
import net.sf.json.util.JSONUtils;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.impl.JsonParserBase;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -90,13 +98,20 @@ public abstract class CollectionUtils {
return strArray;
}
public static String toPerceptsObject(JSONObject jso){
/**
*
* @param jso JSONObject to transform to Percepts
* @return String representation of the JSONObject as Percent
*/
private static String toPerceptsObject(JSONObject jso){
StringBuilder sb = new StringBuilder();
Iterator<String> it = jso.keys();
while(it.hasNext()){
String key = it.next();
try {
Object temp= jso.get(key);
key = NotationUtils.compact(key);
String res = toPercepts(temp);
sb.append(key+"(");
sb.append(res);
@ -112,7 +127,13 @@ public abstract class CollectionUtils {
}
public static String toPerceptsArray(JSONArray jsa){
/**
*
* @param jsa JSONArray to be converted
* @return String Percept representation of the JSONArray
*/
private static String toPerceptsArray(JSONArray jsa){
StringBuilder sb = new StringBuilder();
int top = jsa.length();
int topm = top-1;
@ -132,6 +153,23 @@ public abstract class CollectionUtils {
}
/**
* String representation of a Percept, from a JSON-like object
*
* JSON strings are represented as quoted strings in jason, as it seems to
* be the easiest and most compatible way.
*
* Examples:
* {"key 1":"value 1"} => key_1("value")
*
* {"key1":"value1", "key2":2} => key1("value1"),key2(2)
*
* {"testArray":["value1","value2","value3"],"testKey":"testValue","specialKey":"Ñoña-2_3","testMix":["one",{"twoDic":[1,2,"2",2,2.1]}],"testDic":{"key2":"value2","key1":"value1"}}
* => testarray(["value1","value2","value3"]),testkey("testValue"),specialkey("Ñoña-2_3"),testmix(["one",twodic([1,2,"2",2,2.1])]),testdic(key2("value2"),key1("value1"))
*
* @param obj Object to be translated to Percepts
* @return String representation of the Percept
*/
public static String toPercepts(Object obj){
if(obj instanceof JSONArray){
@ -140,9 +178,86 @@ public abstract class CollectionUtils {
else if (obj instanceof JSONObject) {
return toPerceptsObject((JSONObject) obj);
}
else{
return NotationUtils.compact(obj.toString());
else if (obj instanceof Number){
return obj.toString();
}
else{
return "\""+obj.toString()+"\"";
}
//journey(madrid,barcelona,time(15,30),time(18,40),fare(preferente_nino,105.9))
//journey(madrid,barcelona,time(17,0),time(19,30),fare(turista_nino,83.3))
//;
// Para nuestro ejemplo:
// journey(["madrid","barcelona",time(15,30),time(18,40),fare("preferente nino",105.9)])
// n.
}
/**
* Example of conversion:
*
* Percept: journey(["madrid","barcelona",time(15,30),time(18,40),fare("preferente nino",105.9)])
* Result: { "journey" : { "madrid", "barcelona", { "time" : { 15, 30 } } , { "time" : { 18, 40 } } , { "fare" : { "preferente nino", 105.9 } } } }
*
* @param percept Perception as a String
* @return String representation of the JSON
*/
public static String perceptToJSON(String percept){
//System.out.println("Pasando con:"+percept);
String result = "";
String end = "";
Literal lit = Literal.parseLiteral(percept);
String fun = lit.getFunctor();
if(!fun.equals("")){
result+= "{ \""+ fun + "\" : { ";
end = " } } ";
}
Term[] terms = lit.getTermsArray();
int last = terms.length-1;
for(int i=0;i<=last;i++){
Term term = terms[i];
result+=termToString(term);
if(i<last){
result+=", ";
}
}
return result + end;
}
public static String termToString(Term term){
String result = "";
if(term.isNumeric()){
System.out.println("Numeric:"+ term.toString());
result = term.toString();
}
else if(term.isLiteral()){
System.out.println("Literal:"+ term.toString());
result = perceptToJSON(term.toString());
}
else if (term.isString()){
System.out.println("String:"+ term.toString());
result = term.toString();
}
else if (term.isList()){
System.out.println("List:"+ term.toString());
int last =((List<Term>) term).size()-1;
for(int i=0;i<=last;i++){
Term t = ((List<Term>) term).get(i);
result+=(termToString(t));
if(i<last){
result+=", ";
}
}
}
return result;
}
public static void main(String[] args){
String st = "journey([\"madrid\",\"barcelona\",time(15,30),time(18,40),fare(\"preferente nino\",105.9)])";
String json = perceptToJSON(st);
System.out.println("String: "+st);
System.out.println("Resultado: "+json);
}
}