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 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.OWLNamedClass;
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 RelatedEPCs extends ServerResource{
@SuppressWarnings({ "unused", "rawtypes" })
@Get("json")
public JsonRepresentation toJson() {
try {
try {
String epc = (String)getRequest().getAttributes().get("uid");
String type = null;
String ac = "pub";
//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();
OWLNamedClass humanClass = owlModel.getOWLNamedClass("Human");
OWLNamedClass objectClass = owlModel.getOWLNamedClass("Object");
OWLNamedClass indoorSpaceClass = owlModel.getOWLNamedClass("IndoorSpace");
RDFIndividual humanInd;
RDFIndividual objectInd;
RDFIndividual spaceInd;
String[] epcSplit = epc.split("\\.");
JSONArray resultArray = new JSONArray();
JSONObject resultObject;
JSONArray allProp = getAvailableSocialRelationshipsAsJSONArray();
if(epcSplit[1].equals("1"))
{
type = "Human";
humanInd = owlModel.getOWLIndividual(epc);
if( humanInd == null )
{
return null;
}
for(int i = 0 ; i < allProp.length() ; i ++ )
{
JSONObject propJson = (JSONObject)allProp.get(i);
String propStr = propJson.getString("s");
OWLObjectProperty prop = owlModel.getOWLObjectProperty(propStr);
Collection destCollection = humanInd.getPropertyValues(prop);
Iterator destIter = destCollection.iterator();
while(destIter.hasNext())
{
resultObject = new JSONObject();
RDFIndividual destInd = (RDFIndividual)destIter.next();
resultObject.put("epc",Util.individualToBrowserText(destInd.getBrowserText()));
resultArray.put(resultObject);
}
}
}
else if(epcSplit[1].equals("3"))
{
type = "Object";
objectInd = owlModel.getOWLIndividual(epc);
if( objectInd == null )
{
return null;
}
for(int i = 0 ; i < allProp.length() ; i ++ )
{
JSONObject propJson = (JSONObject)allProp.get(i);
OWLObjectProperty prop = owlModel.getOWLObjectProperty(propJson.getString("s"));
Collection destCollection = objectInd.getPropertyValues(prop);
Iterator destIter = destCollection.iterator();
while(destIter.hasNext())
{
resultObject = new JSONObject();
RDFIndividual destInd = (RDFIndividual)destIter.next();
resultObject.put("epc",Util.individualToBrowserText(destInd.getBrowserText()));
resultArray.put(resultObject);
}
}
}
else if(epcSplit[1].equals("2"))
{
type = "IndoorSpace";
spaceInd = owlModel.getOWLIndividual(epc);
if( spaceInd == null )
{
return null;
}
for(int i = 0 ; i < allProp.length() ; i ++ )
{
JSONObject propJson = (JSONObject)allProp.get(i);
OWLObjectProperty prop = owlModel.getOWLObjectProperty(propJson.getString("s"));
Collection destCollection = spaceInd.getPropertyValues(prop);
Iterator destIter = destCollection.iterator();
while(destIter.hasNext())
{
resultObject = new JSONObject();
RDFIndividual destInd = (RDFIndividual)destIter.next();
resultObject.put("epc",Util.individualToBrowserText(destInd.getBrowserText()));
resultArray.put(resultObject);
}
}
}
p.dispose();
JsonRepresentation representation = new JsonRepresentation(resultArray);
return representation;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
catch (Exception 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;
}
}