Search in sources :

Example 16 with Query

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();
}
Also used : Query(org.apache.jena.query.Query) QueryExecution(org.apache.jena.query.QueryExecution)

Example 17 with Query

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();
    }
}
Also used : SortCondition(org.apache.jena.query.SortCondition) Query(org.apache.jena.query.Query) Expr(org.apache.jena.sparql.expr.Expr) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap)

Example 18 with Query

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());
}
Also used : HPO_Class_Found(org.monarchinitiative.loinc2hpo.util.HPO_Class_Found) Query(org.apache.jena.query.Query) SparqlQuery(org.monarchinitiative.loinc2hpo.util.SparqlQuery) Test(org.junit.Test)

Example 19 with Query

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;
}
Also used : Path(org.apache.jena.ontology.OntTools.Path) Query(org.apache.jena.query.Query) Resource(org.apache.jena.rdf.model.Resource) IOException(java.io.IOException) QueryExecution(org.apache.jena.query.QueryExecution) LEGATO(legato.LEGATO) QuerySolution(org.apache.jena.query.QuerySolution) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) ResultSet(org.apache.jena.query.ResultSet) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 20 with Query

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;
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Aggregations

Query (org.apache.jena.query.Query)265 Test (org.junit.Test)78 ContractTest (org.xenei.junit.contract.ContractTest)65 QueryExecution (org.apache.jena.query.QueryExecution)63 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)49 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)42 Var (org.apache.jena.sparql.core.Var)42 Triple (org.apache.jena.graph.Triple)38 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)32 ResultSet (org.apache.jena.query.ResultSet)29 Node (org.apache.jena.graph.Node)28 TriplePath (org.apache.jena.sparql.core.TriplePath)28 QuerySolution (org.apache.jena.query.QuerySolution)26 ExprVar (org.apache.jena.sparql.expr.ExprVar)24 Binding (org.apache.jena.sparql.engine.binding.Binding)22 Model (org.apache.jena.rdf.model.Model)20 HashMap (java.util.HashMap)19 Dataset (org.apache.jena.query.Dataset)15 Op (org.apache.jena.sparql.algebra.Op)15 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)14