Java Code Examples for org.eclipse.rdf4j.model.Statement#getContext()

The following examples show how to use org.eclipse.rdf4j.model.Statement#getContext() . 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: EnhancedNQuadsWriter.java    From amazon-neptune-tools with Apache License 2.0 6 votes vote down vote up
@Override
public void handleStatement(Statement statement) throws RDFHandlerException {
    prefixes.parse(statement.getSubject().stringValue(), this);
    prefixes.parse(statement.getPredicate().toString(), this);
    prefixes.parse(statement.getObject().stringValue(), this);

    Resource context = statement.getContext();
    if (context != null){
        prefixes.parse(context.stringValue(), this);
    }

    writer.startCommit();
    super.handleStatement(statement);
    writer.endCommit();

    status.update();
}
 
Example 2
Source File: EnhancedTurtleWriter.java    From amazon-neptune-tools with Apache License 2.0 6 votes vote down vote up
@Override
public void handleStatement(Statement statement) throws RDFHandlerException {

    prefixes.parse(statement.getSubject().stringValue(), this);
    prefixes.parse(statement.getPredicate().toString(), this);
    prefixes.parse(statement.getObject().stringValue(), this);

    Resource context = statement.getContext();
    if (context != null){
        prefixes.parse(context.stringValue(), this);
    }

    writer.startCommit();
    super.handleStatement(statement);
    writer.endCommit();

    status.update();
}
 
Example 3
Source File: Models.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static List<Statement> findMatchingStatements(Statement st, Model model,
		Map<Resource, Resource> bNodeMapping) {
	Resource s = isBlank(st.getSubject()) ? null : st.getSubject();
	IRI p = st.getPredicate();
	Value o = isBlank(st.getObject()) ? null : st.getObject();
	Resource[] g = isBlank(st.getContext()) ? new Resource[0] : new Resource[] { st.getContext() };
	List<Statement> result = new ArrayList<>();

	for (Statement modelSt : model.filter(s, p, o, g)) {
		if (statementsMatch(st, modelSt, bNodeMapping)) {
			// All components possibly match
			result.add(modelSt);
		}
	}

	return Collections.unmodifiableList(result);
}
 
Example 4
Source File: RepositoryConfigUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Deprecated
public static RepositoryConfig getRepositoryConfig(Repository repository, String repositoryID)
		throws RepositoryConfigException, RepositoryException {
	try (RepositoryConnection con = repository.getConnection()) {
		Statement idStatement = getIDStatement(con, repositoryID);
		if (idStatement == null) {
			// No such config
			return null;
		}

		Resource repositoryNode = idStatement.getSubject();
		Resource context = idStatement.getContext();

		if (context == null) {
			throw new RepositoryException("No configuration context for repository " + repositoryID);
		}

		Model contextGraph = QueryResults.asModel(con.getStatements(null, null, null, true, context));

		return RepositoryConfig.create(contextGraph, repositoryNode);
	}
}
 
Example 5
Source File: ConstructTupleFunction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<Value> next() throws QueryEvaluationException {
	if (isClosed()) {
		throw new NoSuchElementException("The iteration has been closed.");
	}
	try {
		Statement stmt = queryResult.next();
		Resource ctx = stmt.getContext();
		if (ctx != null) {
			return Arrays.asList(stmt.getSubject(), stmt.getPredicate(), stmt.getObject(), ctx);
		} else {
			return Arrays.asList(stmt.getSubject(), stmt.getPredicate(), stmt.getObject());
		}
	} catch (NoSuchElementException e) {
		close();
		throw e;
	}
}
 
Example 6
Source File: StatementSerializer.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Write a {@link Statement} to a {@link String}
 *
 * @param statement
 *            the {@link Statement} to write
 * @return a {@link String} representation of the statement
 */
public static String writeStatement(final Statement statement) {
    final Resource subject = statement.getSubject();
    final Resource context = statement.getContext();
    final IRI predicate = statement.getPredicate();
    final Value object = statement.getObject();

    Validate.notNull(subject);
    Validate.notNull(predicate);
    Validate.notNull(object);

    String s = "";
    if (context == null) {
        s = SEP + subject.toString() + SEP + predicate.toString() + SEP + object.toString();
    } else {
        s = context.toString() + SEP + subject.toString() + SEP + predicate.toString() + SEP + object.toString();
    }
    return s;
}
 
Example 7
Source File: ContextAwareConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Removes the supplied statements from a specific context in this repository, ignoring any context information
 * carried by the statements themselves.
 *
 * @param statementIter The statements to remove. In case the iterator is a {@link CloseableIteration}, it will be
 *                      closed before this method returns.
 * @throws RepositoryException If the statements could not be removed from the repository, for example because the
 *                             repository is not writable.
 * @see #getRemoveContexts()
 */
@Override
public <E extends Exception> void remove(Iteration<? extends Statement, E> statementIter, Resource... contexts)
		throws RepositoryException, E {
	final IRI[] removeContexts = getRemoveContexts();
	if (isAllContext(contexts) && removeContexts.length == 1) {
		super.remove(new ConvertingIteration<Statement, Statement, E>(statementIter) {

			@Override
			protected Statement convert(Statement st) {
				if (st.getContext() == null) {
					return getValueFactory().createStatement(st.getSubject(), st.getPredicate(), st.getObject(),
							removeContexts[0]);
				}
				return st;
			}
		});
	} else {
		super.remove(statementIter, contexts);
	}
}
 
Example 8
Source File: AbstractRepositoryConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void addWithoutCommit(Statement st, Resource... contexts) throws RepositoryException {
	if (contexts.length == 0 && st.getContext() != null) {
		contexts = new Resource[] { st.getContext() };
	}

	addWithoutCommit(st.getSubject(), st.getPredicate(), st.getObject(), contexts);
}
 
Example 9
Source File: RDFSailRemover.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handleStatement(Statement st) throws RDFHandlerException {
	Resource subj = st.getSubject();
	IRI pred = st.getPredicate();
	Value obj = st.getObject();
	Resource ctxt = st.getContext();

	try {
		if (enforcesContext()) {
			con.removeStatement(uc, subj, pred, obj, contexts);
		} else {
			if (ctxt == null) {
				final Set<IRI> removeGraphs = uc.getDataset().getDefaultRemoveGraphs();
				if (!removeGraphs.isEmpty()) {
					IRI[] ctxts = removeGraphs.toArray(new IRI[removeGraphs.size()]);
					con.removeStatement(uc, subj, pred, obj, ctxts);
				} else {
					con.removeStatement(uc, subj, pred, obj);
				}
			} else {
				con.removeStatement(uc, subj, pred, obj, ctxt);
			}
		}
	} catch (SailException e) {
		throw new RDFHandlerException(e);
	}
}
 
Example 10
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param whereBinding
 * @param insertClause
 * @throws SailException
 */
private void insertBoundTriples(BindingSet whereBinding, TupleExpr insertClause, UpdateContext uc)
		throws SailException {
	if (insertClause != null) {
		List<StatementPattern> insertPatterns = StatementPatternCollector.process(insertClause);

		// bnodes in the insert pattern are locally scoped for each
		// individual source binding.
		MapBindingSet bnodeMapping = new MapBindingSet();
		for (StatementPattern insertPattern : insertPatterns) {
			Statement toBeInserted = createStatementFromPattern(insertPattern, whereBinding, bnodeMapping);

			if (toBeInserted != null) {
				IRI with = uc.getDataset().getDefaultInsertGraph();
				if (with == null && toBeInserted.getContext() == null) {
					con.addStatement(uc, toBeInserted.getSubject(), toBeInserted.getPredicate(),
							toBeInserted.getObject());
				} else if (toBeInserted.getContext() == null) {
					con.addStatement(uc, toBeInserted.getSubject(), toBeInserted.getPredicate(),
							toBeInserted.getObject(), with);
				} else {
					con.addStatement(uc, toBeInserted.getSubject(), toBeInserted.getPredicate(),
							toBeInserted.getObject(), toBeInserted.getContext());
				}
			}
		}
	}
}
 
Example 11
Source File: Models.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static boolean statementsMatch(Statement st1, Statement st2, Map<Resource, Resource> bNodeMapping) {
	IRI pred1 = st1.getPredicate();
	IRI pred2 = st2.getPredicate();

	if (!pred1.equals(pred2)) {
		// predicates don't match
		return false;
	}

	Resource subj1 = st1.getSubject();
	Resource subj2 = st2.getSubject();

	if (bnodeValueMatching(bNodeMapping, subj1, subj2)) {
		return false;
	}

	Value obj1 = st1.getObject();
	Value obj2 = st2.getObject();

	if (bnodeValueMatching(bNodeMapping, obj1, obj2)) {
		return false;
	}

	Resource context1 = st1.getContext();
	Resource context2 = st2.getContext();

	// no match if in different contexts
	if (context1 == null) {
		return context2 == null;
	} else if (context2 == null) {
		return false;
	}

	if (bnodeValueMatching(bNodeMapping, context1, context2)) {
		return false;
	}

	return true;
}
 
Example 12
Source File: AbstractRDFInserter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handleStatement(Statement st) throws RDFHandlerException {
	Resource subj = st.getSubject();
	IRI pred = st.getPredicate();
	Value obj = st.getObject();
	Resource ctxt = st.getContext();

	if (!preserveBNodeIDs) {
		if (subj instanceof BNode) {
			subj = mapBNode((BNode) subj);
		}

		if (obj instanceof BNode) {
			obj = mapBNode((BNode) obj);
		}

		if (!enforcesContext() && ctxt instanceof BNode) {
			ctxt = mapBNode((BNode) ctxt);
		}
	}

	try {
		addStatement(subj, pred, obj, ctxt);
	} catch (RDF4JException e) {
		throw new RDFHandlerException(e);
	}
}
 
Example 13
Source File: ContextAwareConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void add(Statement st, Resource... contexts) throws RepositoryException {
	if (isNilContext(contexts) && st.getContext() == null) {
		super.add(st, getAddContexts());
	} else {
		super.add(st, contexts);
	}
}
 
Example 14
Source File: RdfToRyaConversions.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link Statement} into a {@link RyaStatement} representation
 * of the {@code statement}.
 * @param statement the {@link Statement} to convert.
 * @return the {@link RyaStatement} representation of the {@code statement}.
 */
public static RyaStatement convertStatement(final Statement statement) {
    if (statement == null) {
        return null;
    }
    final Resource subject = statement.getSubject();
    final IRI predicate = statement.getPredicate();
    final Value object = statement.getObject();
    final Resource context = statement.getContext();
    return new RyaStatement(
            convertResource(subject),
            convertIRI(predicate),
            convertValue(object),
            convertResource(context));
}
 
Example 15
Source File: RyaDaoQueryWrapper.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Handles only the first result of a query. Closes the query iterator when
 * done.
 * @param statement the {@link Statement} to query for. (not {@code null})
 * @param rdfStatementHandler the {@link RDFHandler} to use for handling the
 * first statement returned. (not {@code null})
 * @throws QueryEvaluationException
 */
public void queryFirst(final Statement statement, final RDFHandler rdfStatementHandler) throws QueryEvaluationException {
    checkNotNull(statement);
    final Resource subject = statement.getSubject();
    final IRI predicate = statement.getPredicate();
    final Value object = statement.getObject();
    final Resource context = statement.getContext();
    queryFirst(subject, predicate, object, rdfStatementHandler, context);
}
 
Example 16
Source File: GraphToBindingSetConversionIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected BindingSet convert(Statement st) {
	QueryBindingSet result = new QueryBindingSet();
	result.addBinding("subject", st.getSubject());
	result.addBinding("predicate", st.getPredicate());
	result.addBinding("object", st.getObject());
	if (st.getContext() != null) {
		result.addBinding("context", st.getContext());
	}

	return result;
}
 
Example 17
Source File: Transaction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void handleStatement(Statement st) throws RDFHandlerException {
	Resource subject = SESAME.WILDCARD.equals(st.getSubject()) ? null : st.getSubject();
	IRI predicate = SESAME.WILDCARD.equals(st.getPredicate()) ? null : st.getPredicate();
	Value object = SESAME.WILDCARD.equals(st.getObject()) ? null : st.getObject();

	// use the RepositoryConnection.clear operation if we're removing
	// all statements
	final boolean clearAllTriples = subject == null && predicate == null && object == null;

	try {
		Resource context = st.getContext();
		if (context != null) {
			if (clearAllTriples) {
				conn.clear(context);
			} else {
				conn.remove(subject, predicate, object, context);
			}
		} else {
			if (clearAllTriples) {
				conn.clear();
			} else {
				conn.remove(subject, predicate, object);
			}
		}
	} catch (RepositoryException e) {
		throw new RDFHandlerException(e);
	}
}
 
Example 18
Source File: TriXWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void consumeStatement(Statement st) {
	try {
		Resource context = st.getContext();

		if (inActiveContext && !contextsEquals(context, currentContext)) {
			// Close currently active context
			xmlWriter.endTag(CONTEXT_TAG);
			inActiveContext = false;
		}

		if (!inActiveContext) {
			// Open new context
			xmlWriter.startTag(CONTEXT_TAG);

			if (context != null) {
				writeValue(context);
			}

			currentContext = context;
			inActiveContext = true;
		}

		xmlWriter.startTag(TRIPLE_TAG);

		writeValue(st.getSubject());
		writeValue(st.getPredicate());
		writeValue(st.getObject());

		xmlWriter.endTag(TRIPLE_TAG);
	} catch (IOException e) {
		throw new RDFHandlerException(e);
	}
}
 
Example 19
Source File: GraphToBindingSetConversionIteration.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
protected BindingSet convert(Statement st) {
	QueryBindingSet result = new QueryBindingSet();
	result.addBinding("subject", st.getSubject());
	result.addBinding("predicate", st.getPredicate());
	result.addBinding("object", st.getObject());
	if (st.getContext() != null) {
		result.addBinding("context", st.getContext());
	}

	return result;
}
 
Example 20
Source File: ArrangedWriter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private SubjectInContext(Statement st) {
	this(st.getSubject(), st.getContext());
}