use of org.apache.jena.query.QuerySolution in project legato by DOREMUS-ANR.
the class PropertyHandler method getScore.
public static double getScore(Resource property, Model model) {
/**
************
* Get distinct resources having the property "property"
*************
*/
List<Resource> resources = new ArrayList<Resource>();
String sparqlQueryString = "SELECT DISTINCT ?resource WHERE {" + "?resource <" + property + "> ?object" + "}";
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet queryResults = qexec.execSelect();
while (queryResults.hasNext()) {
QuerySolution qs = queryResults.nextSolution();
resources.add(qs.getResource("?resource"));
}
qexec.close();
/**
************
* Get all DISTINCT objects of the property "property"
*************
*/
List<RDFNode> disObj = new ArrayList<RDFNode>();
String sparqlQueryString2 = "SELECT DISTINCT ?object WHERE {" + "?resource <" + property + "> ?object" + "}";
Query query2 = QueryFactory.create(sparqlQueryString2);
QueryExecution qexec2 = QueryExecutionFactory.create(query2, model);
ResultSet queryResults2 = qexec2.execSelect();
while (queryResults2.hasNext()) {
QuerySolution qs = queryResults2.nextSolution();
disObj.add(qs.get("?object"));
}
qexec2.close();
/**
************
* Get all objects of the property "property"
*************
*/
List<RDFNode> obj = new ArrayList<RDFNode>();
String sparqlQueryString3 = "SELECT ?object WHERE {" + "?resource <" + property + "> ?object" + "}";
Query query3 = QueryFactory.create(sparqlQueryString3);
QueryExecution qexec3 = QueryExecutionFactory.create(query3, model);
ResultSet queryResults3 = qexec3.execSelect();
while (queryResults3.hasNext()) {
QuerySolution qs = queryResults3.nextSolution();
obj.add(qs.get("?object"));
}
qexec3.close();
double redondance = (obj.size() - disObj.size());
if (redondance == 0)
redondance = 1;
return (resources.size() / redondance);
}
use of org.apache.jena.query.QuerySolution in project legato by DOREMUS-ANR.
the class ModelManager method getObjResources.
/**
********
** Get all resources having the value "object" in their path.
*********
*/
public static List<Resource> getObjResources(Model model, RDFNode object) {
LEGATO legato = LEGATO.getInstance();
List<Resource> resources = new ArrayList<Resource>();
for (String className : legato.getClassResources()) {
String sparqlQueryString = "prefix : <urn:ex:>" + "SELECT DISTINCT ?resource WHERE {" + "{?resource a <" + className + "> ;" + "(:|!:)* \"" + object.toString() + "\"}" + "}";
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet queryResults = qexec.execSelect();
while (queryResults.hasNext()) {
QuerySolution qs = queryResults.nextSolution();
resources.add(qs.getResource("?resource"));
}
qexec.close();
}
return resources;
}
use of org.apache.jena.query.QuerySolution in project autoindex by dice-group.
the class SparqlHandler method getResults.
public ArrayList<Entity> getResults(int instances_limit) {
ResultSet results = this.getallinstances(instances_limit);
ArrayList<Entity> entity_list = new ArrayList<Entity>();
while (results.hasNext()) {
QuerySolution qs = results.next();
entity_list.add(new Entity(qs.getResource("type").getURI(), qs.getLiteral("label").getString(), Double.parseDouble(qs.getLiteral("v").getString())));
}
return entity_list;
}
use of org.apache.jena.query.QuerySolution in project autoindex by dice-group.
the class AutoIndexTest method testallproperties.
public void testallproperties() {
seh.setLang("en");
seh.setBaseUri(endpoint);
ResultSet sum = seh.getallproperties();
assertNotNull(sum);
List<QuerySolution> asText = ResultSetFormatter.toList(sum);
System.out.println("Properties Results" + Joiner.on("\n").join(asText));
System.out.println("Total Visible Results = " + asText.size());
Assert.assertTrue(asText.size() > 1);
}
use of org.apache.jena.query.QuerySolution in project rdf2neo by Rothamsted.
the class CyRelationLoadingHandler method accept.
@Override
public void accept(Set<QuerySolution> relRecords) {
this.renameThread("cyRelLoad:");
log.trace("Begin of {} relations", relRecords.size());
Map<String, List<Map<String, Object>>> cyData = new HashMap<>();
RdfDataManager rdfMgr = this.getRdfDataManager();
Neo4jDataManager neoMgr = this.getNeo4jDataManager();
//
for (QuerySolution row : relRecords) {
CyRelation cyRelation = rdfMgr.getCyRelation(row);
rdfMgr.setCyRelationProps(cyRelation, this.relationPropsSparql);
String type = cyRelation.getType();
List<Map<String, Object>> cyRels = cyData.get(type);
if (cyRels == null)
cyData.put(type, cyRels = new LinkedList<>());
Map<String, Object> cyparams = new HashMap<>();
// We have a top map containing basic relation elements (from, to, properties)
cyparams.put("fromIri", String.valueOf(cyRelation.getFromIri()));
cyparams.put("toIri", String.valueOf(cyRelation.getToIri()));
// And then we have an inner map containing the relation properties/attributes
cyparams.put("properties", neoMgr.getCypherProperties(cyRelation));
cyRels.add(cyparams);
}
// OK, ready to call Neo!
//
log.trace("Sending {} relation(s) to Cypher", relRecords.size());
// The relation type ('%2$s') is a constant wrt the underlining Cypher processor, for us it is instead a parameter
// that is instantiated by the loop below.
//
// Nodes are always identified by means of their default label ('%1$s'), which is always created for them.
//
String cypherCreateRel = "UNWIND {relations} AS rel\n" + // The property map always contain the relation IRI
"MATCH ( from:`%1$s`{ iri: rel.fromIri } ), ( to:`%1$s`{ iri: rel.toIri } )\n" + "CREATE (from)-[r:`%2$s`]->(to)\n" + "SET r = rel.properties";
long relsCtr = 0;
String defaultLabel = neoMgr.getDefaultLabel();
for (Entry<String, List<Map<String, Object>>> cyDataE : cyData.entrySet()) {
String type = cyDataE.getKey();
String cyCreateStr = String.format(cypherCreateRel, defaultLabel, type);
List<Map<String, Object>> props = cyDataE.getValue();
neoMgr.runCypher(cyCreateStr, "relations", props);
relsCtr += props.size();
}
log.debug("{} actual relations(s) sent to Cypher", relsCtr);
}
Aggregations