package kr.ac.kaist.resl.lilliput.core; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.GregorianCalendar; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; import kr.ac.kaist.resl.lilliput.util.JSONHelper; import kr.ac.kaist.resl.lilliput.util.MongoHelper; import org.json.JSONException; import org.json.JSONObject; import com.restfb.Connection; import com.restfb.DefaultFacebookClient; import com.restfb.FacebookClient; import com.restfb.Parameter; import com.restfb.types.User; import edu.stanford.smi.protege.model.Project; import edu.stanford.smi.protege.server.RemoteProjectManager; import edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty; 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; /** * Servlet implementation class KBPerformTest */ public class KBPerformTest2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public KBPerformTest2() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String accessToken = (String)request.getParameter("accessToken"); String password = (String)request.getParameter("password"); String result = Registration(accessToken, password, "Facebook"); request.setAttribute("result", result); RequestDispatcher dispatcher = request.getRequestDispatcher("ServiceResult.jsp"); dispatcher.forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } public String Registration(String accessToken, String password, String from) { try { //making facebookClient FacebookClient facebookClient = new DefaultFacebookClient(accessToken); //fetch user User user = facebookClient.fetchObject("me", User.class); //creating json object of user JSONHelper jhelper = new JSONHelper(); JSONObject jsonUser = jhelper.getJSONFromUser(user); DatatypeFactory dataFactory = DatatypeFactory.newInstance(); XMLGregorianCalendar now = dataFactory.newXMLGregorianCalendar(new GregorianCalendar()); jsonUser.put("SyncTime", now.toString()); //creating json to save Human entity String fid = jsonUser.getString("fid"); String em = jsonUser.getString("email"); String gender = jsonUser.getString("gender"); JSONObject Human = new JSONObject(); Human.put("email", em); Human.put("fid", fid); Human.put("password", password); Human.put("from", from); Human.put("gender", gender); List<String> queryTypes = new ArrayList<String>(); //Ontology�� �ҷ��� RemoteProjectManager rpm = RemoteProjectManager.getInstance(); Project p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true); OWLModel owlModel = (OWLModel)p.getKnowledgeBase(); //fid�� �˻��Ǹ� late binded //EPC�� �˻��Ǹ� already registrered //�ƹ��͵� �ƴϸ� ���� ó�� MongoHelper mHelper = new MongoHelper(); //Check whether human is already saved String objectIdOfDB = mHelper.isHumanSavedInDB(fid); if( objectIdOfDB != null ) { System.out.println(fid + " is already saved"); mHelper.close(); return "Error : " + fid + " is already saved"; } //���� epc�� �����´� String vacantEPC = mHelper.getVacantHumanEPCinDB(gender); OWLNamedClass humanClass = owlModel.getOWLNamedClass("Human"); RDFIndividual humanInd = owlModel.getRDFIndividual(vacantEPC); if( humanInd != null ) { System.out.println(" UnexpectedError : Human is not in DB and Human is in ontology"); mHelper.close(); return " Unexpected Error : Human is not in DB and Human is in ontology"; } else { humanInd = humanClass.createRDFIndividual(vacantEPC); String result = mHelper.saveHumanEPCToDB(vacantEPC, Human); if( result == null ) { System.out.println(" Unexpected Error : Human cannot saved in DB"); mHelper.close(); return " Unexpected Error : Human cannot saved in DB" ; } } //������ ���� // humanEPC <-- epc�� ��� ���� // ���� ������ ������ //for getting object id queryTypes.add("email"); //creating json to save Information entity String humanInformID = mHelper.saveToDB("HumanInformation", jsonUser, queryTypes); System.out.println(humanInformID); // �����ϰ� inform ID �� ������ //save to Ontology : Information OWLNamedClass informClass = owlModel.getOWLNamedClass("HumanInformation"); RDFIndividual informInd = owlModel.getRDFIndividual(humanInformID); if( informInd != null ) { System.out.println(" Unexpected Error : Human information is made just before, but it's already existed in ontology "); mHelper.close(); return " Unexpected Error : Human information is made just before, but it's already existed in ontology "; } else { informInd = informClass.createRDFIndividual(humanInformID); //making relationship : Human - Information OWLObjectProperty hasInform = owlModel.getOWLObjectProperty("hasHumanInformation"); humanInd.addPropertyValue(hasInform, informInd); } //���� ���� individual�� ����� ������� ���״�. //���� ģ���� ó���Ѵ� //fetch friends Connection<User> myFriendConnections = facebookClient.fetchConnection("me/friends", User.class, Parameter.with("fields", "about, bio,birthday, education,email,gender, link" )); @SuppressWarnings("unused") List<User> myFriends = myFriendConnections.getData(); System.out.println("my Friends"); OWLObjectProperty hasFriend = owlModel.getOWLObjectProperty("hasFriend"); @SuppressWarnings("unused") OWLObjectProperty hasLover = owlModel.getOWLObjectProperty("hasLover"); //ģ���� DB�� �ִٸ� object ID�� ã�� ģ���� �����Ѵ�. FileWriter fw = new FileWriter("KBSplitNon.txt"); BufferedWriter bw = new BufferedWriter(fw); PrintWriter outFile = new PrintWriter(bw); OWLDatatypeProperty fidd = owlModel.getOWLDatatypeProperty("fid"); OWLDatatypeProperty named = owlModel.getOWLDatatypeProperty("name"); OWLDatatypeProperty linkd = owlModel.getOWLDatatypeProperty("link"); OWLDatatypeProperty SyncTimed = owlModel.getOWLDatatypeProperty("SyncTime"); OWLDatatypeProperty genderd = owlModel.getOWLDatatypeProperty("gender"); OWLDatatypeProperty educationd = owlModel.getOWLDatatypeProperty("education"); OWLDatatypeProperty updatedTimed = owlModel.getOWLDatatypeProperty("updatedTime"); for(int i = 0 ; i < 100 ; i ++ ) { //friend individual create time , friend information connection time, friend information save time, friend connection time System.out.print( i + " : "); JSONObject jsonFriend = jhelper.getJSONFromUser(user); jsonFriend.remove("email"); now = dataFactory.newXMLGregorianCalendar(new GregorianCalendar()); jsonUser.put("SyncTime", now.toString()); String vacantEPCf = mHelper.getVacantHumanEPCinDB("male"); long pretime1 = System.nanoTime(); RDFIndividual friendInd = humanClass.createRDFIndividual(vacantEPCf); long posttime1 = System.nanoTime(); double time1 = posttime1-pretime1; System.out.print(time1/1000000000+"\t"); outFile.print(time1/1000000000+"\t"); @SuppressWarnings("unused") String result = mHelper.saveHumanEPCToDB(vacantEPCf, Human); //creating json to save Information entity //long pretime2 = System.nanoTime(); String friendInformID = null; //long posttime2 = System.nanoTime(); //double time2 = posttime2-pretime2; //System.out.print(time2/1000000000+"\t"); //outFile.print(time2/1000000000+"\t"); RDFIndividual informfInd = informClass.createRDFIndividual(friendInformID); //making relationship : Human - Information OWLObjectProperty hasInform = owlModel.getOWLObjectProperty("hasHumanInformation"); long pretime3 = System.nanoTime(); friendInd.addPropertyValue(hasInform, informfInd); long posttime3 = System.nanoTime(); double time3 = posttime3-pretime3; System.out.print(time3/1000000000+"\t"); outFile.print(time3/1000000000+"\t"); long pretimex = System.nanoTime(); informfInd.addPropertyValue(fidd, jsonUser.getString("fid")); informfInd.addPropertyValue(named, jsonUser.getString("name")); informfInd.addPropertyValue(linkd, jsonUser.getString("link")); informfInd.addPropertyValue(SyncTimed, jsonUser.getString("SyncTime")); informfInd.addPropertyValue(genderd, jsonUser.getString("gender")); informfInd.addPropertyValue(educationd, jsonUser.getJSONArray("education").toString()); informfInd.addPropertyValue(updatedTimed, jsonUser.getString("updatedTime")); long posttimex = System.nanoTime(); double timex = posttimex-pretimex; System.out.print(timex/1000000000+"\t"); outFile.print(timex/1000000000+"\t"); long pretime4 = System.nanoTime(); humanInd.addPropertyValue(hasFriend, friendInd); long posttime4 = System.nanoTime(); double time4 = posttime4-pretime4; System.out.print(time4/1000000000+"\t"); outFile.print(time4/1000000000+"\t"); System.out.println( (time1+timex+time3+time4)/1000000000); outFile.println((time1+timex+time3+time4)/1000000000); } outFile.close(); mHelper.close(); p.dispose(); System.out.println("Human is succesfully registrated"); return fid + " is succesfully registrated"; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return e.toString(); } catch (DatatypeConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); return e.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return e.toString(); } } }