private String extractSampleResource(int maxOffset){ long startTime = System.currentTimeMillis(); int random = randomGen.nextInt(maxOffset); logger.info("Extracting sample resource (" + random + ")..."); String query = String.format("SELECT DISTINCT ?s WHERE {?s a ?type} LIMIT 1 OFFSET %d", random); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); String resource = rs.next().getResource("s").getURI(); logger.info("...done in " + (System.currentTimeMillis()-startTime) + "ms."); return resource; }
public static void main(String[] args) throws ClassNotFoundException, SQLException, NoSuchAlgorithmException, UnsupportedEncodingException { SparqlEndpoint endpoint = SparqlEndpoint.getEndpointDBpediaLiveAKSW(); String resource = "http://dbpedia.org/resource/Leipzig"; String query = "CONSTRUCT { <"+resource+"> ?p ?o } WHERE { <"+resource+"> ?p ?o }"; System.out.println("query: " + query); ExtractionDBCache h2 = new ExtractionDBCache("cache"); long startTime = System.nanoTime(); Model m = h2.executeConstructQuery(endpoint, query); // for(int i=0; i<1000; i++) { // h2.executeConstructQuery(endpoint, query); // } long runTime = System.nanoTime() - startTime; System.out.println("Answer obtained in " + Helper.prettyPrintNanoSeconds(runTime)); System.out.println(ExtractionDBCache.toNTriple(m)); }
public SPARQLEndpointMetrics(SparqlEndpoint endpoint, ExtractionDBCache cache) { this.endpoint = endpoint; this.cache = cache; cache.setFreshnessInMilliseconds(Long.MAX_VALUE);//31536000000l); cache.setMaxExecutionTimeInSeconds(300); this.reasoner = new SPARQLReasoner(new SparqlEndpointKS(endpoint), cache); }
byte[] md5 = md5(query); PreparedStatement ps=conn.prepareStatement("SELECT * FROM QUERY_CACHE WHERE QUERYHASH=? LIMIT 1"); ps.setBytes(1, md5);
public Cache(String cacheDir, boolean useDatabase) { this.cacheDir = cacheDir + File.separator; this.useDatabase = useDatabase; if (!new File(cacheDir).exists()) { Files.mkdir(cacheDir); logger.info("Created directory: " + cacheDir + "."); } if(this.useDatabase) { h2 = new ExtractionDBCache(cacheDir); } }
public Model executeConstructQuery(SparqlEndpoint endpoint, String query) throws SQLException, UnsupportedEncodingException { return executeConstructQuery(endpoint, query, maxExecutionTimeInSeconds); }
public Model executeConstructQuery(SparqlEndpoint endpoint, String query, int maxExecutionTimeInSeconds) throws SQLException, UnsupportedEncodingException { byte[] md5 = md5(query);
public static void main(String[] args) throws Exception { Logger.getLogger(SPARQLEndpointMetrics.class).setLevel(Level.DEBUG); SparqlEndpoint endpoint = new SparqlEndpoint(new URL("http://[2001:638:902:2010:0:168:35:138]/sparql"), "http://boa.dbpedia.org"); ExtractionDBCache cache = new ExtractionDBCache("/opt/tbsl/dbpedia_pmi_cache"); String NS = "http://dbpedia.org/ontology/"; String NS_Res = "http://dbpedia.org/resource/";
public static void getPopularity(SparqlEndpoint endpoint, ExtractionDBCache cache, String type, String resource) { String query = String .format("SELECT ?p COUNT(?x) WHERE {?x a <%s>. <%s> ?p ?x.} GROUP BY ?p", type, resource); System.out.println(query); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache .executeSelectQuery(endpoint, query)); while (rs.hasNext()) { System.out.println(rs.next()); } query = String .format("SELECT ?p COUNT(?x) WHERE {?x a <%s>. ?x ?p <%s>.} GROUP BY ?p", type, resource); rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery( endpoint, query)); while (rs.hasNext()) { System.out.println(rs.next()); } }
public SPARQLEndpointMetrics(Knowledgebase knowledgebase, ExtractionDBCache cache) { this.endpoint = ((RemoteKnowledgebase)knowledgebase).getEndpoint(); this.cache = cache; cache.setFreshnessInMilliseconds(Long.MAX_VALUE);//31536000000l); cache.setMaxExecutionTimeInSeconds(300); this.reasoner = new SPARQLReasoner(new SparqlEndpointKS(endpoint), cache); }
private Set<String> extractSampleResourcesChunked(int size){ logger.info("Extracting " + sampleSize + " sample resources..."); long startTime = System.currentTimeMillis(); Set<String> resources = new HashSet<>(); String query = "SELECT COUNT(DISTINCT ?s) WHERE {?s a ?type}"; ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int max = rs.next().getLiteral(rs.getResultVars().get(0)).getInt(); for(int i = 0; i < nrOfChunks; i++){ int offset = (int)(Math.random() * max); offset = Math.min(offset, offset-(size/nrOfChunks));offset = 236242; query = String.format("SELECT DISTINCT ?s WHERE {?s a ?type} LIMIT %d OFFSET %d", (size/nrOfChunks), offset); logger.info(query); rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); while(rs.hasNext()){ resources.add(rs.next().getResource("s").getURI()); } } logger.info("...done in " + (System.currentTimeMillis()-startTime) + "ms."); return resources; }
/** * Returns the number of triples where the object belongs to the given class. * @param cls * @return */ public int getOccurencesInObjectPosition(NamedClass cls){ log.trace(String.format("Computing number of occurences in object position for %s", cls.getName())); String query = String.format("SELECT (COUNT(?s) AS ?cnt) WHERE {?o a <%s>. ?s ?p ?o.}", cls.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int classOccurenceCnt = rs.next().getLiteral("cnt").getInt(); return classOccurenceCnt; }
/** * Returns the number triples with the given property as predicate. * @param prop * @return */ public int getOccurences(Property prop){ log.trace(String.format("Computing number of occurences as predicate for %s", prop.getName())); String query = String.format("SELECT (COUNT(*) AS ?cnt) WHERE {?s <%s> ?o}", prop.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int propOccurenceCnt = rs.next().getLiteral("cnt").getInt(); return propOccurenceCnt; }
/** * Returns the number of triples where the subject belongs to the given class. * @param cls * @return */ public int getOccurencesInSubjectPosition(NamedClass cls){ log.trace(String.format("Computing number of occurences in subject position for %s", cls.getName())); String query = String.format("SELECT (COUNT(?s) AS ?cnt) WHERE {?s a <%s>. ?s ?p ?o.}", cls.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int classOccurenceCnt = rs.next().getLiteral("cnt").getInt(); return classOccurenceCnt; }
/** * Returns the number of triples where the given individual is in subject position(out-going links). * @param cls * @return */ public int getOccurencesInSubjectPosition(Individual ind){ log.trace(String.format("Computing number of occurences in subject position for %s", ind.getName())); String query = String.format("SELECT (COUNT(*) AS ?cnt) WHERE {<%s> ?p ?o.}", ind.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int classOccurenceCnt = rs.next().getLiteral("cnt").getInt(); return classOccurenceCnt; }
/** * Returns the number of triples where the given individual is in object position(in-going links). * @param cls * @return */ public int getOccurencesInObjectPosition(Individual ind){ log.trace(String.format("Computing number of occurences in object position for %s", ind.getName())); String query = String.format("SELECT (COUNT(*) AS ?cnt) WHERE {?s ?p <%s>.}", ind.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int classOccurenceCnt = rs.next().getLiteral("cnt").getInt(); return classOccurenceCnt; }
/** * Returns the number of triples where the subject or object belongs to the given class. * (This is not the same as computing the number of instances of the given class {@link SPARQLEndpointMetrics#getPopularity(NamedClass)}) * @param cls * @return */ public int getOccurences(NamedClass cls){ log.trace(String.format("Computing number of occurences in subject or object position for %s", cls.getName())); String query = String.format("SELECT (COUNT(?s) AS ?cnt) WHERE {?s a <%s>.{?s ?p1 ?o1.} UNION {?o2 ?p2 ?s} }", cls.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int classOccurenceCnt = rs.next().getLiteral("cnt").getInt(); return classOccurenceCnt; }
public void checkIrreflexivityViolation(OWLOntology ontology){ Set<String> properties = new TreeSet<>(); for(OWLAxiom ax : ontology.getAxioms(AxiomType.IRREFLEXIVE_OBJECT_PROPERTY)){ OWLObjectProperty prop = ((OWLIrreflexiveObjectPropertyAxiom)ax).getProperty().asOWLObjectProperty(); properties.add(prop.toStringID()); } for(String prop : properties){ String query = "SELECT * WHERE {?s <%s> ?s.} LIMIT 1".replaceAll("%s", prop); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); while(rs.hasNext()){ QuerySolution qs = rs.next(); irreflexiveLogger.info("********************************************************"); irreflexiveLogger.info(prop); irreflexiveLogger.info(qs.get("s")); } } }
/** * Returns the number of instances of the given class. * @param cls * @return */ public int getPopularity(NamedClass cls){ String query = String.format("SELECT (COUNT(?s) AS ?cnt) WHERE {?s a <%s>.{?s ?p1 ?o1.} UNION {?o2 ?p2 ?s} }", cls.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int classOccurenceCnt = rs.next().getLiteral("cnt").getInt(); return classOccurenceCnt; }
public void checkAsymmetryViolation(OWLOntology ontology){ Set<String> properties = new TreeSet<>(); for(OWLAxiom ax : ontology.getAxioms(AxiomType.ASYMMETRIC_OBJECT_PROPERTY)){ OWLObjectProperty prop = ((OWLAsymmetricObjectPropertyAxiom)ax).getProperty().asOWLObjectProperty(); properties.add(prop.toStringID()); } for(String prop : properties){ String query = "SELECT * WHERE {?s <%s> ?o.?o <%s> ?s.FILTER(?s != ?o)} LIMIT 1".replaceAll("%s", prop); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); while(rs.hasNext()){ QuerySolution qs = rs.next(); asymmetricLogger.info("********************************************************"); asymmetricLogger.info(prop); asymmetricLogger.info(qs.get("s") + "<-->" + qs.get("o")); } } }