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.Objects; 022import java.util.Optional; 023import java.util.stream.Stream; 024 025import org.apache.commons.rdf.api.BlankNodeOrIRI; 026import org.apache.commons.rdf.api.Graph; 027import org.apache.commons.rdf.api.IRI; 028import org.apache.commons.rdf.api.RDFTerm; 029import org.apache.commons.rdf.api.Triple; 030 031import com.github.jsonldjava.core.RDFDataset; 032 033/** 034 * A {@link Graph} view of a JsonLd {@link RDFDataset}. 035 * 036 */ 037public interface JsonLdGraph extends JsonLdGraphLike<Triple>, Graph { 038} 039 040class JsonLdGraphImpl extends AbstractJsonLdGraphLike<Triple> implements JsonLdGraph { 041 042 private final Optional<BlankNodeOrIRI> graphName; 043 044 JsonLdGraphImpl(final RDFDataset rdfDataSet) { 045 super(rdfDataSet); 046 this.graphName = Optional.empty(); 047 } 048 049 JsonLdGraphImpl(final RDFDataset rdfDataSet, final Optional<BlankNodeOrIRI> graphName, final String bnodePrefix) { 050 super(rdfDataSet, bnodePrefix); 051 this.graphName = Objects.requireNonNull(graphName); 052 } 053 054 JsonLdGraphImpl(final String bnodePrefix) { 055 super(bnodePrefix); 056 this.graphName = Optional.empty(); 057 } 058 059 @Override 060 public void clear() { 061 filteredGraphs(graphName).forEach(l -> l.clear()); 062 } 063 064 @Override 065 public void add(final Triple t) { 066 // Ensure it's added in the correct graph 067 super.add(graphName.orElse(null), t.getSubject(), t.getPredicate(), t.getObject()); 068 } 069 070 @Override 071 public void add(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { 072 super.add(graphName.orElse(null), subject, predicate, object); 073 } 074 075 @Override 076 public boolean contains(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { 077 return super.contains(graphName, subject, predicate, object); 078 } 079 080 @Override 081 public boolean contains(final Triple t) { 082 return contains(graphName, t.getSubject(), t.getPredicate(), t.getObject()); 083 } 084 085 @Override 086 public void remove(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { 087 super.remove(graphName, subject, predicate, object); 088 } 089 090 @Override 091 public void remove(final Triple t) { 092 // Only remove from the particular graph 093 remove(graphName, t.getSubject(), t.getPredicate(), t.getObject()); 094 } 095 096 @Override 097 public long size() { 098 final String g = graphName.map(factory::asJsonLdString).orElse("@default"); 099 return Optional.ofNullable(rdfDataSet.getQuads(g)).map(List::size).orElse(0); 100 } 101 102 @Override 103 public Stream<JsonLdTriple> stream(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { 104 return filteredGraphs(graphName).flatMap(List::stream).filter(quadFilter(subject, predicate, object)) 105 .map(factory::asTriple); 106 } 107 108 @Override 109 JsonLdTriple asTripleOrQuad(final com.github.jsonldjava.core.RDFDataset.Quad jsonldQuad) { 110 return factory.asTriple(jsonldQuad); 111 } 112}