@Override public void consume(ResultSet rs) { QuerySolution qs = rs.next(); Literal countLiteral = qs.getLiteral("count"); this.count = countLiteral.getInt(); }
/** * 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 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 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; }
/** * 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; }
/** * Returns the number of triples with the given property as predicate and where the subject belongs to the given class. * @param cls * @return */ public int getOccurencesSubjectPredicate(NamedClass cls, Property prop){ log.trace(String.format("Computing number of occurences as subject and predicate for [%s, %s]", cls.getName(), prop.getName())); String query = String.format("SELECT (COUNT(*) AS ?cnt) WHERE {?s a <%s>. ?s <%s> ?o}", cls.getName(), prop.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int cnt = rs.next().getLiteral("cnt").getInt(); return cnt; }
/** * Returns the number of triples with the given property as predicate and where the object belongs to the given class. * @param cls * @return */ public int getOccurencesPredicateObject(Property prop, NamedClass cls){ log.trace(String.format("Computing number of occurences as predicate and object for [%s, %s]", prop.getName(), cls.getName())); String query = String.format("SELECT (COUNT(*) AS ?cnt) WHERE {?o a <%s>. ?s <%s> ?o}", cls.getName(), prop.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int cnt = rs.next().getLiteral("cnt").getInt(); return cnt; }
/** * Returns the number of triples with the first given class as subject and the second given class as object. * @param cls * @return */ public int getOccurencesSubjectObject(NamedClass subject, NamedClass object){ log.trace(String.format("Computing number of occurences as subject and object for [%s, %s]", subject.getName(), object.getName())); String query = String.format("SELECT (COUNT(*) AS ?cnt) WHERE {?s a <%s>. ?s ?p ?o. ?o a <%s>}", subject.getName(), object.getName()); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); int cnt = rs.next().getLiteral("cnt").getInt(); return cnt; }
public Map<ObjectProperty, Integer> getMostFrequentProperties(NamedClass cls1, NamedClass cls2){ Map<ObjectProperty, Integer> prop2Cnt = new HashMap<ObjectProperty, Integer>(); String query = String.format("SELECT ?p (COUNT(*) AS ?cnt) WHERE {?x1 a <%s>. ?x2 a <%s>. ?x1 ?p ?x2} GROUP BY ?p", cls1, cls2); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery(endpoint, query)); QuerySolution qs; while(rs.hasNext()){ qs = rs.next(); ObjectProperty p = new ObjectProperty(qs.getResource("p").getURI()); int cnt = qs.getLiteral("cnt").getInt(); prop2Cnt.put(p, cnt); } return prop2Cnt; }
private String convertRSToString (QuerySolution soln, String colName) { if (soln.getLiteral(colName).getDatatype() == null) { return soln.getLiteral(colName).getString(); } else { String colType = soln.getLiteral(colName).getDatatype().getURI(); if (colType.equals(XSDDatatype.XSDdecimal.getURI())) { return ConverterUtil.convertToString(ConverterUtil.convertToDecimal(soln .getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDdouble.getURI())) { return ConverterUtil.convertToString(ConverterUtil .convertToDouble(soln.getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDfloat.getURI())) { return ConverterUtil.convertToString(ConverterUtil .convertToFloat(soln.getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDstring.getURI())) { return soln.getLiteral(colName).getString(); } else if (colType.equals(XSDDatatype.XSDboolean.getURI())) { return ConverterUtil .convertToString(ConverterUtil.convertToBoolean(soln .getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDlong.getURI())) { return ConverterUtil.convertToString(ConverterUtil .convertToLong(soln.getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDdate.getURI())) { return ConverterUtil.convertToString(ConverterUtil .convertToDate(soln.getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDdateTime.getURI())) { return ConverterUtil .convertToString(ConverterUtil.convertToDateTime(soln .getLiteral(colName).getString()));
private String convertRSToString (QuerySolution soln, String colName) { if (soln.getLiteral(colName).getDatatype() == null) { return soln.getLiteral(colName).getString(); } else { String colType = soln.getLiteral(colName).getDatatype().getURI(); if (colType.equals(XSDDatatype.XSDdecimal.getURI())) { return ConverterUtil.convertToString(ConverterUtil.convertToDecimal(soln .getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDdouble.getURI())) { return ConverterUtil.convertToString(ConverterUtil .convertToDouble(soln.getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDfloat.getURI())) { return ConverterUtil.convertToString(ConverterUtil .convertToFloat(soln.getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDstring.getURI())) { return soln.getLiteral(colName).getString(); } else if (colType.equals(XSDDatatype.XSDboolean.getURI())) { return ConverterUtil .convertToString(ConverterUtil.convertToBoolean(soln .getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDlong.getURI())) { return ConverterUtil.convertToString(ConverterUtil .convertToLong(soln.getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDdate.getURI())) { return ConverterUtil.convertToString(ConverterUtil .convertToDate(soln.getLiteral(colName).getString())); } else if (colType.equals(XSDDatatype.XSDdateTime.getURI())) { return ConverterUtil .convertToString(ConverterUtil.convertToDateTime(soln .getLiteral(colName).getString()));
public static SymPropertyDirection getDirection(SparqlEndpoint endpoint, ExtractionDBCache cache, String typeURI, String propertyURI) { String query = String.format( "SELECT (COUNT(?x) AS ?cnt) WHERE {?x a <%s>. ?x <%s> ?o.}", typeURI, propertyURI); ResultSet rs = SparqlQuery.convertJSONtoResultSet(cache .executeSelectQuery(endpoint, query)); int classLeftCnt = 0; while (rs.hasNext()) { classLeftCnt = rs.next().getLiteral("cnt").getInt(); } query = String.format( "SELECT (COUNT(?x) AS ?cnt) WHERE {?x a <%s>. ?o <%s> ?x.}", typeURI, propertyURI); rs = SparqlQuery.convertJSONtoResultSet(cache.executeSelectQuery( endpoint, query)); int classRightCnt = 0; while (rs.hasNext()) { classRightCnt = rs.next().getLiteral("cnt").getInt(); } if (classLeftCnt > classRightCnt) { return SymPropertyDirection.VAR_LEFT; } else if (classRightCnt > classLeftCnt) { return SymPropertyDirection.VAR_RIGHT; } else { return SymPropertyDirection.UNKNOWN; } } }
public void toInteractions(ResultSet results) throws StudyImporterException { final InteractionListener listener = new InteractionListenerImpl(nodeFactory, getGeoNamesService(), getLogger()); while (results.hasNext()) { QuerySolution next = results.next(); Iterator<String> nameIter = next.varNames(); Map<String, String> props = new TreeMap<>(); while (nameIter.hasNext()) { String key = nameIter.next(); RDFNode rdfNode = next.get(key); if (rdfNode.isURIResource()) { props.put(key, next.getResource(key).getURI()); } else { props.put(key, next.getLiteral(key).getString()); } } props.put(StudyImporterForTSV.STUDY_SOURCE_CITATION, getDataset().getCitation()); listener.newLink(props); } }
String name = soln.getLiteral("name").getLexicalForm() ; String value = soln.getLiteral("value").getLexicalForm() ; // Works for numbers as well! name = MappingRegistry.mapPrefixName(name) ; Symbol symbol = Symbol.create(name) ;
numRes = soln.getLiteral("count").getInt();
private static List<Pair<String, String>> storesByQuery(String fn) { Model model = FileManager.get().loadModel(fn) ; List<Pair<String, String>> data = new ArrayList<Pair<String, String>>(); Query query = QueryFactory.create(queryString) ; QueryExecution qExec = QueryExecutionFactory.create(query, model) ; try { ResultSet rs = qExec.execSelect() ; for ( ; rs.hasNext() ; ) { QuerySolution qs = rs.nextSolution() ; String label = qs.getLiteral("label").getLexicalForm() ; String desc = qs.getResource("desc").getURI() ; data.add(new Pair<String, String>(label, desc)) ; } } finally { qExec.close() ; } return data ; } }
@Test public void special4() { Dataset ds = create() ; load1(ds.getDefaultModel()) ; load2(ds.getNamedModel("http://example/graph1")) ; load3(ds.getNamedModel("http://example/graph2")) ; Model m = ModelFactory.createDefaultModel() ; load2(m) ; load3(m) ; TDB.sync(ds) ; String qs = "PREFIX : <"+baseNS+"> SELECT (COUNT(?x) as ?c) WHERE { ?x (:p1|:p2) 'x1' }" ; Query q = QueryFactory.create(qs, Syntax.syntaxARQ) ; QueryExecution qExec = QueryExecutionFactory.create(q, ds) ; qExec.getContext().set(TDB.symUnionDefaultGraph, true) ; long c1 = qExec.execSelect().next().getLiteral("c").getLong() ; qExec.close() ; qExec = QueryExecutionFactory.create(q, m) ; long c2 = qExec.execSelect().next().getLiteral("c").getLong() ; assertEquals(c1, c2) ; qExec.close() ; }