Skip to content

Commit

Permalink
JENA-1602 Merge commit 'refs/pull/480/head' of github.com:apache/jena
Browse files Browse the repository at this point in the history
This closes apache#480
  • Loading branch information
Claudenw committed Oct 14, 2018
2 parents a56eb06 + 24a092d commit 3dfdfa8
Show file tree
Hide file tree
Showing 9 changed files with 1,361 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@
import org.apache.jena.arq.querybuilder.clauses.PrologClause;
import org.apache.jena.arq.querybuilder.clauses.WhereClause;
import org.apache.jena.arq.querybuilder.handlers.WhereHandler;
import org.apache.jena.arq.querybuilder.updatebuilder.CollectionQuadHolder;
import org.apache.jena.arq.querybuilder.updatebuilder.ModelQuadHolder;
import org.apache.jena.arq.querybuilder.updatebuilder.PrefixHandler;
import org.apache.jena.arq.querybuilder.updatebuilder.QBQuadHolder;
import org.apache.jena.arq.querybuilder.updatebuilder.QuadHolder;
import org.apache.jena.arq.querybuilder.updatebuilder.SingleQuadHolder;
import org.apache.jena.arq.querybuilder.updatebuilder.WhereProcessor;
import org.apache.jena.arq.querybuilder.updatebuilder.WhereQuadHolder;
import org.apache.jena.graph.FrontsTriple;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.QueryParseException;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.shared.PrefixMapping;
import org.apache.jena.sparql.core.Quad;
Expand All @@ -65,7 +68,7 @@
public class UpdateBuilder {

private final PrefixHandler prefixHandler;
private final WhereProcessor whereProcessor;
private final WhereQuadHolder whereProcessor;
private List<QuadHolder> inserts = new ArrayList<QuadHolder>();
private List<QuadHolder> deletes = new ArrayList<QuadHolder>();
private Map<Var, Node> values;
Expand All @@ -76,7 +79,7 @@ public class UpdateBuilder {
*/
public UpdateBuilder() {
this.prefixHandler = new PrefixHandler();
this.whereProcessor = new WhereProcessor(prefixHandler);
this.whereProcessor = new WhereQuadHolder(prefixHandler);
this.values = new HashMap<Var, Node>();
this.with = null;
}
Expand All @@ -101,7 +104,7 @@ public UpdateBuilder(PrologClause<?> prologClause) {
*/
public UpdateBuilder(PrefixMapping pMap) {
this.prefixHandler = new PrefixHandler(pMap);
this.whereProcessor = new WhereProcessor(prefixHandler);
this.whereProcessor = new WhereQuadHolder(prefixHandler);
}

// conver a collection of QuadHolder to an iterator on quads.
Expand Down Expand Up @@ -358,7 +361,85 @@ public UpdateBuilder addInsert(Object g, Triple t) {
inserts.add(new SingleQuadHolder(q));
return this;
}

/**
* Add all the statements in the model to the insert statement.
*
* @param model The model to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addInsert(Model model) {
inserts.add(new ModelQuadHolder( model ));
return this;
}

/**
* Add all the statements in the model to the insert statement.
*
* @param collection The triples to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addInsert(Collection<Triple> collection) {
inserts.add(new CollectionQuadHolder( collection ));
return this;
}

/**
* Add all the statements in the model to the insert statement.
*
* @param iter The iterator of triples to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addInsert(Iterator<Triple> iter) {
inserts.add(new CollectionQuadHolder( iter ));
return this;
}

/**
* Add all the statements in the model a specified graph to the insert statement.
*
* The graph object is converted by a call to makeNode().
*
* @see #makeNode(Object)
* @param g
* the graph for the triple.
* @param model
* the model to add.
* @return this builder for chaining.
*/
public UpdateBuilder addInsert(Object g, Model model) {
inserts.add( new ModelQuadHolder(
AbstractQueryBuilder.makeNode(g, prefixHandler.getPrefixes()),
model));
return this;
}

/**
* Add all the statements in the model to the insert statement.
*
* @param collection The triples to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addInsert(Object g, Collection<Triple> collection) {
inserts.add(new CollectionQuadHolder(
AbstractQueryBuilder.makeNode(g, prefixHandler.getPrefixes()),
collection ));
return this;
}

/**
* Add all the statements in the model to the insert statement.
*
* @param iter The iterator of triples to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addInsert(Object g, Iterator<Triple> iter) {
inserts.add(new CollectionQuadHolder(
AbstractQueryBuilder.makeNode(g, prefixHandler.getPrefixes()),
iter ));
return this;
}

/**
* Add the statements from the where clause in the specified query builder
* to the insert statement.
Expand Down Expand Up @@ -479,6 +560,89 @@ public UpdateBuilder addDelete(Object g, Triple t) {
return this;
}

/**
* Add all the statements in the model to the delete statement.
*
* @param model The model to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addDelete(Model model) {
deletes.add(new ModelQuadHolder( model ));
return this;
}

/**
* Add all the statements in the model to the delete statement.
*
* @param collection The collection of triples to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addDelete(Collection<Triple> collection) {
deletes.add(new CollectionQuadHolder( collection ));
return this;
}

/**
* Add all the statements in the model to the delete statement.
*
* @param iter The iterator of triples to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addDelete(Iterator<Triple> iter) {
deletes.add(new CollectionQuadHolder( iter ));
return this;
}

/**
* Add all the statements in the model a specified graph to the delete statement.
*
* The graph object is converted by a call to makeNode().
*
* @see #makeNode(Object)
* @param g
* the graph for the triples.
* @param model
* the model to add.
* @return this builder for chaining.
*/
public UpdateBuilder addDelete(Object g, Model model) {
deletes.add( new ModelQuadHolder(
AbstractQueryBuilder.makeNode(g, prefixHandler.getPrefixes()),
model));
return this;
}


/**
* Add all the statements in the model to the delete statement.
*
* @param g
* the graph for the triples.
* @param collection The collection of triples to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addDelete(Object g, Collection<Triple> collection) {
deletes.add(new CollectionQuadHolder(
AbstractQueryBuilder.makeNode(g, prefixHandler.getPrefixes()),
collection ));
return this;
}

/**
* Add all the statements in the model to the delete statement.
*
* @param g
* the graph for the triples.
* @param iter The iterator of triples to insert.
* @return this builder for chaining.
*/
public UpdateBuilder addDelete(Object g, Iterator<Triple> iter) {
deletes.add(new CollectionQuadHolder(
AbstractQueryBuilder.makeNode(g, prefixHandler.getPrefixes()),
iter ));
return this;
}

/**
* Add the statements from the where clause in the specified query builder
* to the delete statement.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jena.arq.querybuilder.updatebuilder;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.apache.jena.util.iterator.WrappedIterator;

/**
* An QuadHolder that creates quads from a collection or iterator of triples.
*
*/
public class CollectionQuadHolder implements QuadHolder {

private final Set<Triple> collection;
private final Node defaultGraphName;
private Map<Var, Node> values;

/**
* Constructor.
*
* @param graph
* the default graph name for the triples
* @param triples
* the collection of triples.
*/
public CollectionQuadHolder(final Node graph, Collection<Triple> triples) {
this.collection = new HashSet<Triple>();
this.collection.addAll( triples );
defaultGraphName = graph;
}

/**
* Constructor.
*
* @param graph
* the default graph name for the triples
* @param triples
* the iterator of triples.
*/
public CollectionQuadHolder(final Node graph, Iterator<Triple> triples) {
this.collection = WrappedIterator.create( triples ).toSet();
defaultGraphName = graph;
}

/**
* Constructor. Uses Quad.defaultGraphNodeGenerated for the graph name.
*
* @see Quad#defaultGraphNodeGenerated
* @param triples
* the collection of triples.
*/
public CollectionQuadHolder(final Collection<Triple> triples) {
this( Quad.defaultGraphNodeGenerated, triples );
}

/**
* Constructor.
*
* @param triples
* the iterator of triples.
*/
public CollectionQuadHolder(Iterator<Triple> triples) {
this.collection = WrappedIterator.create( triples ).toSet();
defaultGraphName = Quad.defaultGraphNodeGenerated;
}

private Node valueMap( Node n )
{
if (n.isVariable())
{
Var v = Var.alloc(n);
Node n2 = values.get( n );
return n2==null?n:n2;
}
return n;
}

@Override
public ExtendedIterator<Quad> getQuads() {
if (values == null)
{
values = Collections.emptyMap();
}
return WrappedIterator.create(collection.iterator())
.mapWith( triple -> new Triple(
valueMap(triple.getSubject()),
valueMap(triple.getPredicate()),
valueMap(triple.getObject())
))
.mapWith( triple -> new Quad( defaultGraphName, triple ) );
}



/**
* This implementation does nothing.
*/
@Override
public QuadHolder setValues(final Map<Var, Node> values) {
this.values = values;
return this;
}

}
Loading

0 comments on commit 3dfdfa8

Please sign in to comment.