use of org.apache.jena.query.Query in project webofneeds by researchstudio-sat.
the class DynamicDatasetToModelBySparqlFunction method apply.
@Override
public Model apply(Dataset dataset) {
Query query = QueryFactory.create(sparql);
QueryExecution qexec = QueryExecutionFactory.create(query, dataset, initialBinding);
return qexec.execConstruct();
}
use of org.apache.jena.query.Query in project webofneeds by researchstudio-sat.
the class SparqlSelectFunction method apply.
@Override
public List<T> apply(Dataset dataset) {
boolean existingTransaction = dataset.isInTransaction();
if (!existingTransaction) {
dataset.begin(ReadWrite.READ);
}
Dataset result = DatasetFactory.createGeneral();
result.begin(ReadWrite.WRITE);
try {
Query theQuery = this.getQuery();
if (this.limit != null) {
theQuery.setLimit(this.limit);
}
if (this.offset != null) {
theQuery.setOffset(this.offset);
}
if (this.orderBy != null) {
for (SortCondition sortCondition : this.orderBy) {
theQuery.addOrderBy(sortCondition);
}
}
if (this.havingCondiditions != null) {
for (Expr havingCondition : this.havingCondiditions) {
theQuery.addHavingCondition(havingCondition);
}
}
QuerySolution binding = this.initialBinding;
if (binding == null) {
binding = new QuerySolutionMap();
}
List<T> ret = new ArrayList<>();
try (QueryExecution queryExecution = QueryExecutionFactory.create(theQuery, dataset, binding)) {
ResultSet resultSet = queryExecution.execSelect();
while (resultSet.hasNext()) {
ret.add(this.resultGenerator.apply(resultSet.next()));
}
}
return ret;
} finally {
if (!existingTransaction) {
dataset.end();
}
result.commit();
}
}
use of org.apache.jena.query.Query in project loinc2hpo by monarch-initiative.
the class SparqlQueryTest method testQueryWithOneKey.
@Test
public void testQueryWithOneKey() {
String key = "Testosterone";
String looseQueryString = SparqlQuery.buildLooseQueryWithSingleKey(key);
String standardQueryString = SparqlQuery.buildStandardQueryWithSingleKey(key);
// System.out.println("loose query:\n" + looseQueryString);
// System.out.println("\n\nstandard query:\n" + standardQueryString);
Query looseQuery = QueryFactory.create(looseQueryString);
Query standardQuery = QueryFactory.create(standardQueryString);
assertNotNull(model);
List<HPO_Class_Found> results_loose = SparqlQuery.query(looseQuery, model, null);
List<HPO_Class_Found> results_standard = SparqlQuery.query(standardQuery, model, null);
/**
* System.out.println(results_loose.size() + " HPO terms are found!");
* for(HPO_Class_Found hpo : results_loose) {
* System.out.println(hpo.getLabel() + "\t" + hpo.getId() + "\n" + hpo.getDefinition());
* }
*/
// interesting that this program identifies 16 classes;
assertEquals(14, results_loose.size());
// while the same query finds 14 in command line
// reason: command line uses hp.owl; this program builds a model from hp.owl(?)
// reason: more likely has something to do with what kind of model is generated from hp.owl
// System.out.println(results_standard.size()+ " HPO terms are found!");
assertEquals(9, results_standard.size());
}
use of org.apache.jena.query.Query in project legato by DOREMUS-ANR.
the class ModelManager method rewrite.
/**
********
** Place all Literals (in resources CBD) to a distance = 1
*********
*/
public static Model rewrite(Model model, boolean ok) throws IOException {
LEGATO legato = LEGATO.getInstance();
Model finalModel = ModelFactory.createDefaultModel();
model.listSubjects().toSet().forEach((resource) -> {
// Parse all resources
if (// If the current resource belongs to a given "type"
legato.hasType(resource) == true) {
Model m = CBDBuilder.getCBD(model, resource);
if (ok == true) {
m.add(CBDBuilder.getCBDDirectPredecessors(model, resource));
m.add(CBDBuilder.getCBDDirectSuccessors(model, resource));
}
try {
m.add(ModelManager.parseCBD(m));
} catch (IOException e1) {
e1.printStackTrace();
}
m.listStatements().toSet().forEach((stmt) -> {
Resource sub = stmt.getSubject();
Property prop = stmt.getPredicate();
RDFNode object = stmt.getObject();
if (// Parse all literals
object.isLiteral() == true) {
// A filter which accepts statements whose predicate matches one of a collection of predicates held by the filter object.
Path path = OntTools.findShortestPath(m, resource, object, Filter.any);
if (!(path == null)) {
// Get the successive properties from the path
List<Property> properties = getPropFromPath(path);
if (legato.getPropList().existProperty(properties) == false) {
int indice = legato.getPropList().size();
finalModel.createResource(resource.toString()).addProperty(finalModel.createProperty("http://model.org/property" + indice), object);
try {
legato.addToPropList("http://model.org/property" + indice, properties);
} catch (IOException e) {
}
} else {
finalModel.createResource(resource.toString()).addProperty(finalModel.createProperty(legato.getPropList().getPropertyName(properties)), object);
}
} else {
String sparqlQueryString = "select ?predec where {" + "?predec ?prop <" + resource + ">." + "}";
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet queryResults = qexec.execSelect();
while (queryResults.hasNext()) {
QuerySolution qs = queryResults.nextSolution();
final PathManager.Path path2 = PathManager.findShortestPath(model, qs.getResource("?predec"), object, prop);
if (!(path2 == null)) {
// Get the successive properties from the path
List<Property> properties = getPropFromPath(path2);
if (legato.getPropList().existProperty(properties) == false) {
int indice = legato.getPropList().size();
finalModel.createResource(resource.toString()).addProperty(finalModel.createProperty("http://model.org/property" + indice), object);
try {
legato.addToPropList("http://model.org/property" + indice, properties);
} catch (IOException e) {
}
} else {
finalModel.createResource(resource.toString()).addProperty(finalModel.createProperty(legato.getPropList().getPropertyName(properties)), object);
}
}
}
qexec.close();
}
} else if (prop.equals(RDF.type) && (legato.hasType(sub))) {
finalModel.createResource(resource.toString()).addProperty(RDF.type, object);
}
// else
// finalModel.createResource(resource.toString()).addProperty(prop, object);
});
}
});
return finalModel;
}
use of org.apache.jena.query.Query in project legato by DOREMUS-ANR.
the class PropertyHandler method getDistinctProperties.
/**
***************************
* Get all distinct properties
****************************
*/
public static List<Resource> getDistinctProperties(Model model) {
List<Resource> properties = new ArrayList<Resource>();
String sparqlQueryString = "SELECT DISTINCT ?prop WHERE {" + "?resource ?prop ?object" + "}";
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet queryResults = qexec.execSelect();
while (queryResults.hasNext()) {
QuerySolution qs = queryResults.nextSolution();
Resource prop = qs.getResource("?prop");
properties.add(prop);
}
qexec.close();
return properties;
}
Aggregations