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; 031 032/** 033 * Marker interface for RDF4J implementations of Graph. 034 * 035 * @see RDF4J#createGraph() 036 * @see RDF4J#asGraph(Model) 037 * @see RDF4J#asGraph(Repository, Option...) 038 * @see RDF4J#asGraphUnion(Repository, Option...) 039 * @see RDF4JDataset#getGraph() 040 * @see RDF4JDataset#getGraph(BlankNodeOrIRI) 041 */ 042public interface RDF4JGraph extends Graph, RDF4JGraphLike<Triple> { 043 044 /** 045 * Return a copy of the context mask as a {@link Set} of 046 * {@link RDF4JBlankNodeOrIRI} graph names. 047 * <p> 048 * If the set is not {@link Set#isEmpty()}, the mask determines which 049 * <em>contexts</em> in the corresponding RDF4J {@link Model} or 050 * {@link Repository} that this graph reflect. Modifications to the graph 051 * (e.g. {@link #add(Triple)} will be performed for all the specified 052 * contexts, while retrieval (e.g. {@link #contains(Triple)}) will succeed 053 * if the triple is in at least one of the specified contexts. 054 * <p> 055 * The context mask array may contain <code>null</code>, indicating the 056 * default context (the <em>default graph</em> in RDF datasets). 057 * <p> 058 * If the context mask is {@link Set#isEmpty()}, then this is a <em>union 059 * graph</em> which triples reflect statements in any contexts. Triples 060 * added to the graph will be added in the default context, e.g. equivalent 061 * to <code>new Resource[1]{null}</code>) in RDF4J. 062 * <p> 063 * Note that the context mask itself cannot be <code>null</code>. 064 * <p> 065 * The returned set is an immutable copy; to specify a different mask, use 066 * {@link RDF4J#asGraph(Repository, Set, Option...)} 067 * 068 * @return The context mask as a set of {@link BlankNodeOrIRI} graph names, 069 * which may contain the value <code>null</code>. 070 */ 071 Set<RDF4JBlankNodeOrIRI> getContextMask(); 072 073 /** 074 * {@inheritDoc} 075 * <p> 076 * Note that for graphs backed by a repository ({@link #asRepository()} is 077 * present), the stream <strong>must be closed</strong> with 078 * {@link Stream#close()}. 079 * <p> 080 * This can generally achieved using a try-with-resources block, e.g.: 081 * 082 * <pre> 083 * int subjects; 084 * try (Stream<RDF4JTriple> s : graph.stream()) { 085 * subjects = s.map(RDF4JTriple::getSubject).distinct().count() 086 * } 087 * </pre> 088 */ 089 @Override 090 Stream<RDF4JTriple> stream(); 091 092 /** 093 * {@inheritDoc} 094 * <p> 095 * Note that for graphs backed by a repository ({@link #asRepository()} is 096 * present), the stream <strong>must be closed</strong> with 097 * {@link Stream#close()}. 098 * <p> 099 * This can generally achieved using a try-with-resources block, e.g.: 100 * 101 * <pre> 102 * int subjects; 103 * try (Stream<RDF4JTriple> s : graph.stream(s,p,o)) { 104 * subjects = s.map(RDF4JTriple::getSubject).distinct().count() 105 * } 106 * </pre> 107 */ 108 @Override 109 Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, IRI predicate, RDFTerm object); 110 111 /** 112 * {@inheritDoc} 113 * <p> 114 * Note that for graphs backed by a repository ({@link #asRepository()} is 115 * present), the iterable <strong>must be closed</strong> with 116 * {@link ClosableIterable#close()}. 117 * <p> 118 * This can generally achieved using a try-with-resources block, e.g.: 119 * 120 * <pre> 121 * try (ClosableIterable<Triple> s : graph.iterate()) { 122 * for (Triple t : triples) { 123 * return t; // OK to terminate for-loop early 124 * } 125 * } 126 * </pre> 127 * 128 * If you don't use a try-with-resources block, the iterator will attempt to 129 * close the ClosableIterable when reaching the end of the iteration. 130 */ 131 @Override 132 ClosableIterable<Triple> iterate() throws ConcurrentModificationException, IllegalStateException; 133 134 /** 135 * {@inheritDoc} 136 * <p> 137 * Note that for graphs backed by a repository ({@link #asRepository()} is 138 * present), the iterable <strong>must be closed</strong> with 139 * {@link ClosableIterable#close()}. 140 * <p> 141 * This can generally achieved using a try-with-resources block, e.g.: 142 * 143 * <pre> 144 * try (ClosableIterable<Triple> s : graph.iterate(s,p,o)) { 145 * for (Triple t : triples) { 146 * return t; // OK to terminate for-loop early 147 * } 148 * } 149 * </pre> 150 * 151 * If you don't use a try-with-resources block, the iterator will attempt to 152 * close the ClosableIterable when reaching the end of the iteration. 153 */ 154 @Override 155 ClosableIterable<Triple> iterate(BlankNodeOrIRI subject, IRI predicate, RDFTerm object); 156}