mirror of
https://github.com/balkian/SOJA.git
synced 2024-12-27 20:28:12 +00:00
Updated conversion and example
This commit is contained in:
parent
09102f4cdc
commit
27b74831fd
@ -3,7 +3,7 @@ io.sockets.on('connection', function (socket) {
|
||||
console.log("New connection!");
|
||||
socket.on('test', function (data,ack) {
|
||||
console.log(data);
|
||||
ack("hola");
|
||||
ack({"success":"yes"});
|
||||
console.log("I acked");
|
||||
});
|
||||
});
|
||||
|
@ -8,5 +8,5 @@
|
||||
.wait(1000);
|
||||
sendSocket("test","hola").
|
||||
|
||||
+success(yes): true
|
||||
+success("yes"): true
|
||||
<- .print("Succes!!!!!").
|
||||
|
@ -133,7 +133,7 @@ public abstract class CollectionUtils {
|
||||
* @return String Percept representation of the JSONArray
|
||||
*/
|
||||
|
||||
private static String toPerceptsArray(JSONArray jsa){
|
||||
private static String toPerceptsJSONArray(JSONArray jsa){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int top = jsa.length();
|
||||
int topm = top-1;
|
||||
@ -152,6 +152,30 @@ public abstract class CollectionUtils {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param jsa Array to be converted
|
||||
* @return String Percept representation of the JSONArray
|
||||
*/
|
||||
|
||||
private static String toPerceptsArray(Object[] jsa){
|
||||
if(jsa.length<2){
|
||||
return toPercepts(jsa[0]);
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int top = jsa.length;
|
||||
int topm = top-1;
|
||||
sb.append("[");
|
||||
for(int i=0;i<jsa.length;i++){
|
||||
sb.append(toPercepts(jsa[i]));
|
||||
if(i<topm){
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* String representation of a Percept, from a JSON-like object
|
||||
@ -172,8 +196,12 @@ public abstract class CollectionUtils {
|
||||
*/
|
||||
|
||||
public static String toPercepts(Object obj){
|
||||
if(obj instanceof Object[]){
|
||||
return toPerceptsArray((Object[]) obj);
|
||||
|
||||
}
|
||||
if(obj instanceof JSONArray){
|
||||
return toPerceptsArray((JSONArray) obj);
|
||||
return toPerceptsJSONArray((JSONArray) obj);
|
||||
}
|
||||
else if (obj instanceof JSONObject) {
|
||||
return toPerceptsObject((JSONObject) obj);
|
||||
|
@ -10,8 +10,10 @@ import jason.asSyntax.Literal;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONException;
|
||||
import net.sf.json.JSONObject;
|
||||
import es.upm.dit.gsi.jason.utils.CollectionUtils;
|
||||
import es.upm.dit.gsi.sojason.SOModel;
|
||||
import es.upm.dit.gsi.sojason.services.AsyncWebServiceConnector;
|
||||
|
||||
@ -94,9 +96,10 @@ public class SocketIOConnector implements AsyncWebServiceConnector{
|
||||
@Override
|
||||
public void ack(Object... args) {
|
||||
logger.info("Package ack'ed");
|
||||
Literal lit = Literal.parseLiteral("success(yes)");
|
||||
String str = CollectionUtils.toPercepts(args);
|
||||
Literal lit = Literal.parseLiteral(str);
|
||||
model.setDataInbox(agName, lit);
|
||||
logger.info("DataInbox set");
|
||||
logger.info("DataInbox set to "+str);
|
||||
}
|
||||
}, new JSONObject().put("Sending", "From Jason"));
|
||||
} catch (JSONException e) {
|
||||
|
Loading…
Reference in New Issue
Block a user