| 1 |
/** |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.apache.commons.rdf.simple; |
| 19 |
|
| 20 |
import org.apache.commons.rdf.api.*; |
| 21 |
import org.apache.commons.rdf.simple.SimpleRDF.SimpleRDFTerm; |
| 22 |
|
| 23 |
import java.util.HashSet; |
| 24 |
import java.util.Set; |
| 25 |
import java.util.function.Predicate; |
| 26 |
import java.util.stream.Collectors; |
| 27 |
import java.util.stream.Stream; |
| 28 |
|
| 29 |
/** |
| 30 |
* A simple, memory-based implementation of Graph. |
| 31 |
* <p> |
| 32 |
* {@link Triple}s in the graph are kept in a {@link Set}. |
| 33 |
* <p> |
| 34 |
* All Stream operations are performed using parallel and unordered directives. |
| 35 |
*/ |
| 36 |
final class GraphImpl implements Graph { |
| 37 |
|
| 38 |
private static final int TO_STRING_MAX = 10; |
| 39 |
private final Set<Triple> triples = new HashSet<>(); |
| 40 |
private final SimpleRDF factory; |
| 41 |
|
| 42 |
GraphImpl(final SimpleRDF simpleRDF) { |
| 43 |
this.factory = simpleRDF; |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public void add(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { |
| 48 |
final BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject); |
| 49 |
final IRI newPredicate = (IRI) internallyMap(predicate); |
| 50 |
final RDFTerm newObject = internallyMap(object); |
| 51 |
final Triple result = factory.createTriple(newSubject, newPredicate, newObject); |
| 52 |
triples.add(result); |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void add(final Triple triple) { |
| 57 |
triples.add(internallyMap(triple)); |
| 58 |
} |
| 59 |
|
| 60 |
private <T extends RDFTerm> RDFTerm internallyMap(final T object) { |
| 61 |
if (object == null || object instanceof SimpleRDFTerm) { |
| 62 |
// No need to re-map our own objects. |
| 63 |
// We support null as internallyMap() is also used by the filters, |
| 64 |
// and the |
| 65 |
// factory constructors later do null checks |
| 66 |
return object; |
| 67 |
} |
| 68 |
if (object instanceof BlankNode) { |
| 69 |
final BlankNode blankNode = (BlankNode) object; |
| 70 |
// This guarantees that adding the same BlankNode multiple times to |
| 71 |
// this graph will generate a local object that is mapped to an |
| 72 |
// equivalent object, based on the code in the package private |
| 73 |
// BlankNodeImpl class |
| 74 |
return factory.createBlankNode(blankNode.uniqueReference()); |
| 75 |
} else if (object instanceof IRI) { |
| 76 |
final IRI iri = (IRI) object; |
| 77 |
return factory.createIRI(iri.getIRIString()); |
| 78 |
} else if (object instanceof Literal) { |
| 79 |
final Literal literal = (Literal) object; |
| 80 |
if (literal.getLanguageTag().isPresent()) { |
| 81 |
return factory.createLiteral(literal.getLexicalForm(), literal.getLanguageTag().get()); |
| 82 |
} |
| 83 |
return factory.createLiteral(literal.getLexicalForm(), (IRI) internallyMap(literal.getDatatype())); |
| 84 |
} else { |
| 85 |
throw new IllegalArgumentException("RDFTerm was neither a BlankNode, IRI nor Literal: " + object); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private Triple internallyMap(final Triple triple) { |
| 90 |
final BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(triple.getSubject()); |
| 91 |
final IRI newPredicate = (IRI) internallyMap(triple.getPredicate()); |
| 92 |
final RDFTerm newObject = internallyMap(triple.getObject()); |
| 93 |
// Check if any of the object references changed during the mapping, to |
| 94 |
// avoid creating a new Triple object if possible |
| 95 |
if (newSubject == triple.getSubject() && newPredicate == triple.getPredicate() |
| 96 |
&& newObject == triple.getObject()) { |
| 97 |
return triple; |
| 98 |
} |
| 99 |
return factory.createTriple(newSubject, newPredicate, newObject); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void clear() { |
| 104 |
triples.clear(); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public boolean contains(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { |
| 109 |
return stream(subject, predicate, object).findFirst().isPresent(); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public boolean contains(final Triple triple) { |
| 114 |
return triples.contains(internallyMap(triple)); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public Stream<Triple> stream() { |
| 119 |
return triples.parallelStream().unordered(); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public Stream<Triple> stream(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { |
| 124 |
final BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject); |
| 125 |
final IRI newPredicate = (IRI) internallyMap(predicate); |
| 126 |
final RDFTerm newObject = internallyMap(object); |
| 127 |
|
| 128 |
return getTriples(t -> { |
| 129 |
// Lacking the requirement for .equals() we have to be silly |
| 130 |
// and test ntriples string equivalance |
| 131 |
if (subject != null && !t.getSubject().equals(newSubject)) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
if (predicate != null && !t.getPredicate().equals(newPredicate)) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
if (object != null && !t.getObject().equals(newObject)) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
return true; |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
private Stream<Triple> getTriples(final Predicate<Triple> filter) { |
| 145 |
return stream().filter(filter); |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public void remove(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { |
| 150 |
final Stream<Triple> toRemove = stream(subject, predicate, object); |
| 151 |
for (final Triple t : toRemove.collect(Collectors.toList())) { |
| 152 |
// Avoid ConcurrentModificationException in ArrayList |
| 153 |
remove(t); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void remove(final Triple triple) { |
| 159 |
triples.remove(internallyMap(triple)); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public long size() { |
| 164 |
return triples.size(); |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public String toString() { |
| 169 |
final String s = stream().limit(TO_STRING_MAX).map(Object::toString).collect(Collectors.joining("\n")); |
| 170 |
if (size() > TO_STRING_MAX) { |
| 171 |
return s + "\n# ... +" + (size() - TO_STRING_MAX) + " more"; |
| 172 |
} |
| 173 |
return s; |
| 174 |
} |
| 175 |
|
| 176 |
} |