Java Code Examples for org.eclipse.rdf4j.rio.RDFHandler#handleStatement()

The following examples show how to use org.eclipse.rdf4j.rio.RDFHandler#handleStatement() . 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: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void render(ParsedBooleanQuery query, RDFHandler handler) throws RDFHandlerException {
	handler.startRDF();
	Resource querySubj = valueFactory.createBNode();
	handler.handleStatement(valueFactory.createStatement(querySubj, RDF.TYPE, SP.ASK_CLASS));
	if (output.text) {
		handler.handleStatement(valueFactory.createStatement(querySubj, SP.TEXT_PROPERTY,
				valueFactory.createLiteral(query.getSourceString())));
	}
	if (output.rdf) {
		Resource whereBNode = valueFactory.createBNode();
		handler.handleStatement(valueFactory.createStatement(querySubj, SP.WHERE_PROPERTY, whereBNode));
		TupleExpr expr = query.getTupleExpr();
		SpinVisitor visitor = new AskVisitor(handler, whereBNode, query.getDataset());
		expr.visit(visitor);
		visitor.end();
	}
	handler.endRDF();
}
 
Example 2
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void render(ParsedTupleQuery query, RDFHandler handler) throws RDFHandlerException {
	handler.startRDF();
	Resource querySubj = valueFactory.createBNode();
	handler.handleStatement(valueFactory.createStatement(querySubj, RDF.TYPE, SP.SELECT_CLASS));
	if (output.text) {
		handler.handleStatement(valueFactory.createStatement(querySubj, SP.TEXT_PROPERTY,
				valueFactory.createLiteral(query.getSourceString())));
	}
	if (output.rdf) {
		TupleExpr expr = query.getTupleExpr();
		SpinVisitor visitor = new SpinVisitor(handler, null, querySubj, query.getDataset());
		expr.visit(visitor);
		visitor.end();
	}
	handler.endRDF();
}
 
Example 3
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void render(ParsedDescribeQuery query, RDFHandler handler) throws RDFHandlerException {
	handler.startRDF();
	Resource querySubj = valueFactory.createBNode();
	handler.handleStatement(valueFactory.createStatement(querySubj, RDF.TYPE, SP.DESCRIBE_CLASS));
	if (output.text) {
		handler.handleStatement(valueFactory.createStatement(querySubj, SP.TEXT_PROPERTY,
				valueFactory.createLiteral(query.getSourceString())));
	}
	if (output.rdf) {
		TupleExpr expr = query.getTupleExpr();
		SpinVisitor visitor = new DescribeVisitor(handler, querySubj, query.getDataset());
		expr.visit(visitor);
		visitor.end();
	}
	handler.endRDF();
}
 
Example 4
Source File: SpinRenderer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void render(ParsedGraphQuery query, RDFHandler handler) throws RDFHandlerException {
	handler.startRDF();
	Resource querySubj = valueFactory.createBNode();
	handler.handleStatement(valueFactory.createStatement(querySubj, RDF.TYPE, SP.CONSTRUCT_CLASS));
	if (output.text) {
		handler.handleStatement(valueFactory.createStatement(querySubj, SP.TEXT_PROPERTY,
				valueFactory.createLiteral(query.getSourceString())));
	}
	if (output.rdf) {
		TupleExpr expr = query.getTupleExpr();
		SpinVisitor visitor = new ConstructVisitor(handler, querySubj, query.getDataset());
		expr.visit(visitor);
		visitor.end();
	}
	handler.endRDF();
}
 
Example 5
Source File: QueryResults.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Reports a graph query result to an {@link RDFHandler}. <br>
 * The {@link GraphQueryResult#close()} method will always be called before this method returns.<br>
 * If there is an exception generated by the GraphQueryResult, {@link RDFHandler#endRDF()} will not be called.
 *
 * @param gqr        The query result to report.
 * @param rdfHandler The handler to report the query result to.
 * @throws RDFHandlerException      If such an exception is thrown by the used RDF writer.
 * @throws QueryEvaluationException
 */
public static void report(GraphQueryResult gqr, RDFHandler rdfHandler)
		throws RDFHandlerException, QueryEvaluationException {
	try {
		rdfHandler.startRDF();

		for (Map.Entry<String, String> entry : gqr.getNamespaces().entrySet()) {
			String prefix = entry.getKey();
			String namespace = entry.getValue();
			rdfHandler.handleNamespace(prefix, namespace);
		}

		while (gqr.hasNext()) {
			Statement st = gqr.next();
			rdfHandler.handleStatement(st);
		}
	} finally {
		gqr.close();
	}
	rdfHandler.endRDF();
}
 
Example 6
Source File: SailRepositoryConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void exportStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, RDFHandler handler,
		Resource... contexts) throws RepositoryException, RDFHandlerException {
	handler.startRDF();

	try ( // Export namespace information
			CloseableIteration<? extends Namespace, RepositoryException> nsIter = getNamespaces()) {
		while (nsIter.hasNext()) {
			Namespace ns = nsIter.next();
			handler.handleNamespace(ns.getPrefix(), ns.getName());
		}
	}

	// Export statements

	try (CloseableIteration<? extends Statement, RepositoryException> stIter = getStatements(subj, pred, obj,
			includeInferred, contexts)) {
		while (stIter.hasNext()) {
			handler.handleStatement(stIter.next());
		}
	}

	handler.endRDF();
}
 
Example 7
Source File: RepositoryConnectionWrapper.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Exports all statements contained in the supplied statement iterator and all relevant namespace information to the
 * supplied RDFHandler.
 */
protected void exportStatements(RepositoryResult<Statement> stIter, RDFHandler handler)
		throws RepositoryException, RDFHandlerException {
	try {
		handler.startRDF();
		try ( // Export namespace information
				RepositoryResult<Namespace> nsIter = getNamespaces()) {
			while (nsIter.hasNext()) {
				Namespace ns = nsIter.next();
				handler.handleNamespace(ns.getPrefix(), ns.getName());
			}
		}
		// Export statemnts
		while (stIter.hasNext()) {
			handler.handleStatement(stIter.next());
		}
		handler.endRDF();
	} finally {
		stIter.close();
	}
}
 
Example 8
Source File: Rio.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Writes the given statements to the given {@link RDFHandler}.
 * <p>
 * If the collection is a {@link Model}, its namespaces will also be written.
 *
 * @param iterable A collection of statements, such as a {@link Model}, to be written.
 */
public static void write(Iterable<Statement> iterable, RDFHandler writer, SesameTransformer transformer,
                         StatementHandler... statementHandlers) {
    writer.startRDF();

    if (iterable instanceof Model) {
        for (Namespace nextNamespace : ((Model) iterable).getNamespaces()) {
            writer.handleNamespace(nextNamespace.getPrefix(), nextNamespace.getName());
        }
    }

    for (final Statement st : iterable) {
        Statement handledStatement = st;
        for (StatementHandler statementHandler : statementHandlers) {
            handledStatement = statementHandler.handleStatement(handledStatement);
        }
        org.eclipse.rdf4j.model.Statement sesameStatement = transformer.sesameStatement(handledStatement);
        writer.handleStatement(sesameStatement);
    }
    writer.endRDF();
}
 
Example 9
Source File: RyaDaoQueryWrapper.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Handles all results of a query. Closes the query iterator when done.
 * @param subject the subject {@link Resource} to query for.
 * @param predicate the predicate {@link IRI} to query for.
 * @param object the object {@link Value} to query for.
 * @param rdfStatementHandler the {@link RDFHandler} to use for handling
 * each statement returned. (not {@code null})
 * @param contexts the context {@link Resource}s to query for.
 * @throws QueryEvaluationException
 */
public void queryAll(final Resource subject, final IRI predicate, final Value object, final RDFHandler rdfStatementHandler, final Resource... contexts) throws QueryEvaluationException {
    checkNotNull(rdfStatementHandler);
    final CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDao, subject, predicate, object, conf, contexts);
    try {
        while (iter.hasNext()) {
            final Statement statement = iter.next();
            try {
                rdfStatementHandler.handleStatement(statement);
            } catch (final Exception e) {
                throw new QueryEvaluationException("Error handling statement.", e);
            }
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}
 
Example 10
Source File: RyaDaoQueryWrapper.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Handles only the first result of a query. Closes the query iterator when
 * done.
 * @param subject the subject {@link Resource} to query for.
 * @param predicate the predicate {@link IRI} to query for.
 * @param object the object {@link Value} to query for.
 * @param rdfStatementHandler the {@link RDFHandler} to use for handling the
 * first statement returned. (not {@code null})
 * @param contexts the context {@link Resource}s to query for.
 * @throws QueryEvaluationException
 */
public void queryFirst(final Resource subject, final IRI predicate, final Value object, final RDFHandler rdfStatementHandler, final Resource... contexts) throws QueryEvaluationException {
    checkNotNull(rdfStatementHandler);
    final CloseableIteration<Statement, QueryEvaluationException> iter = RyaDAOHelper.query(ryaDao, subject, predicate, object, conf, contexts);
    try {
        if (iter.hasNext()) {
            final Statement statement = iter.next();
            try {
                rdfStatementHandler.handleStatement(statement);
            } catch (final Exception e) {
                throw new QueryEvaluationException("Error handling statement.", e);
            }
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}
 
Example 11
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 12
Source File: RDFHandlerWrapper.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void handleStatement(Statement st) throws RDFHandlerException {
	for (RDFHandler rdfHandler : rdfHandlers) {
		rdfHandler.handleStatement(st);
	}
}