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