package kr.ac.kaist.resl.lilliput.ac; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; import kr.ac.kaist.resl.lilliput.util.Util; import org.json.JSONArray; 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.query.QueryResults; public class AccessController { public static boolean isAccessible(List<AccessibilityPair> acPair, String sourceEPC, String destEPC) { for(int i = 0 ; i < acPair.size() ; i++ ) { AccessibilityPair act = acPair.get(i); if( act.subject.equals(sourceEPC) && act.object.equals(destEPC)) { return true; } } return false; } public static List<AccessibilityPair> getAccessibilityList(OWLModel owlModel, String sourceEPC) { try { List<AccessibilityPair> acPair = new ArrayList<AccessibilityPair>(); //Save Space to Ontology if it is not saved List<String> queries = new ArrayList<String>(); String[] ss = sourceEPC.split(":"); String sstr = ss[3]; if( sstr.equals("gid")) { queries.add(getHHQuery()); queries.add(getHOQuery()); queries.add(getHPQuery()); }else if( sstr.equals("sgtin") ) { queries.add(getOHQuery()); queries.add(getOOQuery()); queries.add(getOPQuery()); }else if( sstr.equals("sgln") ) { queries.add(getPHQuery()); queries.add(getPOQuery()); queries.add(getPPQuery()); } for(int k = 0 ; k < queries.size() ; k++ ) { String query = queries.get(k); //FileWriter fw = new FileWriter( k + "ac.txt"); //BufferedWriter bw = new BufferedWriter(fw); //PrintWriter outFile = new PrintWriter(bw); QueryResults queryResults = null; //for(int i = 0 ; i < 10000 ; i++ ) //{ // long pretime2 = System.nanoTime(); queryResults = owlModel.executeSPARQLQuery(query); // long posttime2 = System.nanoTime(); // double time2 = posttime2-pretime2; // System.out.print(time2/1000000000+"\t"); // outFile.println(time2/1000000000+"\t"); //} //outFile.close(); @SuppressWarnings("unchecked") List<String> x = queryResults.getVariables(); @SuppressWarnings("unused") JSONArray resultsJson = new JSONArray(); while( queryResults.hasNext()) { @SuppressWarnings("rawtypes") Map y = queryResults.next(); AccessibilityPair ret = new AccessibilityPair(); for(int i = 0 ; i < x.size() ; i++ ) { String vars = x.get(i); String results = y.get(vars).toString(); if( results.contains("Default")) { String[] temp = results.split(" "); String[] temp2 = temp[0].split("\\("); results = temp2[1]; } else { results = Util.individualToBrowserText(results); } if( i == 0 ) { ret.subject = results; } else { ret.object = results; } } acPair.add(ret); } } return acPair; //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; } public static List<AccessibilityPair> getAccessibilityList(String sourceEPC, String destEPC) { try { List<AccessibilityPair> acPair = new ArrayList<AccessibilityPair>(); //Save Space to Ontology if it is not saved String[] ss = sourceEPC.split(":"); String[] ds = destEPC.split(":"); String sstr = ss[3]; String dstr = ds[3]; String query = null; if( sstr.equals("gid") && dstr.equals("gid")) { query = getHHQuery(); }else if( sstr.equals("gid") && dstr.equals("sgtin")) { query = getHOQuery(); }else if( sstr.equals("gid") && dstr.equals("sgln")) { query = getHPQuery(); }else if( sstr.equals("sgtin") && dstr.equals("gid")) { query = getOHQuery(); }else if( sstr.equals("sgtin") && dstr.equals("sgtin")) { query = getOOQuery(); }else if( sstr.equals("sgtin") && dstr.equals("sgln")) { query = getOPQuery(); }else if( sstr.equals("sgln") && dstr.equals("gid")) { query = getPHQuery(); }else if( sstr.equals("sgln") && dstr.equals("sgtin")) { query = getPOQuery(); }else if( sstr.equals("sgln") && dstr.equals("sgln")) { query = getPPQuery(); } RemoteProjectManager rpm = RemoteProjectManager.getInstance(); Project p = rpm.getProject("localhost:5100", "Lilliput", "1234", "IoTSocialGraph", true); OWLModel owlModel = (OWLModel)p.getKnowledgeBase(); QueryResults queryResults = owlModel.executeSPARQLQuery(query); @SuppressWarnings("unchecked") List<String> x = queryResults.getVariables(); @SuppressWarnings("unused") JSONArray resultsJson = new JSONArray(); while( queryResults.hasNext()) { @SuppressWarnings("rawtypes") Map y = queryResults.next(); AccessibilityPair ret = new AccessibilityPair(); for(int i = 0 ; i < x.size() ; i++ ) { String vars = x.get(i); String results = y.get(vars).toString(); results = Util.individualToBrowserText(results); if( i == 0 ) { ret.subject = results; } else { ret.object = results; } } acPair.add(ret); } p.dispose(); return acPair; //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; } public static String getHHQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?human1 ?human2 WHERE { { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . " + " FILTER( ?human1 = ?human2 ) }UNION{ ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Public\"@en . " + " FILTER( ?human1 != ?human2 ) } UNION { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Follower\"@en . ?human1 iot:isFollowing ?human2 . FILTER( ?human1 != ?human2 ) " + " } UNION { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Follower\"@en . ?human1 iot:hasFriend ?human2 . FILTER( ?human1 != ?human2 ) } "+ " UNION { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Friend\"@en . ?human1 iot:hasFriend ?human2 . FILTER( ?human1 != ?human2 ) } }"; } public static String getHOQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?human1 ?object1 WHERE { { ?human1 rdf:type iot:Human . ?object1 rdf:type iot:Object . ?object1 iot:AC \"Public\"@en . } UNION { ?human1 rdf:type iot:Human . ?object1 rdf:type iot:Object . ?human1 iot:hasOwnership ?object1 . } UNION " + "{ ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?object1 rdf:type iot:Object . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?object1 . ?object1 iot:AC \"Follower\"@en . FILTER( ?human1 != ?human2 ) }"+ " UNION { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?object1 rdf:type iot:Object . ?human1 iot:isFollowing ?human2 . ?human2 iot:hasOwnership ?object1 . ?object1 iot:AC \"Follower\"@en . FILTER( ?human1 != ?human2 ) } " + " UNION { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?object1 rdf:type iot:Object . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?object1 . ?object1 iot:AC \"Friend\"@en . FILTER( ?human1 != ?human2 )} }"; } public static String getHPQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?human1 ?place1 WHERE { { ?human1 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?place1 iot:AC \"Public\"@en . } " + " UNION { ?human1 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?human1 iot:hasOwnership ?place1 . } " + " UNION { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?place1 . ?place1 iot:AC \"Follower\"@en . FILTER( ?human1 != ?human2 ) } " + " UNION { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?human1 iot:isFollowing ?human2 . ?human2 iot:hasOwnership ?place1 . ?place1 iot:AC \"Follower\"@en . FILTER( ?human1 != ?human2 ) } " + " UNION { ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?place1 . ?place1 iot:AC \"Friend\"@en . FILTER( ?human1 != ?human2 ) } }"; } public static String getOHQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?object1 ?human2 WHERE { { ?object1 rdf:type iot:Object . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Public\"@en . } " + " UNION { ?object1 rdf:type iot:Object . ?human2 rdf:type iot:Human . ?object1 iot:isOwned ?human2 . } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Follower\"@en . ?object1 iot:isOwned ?human2 . ?human1 iot:hasFriend ?human2 . FILTER( ?human1 != ?human2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Follower\"@en . ?object1 iot:isOwned ?human2 . ?human1 iot:isFollowing ?human2 . FILTER( ?human1 != ?human2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Friend\"@en . ?object1 iot:isOwned ?human2 . ?human1 iot:hasFriend ?human2 . FILTER( ?human1 != ?human2 ) } } "; } public static String getOOQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?object1 ?object2 WHERE { { ?object1 rdf:type iot:Object . ?object2 rdf:type iot:Object . ?object2 iot:AC \"Public\"@en . } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?object2 rdf:type iot:Object . FILTER( ?object1 = ?object2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?object2 rdf:type iot:Object . ?object1 iot:isOwned ?human1 . ?human1 iot:hasOwnership ?object2 . FILTER( ?object1 != ?object2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?object2 rdf:type iot:Object . ?object2 iot:AC \"Friend\"@en . ?object1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?object2 . FILTER( ?object1 != ?object2 && ?human1 != ?human2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?object2 rdf:type iot:Object . ?object2 iot:AC \"Follower\"@en . ?object1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?object2 . FILTER( ?object1 != ?object2 && ?human1 != ?human2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?object2 rdf:type iot:Object . ?object2 iot:AC \"Follower\"@en . ?object1 iot:isOwned ?human1 . ?human1 iot:isFollowing ?human2 . ?human2 iot:hasOwnership ?object2 . FILTER( ?object1 != ?object2 && ?human1 != ?human2 ) }} "; } public static String getOPQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?object1 ?place1 WHERE { { ?object1 rdf:type iot:Object . ?place1 rdf:type iot:IndoorSpace . ?place1 iot:AC \"Public\"@en . } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?object1 iot:isOwned ?human1 . ?human1 iot:hasOwnership ?place1 . } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?place1 iot:AC \"Friend\"@en . ?object1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?place1 . FILTER( ?human1 != ?human2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?place1 iot:AC \"Follower\"@en . ?object1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?place1 . FILTER( ?human1 != ?human2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?place1 iot:AC \"Follower\"@en . ?object1 iot:isOwned ?human1 . ?human1 iot:isFollowing ?human2 . ?human2 iot:hasOwnership ?place1 . FILTER( ?human1 != ?human2 ) }}"; } public static String getPHQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?place1 ?human2 WHERE { { ?place1 rdf:type iot:IndoorSpace . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Public\"@en . } " + " UNION { ?place1 rdf:type iot:IndoorSpace . ?human2 rdf:type iot:Human . ?place1 iot:isOwned ?human2 . } " + " UNION { ?place1 rdf:type iot:IndoorSpace . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Follower\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . FILTER( ?human1 != ?human2 ) } " + " UNION { ?place1 rdf:type iot:IndoorSpace . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Follower\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:isFollowing ?human2 . FILTER( ?human1 != ?human2 ) } " + " UNION { ?place1 rdf:type iot:IndoorSpacet . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?human2 iot:AC \"Friend\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . FILTER( ?human1 != ?human2 ) } } "; } public static String getPOQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?place1 ?object1 WHERE { { ?object1 rdf:type iot:Object . ?place1 rdf:type iot:IndoorSpace . ?object1 iot:AC \"Public\"@en . } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?place1 iot:isOwned ?human1 . ?human1 iot:hasOwnership ?object1 . } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?object1 iot:AC \"Friend\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?object1 . FILTER( ?human1 != ?human2 )} " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?object1 iot:AC \"Follower\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?object1 . FILTER( ?human1 != ?human2 ) } " + " UNION { ?object1 rdf:type iot:Object . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place1 rdf:type iot:IndoorSpace . ?object1 iot:AC \"Follower\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:isFollowing ?human2 . ?human2 iot:hasOwnership ?object1 . FILTER( ?human1 != ?human2 ) } }"; } public static String getPPQuery() { return "PREFIX iot: <http://www.owl-ontologies.com/Ontology1334066780.owl#> Select DISTINCT ?place1 ?place2 WHERE { { ?place1 rdf:type iot:IndoorSpace . ?place2 rdf:type iot:IndoorSpace . ?place2 iot:AC \"Public\"@en . } " + " UNION { ?place1 rdf:type iot:IndoorSpace . ?human1 rdf:type iot:Human . ?place2 rdf:type iot:IndoorSpace . FILTER( ?place1 = ?place2 ) } "+ " UNION { ?place1 rdf:type iot:IndoorSpace . ?human1 rdf:type iot:Human . ?place2 rdf:type iot:IndoorSpace . ?place1 iot:isOwned ?human1 . ?human1 iot:hasOwnership ?place2 . FILTER( ?place1 != ?place2 )} " + " UNION { ?place1 rdf:type iot:IndoorSpace . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place2 rdf:type iot:IndoorSpace . ?place2 iot:AC \"Friend\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?place2 . FILTER( ?place1 != ?place2 && ?human1 != ?human2 )} " + " UNION { ?place1 rdf:type iot:IndoorSpace . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place2 rdf:type iot:IndoorSpace . ?place2 iot:AC \"Follower\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:hasFriend ?human2 . ?human2 iot:hasOwnership ?place2 . FILTER( ?place1 != ?place2 && ?human1 != ?human2 )} " + " UNION { ?place1 rdf:type iot:IndoorSpace . ?human1 rdf:type iot:Human . ?human2 rdf:type iot:Human . ?place2 rdf:type iot:IndoorSpace . ?place2 iot:AC \"Follower\"@en . ?place1 iot:isOwned ?human1 . ?human1 iot:isFollowing ?human2 . ?human2 iot:hasOwnership ?place2 . FILTER( ?place1 != ?place2 && ?human1 != ?human2 )}}"; } }