package kr.ac.kaist.resl.lilliput.rest;
import java.util.Collection;
import java.util.Iterator;
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 com.mongodb.MongoException;
import edu.stanford.smi.protege.exception.OntologyLoadException;
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.OWLObjectProperty;
import edu.stanford.smi.protegex.owl.model.RDFIndividual;
import edu.stanford.smi.protegex.owl.model.query.QueryResults;
public class BetSocialRelationships extends ServerResource {
@SuppressWarnings("rawtypes")
@Get("json")
public JsonRepresentation toJson() {
try {
String sourceEpc = (String)getRequest().getAttributes().get("subject");
String targetEpc = (String)getRequest().getAttributes().get("object");
//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();
RDFIndividual sourceInd = owlModel.getOWLIndividual(sourceEpc);
RDFIndividual destInd = owlModel.getOWLIndividual(targetEpc);
JSONArray allProp = getAvailableSocialRelationshipsAsJSONArray();
JSONArray results = new JSONArray();
JSONObject result;
for(int i = 0 ; i < allProp.length(); i ++ )
{
result = new JSONObject();
JSONObject propJson = (JSONObject)allProp.get(i);
String propStr = propJson.getString("s");
OWLObjectProperty prop = owlModel.getOWLObjectProperty(propStr);
Collection destCollection = sourceInd.getPropertyValues(prop);
Iterator destIter = destCollection.iterator();
while(destIter.hasNext())
{
RDFIndividual destRDF = (RDFIndividual)destIter.next();
if( destRDF.getBrowserText().equals(destInd.getBrowserText()))
{
result.put("SocialRelationship", propStr);
results.put(result);
break;
}
}
}
p.dispose();
JsonRepresentation representation = new JsonRepresentation(results);
return representation;
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//3#: Get all available Relationships
@SuppressWarnings({ "unchecked", "rawtypes" })
public JSONArray getAvailableSocialRelationshipsAsJSONArray()
{
try {
//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();
JSONArray resultsJson = new JSONArray();
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);
}
resultsJson.put(resultJson);
}
p.dispose();
return resultsJson;
//PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#>
// SELECT ?subject ?object
// WHERE { ?subject iot:hasSpaceEvent ?object }
}
catch (OntologyLoadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}