org.eclipse.rdf4j.query.algebra.UpdateExpr Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.UpdateExpr. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Verify that an INSERT with a subselect using a wildcard correctly adds vars to projection
 *
 * @see <a href="https://github.com/eclipse/rdf4j/issues/686">#686</a>
 */
@Test
public void testParseWildcardSubselectInUpdate() throws Exception {
	StringBuilder update = new StringBuilder();
	update.append("INSERT { <urn:a> <urn:b> <urn:c> . } WHERE { SELECT * {?s ?p ?o } }");

	ParsedUpdate parsedUpdate = parser.parseUpdate(update.toString(), null);
	List<UpdateExpr> exprs = parsedUpdate.getUpdateExprs();
	assertEquals(1, exprs.size());

	UpdateExpr expr = exprs.get(0);
	assertTrue(expr instanceof Modify);
	Modify m = (Modify) expr;
	TupleExpr whereClause = m.getWhereExpr();
	assertTrue(whereClause instanceof Projection);
	ProjectionElemList projectionElemList = ((Projection) whereClause).getProjectionElemList();
	assertNotNull(projectionElemList);
	List<ProjectionElem> elements = projectionElemList.getElements();
	assertNotNull(elements);

	assertEquals("projection should contain all three variables", 3, elements.size());
}
 
Example #2
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meetOther(QueryModelNode node) throws X {
	if (node instanceof UnaryTupleOperator) {
		meetUnaryTupleOperator((UnaryTupleOperator) node);
	} else if (node instanceof BinaryTupleOperator) {
		meetBinaryTupleOperator((BinaryTupleOperator) node);
	} else if (node instanceof CompareSubQueryValueOperator) {
		meetCompareSubQueryValueOperator((CompareSubQueryValueOperator) node);
	} else if (node instanceof SubQueryValueOperator) {
		meetSubQueryValueOperator((SubQueryValueOperator) node);
	} else if (node instanceof UnaryValueOperator) {
		meetUnaryValueOperator((UnaryValueOperator) node);
	} else if (node instanceof BinaryValueOperator) {
		meetBinaryValueOperator((BinaryValueOperator) node);
	} else if (node instanceof UpdateExpr) {
		meetUpdateExpr((UpdateExpr) node);
	} else {
		meetNode(node);
	}
}
 
Example #3
Source File: AbstractQueryPreparer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void execute() throws UpdateExecutionException {
	ParsedUpdate parsedUpdate = getParsedUpdate();
	List<UpdateExpr> updateExprs = parsedUpdate.getUpdateExprs();
	Map<UpdateExpr, Dataset> datasetMapping = parsedUpdate.getDatasetMapping();
	for (UpdateExpr updateExpr : updateExprs) {
		Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr));

		try {
			AbstractQueryPreparer.this.execute(updateExpr, activeDataset, getBindings(), getIncludeInferred(),
					getMaxExecutionTime());
		} catch (UpdateExecutionException e) {
			if (!updateExpr.isSilent()) {
				throw e;
			}
		}
	}
}
 
Example #4
Source File: SailUpdate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void execute() throws UpdateExecutionException {
	ParsedUpdate parsedUpdate = getParsedUpdate();
	List<UpdateExpr> updateExprs = parsedUpdate.getUpdateExprs();
	Map<UpdateExpr, Dataset> datasetMapping = parsedUpdate.getDatasetMapping();

	SailUpdateExecutor executor = new SailUpdateExecutor(con.getSailConnection(), con.getValueFactory(),
			con.getParserConfig());

	boolean localTransaction = false;
	try {
		if (!getConnection().isActive()) {
			localTransaction = true;
			beginLocalTransaction();
		}
		for (UpdateExpr updateExpr : updateExprs) {

			Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr));

			try {
				executor.executeUpdate(updateExpr, activeDataset, getBindings(), getIncludeInferred(),
						getMaxExecutionTime());
			} catch (RDF4JException | IOException e) {
				logger.warn("exception during update execution: ", e);
				if (!updateExpr.isSilent()) {
					throw new UpdateExecutionException(e);
				}
			}
		}

		if (localTransaction) {
			commitLocalTransaction();
			localTransaction = false;
		}
	} finally {
		if (localTransaction) {
			rollbackLocalTransaction();
		}
	}
}
 
Example #5
Source File: ParallelSplitFunction.java    From Halyard with Apache License 2.0 5 votes vote down vote up
public static int getNumberOfForksFromFunctionArgument(String query, boolean sparqlUpdate, int stage) throws IllegalArgumentException{
    ParallelSplitFunctionVisitor psfv = new ParallelSplitFunctionVisitor();
    if (sparqlUpdate) {
        List<UpdateExpr> exprs = QueryParserUtil.parseUpdate(QueryLanguage.SPARQL, query, null).getUpdateExprs();
        if (exprs.size() > stage) {
            exprs.get(stage).visit(psfv);
        }
    } else {
        QueryParserUtil.parseQuery(QueryLanguage.SPARQL, query, null).getTupleExpr().visit(psfv);
    }
    return psfv.forks;
}
 
Example #6
Source File: ParsedUpdate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toString() {
	StringBuilder stringBuilder = new StringBuilder();
	// TODO visualize dataset in toString()?
	for (UpdateExpr updateExpr : updateExprs) {
		stringBuilder.append(updateExpr.toString());
		stringBuilder.append("; ");
	}
	return stringBuilder.toString();
}
 
Example #7
Source File: UpdateExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public UpdateExpr visit(ASTUpdate node, Object data) throws VisitorException {
	if (node instanceof ASTModify) {
		return this.visit((ASTModify) node, data);
	} else if (node instanceof ASTInsertData) {
		return this.visit((ASTInsertData) node, data);
	}

	return null;
}
 
Example #8
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param maxExecutionTime in seconds.
 */
public void executeUpdate(UpdateExpr updateExpr, Dataset dataset, BindingSet bindings, boolean includeInferred,
		int maxExecutionTime) throws SailException, RDFParseException, IOException {
	UpdateContext uc = new UpdateContext(updateExpr, dataset, bindings, includeInferred);
	logger.trace("Incoming update expression:\n{}", uc);

	con.startUpdate(uc);
	try {
		if (updateExpr instanceof Load) {
			executeLoad((Load) updateExpr, uc);
		} else if (updateExpr instanceof Modify) {
			executeModify((Modify) updateExpr, uc, maxExecutionTime);
		} else if (updateExpr instanceof InsertData) {
			executeInsertData((InsertData) updateExpr, uc, maxExecutionTime);
		} else if (updateExpr instanceof DeleteData) {
			executeDeleteData((DeleteData) updateExpr, uc, maxExecutionTime);
		} else if (updateExpr instanceof Clear) {
			executeClear((Clear) updateExpr, uc, maxExecutionTime);
		} else if (updateExpr instanceof Create) {
			executeCreate((Create) updateExpr, uc);
		} else if (updateExpr instanceof Copy) {
			executeCopy((Copy) updateExpr, uc, maxExecutionTime);
		} else if (updateExpr instanceof Add) {
			executeAdd((Add) updateExpr, uc, maxExecutionTime);
		} else if (updateExpr instanceof Move) {
			executeMove((Move) updateExpr, uc, maxExecutionTime);
		} else if (updateExpr instanceof Load) {
			throw new SailException("load operations can not be handled directly by the SAIL");
		}
	} finally {
		con.endUpdate(uc);
	}
}
 
Example #9
Source File: SailConnectionUpdate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void execute() throws UpdateExecutionException {
	ParsedUpdate parsedUpdate = getParsedUpdate();
	List<UpdateExpr> updateExprs = parsedUpdate.getUpdateExprs();
	Map<UpdateExpr, Dataset> datasetMapping = parsedUpdate.getDatasetMapping();

	SailUpdateExecutor executor = new SailUpdateExecutor(con, vf, parserConfig);

	for (UpdateExpr updateExpr : updateExprs) {

		Dataset activeDataset = getMergedDataset(datasetMapping.get(updateExpr));

		try {
			boolean localTransaction = isLocalTransaction();
			if (localTransaction) {
				beginLocalTransaction();
			}

			executor.executeUpdate(updateExpr, activeDataset, getBindings(), getIncludeInferred(),
					getMaxExecutionTime());

			if (localTransaction) {
				commitLocalTransaction();
			}
		} catch (RDF4JException | IOException e) {
			logger.warn("exception during update execution: ", e);
			if (!updateExpr.isSilent()) {
				throw new UpdateExecutionException(e);
			}
		}
	}
}
 
Example #10
Source File: UpdateContext.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public UpdateContext(UpdateExpr updateExpr, Dataset dataset, BindingSet bindings, boolean includeInferred) {
	assert updateExpr != null;
	this.updateExpr = updateExpr;
	if (dataset == null) {
		this.dataset = new SimpleDataset();
	} else {
		this.dataset = dataset;
	}
	if (bindings == null) {
		this.bindings = EmptyBindingSet.getInstance();
	} else {
		this.bindings = bindings;
	}
	this.includeInferred = includeInferred;
}
 
Example #11
Source File: ParsedUpdateTemplate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ParsedUpdateTemplate(Template template, ParsedUpdate update, BindingSet args) {
	super(update.getSourceString(), update.getNamespaces());
	for (UpdateExpr updateExpr : update.getUpdateExprs()) {
		addUpdateExpr(updateExpr);
	}
	for (Map.Entry<UpdateExpr, Dataset> entry : update.getDatasetMapping().entrySet()) {
		map(entry.getKey(), entry.getValue());
	}
	this.template = template;
	this.args = args;
}
 
Example #12
Source File: AbstractQueryPreparer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected abstract void execute(UpdateExpr updateExpr, Dataset dataset, BindingSet bindings,
boolean includeInferred, int maxExecutionTime) throws UpdateExecutionException;
 
Example #13
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void render(ParsedUpdate update, RDFHandler handler) throws RDFHandlerException {
	handler.startRDF();

	for (Map.Entry<String, String> entry : update.getNamespaces().entrySet()) {
		handler.handleNamespace(entry.getKey(), entry.getValue());
	}

	String[] sourceStrings = update.getSourceString().split("\\s*;\\s*");
	List<UpdateExpr> updateExprs = update.getUpdateExprs();
	Map<UpdateExpr, Dataset> datasets = update.getDatasetMapping();
	for (int i = 0; i < updateExprs.size(); i++) {
		UpdateExpr updateExpr = updateExprs.get(i);
		Resource updateSubj = valueFactory.createBNode();
		Dataset dataset = datasets.get(updateExpr);
		IRI updateClass;
		if (updateExpr instanceof Modify) {
			Modify modify = (Modify) updateExpr;
			if (modify.getInsertExpr() == null && modify.getWhereExpr().equals(modify.getDeleteExpr())) {
				updateClass = SP.DELETE_WHERE_CLASS;
			} else {
				updateClass = SP.MODIFY_CLASS;
			}
		} else if (updateExpr instanceof InsertData) {
			updateClass = SP.INSERT_DATA_CLASS;
		} else if (updateExpr instanceof DeleteData) {
			updateClass = SP.DELETE_DATA_CLASS;
		} else if (updateExpr instanceof Load) {
			updateClass = SP.LOAD_CLASS;
		} else if (updateExpr instanceof Clear) {
			updateClass = SP.CLEAR_CLASS;
		} else if (updateExpr instanceof Create) {
			updateClass = SP.CREATE_CLASS;
		} else {
			throw new RDFHandlerException("Unrecognised UpdateExpr: " + updateExpr.getClass());
		}
		handler.handleStatement(valueFactory.createStatement(updateSubj, RDF.TYPE, updateClass));
		if (output.text) {
			handler.handleStatement(valueFactory.createStatement(updateSubj, SP.TEXT_PROPERTY,
					valueFactory.createLiteral(sourceStrings[i])));
		}
		if (output.rdf) {
			SpinVisitor visitor = new SpinVisitor(handler, null, updateSubj, dataset);
			updateExpr.visit(visitor);
			visitor.end();
		}
	}
	handler.endRDF();
}
 
Example #14
Source File: UpdateContext.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @return Returns the updateExpr.
 */
public UpdateExpr getUpdateExpr() {
	return updateExpr;
}
 
Example #15
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseInInsertFromStatementPattern() throws Exception {
	String simpleSparqlQuery = "Insert {<<?s ?p ?o>> <urn:pred> <urn:value>} WHERE {?s ?p ?o}";

	ParsedUpdate q = parser.parseUpdate(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect single UpdateExpr", q.getUpdateExprs().size() == 1);
	UpdateExpr updateExpr = q.getUpdateExprs().get(0);
	assertTrue("expect Modify UpdateExpr", updateExpr instanceof Modify);
	Modify modify = (Modify) updateExpr;
	assertTrue("expect no DELETE", modify.getDeleteExpr() == null);

	assertTrue("expect INSERT", modify.getInsertExpr() != null);
	assertTrue("expect INSERT as statamentPattern", modify.getInsertExpr() instanceof StatementPattern);
	StatementPattern insert = (StatementPattern) modify.getInsertExpr();
	String anonVar = insert.getSubjectVar().getName();
	assertEquals("expect predicate", "urn:pred", insert.getPredicateVar().getValue().toString());
	assertEquals("expect object", "urn:value", insert.getObjectVar().getValue().toString());

	assertTrue("expect WHERE", modify.getWhereExpr() != null);
	assertTrue("expect WHERE as extension", modify.getWhereExpr() instanceof Extension);

	Extension where = (Extension) modify.getWhereExpr();

	Extension ext = (Extension) where;
	assertTrue("one extention element", ext.getElements().size() == 1);
	ExtensionElem elem = ext.getElements().get(0);

	assertEquals("anon name should match first", elem.getName(), anonVar);

	assertTrue("expect ValueExprTripleRef in extention element", elem.getExpr() instanceof ValueExprTripleRef);
	ValueExprTripleRef ref = (ValueExprTripleRef) elem.getExpr();
	assertEquals("subject var name", "s", ref.getSubjectVar().getName());
	assertEquals("predicate var name", "p", ref.getPredicateVar().getName());
	assertEquals("object var name", "o", ref.getObjectVar().getName());

	assertTrue("expect StatementPattern as extension argument", ext.getArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) ext.getArg();
	assertEquals("subject var name should match", pattern.getSubjectVar().getName(), ref.getSubjectVar().getName());
	assertEquals("predicate var name should match", pattern.getPredicateVar().getName(),
			ref.getPredicateVar().getName());
	assertEquals("object var name should match", pattern.getObjectVar().getName(), ref.getObjectVar().getName());

}
 
Example #16
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseInDeleteFromStatementPattern() throws Exception {
	String simpleSparqlQuery = "DELETE {<<?s ?p ?o>> <urn:pred> <urn:value>} WHERE {?s ?p ?o}";

	ParsedUpdate q = parser.parseUpdate(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect single UpdateExpr", q.getUpdateExprs().size() == 1);
	UpdateExpr updateExpr = q.getUpdateExprs().get(0);
	assertTrue("expect Modify UpdateExpr", updateExpr instanceof Modify);
	Modify modify = (Modify) updateExpr;
	assertTrue("expect no INSERT", modify.getInsertExpr() == null);

	assertTrue("expect DELETE", modify.getDeleteExpr() != null);
	assertTrue("expect DETELE as statamentPattern", modify.getDeleteExpr() instanceof StatementPattern);
	StatementPattern insert = (StatementPattern) modify.getDeleteExpr();
	String anonVar = insert.getSubjectVar().getName();
	assertEquals("expect predicate", "urn:pred", insert.getPredicateVar().getValue().toString());
	assertEquals("expect object", "urn:value", insert.getObjectVar().getValue().toString());

	assertTrue("expect WHERE", modify.getWhereExpr() != null);
	assertTrue("expect WHERE as extension", modify.getWhereExpr() instanceof Extension);

	Extension where = (Extension) modify.getWhereExpr();

	Extension ext = (Extension) where;
	assertTrue("one extention element", ext.getElements().size() == 1);
	ExtensionElem elem = ext.getElements().get(0);

	assertEquals("anon name should match first", elem.getName(), anonVar);

	assertTrue("expect ValueExprTripleRef in extention element", elem.getExpr() instanceof ValueExprTripleRef);
	ValueExprTripleRef ref = (ValueExprTripleRef) elem.getExpr();
	assertEquals("subject var name", "s", ref.getSubjectVar().getName());
	assertEquals("predicate var name", "p", ref.getPredicateVar().getName());
	assertEquals("object var name", "o", ref.getObjectVar().getName());

	assertTrue("expect StatementPattern as extension argument", ext.getArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) ext.getArg();
	assertEquals("subject var name should match", pattern.getSubjectVar().getName(), ref.getSubjectVar().getName());
	assertEquals("predicate var name should match", pattern.getPredicateVar().getName(),
			ref.getPredicateVar().getName());
	assertEquals("object var name should match", pattern.getObjectVar().getName(), ref.getObjectVar().getName());

}
 
Example #17
Source File: ParsedUpdate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void addUpdateExpr(UpdateExpr updateExpr) {
	updateExprs.add(updateExpr);
}
 
Example #18
Source File: ParsedUpdate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public List<UpdateExpr> getUpdateExprs() {
	return updateExprs;
}
 
Example #19
Source File: ParsedUpdate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @return Returns the map of update expressions and associated datasets.
 */
public Map<UpdateExpr, Dataset> getDatasetMapping() {
	return datasetMapping;
}
 
Example #20
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public UpdateExpr getUpdateExpr() {
	return updateRoot;
}
 
Example #21
Source File: AbstractQueryModelVisitor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Method called by all <tt>meet</tt> methods with a {@link UpdateExpr} node as argument. Forwards the call to
 * {@link #meetNode} by default.
 *
 * @param node The node that is being visited.
 */
protected void meetUpdateExpr(UpdateExpr node) throws X {
	meetNode(node);
}
 
Example #22
Source File: ParsedUpdate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * @param updateExpr The updateExpr to map to a dataset.
 * @param dataset    the dataset that applies to the updateExpr. May be null.
 */
public void map(UpdateExpr updateExpr, Dataset dataset) {
	datasetMapping.put(updateExpr, dataset);
}