Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.jena.sparql.core.DatasetGraphFactory;
import org.apache.jena.sparql.core.GraphView;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.engine.binding.BindingLib;
import org.apache.jena.sparql.exec.QueryExecDataset;
import org.apache.jena.sparql.exec.QueryExecDatasetBuilder;
import org.apache.jena.sparql.exec.QueryExecutionCompat;
Expand Down Expand Up @@ -64,7 +63,7 @@ protected QueryExecutionFactory() {}
*/
public static QueryExecution create(Query query) {
checkArg(query);
return make(query);
return makeExecution(query);
}

/**
Expand Down Expand Up @@ -100,8 +99,7 @@ public static QueryExecution create(String queryStr, Syntax syntax) {
* @return QueryExecution
*/
public static QueryExecution create(Query query, Dataset dataset) {
// checkArg(dataset); // Allow null
return make(query, dataset, null, null);
return make(query, dataset, null);
}

/**
Expand All @@ -114,7 +112,7 @@ public static QueryExecution create(Query query, Dataset dataset) {
public static QueryExecution create(Query query, DatasetGraph datasetGraph) {
requireNonNull(query, "Query is null");
requireNonNull(datasetGraph, "DatasetGraph is null");
return make(query, null, datasetGraph, null);
return make(query, null, datasetGraph);
}

/** Create a QueryExecution to execute over the Dataset.
Expand All @@ -125,8 +123,7 @@ public static QueryExecution create(Query query, DatasetGraph datasetGraph) {
*/
public static QueryExecution create(String queryStr, Dataset dataset) {
checkArg(queryStr);
// checkArg(dataset); // Allow null
return make(makeQuery(queryStr), dataset, null, null);
return make(makeQuery(queryStr), dataset, null);
}

/** Create a QueryExecution to execute over the Dataset.
Expand All @@ -138,8 +135,7 @@ public static QueryExecution create(String queryStr, Dataset dataset) {
*/
public static QueryExecution create(String queryStr, Syntax syntax, Dataset dataset) {
checkArg(queryStr);
// checkArg(dataset); // Allow null
return make(makeQuery(queryStr, syntax), dataset, null, null);
return make(makeQuery(queryStr, syntax), dataset, null);
}

// ---------------- Query + Model
Expand All @@ -153,13 +149,13 @@ public static QueryExecution create(String queryStr, Syntax syntax, Dataset data
public static QueryExecution create(Query query, Model model) {
checkArg(query);
checkArg(model);
return make(query, model);
return makeExecution(query, model);
}

/** Create a QueryExecution to execute over the Model.
*
* @param queryStr Query string
* @param model Target of the query
* @param model Target of the query
* @return QueryExecution
*/
public static QueryExecution create(String queryStr, Model model) {
Expand All @@ -182,33 +178,31 @@ public static QueryExecution create(String queryStr, Syntax lang, Model model) {
}

/** Create a QueryExecution over a Dataset given some initial values of variables.
* Use {@code QueryExecution#create(Dataset).query(Query).substitution(QuerySolution).build()}.
*
* @param query Query
* @param dataset Target of the query
* @param initialBinding Any initial binding of variables
* @param querySolution Any initial binding of variables
* @return QueryExecution
* @deprecated Use {@code QueryExecution#dataset(Dataset).query(Query).substitution(QuerySolution).build()}.
* @deprecated Use {@code QueryExecution.dataset(dataset).query(query).substitution(querySolution).build()}.
*/
@Deprecated
public static QueryExecution create(Query query, Dataset dataset, QuerySolution initialBinding) {
public static QueryExecution create(Query query, Dataset dataset, QuerySolution querySolution) {
checkArg(query);
return QueryExecution.dataset(dataset).query(query).substitution(initialBinding).build();
return QueryExecution.dataset(dataset).query(query).substitution(querySolution).build();
}

/** Create a QueryExecution over a Dataset given some initial values of variables.
* Use {@code QueryExecution#create(Dataset).query(Query).substitution(QuerySolution).build()}.
/** Create a QueryExecution over a Model given some initial values of variables.
*
* @param query Query
* @param model Target of the query
* @param initialBinding Any initial binding of variables
* @param querySolution Any initial binding of variables
* @return QueryExecution
* @deprecated Use {@code QueryExecution#model(Model).query(Query).substitution(QuerySolution).build()}.
* @deprecated Use {@code QueryExecution#model(model).query(query).substitution(querySolution).build()}.
*/
@Deprecated
public static QueryExecution create(Query query, Model model, QuerySolution initialBinding) {
public static QueryExecution create(Query query, Model model, QuerySolution querySolution) {
checkArg(query);
return QueryExecution.model(model).query(query).substitution(initialBinding).build();
return QueryExecution.model(model).query(query).substitution(querySolution).build();
}

// ---------------- Internal routines
Expand All @@ -229,11 +223,11 @@ static private Query makeQuery(String queryStr, Syntax syntax) {
return QueryFactory.create(queryStr, syntax);
}

static protected QueryExecution make(Query query) {
static protected QueryExecution makeExecution(Query query) {
return QueryExecution.create().query(query).build();
}

protected static QueryExecution make(Query query, Model model) {
protected static QueryExecution makeExecution(Query query, Model model) {
Graph graph = model.getGraph();
DatasetGraph dataset = DatasetGraphFactory.wrap(graph);
Graph g = unwrap(graph);
Expand All @@ -242,7 +236,7 @@ protected static QueryExecution make(Query query, Model model) {
// Copy context of the storage dataset to the wrapper dataset.
dataset.getContext().putAll(gv.getDataset().getContext());
}
return make(query, null, dataset, (Binding)null);
return make(query, null, dataset);
}

private static Graph unwrap(Graph graph) {
Expand All @@ -255,15 +249,17 @@ else if ( graph instanceof WrappedGraph )
}
}

private static QueryExecution make(Query query, Dataset dataset, QuerySolution initialBinding) {
Binding binding = null;
if ( initialBinding != null )
binding = BindingLib.toBinding(initialBinding);
return make(query, dataset, null, binding);
// Preferred base of all QueryExecution creation in QueryExecutionFactory.
// dataset and datasetGraph can't both be set.
// Null for both of them is allowed and assumes the query has a dataset description.
private static QueryExecution make(Query query, Dataset dataset, DatasetGraph datasetGraph) {
return make$(query, dataset, datasetGraph, null);
}

// This form of "make" has support for "initialBinding" (seed the execution)
// The preferred approach is to use "substitution" )(replace variables with RDF terms).
@SuppressWarnings("deprecation")
private static QueryExecution make(Query query, Dataset dataset, DatasetGraph datasetGraph, Binding initialBinding) {
private static QueryExecution make$(Query query, Dataset dataset, DatasetGraph datasetGraph, Binding initialBinding) {
QueryExecDatasetBuilder builder = QueryExecDataset.newBuilder().query(query);
if ( initialBinding != null )
builder.initialBinding(initialBinding);
Expand All @@ -281,9 +277,6 @@ private static QueryExecution make(Query query, Dataset dataset, DatasetGraph da
static private void checkArg(Model model)
{ requireNonNull(model, "Model is a null pointer"); }

// static private void checkArg(Dataset dataset)
// { requireNonNull(dataset, "Dataset is a null pointer"); }

static private void checkArg(String queryStr)
{ requireNonNull(queryStr, "Query string is null"); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@
* limitations under the License.
*/

package org.apache.jena.sparql.graph ;
package org.apache.jena.sparql.graph;

import java.util.Collection ;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Consumer ;
import java.util.function.Consumer;

import org.apache.jena.atlas.iterator.Iter ;
import org.apache.jena.atlas.iterator.IteratorConcat ;
import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.atlas.iterator.IteratorConcat;
import org.apache.jena.atlas.lib.CollectionUtils;
import org.apache.jena.graph.Graph ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.TransactionHandler;
import org.apache.jena.graph.Triple ;
import org.apache.jena.graph.impl.GraphBase ;
import org.apache.jena.shared.AddDeniedException ;
import org.apache.jena.shared.DeleteDeniedException ;
import org.apache.jena.shared.PrefixMapping ;
import org.apache.jena.shared.impl.PrefixMappingImpl ;
import org.apache.jena.graph.Triple;
import org.apache.jena.graph.impl.GraphBase;
import org.apache.jena.shared.AddDeniedException;
import org.apache.jena.shared.DeleteDeniedException;
import org.apache.jena.shared.PrefixMapping;
import org.apache.jena.shared.impl.PrefixMappingImpl;
import org.apache.jena.sparql.core.*;
import org.apache.jena.util.iterator.ExtendedIterator ;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.apache.jena.util.iterator.NullIterator;
import org.apache.jena.util.iterator.WrappedIterator ;
import org.apache.jena.util.iterator.WrappedIterator;

/** Immutable graph that is the view of a union of graphs in a dataset.
* This union can be a fixed set of graph or all named graph.
* This union iterates over graphs.
* <p>
* {@link GraphView} provides a view over a dataset and does support union graph but
* assumes quad access is efficient and does not end up looping.
*
*
* @see GraphView
* @see DatasetGraphMap
*/
public class GraphUnionRead extends GraphBase {
private final DatasetGraph dataset ;
private final Collection<Node> graphs ;
private final DatasetGraph dataset;
private final Collection<Node> graphs;
// Special case.
private final Node graphName;

Expand All @@ -60,13 +60,13 @@ public class GraphUnionRead extends GraphBase {
* the {@code find} call.
*/
public GraphUnionRead(DatasetGraph dsg) {
this(dsg, null) ;
this(dsg, null);
}
/** Read-only graph view of a set of graphs from the dataset */

/** Read-only graph view of a set of graphs from the dataset */
public GraphUnionRead(DatasetGraph dsg, Collection<Node> graphs) {
this.dataset = dsg ;
this.graphs = graphs ;
this.dataset = dsg;
this.graphs = graphs;
// Special case.
if ( graphs != null && graphs.size() == 1 ) {
// No need to suppress duplicates because there aren't any.
Expand All @@ -79,19 +79,19 @@ public GraphUnionRead(DatasetGraph dsg, Collection<Node> graphs) {

@Override
protected PrefixMapping createPrefixMapping() {
PrefixMapping pmap = new PrefixMappingImpl() ;
PrefixMapping pmap = new PrefixMappingImpl();
forEachGraph((g) -> {
PrefixMapping pmapNamedGraph = g.getPrefixMapping() ;
pmap.setNsPrefixes(pmapNamedGraph) ;
}) ;
return pmap ;
PrefixMapping pmapNamedGraph = g.getPrefixMapping();
pmap.setNsPrefixes(pmapNamedGraph);
});
return pmap;
}

@Override
protected ExtendedIterator<Triple> graphBaseFind(Triple m) {
if ( graphs == null ) {
// This produces unique quads with the same graph node,
// hence the triples are distinct.
// hence the triples are distinct.
return quadsToTriples(dataset, Quad.unionGraph, m);
}
if ( graphs.isEmpty() )
Expand All @@ -104,46 +104,46 @@ protected ExtendedIterator<Triple> graphBaseFind(Triple m) {
return dataset.getGraph(graphName).find(m);
}
// Only certain graphs.
IteratorConcat<Triple> iter = new IteratorConcat<>() ;
forEachGraph((g) -> iter.add(g.find(m))) ;
return WrappedIterator.createNoRemove(Iter.distinct(iter)) ;
IteratorConcat<Triple> iter = new IteratorConcat<>();
forEachGraph((g) -> iter.add(g.find(m)));
return WrappedIterator.createNoRemove(Iter.distinct(iter));
}

private static ExtendedIterator<Triple> quadsToTriples(DatasetGraph dsg, Node graphName, Triple m) {
Iterator<Quad> qIter = dsg.findNG(graphName, m.getSubject(), m.getPredicate(), m.getObject());
Iterator<Triple> tIter = Iter.map(qIter, quad->quad.asTriple());
return WrappedIterator.createNoRemove(tIter) ;
return WrappedIterator.createNoRemove(tIter);
}

/** Execute action for each graph that exists */
private void forEachGraph(Consumer<Graph> action) {
if ( graphs == null ) {
// Fast-path the dynamic union of all named graphs.
dataset.listGraphNodes().forEachRemaining((gn) -> action.accept(dataset.getGraph(gn)));
return ;
return;
}

graphs.stream()
// Need to check to avoid auto-creation.
.filter(gn -> dataset.containsGraph(gn))
// For the explicit name of the default graph.
.map(gn -> Quad.isDefaultGraph(gn) ? dataset.getDefaultGraph() : dataset.getGraph(gn))
.forEach(action);
}

@Override public TransactionHandler getTransactionHandler() {
return new TransactionHandlerView(dataset);
}

// Override to give more specific message.

@Override
public void performAdd(Triple t) {
throw new AddDeniedException("GraphUnionRead::performAdd - Read-only graph") ;
throw new AddDeniedException("GraphUnionRead::performAdd - Read-only graph");
}

@Override
public void performDelete(Triple t) {
throw new DeleteDeniedException("GraphUnionRead::performDelete - Read-only graph") ;
throw new DeleteDeniedException("GraphUnionRead::performDelete - Read-only graph");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ protected void execute(String queryString, HttpAction action) {
if ( str.endsWith("\n") )
str = str.substring(0, str.length()-1);
action.log.info(format("[%d] Query = \n%s", action.id, str));
}
else
} else {
action.log.info(format("[%d] Query = %s", action.id, queryStringLog));
}

Query query = null;
try {
Expand Down
2 changes: 1 addition & 1 deletion jena-permissions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
<goal>jar</goal>
</goals>
<configuration>
<classedDirectory>target</classedDirectory>
<classesDirectory>target</classesDirectory>
<classifier>example</classifier>
<includes>
<include>../src/example/**</include>
Expand Down