001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.commons.rdf.rdf4j;
019
020import java.util.ConcurrentModificationException;
021import java.util.Set;
022import java.util.stream.Stream;
023
024import org.apache.commons.rdf.api.BlankNodeOrIRI;
025import org.apache.commons.rdf.api.Graph;
026import org.apache.commons.rdf.api.IRI;
027import org.apache.commons.rdf.api.RDFTerm;
028import org.apache.commons.rdf.api.Triple;
029import org.eclipse.rdf4j.model.Model;
030import org.eclipse.rdf4j.repository.Repository;
031import org.apache.commons.rdf.rdf4j.RDF4J.Option;
032
033/**
034 * Marker interface for RDF4J implementations of Graph.
035 *
036 * @see RDF4J#createGraph()
037 * @see RDF4J#asGraph(Model)
038 * @see RDF4J#asGraph(Repository, Option...)
039 * @see RDF4J#asGraphUnion(Repository, Option...)
040 * @see RDF4JDataset#getGraph()
041 * @see RDF4JDataset#getGraph(BlankNodeOrIRI)
042 */
043public interface RDF4JGraph extends Graph, RDF4JGraphLike<Triple> {
044
045    /**
046     * Return a copy of the context mask as a {@link Set} of
047     * {@link RDF4JBlankNodeOrIRI} graph names.
048     * <p>
049     * If the set is not {@link Set#isEmpty()}, the mask determines which
050     * <em>contexts</em> in the corresponding RDF4J {@link Model} or
051     * {@link Repository} that this graph reflect. Modifications to the graph
052     * (e.g. {@link #add(Triple)} will be performed for all the specified
053     * contexts, while retrieval (e.g. {@link #contains(Triple)}) will succeed
054     * if the triple is in at least one of the specified contexts.
055     * <p>
056     * The context mask array may contain <code>null</code>, indicating the
057     * default context (the <em>default graph</em> in RDF datasets).
058     * <p>
059     * If the context mask is {@link Set#isEmpty()}, then this is a <em>union
060     * graph</em> which triples reflect statements in any contexts. Triples
061     * added to the graph will be added in the default context, e.g. equivalent
062     * to <code>new Resource[1]{null}</code>) in RDF4J.
063     * <p>
064     * Note that the context mask itself cannot be <code>null</code>.
065     * <p>
066     * The returned set is an immutable copy; to specify a different mask, use
067     * {@link RDF4J#asGraph(Repository, Set, Option...)}
068     *
069     * @return The context mask as a set of {@link BlankNodeOrIRI} graph names,
070     *         which may contain the value <code>null</code>.
071     */
072    public Set<RDF4JBlankNodeOrIRI> getContextMask();
073
074    /**
075     * {@inheritDoc}
076     * <p>
077     * Note that for graphs backed by a repository ({@link #asRepository()} is
078     * present), the stream <strong>must be closed</strong> with
079     * {@link Stream#close()}.
080     * <p>
081     * This can generally achieved using a try-with-resources block, e.g.:
082     *
083     * <pre>
084     * int subjects;
085     * try (Stream&lt;RDF4JTriple&gt; s : graph.stream()) {
086     *   subjects = s.map(RDF4JTriple::getSubject).distinct().count()
087     * }
088     * </pre>
089     */
090    @Override
091    Stream<RDF4JTriple> stream();
092
093    /**
094     * {@inheritDoc}
095     * <p>
096     * Note that for graphs backed by a repository ({@link #asRepository()} is
097     * present), the stream <strong>must be closed</strong> with
098     * {@link Stream#close()}.
099     * <p>
100     * This can generally achieved using a try-with-resources block, e.g.:
101     *
102     * <pre>
103     * int subjects;
104     * try (Stream&lt;RDF4JTriple&gt; s : graph.stream(s,p,o)) {
105     *   subjects = s.map(RDF4JTriple::getSubject).distinct().count()
106     * }
107     * </pre>
108     */
109    @Override
110    Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, IRI predicate, RDFTerm object);
111
112    /**
113     * {@inheritDoc}
114     * <p>
115     * Note that for graphs backed by a repository ({@link #asRepository()} is
116     * present), the iterable <strong>must be closed</strong> with
117     * {@link ClosableIterable#close()}.
118     * <p>
119     * This can generally achieved using a try-with-resources block, e.g.:
120     *
121     * <pre>
122     * try (ClosableIterable&lt;Triple&gt; s : graph.iterate()) {
123     *   for (Triple t : triples) {
124     *       return t; // OK to terminate for-loop early
125     *   }
126     * }
127     * </pre>
128     *
129     * If you don't use a try-with-resources block, the iterator will attempt to
130     * close the ClosableIterable when reaching the end of the iteration.
131     */
132    @Override
133    ClosableIterable<Triple> iterate() throws ConcurrentModificationException, IllegalStateException;
134
135    /**
136     * {@inheritDoc}
137     * <p>
138     * Note that for graphs backed by a repository ({@link #asRepository()} is
139     * present), the iterable <strong>must be closed</strong> with
140     * {@link ClosableIterable#close()}.
141     * <p>
142     * This can generally achieved using a try-with-resources block, e.g.:
143     *
144     * <pre>
145     * try (ClosableIterable&lt;Triple&gt; s : graph.iterate(s,p,o)) {
146     *   for (Triple t : triples) {
147     *       return t; // OK to terminate for-loop early
148     *   }
149     * }
150     * </pre>
151     *
152     * If you don't use a try-with-resources block, the iterator will attempt to
153     * close the ClosableIterable when reaching the end of the iteration.
154     */
155    @Override
156    ClosableIterable<Triple> iterate(BlankNodeOrIRI subject, IRI predicate, RDFTerm object);
157}