package kr.ac.kaist.resl.lilliput.rest;
import java.util.Collection;
import java.util.Iterator;
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.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;
public class PresenceOfPlace extends ServerResource{
@Get("json")
public JsonRepresentation toJson() throws Exception {
try {
String epc = (String)getRequest().getAttributes().get("uid");
JSONArray resultArr =getIdentifiedObjects(epc);
JsonRepresentation representation = new JsonRepresentation(resultArr);
return representation;
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@SuppressWarnings("rawtypes")
public JSONArray getIdentifiedObjects(String epc) throws Exception
{
RemoteProjectManager rpm = RemoteProjectManager.getInstance();
Project p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true);
OWLModel owlModel = (OWLModel)p.getKnowledgeBase();
RDFIndividual indoorInd = owlModel.getRDFIndividual(epc);
if( indoorInd == null )
{
return null;
}
JSONArray identifiedJson = new JSONArray();
OWLObjectProperty isIdentifying = owlModel.getOWLObjectProperty("isIdentifying");
OWLObjectProperty isNear = owlModel.getOWLObjectProperty("isNear");
OWLObjectProperty isOwned = owlModel.getOWLObjectProperty("isOwned");
Collection idenCol = indoorInd.getPropertyValues(isIdentifying);
Iterator idenIter = idenCol.iterator();
while( idenIter.hasNext())
{
JSONObject resultObj = new JSONObject();
RDFIndividual identified = (RDFIndividual)idenIter.next();
resultObj.put("epc", identified.getBrowserText());
JSONArray nearArr = new JSONArray();
Collection nearCol = identified.getPropertyValues(isNear);
Iterator nearIter = nearCol.iterator();
while( nearIter.hasNext())
{
JSONObject nearObj = new JSONObject();
RDFIndividual nearInd = (RDFIndividual)nearIter.next();
nearObj.put("epc", nearInd.getBrowserText());
JSONArray ownerArr = new JSONArray();
Collection ownCol = nearInd.getPropertyValues( isOwned);
Iterator ownIter = ownCol.iterator();
while( ownIter.hasNext())
{
JSONObject ownObj = new JSONObject();
RDFIndividual ownerInd = (RDFIndividual)ownIter.next();
ownObj.put("epc", ownerInd.getBrowserText());
ownerArr.put(ownObj);
}
nearObj.put("owners", ownerArr);
nearArr.put(nearObj);
}
resultObj.put("nears", nearArr);
identifiedJson.put(resultObj);
}
p.dispose();
return identifiedJson;
}
}