package kr.ac.kaist.resl.lilliput.rest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import kr.ac.kaist.resl.lilliput.util.DisappearThread;
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 PresenceReasoning extends ServerResource {
@Get("json")
public JsonRepresentation toJson() throws Exception {
try {
String epc = (String)getRequest().getAttributes().get("uid");
JSONObject message = new JSONObject();
message.put("message", "success");
PresenceReasoningThread presenceReasoningThread = new PresenceReasoningThread(epc,10);
presenceReasoningThread.start();
JsonRepresentation representation = new JsonRepresentation(message);
return representation;
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject message = new JSONObject();
message.put("message", "failed");
JsonRepresentation representation = new JsonRepresentation(message);
return representation;
}
}
class PresenceReasoningThread extends Thread
{
public int period;
public String EPCLocation;
public RemoteProjectManager rpm;
public Project p;
public OWLModel owlModel;
PresenceReasoningThread(String EPCLocation, int period ){
this.period = period;
this.EPCLocation = EPCLocation;
rpm = RemoteProjectManager.getInstance();
p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true);
owlModel = (OWLModel)p.getKnowledgeBase();
}
@SuppressWarnings("rawtypes")
public void run()
{
try {
while(true)
{
Thread.sleep(period*1000);
//�̰� period thread�� ��������
RDFIndividual indoorInd = owlModel.getRDFIndividual(EPCLocation);
if( indoorInd == null )
{
continue;
}
OWLObjectProperty isIdentifying = owlModel.getOWLObjectProperty("isIdentifying");
OWLObjectProperty isNear = owlModel.getOWLObjectProperty("isNear");
OWLObjectProperty isOwned = owlModel.getOWLObjectProperty("isOwned");
OWLObjectProperty isIdentifyingHuman = owlModel.getOWLObjectProperty("isIdentifyingHuman");
Collection idenCol = indoorInd.getPropertyValues(isIdentifying);
Iterator idenIter = idenCol.iterator();
while( idenIter.hasNext())
{
RDFIndividual identified = (RDFIndividual)idenIter.next();
Collection nearCol = identified.getPropertyValues(isNear);
Iterator nearIter = nearCol.iterator();
while( nearIter.hasNext())
{
RDFIndividual nearInd = (RDFIndividual)nearIter.next();
Collection ownCol = nearInd.getPropertyValues( isOwned);
Iterator ownIter = ownCol.iterator();
while( ownIter.hasNext())
{
RDFIndividual ownerInd = (RDFIndividual)ownIter.next();
//�ᱹ spaceEPC�� ownerInd ������ isIdentifyingHuman ���谡 ����� ��
//���踦 �����
indoorInd.addPropertyValue(isIdentifyingHuman, ownerInd);
//������� �����带 �����
List<String> owners = new ArrayList<String>();
owners.add(ownerInd.getBrowserText());
DisappearThread disappearThread2 = new DisappearThread(EPCLocation, owners,"isIdentifyingHuman",20);
disappearThread2.start();
}
}
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//p.dispose();
p.dispose();
e.printStackTrace();
}
}
}