@Override public Integer getValInteger(Ident fieldID, Integer defaultVal) { Literal lit = getLiteralVal(fieldID, false); if (lit != null) { return lit.getInt(); } else { return defaultVal; } }
public int getInt() { return getLiteral().getInt(); }
@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; }
@Override public Integer call() throws Exception { int size = qe.execSelect().next().get("count").asLiteral().getInt(); System.out.println(size); return size; } });
/** * 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; }
/** * Given a query result from a SPARQL query, obtain the number value at the * given variable * * @param resultRow the result from a SPARQL query * @param variableName the name of the variable to obtain * @return the Integer value, or null otherwise */ public static Integer getIntegerValue(QuerySolution resultRow, String variableName) { if (resultRow != null) { Resource res = resultRow.getResource(variableName); if (res != null && res.isLiteral()) { Literal val = res.asLiteral(); if (val != null) { return Integer.valueOf(val.getInt()); } } } return null; }
/** * 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; }
private int loadPopularity(String uri, EntityType entityType){ String query; if(entityType == EntityType.CLASS){ query = String.format("SELECT COUNT(?s) WHERE {?s a <%s>}", uri); } else if(entityType == EntityType.PROPERTY){ query = String.format("SELECT COUNT(*) WHERE {?s <%s> ?o}", uri); } else { query = String.format("SELECT COUNT(*) WHERE {?s ?p <%s>}", uri); } int pop = 0; ResultSet rs = queryable.query(query); QuerySolution qs; String projectionVar; while(rs.hasNext()){ qs = rs.next(); projectionVar = qs.varNames().next(); pop = qs.get(projectionVar).asLiteral().getInt(); } return pop; }
/** * Helper method - extracts the value of an integer configuration * predicate. * @param predicate the predicate to be tested * @param configuration the configuration node * @return null if there is no such configuration parameter otherwise the value as an integer */ public static Integer getIntegerPredicate(Property predicate, Resource configuration) { StmtIterator i = configuration.listProperties(predicate); if (i.hasNext()) { RDFNode lit = i.nextStatement().getObject(); if (lit instanceof Literal) { return new Integer(((Literal)lit).getInt()); } } return null; }
/** * 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; }
@Override public Integer call() throws Exception{ String query = "SELECT DISTINCT (count(?s) AS ?count) { { ?s ?p ?o . } UNION { GRAPH ?g { ?s ?p ?o .} } }"; QueryEngineHTTP qe = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(sparqlEndPoint,query); int size = qe.execSelect().next().get("count").asLiteral().getInt(); return size; } });
public static Map<String, Integer> getNumberOfDimensionsInCategory(){ Map<String, Integer> dimensionPerCategory = new HashMap<String, Integer>(); Model m = InternalModelConf.getFlatModel(); String whereClause = "?categoryURI a " + SPARQLHelper.toSPARQL(DAQ.Category) + " . ?categoryURI ?hasDimensionProperty ?dimensionURI . " + "?hasMetricProperty " + SPARQLHelper.toSPARQL(RDFS.subPropertyOf) + SPARQLHelper.toSPARQL(DAQ.hasDimension) + " ."; String variables = "?categoryURI COUNT(?dimensionURI) as ?count"; String query = SPARQLHelper.SELECT_STATEMENT.replace("[variables]", variables).replace("[whereClauses]", whereClause); Query qry = QueryFactory.create(query); QueryExecution qe = QueryExecutionFactory.create(qry, m); ResultSet rs = qe.execSelect(); while (rs.hasNext()){ QuerySolution qs = rs.next(); String cat = qs.get("categoryURI").asResource().getURI(); Integer count = qs.get("count").asLiteral().getInt(); dimensionPerCategory.put(cat, count); } return dimensionPerCategory; }