package kr.ac.kaist.resl.lilliput.rest;
import java.util.List;
import java.util.Map;
import kr.ac.kaist.resl.lilliput.util.Util;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protege.server.RemoteProjectManager;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.query.QueryResults;
public class SocialRelationships extends ServerResource {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Get("json")
public JsonRepresentation toJson() {
try {
JSONArray returnJson = new JSONArray();
//Save Space to Ontology if it is not saved
RemoteProjectManager rpm = RemoteProjectManager.getInstance();
Project p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true);
OWLModel owlModel = (OWLModel)p.getKnowledgeBase();
String query = "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> SELECT ?s WHERE { ?s rdfs:subPropertyOf iot:IoTSocialRelationship }";
QueryResults queryResults = owlModel.executeSPARQLQuery(query);
List<String> x = queryResults.getVariables();
while( queryResults.hasNext())
{
Map y = queryResults.next();
JSONObject resultJson = new JSONObject();
for(int i = 0 ; i < x.size() ; i++ )
{
String vars = x.get(i);
String results = y.get(vars).toString();
results = Util.individualToBrowserText(results);
resultJson.put(vars, results);
}
returnJson.put(resultJson);
}
JsonRepresentation representation = new JsonRepresentation(returnJson);
p.dispose();
// Returns the XML representation of this document.
return representation;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}