commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [07/19] incubator-commonsrdf git commit: Added DatasetImpl, QuadImpl
Date Mon, 05 Sep 2016 15:53:51 GMT
Added DatasetImpl, QuadImpl


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/683a2bbd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/683a2bbd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/683a2bbd

Branch: refs/heads/master
Commit: 683a2bbdf7539772950d4eaaa0018745eaf6cc2d
Parents: 8ee8e2c
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Apr 8 16:21:49 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Apr 8 16:21:49 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/simple/DatasetImpl.java  | 223 +++++++++++++++++++
 .../org/apache/commons/rdf/simple/QuadImpl.java | 109 +++++++++
 .../rdf/simple/SimpleRDFTermFactory.java        |  13 ++
 3 files changed, 345 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/683a2bbd/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java
new file mode 100644
index 0000000..64cf57d
--- /dev/null
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java
@@ -0,0 +1,223 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rdf.simple;
+
+import org.apache.commons.rdf.api.*;
+import org.apache.commons.rdf.simple.SimpleRDFTermFactory.SimpleRDFTerm;
+
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * A simple, memory-based implementation of Dataset.
+ * <p>
+ * {@link Quad}s in the graph are kept in a {@link Set}.
+ * <p>
+ * All Stream operations are performed using parallel and unordered directives.
+ */
+final class DatasetImpl implements Dataset {
+	
+    private static final int TO_STRING_MAX = 10;
+    private final Set<Quad> quads = new HashSet<Quad>();
+    private final SimpleRDFTermFactory factory;
+
+    DatasetImpl(SimpleRDFTermFactory simpleRDFTermFactory) {
+        this.factory = simpleRDFTermFactory;
+    }
+
+    @Override
+    public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+    	// TODO: Should this method be promoted as a default method in Dataset?     	    	 
  	
+    	add(null, subject, predicate, object);
+    }
+    
+	@Override
+	public void add(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm
object) {
+		BlankNodeOrIRI newGraphName = (BlankNodeOrIRI) internallyMap(graphName);
+		BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject);
+        IRI newPredicate = (IRI) internallyMap(predicate);
+        RDFTerm newObject = internallyMap(object);
+        Quad result = factory.createQuad(newGraphName, newSubject, newPredicate, newObject);
+        quads.add(result);
+	}    
+
+    @Override
+    public void add(Quad quad) {    	
+    	BlankNodeOrIRI newGraph = (BlankNodeOrIRI) internallyMap(quad.getGraphName().orElse(null));
+    	BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(quad
+                .getSubject());        
+        IRI newPredicate = (IRI) internallyMap(quad.getPredicate());
+        RDFTerm newObject = internallyMap(quad.getObject());
+        // Check if any of the object references changed during the mapping, to
+        // avoid creating a new Quad object if possible
+        if (newGraph == quad.getGraphName().orElse(null) 
+        	&& newSubject == quad.getSubject()
+            && newPredicate == quad.getPredicate()
+            && newObject == quad.getObject()) {
+            quads.add(quad);
+        } else {
+        	// Make a new Quad with our mapped instances
+            Quad result = factory.createQuad(newGraph, newSubject, newPredicate,
+                    newObject);
+            quads.add(result);
+        }
+    }
+
+    private <T extends RDFTerm> RDFTerm internallyMap(T object) {
+    	if (object == null || object instanceof SimpleRDFTerm) {
+    		return object;
+    	}    	
+        if (object instanceof BlankNode && !(object instanceof BlankNodeImpl)) {
+            BlankNode blankNode = (BlankNode) object;
+            // This guarantees that adding the same BlankNode multiple times to
+            // this graph will generate a local object that is mapped to an
+            // equivalent object, based on the code in the package private
+            // BlankNodeImpl class
+            return factory.createBlankNode(blankNode.uniqueReference());
+        } else if (object instanceof IRI && !(object instanceof IRIImpl)) {
+            IRI iri = (IRI) object;
+            return factory.createIRI(iri.getIRIString());
+        } else if (object instanceof Literal
+                && !(object instanceof LiteralImpl)) {
+            Literal literal = (Literal) object;
+            if (literal.getLanguageTag().isPresent()) {
+                return factory.createLiteral(literal.getLexicalForm(), literal
+                        .getLanguageTag().get());
+            } else {
+                return factory.createLiteral(literal.getLexicalForm(),
+                        (IRI) internallyMap(literal.getDatatype()));
+            }
+        } else {
+        	throw new IllegalArgumentException("Not a BlankNode, IRI or Literal: " + object);
+        }
+    }
+
+    @Override
+    public void clear() {
+        quads.clear();
+    }
+
+    @Override
+    public boolean contains(BlankNodeOrIRI subject, IRI predicate,
+                            RDFTerm object) {
+    	// TODO: Should this method be promoted as a default method in Dataset?     	    	
+        return getQuads(Optional.empty(), subject, predicate, object).findAny().isPresent();
+    }
+
+	@Override
+	public boolean contains(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject,
IRI predicate, RDFTerm object) {
+		return getQuads(graphName, subject, predicate, object).findAny().isPresent();
+	}
+    
+    @Override
+    public boolean contains(Quad quad) {
+        return quads.contains(Objects.requireNonNull(quad));
+    }
+    
+	@Override
+	public Stream<Quad> getQuads() {
+        return quads.parallelStream().unordered();
+	}
+
+	@Override
+	public Stream<Quad> getQuads(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate,
+			RDFTerm object) {
+		final Optional<BlankNodeOrIRI> newGraphName = graphName.map(g -> (BlankNodeOrIRI)internallyMap(g));
+        final BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject);
+        final IRI newPredicate = (IRI) internallyMap(predicate);
+        final RDFTerm newObject = internallyMap(object);
+
+        return getQuads(t -> {
+            if (newGraphName != null && !t.getGraphName().equals(newGraphName)) {
+            	// This would check Optional.empty() == Optional.empty()
+                return false;
+            }        	
+            if (subject != null && !t.getSubject().equals(newSubject)) {
+                return false;
+            }
+            if (predicate != null && !t.getPredicate().equals(newPredicate)) {
+                return false;
+            }
+            if (object != null && !t.getObject().equals(newObject)) {
+                return false;
+            }
+            return true;
+        });
+	}    
+
+    @Override
+    public Stream<Triple> getTriples() {
+    	// TODO: Should this method be promoted as a default method in Dataset?     	
+    	return getQuads(Optional.empty(), null, null, null).map(Quad::asTriple);
+    }
+
+    @Override
+    public Stream<Triple> getTriples(final BlankNodeOrIRI subject,
+                                     final IRI predicate, final RDFTerm object) {
+    	// TODO: Should this method be promoted as a default method in Dataset?     	
+    	return getQuads(Optional.empty(), subject, predicate, object).map(Quad::asTriple);
+    }
+
+    private Stream<Quad> getQuads(final Predicate<Quad> filter) {
+        return getQuads().filter(filter);
+    }
+
+    @Override
+    public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+    	// TODO: Should this method be promoted as a default method in Dataset?     	    	
+    	remove(Optional.empty(), subject, predicate, object);
+    }
+
+	@Override
+	public void remove(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject, IRI
predicate, RDFTerm object) {
+        Stream<Quad> toRemove = getQuads(graphName, subject, predicate, object);
+        for (Quad t : toRemove.collect(Collectors.toList())) {
+            // Avoid ConcurrentModificationException in ArrayList
+            remove(t);
+        }
+	}
+
+
+    @Override
+    public void remove(Quad quad) {
+        quads.remove(Objects.requireNonNull(quad));
+    }
+
+    @Override
+    public long size() {
+        return quads.size();
+    }
+
+    @Override
+    public String toString() {
+        String s = getTriples().limit(TO_STRING_MAX).map(Object::toString)
+                .collect(Collectors.joining("\n"));
+        if (size() > TO_STRING_MAX) {
+            return s + "\n# ... +" + (size() - TO_STRING_MAX) + " more";
+        } else {
+            return s;
+        }
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/683a2bbd/simple/src/main/java/org/apache/commons/rdf/simple/QuadImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/QuadImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/QuadImpl.java
new file mode 100644
index 0000000..54921bb
--- /dev/null
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/QuadImpl.java
@@ -0,0 +1,109 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rdf.simple;
+
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.Quad;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
+
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * A simple implementation of Quad.
+ */
+final class QuadImpl implements Quad {
+
+	private final BlankNodeOrIRI graphName;
+    private final BlankNodeOrIRI subject;
+    private final IRI predicate;
+    private final RDFTerm object;
+
+    /**
+     * Construct Quad from its constituent parts.
+     * <p>
+     * The objects are not changed. All mapping of BNode objects is done in
+     * {@link SimpleRDFTermFactory#createQuad(BlankNodeOrIRI, BlankNodeOrIRI, IRI, RDFTerm)}.
+     *
+     * @param graphName  graphName of triple
+     * @param subject   subject of triple
+     * @param predicate predicate of triple
+     * @param object    object of triple
+     */
+    public QuadImpl(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm
object) {
+    	this.graphName = graphName; // possibly null
+        this.subject = Objects.requireNonNull(subject);
+        this.predicate = Objects.requireNonNull(predicate);
+        this.object = Objects.requireNonNull(object);
+    }
+
+    @Override
+    public Optional<BlankNodeOrIRI> getGraphName() {
+    	return Optional.ofNullable(graphName);
+    }
+    
+    @Override
+    public BlankNodeOrIRI getSubject() {
+        return subject;
+    }
+
+    @Override
+    public IRI getPredicate() {
+        return predicate;
+    }
+
+    @Override
+    public RDFTerm getObject() {
+        return object;
+    }
+
+    @Override
+    public String toString() {    	
+        return 
+			getSubject().ntriplesString() + " "
+	        + getPredicate().ntriplesString() + " "
+	        + getObject().ntriplesString() + " " 
+	        + getGraphName().map(g -> g.ntriplesString() + " ").orElse("")
+	        + ".";    	
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(subject, predicate, object, graphName);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (!(obj instanceof Quad)) {
+            return false;
+        }
+        Quad other = (Quad) obj;
+        return getGraphName().equals(other.getGraphName()) 
+        		&& getSubject().equals(other.getSubject())
+                && getPredicate().equals(other.getPredicate())
+                && getObject().equals(other.getObject());
+    }
+    
+    @Override
+    public Triple asTriple() {
+    	return new TripleImpl(getSubject(), getPredicate(), getObject());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/683a2bbd/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
b/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
index 36694bf..4f54d03 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/SimpleRDFTermFactory.java
@@ -21,9 +21,11 @@ import java.util.UUID;
 
 import org.apache.commons.rdf.api.BlankNode;
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.Dataset;
 import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Literal;
+import org.apache.commons.rdf.api.Quad;
 import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.api.RDFTermFactory;
 import org.apache.commons.rdf.api.Triple;
@@ -71,6 +73,11 @@ public class SimpleRDFTermFactory implements RDFTermFactory {
     }
 
     @Override
+    public Dataset createDataset() throws UnsupportedOperationException {
+    	return new DatasetImpl(this);
+    }
+    
+    @Override
     public IRI createIRI(String iri) {
         IRI result = new IRIImpl(iri);
         // Reuse any IRI objects already created in Types
@@ -97,4 +104,10 @@ public class SimpleRDFTermFactory implements RDFTermFactory {
                                RDFTerm object) {
         return new TripleImpl(subject, predicate, object);
     }
+    
+    @Override
+    public Quad createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate,
RDFTerm object)
+    		throws IllegalArgumentException, UnsupportedOperationException {
+    	return new QuadImpl(graphName, subject, predicate, object);
+    }
 }


Mime
View raw message