Java Code Examples for org.eclipse.rdf4j.query.Dataset#getNamedGraphs()

The following examples show how to use org.eclipse.rdf4j.query.Dataset#getNamedGraphs() . 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: QueryStringUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static StringBuilder appendDatasetClause(StringBuilder sb, Dataset dataset) {
	if (dataset == null) {
		return sb;
	}
	for (IRI context : dataset.getDefaultGraphs()) {
		sb.append("FROM <").append(context.stringValue()).append("> ");
	}
	for (IRI namedContext : dataset.getNamedGraphs()) {
		sb.append("FROM NAMED <").append(namedContext.stringValue()).append("> ");
	}
	return sb;
}
 
Example 2
Source File: RDF4JProtocolSession.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected List<NameValuePair> getQueryMethodParameters(QueryLanguage ql, String query, String baseURI,
		Dataset dataset, boolean includeInferred, int maxQueryTime, Binding... bindings) {
	Objects.requireNonNull(ql, "QueryLanguage may not be null");

	List<NameValuePair> queryParams = new ArrayList<>();
	queryParams.add(new BasicNameValuePair(Protocol.QUERY_LANGUAGE_PARAM_NAME, ql.getName()));
	queryParams.add(new BasicNameValuePair(Protocol.QUERY_PARAM_NAME, query));

	if (baseURI != null) {
		queryParams.add(new BasicNameValuePair(Protocol.BASEURI_PARAM_NAME, baseURI));
	}

	queryParams
			.add(new BasicNameValuePair(Protocol.INCLUDE_INFERRED_PARAM_NAME, Boolean.toString(includeInferred)));

	if (maxQueryTime > 0) {
		queryParams.add(new BasicNameValuePair(Protocol.TIMEOUT_PARAM_NAME, Integer.toString(maxQueryTime)));
	}

	if (dataset != null) {
		for (IRI defaultGraphURI : dataset.getDefaultGraphs()) {
			queryParams.add(
					new BasicNameValuePair(Protocol.DEFAULT_GRAPH_PARAM_NAME, String.valueOf(defaultGraphURI)));
		}
		for (IRI namedGraphURI : dataset.getNamedGraphs()) {
			queryParams.add(new BasicNameValuePair(Protocol.NAMED_GRAPH_PARAM_NAME, String.valueOf(namedGraphURI)));
		}
	}

	for (int i = 0; i < bindings.length; i++) {
		String paramName = Protocol.BINDING_PREFIX + bindings[i].getName();
		String paramValue = Protocol.encodeValue(bindings[i].getValue());
		queryParams.add(new BasicNameValuePair(paramName, paramValue));
	}

	return queryParams;
}
 
Example 3
Source File: SPARQLProtocolSession.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected List<NameValuePair> getQueryMethodParameters(QueryLanguage ql, String query, String baseURI,
		Dataset dataset, boolean includeInferred, int maxQueryTime, Binding... bindings) {
	List<NameValuePair> queryParams = new ArrayList<>();

	/*
	 * Only query, default-graph-uri, and named-graph-uri are standard parameters in SPARQL Protocol 1.1.
	 */

	if (query != null) {
		if (baseURI != null && !baseURI.isEmpty()) {
			// prepend query string with base URI declaration
			query = "BASE <" + baseURI + "> \n" + query;
		}
		queryParams.add(new BasicNameValuePair(Protocol.QUERY_PARAM_NAME, query));
	}

	if (dataset != null) {
		for (IRI defaultGraphURI : dataset.getDefaultGraphs()) {
			queryParams.add(
					new BasicNameValuePair(Protocol.DEFAULT_GRAPH_PARAM_NAME, String.valueOf(defaultGraphURI)));
		}
		for (IRI namedGraphURI : dataset.getNamedGraphs()) {
			queryParams.add(new BasicNameValuePair(Protocol.NAMED_GRAPH_PARAM_NAME, String.valueOf(namedGraphURI)));
		}
	}

	return queryParams;
}
 
Example 4
Source File: TransactionWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected void serialize(SPARQLUpdateOperation op, XMLWriter xmlWriter) throws IOException {
	String baseURI = op.getBaseURI();
	if (baseURI != null) {
		xmlWriter.setAttribute(TransactionXMLConstants.BASE_URI_ATT, baseURI);
	}
	xmlWriter.setAttribute(TransactionXMLConstants.INCLUDE_INFERRED_ATT, op.isIncludeInferred());
	xmlWriter.startTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);

	// serialize update string
	String updateString = op.getUpdateString();
	xmlWriter.textElement(TransactionXMLConstants.UPDATE_STRING_TAG, updateString);

	// serialize dataset definition (if any)
	Dataset dataset = op.getDataset();
	if (dataset != null) {
		xmlWriter.startTag(TransactionXMLConstants.DATASET_TAG);

		xmlWriter.startTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
		for (IRI defaultGraph : dataset.getDefaultGraphs()) {
			xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultGraph.stringValue());
		}
		xmlWriter.endTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);

		xmlWriter.startTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
		for (IRI namedGraph : dataset.getNamedGraphs()) {
			xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, namedGraph.stringValue());
		}
		xmlWriter.endTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);

		xmlWriter.startTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
		for (IRI defaultRemoveGraph : dataset.getDefaultRemoveGraphs()) {
			xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultRemoveGraph.stringValue());
		}
		xmlWriter.endTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);

		if (dataset.getDefaultInsertGraph() != null) {
			xmlWriter.textElement(TransactionXMLConstants.DEFAULT_INSERT_GRAPH,
					dataset.getDefaultInsertGraph().stringValue());
		}
		xmlWriter.endTag(TransactionXMLConstants.DATASET_TAG);
	}

	if (op.getBindings() != null && op.getBindings().length > 0) {
		xmlWriter.startTag(TransactionXMLConstants.BINDINGS);

		for (Binding binding : op.getBindings()) {
			if (binding.getName() != null && binding.getValue() != null
					&& binding.getValue().stringValue() != null) {
				if (binding.getValue() instanceof IRI) {
					xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
					xmlWriter.textElement(TransactionXMLConstants.BINDING_URI, binding.getValue().stringValue());
				}

				if (binding.getValue() instanceof BNode) {
					xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
					xmlWriter.textElement(TransactionXMLConstants.BINDING_BNODE, binding.getValue().stringValue());
				}

				if (binding.getValue() instanceof Literal) {
					xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());

					Literal literal = (Literal) binding.getValue();
					if (Literals.isLanguageLiteral(literal)) {
						xmlWriter.setAttribute(TransactionXMLConstants.LANGUAGE_ATT, literal.getLanguage().get());
					} else {
						xmlWriter.setAttribute(TransactionXMLConstants.DATA_TYPE_ATT,
								literal.getDatatype().stringValue());
					}

					xmlWriter.textElement(TransactionXMLConstants.BINDING_LITERAL,
							binding.getValue().stringValue());
				}
			}
		}

		xmlWriter.endTag(TransactionXMLConstants.BINDINGS);
	}

	xmlWriter.endTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);

}
 
Example 5
Source File: RDF4JProtocolSession.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected List<NameValuePair> getUpdateMethodParameters(QueryLanguage ql, String update, String baseURI,
		Dataset dataset, boolean includeInferred, int maxQueryTime, Binding... bindings) {
	Objects.requireNonNull(ql, "QueryLanguage may not be null");

	List<NameValuePair> queryParams = new ArrayList<>();

	queryParams.add(new BasicNameValuePair(Protocol.QUERY_LANGUAGE_PARAM_NAME, ql.getName()));

	if (update != null) {
		queryParams.add(new BasicNameValuePair(Protocol.UPDATE_PARAM_NAME, update));
	}

	if (baseURI != null) {
		queryParams.add(new BasicNameValuePair(Protocol.BASEURI_PARAM_NAME, baseURI));
	}

	queryParams
			.add(new BasicNameValuePair(Protocol.INCLUDE_INFERRED_PARAM_NAME, Boolean.toString(includeInferred)));

	if (dataset != null) {
		for (IRI graphURI : dataset.getDefaultRemoveGraphs()) {
			queryParams.add(new BasicNameValuePair(Protocol.REMOVE_GRAPH_PARAM_NAME, String.valueOf(graphURI)));
		}
		if (dataset.getDefaultInsertGraph() != null) {
			queryParams.add(new BasicNameValuePair(Protocol.INSERT_GRAPH_PARAM_NAME,
					String.valueOf(dataset.getDefaultInsertGraph())));
		}
		for (IRI defaultGraphURI : dataset.getDefaultGraphs()) {
			queryParams
					.add(new BasicNameValuePair(Protocol.USING_GRAPH_PARAM_NAME, String.valueOf(defaultGraphURI)));
		}
		for (IRI namedGraphURI : dataset.getNamedGraphs()) {
			queryParams.add(
					new BasicNameValuePair(Protocol.USING_NAMED_GRAPH_PARAM_NAME, String.valueOf(namedGraphURI)));
		}
	}

	if (maxQueryTime > 0) {
		queryParams.add(new BasicNameValuePair(Protocol.TIMEOUT_PARAM_NAME, Integer.toString(maxQueryTime)));
	}

	for (int i = 0; i < bindings.length; i++) {
		String paramName = Protocol.BINDING_PREFIX + bindings[i].getName();
		String paramValue = Protocol.encodeValue(bindings[i].getValue());
		queryParams.add(new BasicNameValuePair(paramName, paramValue));
	}

	return queryParams;
}
 
Example 6
Source File: SPARQLProtocolSession.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected List<NameValuePair> getUpdateMethodParameters(QueryLanguage ql, String update, String baseURI,
		Dataset dataset, boolean includeInferred, int maxQueryTime, Binding... bindings) {

	List<NameValuePair> queryParams = new ArrayList<>();

	if (update != null) {
		if (baseURI != null && !baseURI.isEmpty()) {
			// prepend update string with base URI declaration
			update = "BASE <" + baseURI + "> \n" + update;
		}
		queryParams.add(new BasicNameValuePair(Protocol.UPDATE_PARAM_NAME, update));
		logger.debug("added update string {}", update);
	}

	if (dataset != null) {
		if (dataset.getDefaultRemoveGraphs().size() > 0) {
			if (!(dataset.getDefaultRemoveGraphs().equals(dataset.getDefaultGraphs()))) {
				logger.warn(
						"ambiguous dataset spec for SPARQL endpoint: default graphs and default remove graphs both defined but not equal");
			}
			for (IRI graphURI : dataset.getDefaultRemoveGraphs()) {
				if (dataset.getDefaultInsertGraph() != null) {
					if (!dataset.getDefaultInsertGraph().equals(graphURI)) {
						logger.warn(
								"ambiguous dataset spec for SPARQL endpoint: default insert graph ({}) and default remove graph ({}) both defined but not equal. ",
								dataset.getDefaultInsertGraph(), graphURI);
					}
				}
				queryParams.add(new BasicNameValuePair(Protocol.USING_GRAPH_PARAM_NAME, String.valueOf(graphURI)));
			}
		}

		if (dataset.getDefaultInsertGraph() != null) {
			if (!dataset.getDefaultGraphs().isEmpty()) {
				if (!(dataset.getDefaultGraphs().size() == 1
						&& dataset.getDefaultGraphs().contains(dataset.getDefaultInsertGraph()))) {
					logger.warn(
							"ambiguous dataset spec for SPARQL endpoint: default insert graph ({}) and default graphs both defined but not equal. ",
							dataset.getDefaultInsertGraph());
				}
			}

			queryParams.add(new BasicNameValuePair(Protocol.USING_GRAPH_PARAM_NAME,
					String.valueOf(dataset.getDefaultInsertGraph())));
		}

		for (IRI defaultGraphURI : dataset.getDefaultGraphs()) {
			queryParams
					.add(new BasicNameValuePair(Protocol.USING_GRAPH_PARAM_NAME, String.valueOf(defaultGraphURI)));
		}
		for (IRI namedGraphURI : dataset.getNamedGraphs()) {
			queryParams.add(
					new BasicNameValuePair(Protocol.USING_NAMED_GRAPH_PARAM_NAME, String.valueOf(namedGraphURI)));
		}
	}

	return queryParams;
}