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.Optional; 022import java.util.function.Predicate; 023import java.util.stream.Collectors; 024import java.util.stream.Stream; 025 026import org.apache.commons.rdf.api.BlankNodeOrIRI; 027import org.apache.commons.rdf.api.Dataset; 028import org.apache.commons.rdf.api.Graph; 029import org.apache.commons.rdf.api.IRI; 030import org.apache.commons.rdf.api.Quad; 031import org.apache.commons.rdf.api.RDFTerm; 032 033import com.github.jsonldjava.core.RDFDataset; 034 035public interface JsonLdDataset extends JsonLdGraphLike<org.apache.commons.rdf.api.Quad>, Dataset { 036} 037 038class JsonLdDatasetImpl extends AbstractJsonLdGraphLike<org.apache.commons.rdf.api.Quad> implements JsonLdDataset { 039 040 JsonLdDatasetImpl(final RDFDataset rdfDataSet) { 041 super(rdfDataSet); 042 } 043 044 JsonLdDatasetImpl(final RDFDataset rdfDataset, final String bnodePrefix) { 045 super(rdfDataset, bnodePrefix); 046 } 047 048 JsonLdDatasetImpl(final String bnodePrefix) { 049 super(bnodePrefix); 050 } 051 052 @Override 053 public void add(final BlankNodeOrIRI graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { 054 super.add(graphName, subject, predicate, object); 055 } 056 057 @Override 058 public boolean contains(final Optional<BlankNodeOrIRI> graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { 059 return super.contains(graphName, subject, predicate, object); 060 } 061 062 @Override 063 public Graph getGraph() { 064 return new JsonLdGraphImpl(rdfDataSet, Optional.empty(), bnodePrefix); 065 } 066 067 @Override 068 public Optional<Graph> getGraph(final BlankNodeOrIRI graphName) { 069 if (graphName == null) { 070 return Optional.of(getGraph()); 071 } 072 073 return Optional.of(new JsonLdGraphImpl(rdfDataSet, Optional.of(graphName), bnodePrefix)); 074 } 075 076 @Override 077 public Stream<BlankNodeOrIRI> getGraphNames() { 078 return rdfDataSet.graphNames().parallelStream().filter(Predicate.isEqual("@default").negate()) 079 .map(s -> s.startsWith("_:") ? new RDFDataset.BlankNode(s) : new RDFDataset.IRI(s)) 080 .map(n -> (BlankNodeOrIRI) factory.asRDFTerm(n)); 081 } 082 083 @Override 084 public void remove(final Optional<BlankNodeOrIRI> graphName, final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { 085 super.remove(graphName, subject, predicate, object); 086 } 087 088 @Override 089 public void remove(final Quad q) { 090 remove(q.getGraphName(), q.getSubject(), q.getPredicate(), q.getObject()); 091 } 092 093 @Override 094 public Stream<? extends Quad> stream(final Optional<BlankNodeOrIRI> graphName, final BlankNodeOrIRI subject, final IRI predicate, 095 final RDFTerm object) { 096 return filteredGraphs(graphName).flatMap(List::stream).filter(quadFilter(subject, predicate, object)) 097 .map(factory::asQuad); 098 } 099 100 @Override 101 public long size() { 102 return rdfDataSet.graphNames().stream().map(rdfDataSet::getQuads) 103 .collect(Collectors.summingLong(List::size)); 104 } 105 106 @Override 107 Quad asTripleOrQuad(final com.github.jsonldjava.core.RDFDataset.Quad jsonldQuad) { 108 return factory.asQuad(jsonldQuad); 109 } 110 111}