Search in sources :

Example 1 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project webofneeds by researchstudio-sat.

the class SparqlSelectFunction method addInitialBindings.

public SparqlSelectFunction<T> addInitialBindings(QuerySolution moreInitialBindings) {
    if (this.initialBinding == null) {
        this.initialBinding = new QuerySolutionMap();
    }
    for (Iterator<String> it = moreInitialBindings.varNames(); it.hasNext(); ) {
        String varName = it.next();
        this.initialBinding.add(varName, moreInitialBindings.get(varName));
    }
    return this;
}
Also used : QuerySolutionMap(org.apache.jena.query.QuerySolutionMap)

Example 2 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap 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 3 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project webofneeds by researchstudio-sat.

the class WonMessageQueryBuilder method senderConnection.

public WonMessageQueryBuilder senderConnection(URI senderConnection) {
    QuerySolutionMap initialBinding = new QuerySolutionMap();
    initialBinding.add("senderConnection", new ResourceImpl(senderConnection.toString()));
    delegate.addInitialBindings(initialBinding);
    return this;
}
Also used : ResourceImpl(org.apache.jena.rdf.model.impl.ResourceImpl) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap)

Example 4 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project rdf2neo by Rothamsted.

the class RdfDataManager method addCypherProps.

/**
 * Take an existing {@link CypherEntity} and adds the properties that can be mapped from the underlining TDB by means
 * of a property query, like {@link CyNodeLoadingHandler#getNodePropsSparql()}, or
 * {@link CyRelationLoadingHandler#getRelationPropsSparql()}.
 *
 * It doesn't do anything if the query is null.
 */
protected void addCypherProps(CypherEntity cyEnt, String propsSparql) {
    ensureOpen();
    Model model = this.dataSet.getDefaultModel();
    QuerySolutionMap params = new QuerySolutionMap();
    params.add("iri", model.getResource(cyEnt.getIri()));
    // It may be omitted, if you don't have any property except the IRI.
    if (propsSparql == null)
        return;
    Query qry = this.queryCache.getUnchecked(propsSparql);
    Function<String, String> propIdConverter = this.getCyPropertyIdConverter();
    boolean wasInTnx = dataSet.isInTransaction();
    if (!wasInTnx)
        dataSet.begin(ReadWrite.READ);
    try {
        QueryExecution qx = QueryExecutionFactory.create(qry, model, params);
        qx.execSelect().forEachRemaining(row -> {
            String propName = this.getCypherId(row.get("name"), propIdConverter);
            if (propName == null)
                throw new IllegalArgumentException("Null property name for " + cyEnt.getIri());
            String propValue = JENAUTILS.literal2Value(row.getLiteral("value")).get();
            cyEnt.addPropValue(propName, propValue);
        });
    } finally {
        if (!wasInTnx && dataSet.isInTransaction())
            dataSet.end();
    }
}
Also used : Query(org.apache.jena.query.Query) Model(org.apache.jena.rdf.model.Model) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap)

Example 5 with QuerySolutionMap

use of org.apache.jena.query.QuerySolutionMap in project jena by apache.

the class EvalSparql method evalSparqlComponent.

public static Collection<Node> evalSparqlComponent(Graph data, Node node, SparqlComponent sparqlComponent) {
    checkForRequiredParams(data, node, sparqlComponent);
    Query query = sparqlComponent.getQuery();
    if (!query.isSelectType())
        throw new ShaclException("Not a SELECT query");
    // Parameters
    if (USE_QueryTransformOps) {
        // Done with QueryTransformOps.transform
        DatasetGraph dsg = DatasetGraphFactory.wrap(data);
        Map<Var, Node> substitutions = parametersToSyntaxSubstitutions(data, node, sparqlComponent.getParams());
        Query query2 = QueryTransformOps.transform(query, substitutions);
        try (QueryExecution qExec = QueryExecutionFactory.create(query2, dsg)) {
            return evalSparqlOneVar(qExec);
        }
    } else {
        // Done with pre-binding.
        Model model = ModelFactory.createModelForGraph(data);
        QuerySolutionMap qsm = parametersToPreBinding(model, node, sparqlComponent.getParams());
        try (QueryExecution qExec = QueryExecution.create().query(query).model(model).initialBinding(qsm).build()) {
            return evalSparqlOneVar(qExec);
        }
    }
}
Also used : Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) Model(org.apache.jena.rdf.model.Model) ShaclException(org.apache.jena.shacl.ShaclException) QueryExecution(org.apache.jena.query.QueryExecution) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) QuerySolutionMap(org.apache.jena.query.QuerySolutionMap)

Aggregations

QuerySolutionMap (org.apache.jena.query.QuerySolutionMap)17 QueryExecution (org.apache.jena.query.QueryExecution)13 Test (org.junit.Test)10 Query (org.apache.jena.query.Query)8 Model (org.apache.jena.rdf.model.Model)7 QuerySolution (org.apache.jena.query.QuerySolution)3 ResultSet (org.apache.jena.query.ResultSet)3 Node (org.apache.jena.graph.Node)2 Dataset (org.apache.jena.query.Dataset)2 Property (org.apache.jena.rdf.model.Property)2 ArrayList (java.util.ArrayList)1 SortCondition (org.apache.jena.query.SortCondition)1 ResourceImpl (org.apache.jena.rdf.model.impl.ResourceImpl)1 ShaclException (org.apache.jena.shacl.ShaclException)1 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)1 Var (org.apache.jena.sparql.core.Var)1 Expr (org.apache.jena.sparql.expr.Expr)1