package kr.ac.kaist.resl.lilliput.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.xml.datatype.DatatypeConfigurationException;
import org.fosstrak.epcis.model.Poll;
import org.fosstrak.epcis.model.QueryResults;
import org.fosstrak.epcis.queryclient.QueryControlClient;
import org.fosstrak.epcis.soap.ImplementationExceptionResponse;
import org.fosstrak.epcis.soap.NoSuchNameExceptionResponse;
import org.fosstrak.epcis.soap.QueryParameterExceptionResponse;
import org.fosstrak.epcis.soap.QueryTooComplexExceptionResponse;
import org.fosstrak.epcis.soap.QueryTooLargeExceptionResponse;
import org.fosstrak.epcis.soap.SecurityExceptionResponse;
import org.fosstrak.epcis.soap.ValidationExceptionResponse;
import org.fosstrak.epcis.utils.QueryResultsParser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
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;
public class ObjectEventThread extends Thread{
public String queryURL;
public String epcLocation;
public int period;
public QueryResults results;
public final static String owlURI = "http://143.248.106.26:8880/Lilliput/OWLFile/IoTSocialGraph0531.owl";
public MongoHelper mHelper;
public EPCISHelper eHelper;
public ObjectEventThread(String queryURL, String epcLocation, int period)
{
this.queryURL = queryURL;
this.epcLocation = epcLocation;
this.period = period;
this.mHelper = new MongoHelper();
this.eHelper = new EPCISHelper();
}
@SuppressWarnings("rawtypes")
public void run()
{
try{
while(true)
{
Thread.sleep(period*1000);
//Query to EPCIS repository
QueryControlClient client = new QueryControlClient(queryURL);
//Poll
Poll poll = eHelper.createRecentPoll(epcLocation);
results = client.poll(poll);
//Retrieve Document
JSONHelper jHelper = new JSONHelper();
Document result = QueryResultsParser.queryResultsToDocument(results);
List<JSONObject> returnedEvent = jHelper.getJSONFromEPC(result);
RemoteProjectManager rpm = RemoteProjectManager.getInstance();
Project p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true);
OWLModel owlModel = (OWLModel)p.getKnowledgeBase();
//OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI(owlURI);
List<String> queryTypes = new ArrayList<String>();
//DB Processing Object and IndoorSpace
//�̺�Ʈ�� ������ �׳� ����
if(returnedEvent == null)
{
System.out.println("There is no Events");
continue;
}
//Business location ���
//Save Space to Ontology if it is not saved
OWLNamedClass indoorSpaceClass = owlModel.getOWLNamedClass("IndoorSpace");
OWLNamedClass objectEventClass = owlModel.getOWLNamedClass("ObjectEvent");
//Identify whethere there is this space or not
Collection indoorSpaces = indoorSpaceClass.getInstances(false);
Iterator indoorSpaceIter = indoorSpaces.iterator();
RDFIndividual epcLocationInd = null;
boolean isItSaved = false;
while( indoorSpaceIter.hasNext() )
{
epcLocationInd = (RDFIndividual)indoorSpaceIter.next();
if( epcLocationInd.getBrowserText().equals(epcLocation))
{
isItSaved = true;
break;
}
}
String eventObjectID;
OWLObjectProperty hasObjectEventIn = owlModel.getOWLObjectProperty("hasObjectEventIn");
OWLObjectProperty hasObjectEvent = owlModel.getOWLObjectProperty("hasObjectEvent");
//
//
RDFIndividual spaceEventInd;
if( isItSaved == false )
{
//���� ����ؾ���
//��������� ���
epcLocationInd = indoorSpaceClass.createRDFIndividual(epcLocation);
JSONObject spaceEvent = new JSONObject();
spaceEvent.put("epc", epcLocation);
JSONArray events = new JSONArray();
spaceEvent.put("Events", events);
queryTypes.add("epc");
//epc�� �����ϰ� objectID�� ������
eventObjectID = mHelper.saveToDB("ObjectEvent", spaceEvent, queryTypes);
//spaceEvent�� ��������� objectID�� ������
spaceEventInd = objectEventClass.createRDFIndividual(eventObjectID);
//������
epcLocationInd.setPropertyValue(hasObjectEventIn, spaceEventInd);
}
else
{
//���� ����ϴ°� �ƴ϶�� objecteventind�� ���
System.out.println(epcLocationInd.getPropertyValueCount(hasObjectEventIn));
spaceEventInd = (RDFIndividual)epcLocationInd.getPropertyValue(hasObjectEventIn);
//objectID�� �����´�
String[] objectIDs = spaceEventInd.getBrowserText().split("#");
eventObjectID = objectIDs[1];
}
//���� ������ ��������� ����ϰ� �̺�Ʈ���� ����� ������
//������Ʈ �̺�Ʈ�� ó���Ѵ�
//�������̺�Ʈ���� Ȯ���Ѵ�
//Processing Object
JSONObject epcEvent = returnedEvent.get(0);
//epcEvent���� eventTime�� �ִ�. �̰��� �����Ϲ� �������� �ʴ´�
boolean isLastEvent = mHelper.isLastEventInDB("ObjectEvent", eventObjectID, epcEvent, "Events");
if( isLastEvent == true )
{
System.out.println("It is last event");
continue;
}
//������ �̺�Ʈ�� �ƴ϶�� �����Ѵ�.
//�����ؾ��� ������Ʈ�̺�Ʈ�� �˰� �ִ�. spaceEventInd
mHelper.saveToDBInArray("ObjectEvent", eventObjectID , epcEvent, "Events");
//���峡
//���� ��������ʿ� ���������ߵ�
JSONArray tempEpcs = epcEvent.getJSONArray("epcList");
//�켱 ��������� ��� objectClass�� �ν��Ͻ��� ���� hasObjectEvent�� �ش��ϴ� ���踦 ���� ������
OWLNamedClass objectClass = owlModel.getOWLNamedClass("Object");
Collection objectClasses = objectClass.getInstances(false);
Iterator objectIter = objectClasses.iterator();
while(objectIter.hasNext())
{
RDFIndividual objectInd = (RDFIndividual)objectIter.next();
Collection objectEvents = objectInd.getPropertyValues(hasObjectEvent);
Iterator eventIter = objectEvents.iterator();
while( eventIter.hasNext())
{
RDFIndividual eventRDF = (RDFIndividual)eventIter.next();
objectInd.removePropertyValue(hasObjectEvent, eventRDF);
}
}
//��������
//�� OBJECT EPC���� ����
for( int i = 0 ; i < tempEpcs.length(); i++)
{
JSONObject tempEpc = tempEpcs.getJSONObject(i);
//epc ���ڿ�
String EPCObject = tempEpc.get("epc").toString();
RDFIndividual objectInd = null;
objectClasses = objectClass.getInstances(false);
Iterator objectClassIter = objectClasses.iterator();
isItSaved = false;
while( objectClassIter.hasNext())
{
objectInd = (RDFIndividual)objectClassIter.next();
if( objectInd.getBrowserText().equals(EPCObject))
{
isItSaved = true;
break;
}
}
if( isItSaved == false )
{
//���� ����ؾ���
//��������� ���
objectInd = objectClass.createRDFIndividual(EPCObject);
}
//���� ���������� hasObjectEvent�� �������ش�
//objectInd -> hasObjectEvent -> objectEventInd;
objectInd.setPropertyValue(hasObjectEvent, spaceEventInd);
}
// JenaOWLModel jenaOWL = (JenaOWLModel)owlModel;
// jenaOWL.save(new URI("file:///d:/Eclipses/Lilliput_WorkspaceU/Lilliput/WebContent/OWLFile/IoTSocialGraph0531.owl"), FileUtils.langXMLAbbrev, new ArrayList());
p.dispose();
System.out.println("Object Event is processed");
}
}catch( InterruptedException e )
{
e.printStackTrace();
} catch (DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ImplementationExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (QueryTooComplexExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (QueryTooLargeExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValidationExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchNameExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (QueryParameterExceptionResponse e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}