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.jsonldjava;
019
020import java.util.List;
021import java.util.stream.Stream;
022
023import org.apache.commons.rdf.api.BlankNodeOrIRI;
024import org.apache.commons.rdf.api.Graph;
025import org.apache.commons.rdf.api.IRI;
026import org.apache.commons.rdf.api.RDFTerm;
027import org.apache.commons.rdf.api.Triple;
028
029import com.github.jsonldjava.core.RDFDataset;
030
031/**
032 * A <strong>union graph</strong> representation of a JsonLd {@link RDFDataset}.
033 * <p>
034 * A union graph contains all the triples of the dataset, irregardless of their
035 * graph names.
036 * <p>
037 * {@link #add(Triple)} and {@link #add(BlankNodeOrIRI, IRI, RDFTerm)} will add
038 * the triple to the default graph (e.g. <code>@default</code> in JSON-LD),
039 * while the remaining methods (including {@link #remove(Triple)} or
040 * {@link #remove(BlankNodeOrIRI, IRI, RDFTerm)}) relate to triples from
041 * <strong>all</strong> graphs.
042 * <p>
043 * <strong>Note:</strong> Some operations like {@link #stream()} and
044 * {@link #size()} are inefficient as they skip any duplicate triples from
045 * multiple graphs.
046 */
047public interface JsonLdUnionGraph extends JsonLdGraphLike<org.apache.commons.rdf.api.Triple>, Graph {
048}
049
050class JsonLdUnionGraphImpl extends AbstractJsonLdGraphLike<org.apache.commons.rdf.api.Triple>
051        implements JsonLdUnionGraph {
052
053    JsonLdUnionGraphImpl(final String bnodePrefix) {
054        super(bnodePrefix);
055    }
056
057    JsonLdUnionGraphImpl(final RDFDataset rdfDataSet) {
058        super(rdfDataSet);
059    }
060
061    JsonLdUnionGraphImpl(final RDFDataset rdfDataSet, final String bnodePrefix) {
062        super(rdfDataSet, bnodePrefix);
063    }
064
065    @Override
066    public void add(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
067        super.add(null, subject, predicate, object);
068    }
069
070    @Override
071    public boolean contains(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
072        return super.contains(null, subject, predicate, object);
073    }
074
075    @Override
076    public void remove(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
077        super.remove(null, subject, predicate, object);
078    }
079
080    @Override
081    public void remove(final Triple t) {
082        // Remove from ALL graphs, not just default graph
083        super.remove(null, t.getSubject(), t.getPredicate(), t.getObject());
084    }
085
086    @Override
087    public Stream<JsonLdTriple> stream(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
088        return filteredGraphs(null).flatMap(List::stream).filter(quadFilter(subject, predicate, object))
089                .map(factory::asTriple)
090                // Make sure we don't have duplicate triples
091                // NOTE: This can be quite inefficient
092                .distinct();
093    }
094
095    @Override
096    public Stream<? extends Triple> stream() {
097        // NOTE: inefficient as we have to remove duplicate triples
098        // in different graphs :-(
099        return super.stream().distinct();
100    }
101
102    @Override
103    JsonLdTriple asTripleOrQuad(final com.github.jsonldjava.core.RDFDataset.Quad jsonldQuad) {
104        return factory.asTriple(jsonldQuad);
105    }
106
107    @Override
108    public long size() {
109        // Note: Our specialized stream() already removes duplicates using
110        // .distinct()
111        return stream().count();
112    }
113}