You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2015/03/19 22:34:38 UTC
[4/6] clerezza git commit: CLEREZZA-964: using rdf-commons from
clerezza-rdf-core repository
CLEREZZA-964: using rdf-commons from clerezza-rdf-core repository
Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/4bc320a8
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/4bc320a8
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/4bc320a8
Branch: refs/heads/rdf-commons
Commit: 4bc320a87f0fd8c5f52b9153924a09d114d9ea01
Parents: 47cc373
Author: Reto Gmuer <re...@apache.org>
Authored: Thu Mar 19 21:34:00 2015 +0000
Committer: Reto Gmuer <re...@apache.org>
Committed: Thu Mar 19 21:34:00 2015 +0000
----------------------------------------------------------------------
rdf.core.test/pom.xml | 4 +-
.../clerezza/rdf/core/test/GraphTest.java | 580 ++++++++++++++++++
.../clerezza/rdf/core/test/GraphWrapper.java | 126 ++++
.../test/LockableMGraphWrapperForTesting.java | 29 +-
.../core/test/LockingIteratorForTesting.java | 2 +-
.../clerezza/rdf/core/test/MGraphTest.java | 581 -------------------
.../clerezza/rdf/core/test/MGraphWrapper.java | 40 --
.../clerezza/rdf/core/test/RandomGraph.java | 237 ++++++++
.../clerezza/rdf/core/test/RandomMGraph.java | 237 --------
.../clerezza/rdf/core/test/TcProviderTest.java | 260 ++++-----
.../rdf/core/test/TripleCollectionWrapper.java | 129 ----
11 files changed, 1085 insertions(+), 1140 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/pom.xml
----------------------------------------------------------------------
diff --git a/rdf.core.test/pom.xml b/rdf.core.test/pom.xml
index ae07e20..c8d76b8 100644
--- a/rdf.core.test/pom.xml
+++ b/rdf.core.test/pom.xml
@@ -24,7 +24,7 @@
<parent>
<artifactId>clerezza</artifactId>
<groupId>org.apache.clerezza</groupId>
- <version>0.5</version>
+ <version>1.0.0-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<groupId>org.apache.clerezza</groupId>
@@ -41,7 +41,7 @@
<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.core</artifactId>
- <version>0.14</version>
+ <version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/GraphTest.java
----------------------------------------------------------------------
diff --git a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/GraphTest.java b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/GraphTest.java
new file mode 100644
index 0000000..4741e2e
--- /dev/null
+++ b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/GraphTest.java
@@ -0,0 +1,580 @@
+/*
+ * 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.clerezza.rdf.core.test;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import org.apache.commons.rdf.BlankNode;
+import org.apache.commons.rdf.Literal;
+import org.apache.commons.rdf.Graph;
+import org.apache.commons.rdf.BlankNodeOrIri;
+import org.apache.commons.rdf.RdfTerm;
+import org.apache.commons.rdf.Triple;
+import org.apache.commons.rdf.Iri;
+import org.apache.commons.rdf.impl.utils.TripleImpl;
+import org.junit.Test;
+import org.apache.commons.rdf.Language;
+import org.apache.commons.rdf.event.AddEvent;
+import org.apache.commons.rdf.event.FilterTriple;
+import org.apache.commons.rdf.event.GraphEvent;
+import org.apache.commons.rdf.event.GraphListener;
+import org.apache.commons.rdf.event.RemoveEvent;
+import org.apache.commons.rdf.impl.utils.PlainLiteralImpl;
+import org.apache.commons.rdf.impl.utils.TypedLiteralImpl;
+import org.apache.commons.rdf.impl.utils.simple.SimpleGraph;
+import org.junit.Assert;
+
+
+
+/**
+ * A generic abstract test class, implementations overwrite this class,
+ * providing an implementation of the getEmptyGraph method.
+ *
+ * @author reto, szalay, mir, hhn
+ */
+public abstract class GraphTest {
+
+ private final Iri uriRef1 =
+ new Iri("http://example.org/ontology#res1");
+ private final Iri uriRef2 =
+ new Iri("http://example.org/ontology#res2");
+ private final Iri uriRef3 =
+ new Iri("http://example.org/ontology#res3");
+ private final Iri uriRef4 =
+ new Iri("http://example.org/ontology#res4");
+ private final Iri xmlLiteralType =
+ new Iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral");
+ private Literal literal1 = new PlainLiteralImpl("literal1");
+ private Literal literal2 = new PlainLiteralImpl("literal2");
+ private BlankNode bnode1 = new BlankNode();
+ private BlankNode bnode2 = new BlankNode();
+ private Triple trpl1 = new TripleImpl(uriRef2, uriRef2, literal1);
+ private Triple trpl2 = new TripleImpl(uriRef1, uriRef2, uriRef1);
+ private Triple trpl3 = new TripleImpl(bnode2, uriRef3, literal2);
+ private Triple trpl4 = new TripleImpl(uriRef3, uriRef4, literal2);
+
+ /**
+ * Subclasses implement this method to provide implementation instances of
+ * Graph. This method may be called an arbitrary amount of time,
+ * independently whether previously returned Graph are still in use or not.
+ *
+ * @return an empty Graph of the implementation to be tested
+ */
+ protected abstract Graph getEmptyGraph();
+
+ @Test
+ public void testAddCountAndGetTriples() {
+ Graph graph = getEmptyGraph();
+ Assert.assertEquals(0, graph.size());
+ final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1);
+ graph.add(triple1);
+ Assert.assertEquals(1, graph.size());
+ Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, uriRef1);
+ Assert.assertTrue(tripleIter.hasNext());
+ Triple tripleGot = tripleIter.next();
+ Assert.assertEquals(triple1, tripleGot);
+ Assert.assertFalse(tripleIter.hasNext());
+ BlankNode bnode = new BlankNode() {};
+ graph.add(new TripleImpl(bnode, uriRef1, uriRef3));
+ graph.add(new TripleImpl(bnode, uriRef1, uriRef4));
+ tripleIter = graph.filter(null, uriRef1, null);
+ Set<BlankNodeOrIri> subjectInMatchingTriples = new HashSet<BlankNodeOrIri>();
+ Set<RdfTerm> objectsInMatchingTriples = new HashSet<RdfTerm>();
+ while (tripleIter.hasNext()) {
+ Triple triple = tripleIter.next();
+ subjectInMatchingTriples.add(triple.getSubject());
+ objectsInMatchingTriples.add(triple.getObject());
+ }
+ Assert.assertEquals(1, subjectInMatchingTriples.size());
+ Assert.assertEquals(2, objectsInMatchingTriples.size());
+ Set<RdfTerm> expectedObjects = new HashSet<RdfTerm>();
+ expectedObjects.add(uriRef3);
+ expectedObjects.add(uriRef4);
+ Assert.assertEquals(expectedObjects, objectsInMatchingTriples);
+ graph.add(new TripleImpl(bnode, uriRef4, bnode));
+ tripleIter = graph.filter(null, uriRef4, null);
+ Assert.assertTrue(tripleIter.hasNext());
+ Triple retrievedTriple = tripleIter.next();
+ Assert.assertFalse(tripleIter.hasNext());
+ Assert.assertEquals(retrievedTriple.getSubject(), retrievedTriple.getObject());
+ tripleIter = graph.filter(uriRef1, uriRef2, null);
+ Assert.assertTrue(tripleIter.hasNext());
+ retrievedTriple = tripleIter.next();
+ Assert.assertFalse(tripleIter.hasNext());
+ Assert.assertEquals(retrievedTriple.getSubject(), retrievedTriple.getObject());
+ }
+
+ @Test
+ public void testRemoveAllTriples() {
+ Graph graph = getEmptyGraph();
+ Assert.assertEquals(0, graph.size());
+ graph.add(new TripleImpl(uriRef1, uriRef2, uriRef3));
+ graph.add(new TripleImpl(uriRef2, uriRef3, uriRef4));
+ Assert.assertEquals(2, graph.size());
+ graph.clear();
+ Assert.assertEquals(0, graph.size());
+ }
+
+ @Test
+ public void testUseTypedLiterals() {
+ Graph graph = getEmptyGraph();
+ Assert.assertEquals(0, graph.size());
+ Literal value = new TypedLiteralImpl("<elem>value</elem>",xmlLiteralType);
+ final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, value);
+ graph.add(triple1);
+ Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, null);
+ Assert.assertTrue(tripleIter.hasNext());
+ RdfTerm gotValue = tripleIter.next().getObject();
+ Assert.assertEquals(value, gotValue);
+ }
+
+ @Test
+ public void testUseLanguageLiterals() {
+ Graph graph = getEmptyGraph();
+ Assert.assertEquals(0, graph.size());
+ Language language = new Language("it");
+ Literal value = new PlainLiteralImpl("<elem>value</elem>",language);
+ final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, value);
+ graph.add(triple1);
+ Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, null);
+ Assert.assertTrue(tripleIter.hasNext());
+ RdfTerm gotValue = tripleIter.next().getObject();
+ Assert.assertEquals(value, gotValue);
+ Assert.assertEquals(language, ((Literal)gotValue).getLanguage());
+ }
+
+ @Test
+ public void testRemoveViaIterator() {
+ Graph graph = getEmptyGraph();
+ Assert.assertEquals(0, graph.size());
+ final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1);
+ graph.add(triple1);
+ final TripleImpl triple2 = new TripleImpl(uriRef1, uriRef2, uriRef4);
+ graph.add(triple2);
+ Assert.assertEquals(2, graph.size());
+ Iterator<Triple> iterator = graph.iterator();
+ while (iterator.hasNext()) {
+ iterator.next();
+ iterator.remove();
+ }
+ Assert.assertEquals(0, graph.size());
+ }
+
+ @Test
+ public void testGetSize() throws Exception {
+ Graph graph = getEmptyGraph();
+ // The test graph must always be empty after test fixture setup
+ Assert.assertEquals(0, graph.size());
+ }
+
+
+ @Test
+ public void testAddSingleTriple() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple triple= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ Assert.assertEquals(0, graph.size());
+ Assert.assertTrue(graph.add(triple));
+ Assert.assertEquals(1, graph.size());
+ }
+
+
+ @Test
+ public void testAddSameTripleTwice() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple triple= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ Assert.assertEquals(0, graph.size());
+ Assert.assertTrue(graph.add(triple));
+ Assert.assertFalse(graph.add(triple)); // ImmutableGraph does not change
+ Assert.assertEquals(1, graph.size());
+ }
+
+
+ @Test
+ public void testRemoveSingleTriple() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple triple= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ Assert.assertTrue(graph.add(triple));
+ Assert.assertTrue(graph.remove(triple));
+ Assert.assertEquals(0, graph.size());
+ }
+
+ @Test
+ public void testRemoveSameTripleTwice() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple tripleAlice= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ final Triple tripleBob= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/bob");
+ Assert.assertTrue(graph.add(tripleAlice));
+ Assert.assertTrue(graph.add(tripleBob));
+ Assert.assertTrue(graph.remove(tripleAlice));
+ Assert.assertFalse(graph.remove(tripleAlice));
+ Assert.assertEquals(1, graph.size());
+ }
+
+ @Test
+ public void testGetSameBlankNode() throws Exception {
+ Graph graph = getEmptyGraph();
+ BlankNode bNode = new BlankNode();
+ final Iri HAS_NAME = new Iri("http://example.org/ontology/hasName");
+ final PlainLiteralImpl name = new PlainLiteralImpl("http://example.org/people/alice");
+ final PlainLiteralImpl name2 = new PlainLiteralImpl("http://example.org/people/bob");
+ final Triple tripleAlice = new TripleImpl(bNode, HAS_NAME, name);
+ final Triple tripleBob = new TripleImpl(bNode, HAS_NAME, name2);
+ Assert.assertTrue(graph.add(tripleAlice));
+ Assert.assertTrue(graph.add(tripleBob));
+ Iterator<Triple> result = graph.filter(null, HAS_NAME, name);
+ Assert.assertEquals(bNode, result.next().getSubject());
+ }
+
+ @Test
+ public void testContainsIfContained() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple triple= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ Assert.assertTrue(graph.add(triple));
+ Assert.assertTrue(graph.contains(triple));
+ }
+
+
+ @Test
+ public void testContainsIfEmpty() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple triple= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ Assert.assertFalse(graph.contains(triple));
+ }
+
+
+ @Test
+ public void testContainsIfNotContained() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple tripleAdd= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ final Triple tripleTest= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/bob");
+ Assert.assertTrue(graph.add(tripleAdd));
+ Assert.assertFalse(graph.contains(tripleTest));
+ }
+
+
+ @Test
+ public void testFilterEmptyGraph() throws Exception {
+ Graph graph = getEmptyGraph();
+ Iterator<Triple> i = graph.filter(null, null, null);
+ Assert.assertFalse(i.hasNext());
+ }
+
+
+ @Test
+ public void testFilterSingleEntry() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple triple= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ Assert.assertTrue(graph.add(triple));
+
+ Iterator<Triple> i = graph.filter(null, null, null);
+ Collection<Triple> resultSet= toCollection(i);
+ Assert.assertEquals(1, resultSet.size());
+ Assert.assertTrue(resultSet.contains(triple));
+ }
+
+
+ @Test
+ public void testFilterByObject() throws Exception {
+ Graph graph = getEmptyGraph();
+ final Triple tripleAlice= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/alice");
+ final Triple tripleBob= createTriple(
+ "http://example.org/ontology/Person",
+ "http://example.org/ontology/hasName",
+ "http://example.org/people/bob");
+ Assert.assertTrue(graph.add(tripleAlice));
+ Assert.assertTrue(graph.add(tripleBob));
+
+ Iterator<Triple> iterator;
+ Collection<Triple> resultSet;
+
+ // Find bob
+ iterator = graph.filter(null, null,
+ new Iri("http://example.org/people/bob"));
+ resultSet= toCollection(iterator);
+ Assert.assertEquals(1, resultSet.size());
+ Assert.assertTrue(resultSet.contains(tripleBob));
+
+ // Find alice
+ iterator = graph.filter(null, null,
+ new Iri("http://example.org/people/alice"));
+ resultSet= toCollection(iterator);
+ Assert.assertEquals(1, resultSet.size());
+ Assert.assertTrue(resultSet.contains(tripleAlice));
+
+ // Find both
+ iterator = graph.filter(null, null, null);
+ resultSet= toCollection(iterator);
+ Assert.assertEquals(2, resultSet.size());
+ Assert.assertTrue(resultSet.contains(tripleAlice));
+ Assert.assertTrue(resultSet.contains(tripleBob));
+ }
+/*
+ @Test
+ public void graphEventTestAddRemove() {
+ Graph mGraph = getEmptyGraph();
+ TestGraphListener listener = new TestGraphListener();
+ mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
+ mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
+ mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
+ mGraph.add(trpl1);
+ Assert.assertNull(listener.getEvents());
+ mGraph.add(trpl2);
+ Assert.assertEquals(1, listener.getEvents().size());
+ Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple());
+ Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
+ listener.resetEvents();
+ mGraph.remove(trpl2);
+ Assert.assertEquals(1, listener.getEvents().size());
+ Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple());
+ Assert.assertTrue(listener.getEvents().get(0) instanceof RemoveEvent);
+ listener.resetEvents();
+ mGraph.add(trpl3);
+ Assert.assertEquals(1, listener.getEvents().size());
+ Assert.assertEquals(trpl3, listener.getEvents().get(0).getTriple());
+ Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
+ listener.resetEvents();
+ mGraph.remove(trpl4);
+ Assert.assertNull(listener.getEvents());
+ }
+
+ @Test
+ public void graphEventTestAddAllRemoveAll() {
+ Graph mGraph = getEmptyGraph();
+ TestGraphListener listener = new TestGraphListener();
+ mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
+ mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
+ mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
+ Graph triples = new SimpleGraph();
+ triples.add(trpl1);
+ triples.add(trpl2);
+ triples.add(trpl3);
+ triples.add(trpl4);
+ mGraph.addAll(triples);
+ List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents();
+ Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents);
+ Assert.assertEquals(3, cumulatedEvents.size());
+ Assert.assertTrue(cumulatedEvents.get(0) instanceof AddEvent);
+ Assert.assertTrue(cumulatedTriples.contains(trpl2));
+ Assert.assertTrue(cumulatedTriples.contains(trpl3));
+ Assert.assertTrue(cumulatedTriples.contains(trpl4));
+ listener.resetCumulatedEvents();
+ mGraph.removeAll(triples);
+ cumulatedEvents = listener.getCumulatedEvents();
+ cumulatedTriples = getCumulatedTriples(cumulatedEvents);
+ Assert.assertEquals(3, cumulatedEvents.size());
+ Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent);
+ Assert.assertTrue(cumulatedTriples.contains(trpl2));
+ Assert.assertTrue(cumulatedTriples.contains(trpl3));
+ Assert.assertTrue(cumulatedTriples.contains(trpl4));
+ }
+
+ @Test
+ public void graphEventTestFilterRemove() {
+ Graph mGraph = getEmptyGraph();
+ TestGraphListener listener = new TestGraphListener();
+ mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
+ mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
+ mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
+ mGraph.add(trpl1);
+ mGraph.add(trpl2);
+ mGraph.add(trpl3);
+ mGraph.add(trpl4);
+ listener.resetCumulatedEvents();
+ Iterator<Triple> result = mGraph.filter(null, uriRef2, null);
+ while (result.hasNext()) {
+ result.next();
+ result.remove();
+ }
+ List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents();
+ Assert.assertEquals(1, cumulatedEvents.size());
+ Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent);
+ Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple());
+ }
+
+ @Test
+ public void graphEventTestIteratorRemove() {
+ Graph mGraph = getEmptyGraph();
+ TestGraphListener listener = new TestGraphListener();
+ mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
+ mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
+ mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
+ mGraph.add(trpl1);
+ mGraph.add(trpl2);
+ mGraph.add(trpl3);
+ mGraph.add(trpl4);
+ listener.resetCumulatedEvents();
+ Iterator<Triple> result = mGraph.iterator();
+ while (result.hasNext()) {
+ result.next();
+ result.remove();
+ }
+ List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents();
+ Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents);
+ Assert.assertEquals(3, cumulatedEvents.size());
+ Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent);
+ Assert.assertTrue(cumulatedTriples.contains(trpl2));
+ Assert.assertTrue(cumulatedTriples.contains(trpl3));
+ Assert.assertTrue(cumulatedTriples.contains(trpl4));
+ }
+
+ @Test
+ public void graphEventTestClear() {
+ Graph mGraph = getEmptyGraph();
+ TestGraphListener listener = new TestGraphListener();
+ mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
+ mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
+ mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
+ mGraph.add(trpl1);
+ mGraph.add(trpl2);
+ mGraph.add(trpl3);
+ mGraph.add(trpl4);
+ listener.resetCumulatedEvents();
+ mGraph.clear();
+ List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents();
+ Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents);
+ Assert.assertEquals(3, cumulatedEvents.size());
+ Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent);
+ Assert.assertTrue(cumulatedTriples.contains(trpl2));
+ Assert.assertTrue(cumulatedTriples.contains(trpl3));
+ Assert.assertTrue(cumulatedTriples.contains(trpl4));
+ }
+
+ private Set<Triple> getCumulatedTriples(List<GraphEvent> cumulatedEvents) {
+ Set<Triple> triples = new HashSet<Triple>();
+ for(GraphEvent event: cumulatedEvents) {
+ triples.add(event.getTriple());
+ }
+ return triples;
+ }
+
+ @Test
+ public void graphEventTestWithDelay() throws Exception{
+ Graph mGraph = getEmptyGraph();
+ TestGraphListener listener = new TestGraphListener();
+ mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null),
+ 1000);
+
+ Triple triple0 = new TripleImpl(uriRef2, uriRef2, literal1);
+ Triple triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1);
+ Triple triple2 = new TripleImpl(uriRef1, uriRef2, literal1);
+ Triple triple3 = new TripleImpl(uriRef1, uriRef2, bnode1);
+ mGraph.add(triple0);
+ mGraph.add(triple1);
+ mGraph.add(triple2);
+ mGraph.add(triple3);
+ Thread.sleep(1500);
+ Assert.assertEquals(3, listener.getEvents().size());
+ Assert.assertEquals(triple1, listener.getEvents().get(0).getTriple());
+ Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
+ Assert.assertEquals(triple2, listener.getEvents().get(1).getTriple());
+ Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
+ Assert.assertEquals(triple3, listener.getEvents().get(2).getTriple());
+ Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
+ }
+
+ private static class TestGraphListener implements GraphListener {
+ private List<GraphEvent> events = null;
+ private List<GraphEvent> cumulatedEvents = new ArrayList<GraphEvent>();
+
+ @Override
+ public void graphChanged(List<GraphEvent> events) {
+ this.events = events;
+ Iterator<GraphEvent> eventIter = events.iterator();
+ while (eventIter.hasNext()) {
+ GraphEvent graphEvent = eventIter.next();
+ this.cumulatedEvents.add(graphEvent);
+ }
+ }
+
+ public List<GraphEvent> getEvents() {
+ return events;
+ }
+
+ public List<GraphEvent> getCumulatedEvents() {
+ return cumulatedEvents;
+ }
+
+ public void resetEvents() {
+ events = null;
+ }
+
+ public void resetCumulatedEvents() {
+ cumulatedEvents = new ArrayList<GraphEvent>();
+ }
+ }
+*/
+ private Collection<Triple> toCollection(Iterator<Triple> iterator) {
+ Collection<Triple> result = new ArrayList<Triple>();
+ while (iterator.hasNext()) {
+ result.add(iterator.next());
+ }
+ return result;
+ }
+
+ /**
+ * Creates a new <code>Triple</code>.
+ * @param subject the subject.
+ * @param predicate the predicate.
+ * @param object the object.
+ * @throws IllegalArgumentException If an attribute is <code>null</code>.
+ */
+ private Triple createTriple(String subject, String predicate,
+ String object) {
+ return new TripleImpl(new Iri(subject), new Iri(predicate),
+ new Iri(object));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/GraphWrapper.java
----------------------------------------------------------------------
diff --git a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/GraphWrapper.java b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/GraphWrapper.java
new file mode 100644
index 0000000..57fd89a
--- /dev/null
+++ b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/GraphWrapper.java
@@ -0,0 +1,126 @@
+/*
+ * 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.clerezza.rdf.core.test;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.concurrent.locks.ReadWriteLock;
+import org.apache.commons.rdf.BlankNodeOrIri;
+import org.apache.commons.rdf.Graph;
+import org.apache.commons.rdf.ImmutableGraph;
+import org.apache.commons.rdf.RdfTerm;
+import org.apache.commons.rdf.Triple;
+import org.apache.commons.rdf.Iri;
+
+
+/**
+ *
+ * @author mir
+ */
+class GraphWrapper implements Graph {
+
+ protected Graph wrapped;
+
+ public GraphWrapper(Graph tc) {
+ this.wrapped = tc;
+ }
+
+ @Override
+ public Iterator<Triple> filter(BlankNodeOrIri subject, Iri predicate, RdfTerm object) {
+ return wrapped.filter(subject, predicate, object);
+ }
+
+
+ @Override
+ public int size() {
+ return wrapped.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return wrapped.isEmpty();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return wrapped.contains(o);
+ }
+
+ @Override
+ public Iterator<Triple> iterator() {
+ return wrapped.iterator();
+ }
+
+ @Override
+ public Object[] toArray() {
+ return wrapped.toArray();
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a) {
+ return wrapped.toArray(a);
+ }
+
+ @Override
+ public boolean add(Triple e) {
+ return wrapped.add(e);
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ return wrapped.remove(o);
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return wrapped.containsAll(c);
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends Triple> c) {
+ return wrapped.addAll(c);
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ return wrapped.removeAll(c);
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ return wrapped.retainAll(c);
+ }
+
+ @Override
+ public void clear() {
+ wrapped.clear();
+ }
+
+ @Override
+ public ImmutableGraph getImmutableGraph() {
+ return wrapped.getImmutableGraph();
+ }
+
+ @Override
+ public ReadWriteLock getLock() {
+ return wrapped.getLock();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockableMGraphWrapperForTesting.java
----------------------------------------------------------------------
diff --git a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockableMGraphWrapperForTesting.java b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockableMGraphWrapperForTesting.java
index 1869787..3f009cc 100644
--- a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockableMGraphWrapperForTesting.java
+++ b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockableMGraphWrapperForTesting.java
@@ -21,38 +21,37 @@ package org.apache.clerezza.rdf.core.test;
import java.util.Iterator;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
-import org.apache.clerezza.rdf.core.MGraph;
-import org.apache.clerezza.rdf.core.NonLiteral;
-import org.apache.clerezza.rdf.core.Resource;
-import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.access.LockableMGraphWrapper;
+import org.apache.commons.rdf.Graph;
+import org.apache.commons.rdf.BlankNodeOrIri;
+import org.apache.commons.rdf.RdfTerm;
+import org.apache.commons.rdf.Triple;
+import org.apache.commons.rdf.Iri;
/**
- * Wrappes an MGraph as a LockableMGraph. If a method is called that reads
+ * Wrappes an Graph as a LockableGraph. If a method is called that reads
* or modifies the wrapped graph and the appropriate lock is not set, then a
* RuntimeException is thrown.
*
* @author rbn, mir
*/
-public class LockableMGraphWrapperForTesting extends LockableMGraphWrapper {
+public class LockableMGraphWrapperForTesting extends GraphWrapper {
private final ReentrantReadWriteLock lock = (ReentrantReadWriteLock) getLock();
private final Lock readLock = lock.readLock();
- private final MGraph wrapped;
+ private final Graph wrapped;
/**
- * Constructs a LocalbleMGraph for an MGraph.
+ * Constructs a LocalbleGraph for an Graph.
*
- * @param providedMGraph a non-lockable mgraph
+ * @param providedGraph a non-lockable mgraph
*/
- public LockableMGraphWrapperForTesting(final MGraph providedMGraph) {
- super(providedMGraph);
- this.wrapped = providedMGraph;
+ public LockableMGraphWrapperForTesting(final Graph providedGraph) {
+ super(providedGraph);
+ this.wrapped = providedGraph;
}
@Override
- public Iterator<Triple> filter(NonLiteral subject, UriRef predicate, Resource object) {
+ public Iterator<Triple> filter(BlankNodeOrIri subject, Iri predicate, RdfTerm object) {
LockChecker.checkIfReadLocked(lock);
readLock.lock();
try {
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockingIteratorForTesting.java
----------------------------------------------------------------------
diff --git a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockingIteratorForTesting.java b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockingIteratorForTesting.java
index d8bbc9a..ae911d4 100644
--- a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockingIteratorForTesting.java
+++ b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/LockingIteratorForTesting.java
@@ -21,7 +21,7 @@ package org.apache.clerezza.rdf.core.test;
import java.util.Iterator;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
-import org.apache.clerezza.rdf.core.Triple;
+import org.apache.commons.rdf.Triple;
/**
* Wrapps an iterator<Triple> reading all elements to a cache on construction
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphTest.java
----------------------------------------------------------------------
diff --git a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphTest.java b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphTest.java
deleted file mode 100644
index 5b43e65..0000000
--- a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphTest.java
+++ /dev/null
@@ -1,581 +0,0 @@
-/*
- * 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.clerezza.rdf.core.test;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import org.apache.clerezza.rdf.core.BNode;
-import org.apache.clerezza.rdf.core.Literal;
-import org.apache.clerezza.rdf.core.MGraph;
-import org.apache.clerezza.rdf.core.NonLiteral;
-import org.apache.clerezza.rdf.core.Resource;
-import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.impl.TripleImpl;
-import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
-import org.junit.Test;
-import junit.framework.Assert;
-import org.apache.clerezza.rdf.core.Language;
-import org.apache.clerezza.rdf.core.PlainLiteral;
-import org.apache.clerezza.rdf.core.event.AddEvent;
-import org.apache.clerezza.rdf.core.event.FilterTriple;
-import org.apache.clerezza.rdf.core.event.GraphEvent;
-import org.apache.clerezza.rdf.core.event.GraphListener;
-import org.apache.clerezza.rdf.core.event.RemoveEvent;
-import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
-import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
-
-
-
-/**
- * A generic abstract test class, implementations overwrite this class,
- * providing an implementation of the getEmptyMGraph method.
- *
- * @author reto, szalay, mir, hhn
- */
-public abstract class MGraphTest {
-
- private final UriRef uriRef1 =
- new UriRef("http://example.org/ontology#res1");
- private final UriRef uriRef2 =
- new UriRef("http://example.org/ontology#res2");
- private final UriRef uriRef3 =
- new UriRef("http://example.org/ontology#res3");
- private final UriRef uriRef4 =
- new UriRef("http://example.org/ontology#res4");
- private final UriRef xmlLiteralType =
- new UriRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral");
- private Literal literal1 = new PlainLiteralImpl("literal1");
- private Literal literal2 = new PlainLiteralImpl("literal2");
- private BNode bnode1 = new BNode();
- private BNode bnode2 = new BNode();
- private Triple trpl1 = new TripleImpl(uriRef2, uriRef2, literal1);
- private Triple trpl2 = new TripleImpl(uriRef1, uriRef2, uriRef1);
- private Triple trpl3 = new TripleImpl(bnode2, uriRef3, literal2);
- private Triple trpl4 = new TripleImpl(uriRef3, uriRef4, literal2);
-
- /**
- * Subclasses implement this method to provide implementation instances of
- * MGraph. This method may be called an arbitrary amount of time,
- * independently whether previously returned MGraph are still in use or not.
- *
- * @return an empty MGraph of the implementation to be tested
- */
- protected abstract MGraph getEmptyMGraph();
-
- @Test
- public void testAddCountAndGetTriples() {
- MGraph graph = getEmptyMGraph();
- Assert.assertEquals(0, graph.size());
- final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1);
- graph.add(triple1);
- Assert.assertEquals(1, graph.size());
- Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, uriRef1);
- Assert.assertTrue(tripleIter.hasNext());
- Triple tripleGot = tripleIter.next();
- Assert.assertEquals(triple1, tripleGot);
- Assert.assertFalse(tripleIter.hasNext());
- BNode bnode = new BNode() {};
- graph.add(new TripleImpl(bnode, uriRef1, uriRef3));
- graph.add(new TripleImpl(bnode, uriRef1, uriRef4));
- tripleIter = graph.filter(null, uriRef1, null);
- Set<NonLiteral> subjectInMatchingTriples = new HashSet<NonLiteral>();
- Set<Resource> objectsInMatchingTriples = new HashSet<Resource>();
- while (tripleIter.hasNext()) {
- Triple triple = tripleIter.next();
- subjectInMatchingTriples.add(triple.getSubject());
- objectsInMatchingTriples.add(triple.getObject());
- }
- Assert.assertEquals(1, subjectInMatchingTriples.size());
- Assert.assertEquals(2, objectsInMatchingTriples.size());
- Set<Resource> expectedObjects = new HashSet<Resource>();
- expectedObjects.add(uriRef3);
- expectedObjects.add(uriRef4);
- Assert.assertEquals(expectedObjects, objectsInMatchingTriples);
- graph.add(new TripleImpl(bnode, uriRef4, bnode));
- tripleIter = graph.filter(null, uriRef4, null);
- Assert.assertTrue(tripleIter.hasNext());
- Triple retrievedTriple = tripleIter.next();
- Assert.assertFalse(tripleIter.hasNext());
- Assert.assertEquals(retrievedTriple.getSubject(), retrievedTriple.getObject());
- tripleIter = graph.filter(uriRef1, uriRef2, null);
- Assert.assertTrue(tripleIter.hasNext());
- retrievedTriple = tripleIter.next();
- Assert.assertFalse(tripleIter.hasNext());
- Assert.assertEquals(retrievedTriple.getSubject(), retrievedTriple.getObject());
- }
-
- @Test
- public void testRemoveAllTriples() {
- MGraph graph = getEmptyMGraph();
- Assert.assertEquals(0, graph.size());
- graph.add(new TripleImpl(uriRef1, uriRef2, uriRef3));
- graph.add(new TripleImpl(uriRef2, uriRef3, uriRef4));
- Assert.assertEquals(2, graph.size());
- graph.clear();
- Assert.assertEquals(0, graph.size());
- }
-
- @Test
- public void testUseTypedLiterals() {
- MGraph graph = getEmptyMGraph();
- Assert.assertEquals(0, graph.size());
- Literal value = new TypedLiteralImpl("<elem>value</elem>",xmlLiteralType);
- final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, value);
- graph.add(triple1);
- Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, null);
- Assert.assertTrue(tripleIter.hasNext());
- Resource gotValue = tripleIter.next().getObject();
- Assert.assertEquals(value, gotValue);
- }
-
- @Test
- public void testUseLanguageLiterals() {
- MGraph graph = getEmptyMGraph();
- Assert.assertEquals(0, graph.size());
- Language language = new Language("it");
- Literal value = new PlainLiteralImpl("<elem>value</elem>",language);
- final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, value);
- graph.add(triple1);
- Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, null);
- Assert.assertTrue(tripleIter.hasNext());
- Resource gotValue = tripleIter.next().getObject();
- Assert.assertEquals(value, gotValue);
- Assert.assertEquals(language, ((PlainLiteral)gotValue).getLanguage());
- }
-
- @Test
- public void testRemoveViaIterator() {
- MGraph graph = getEmptyMGraph();
- Assert.assertEquals(0, graph.size());
- final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1);
- graph.add(triple1);
- final TripleImpl triple2 = new TripleImpl(uriRef1, uriRef2, uriRef4);
- graph.add(triple2);
- Assert.assertEquals(2, graph.size());
- Iterator<Triple> iterator = graph.iterator();
- while (iterator.hasNext()) {
- iterator.next();
- iterator.remove();
- }
- Assert.assertEquals(0, graph.size());
- }
-
- @Test
- public void testGetSize() throws Exception {
- MGraph graph = getEmptyMGraph();
- // The test graph must always be empty after test fixture setup
- Assert.assertEquals(0, graph.size());
- }
-
-
- @Test
- public void testAddSingleTriple() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple triple= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- Assert.assertEquals(0, graph.size());
- Assert.assertTrue(graph.add(triple));
- Assert.assertEquals(1, graph.size());
- }
-
-
- @Test
- public void testAddSameTripleTwice() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple triple= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- Assert.assertEquals(0, graph.size());
- Assert.assertTrue(graph.add(triple));
- Assert.assertFalse(graph.add(triple)); // Graph does not change
- Assert.assertEquals(1, graph.size());
- }
-
-
- @Test
- public void testRemoveSingleTriple() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple triple= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- Assert.assertTrue(graph.add(triple));
- Assert.assertTrue(graph.remove(triple));
- Assert.assertEquals(0, graph.size());
- }
-
- @Test
- public void testRemoveSameTripleTwice() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple tripleAlice= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- final Triple tripleBob= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/bob");
- Assert.assertTrue(graph.add(tripleAlice));
- Assert.assertTrue(graph.add(tripleBob));
- Assert.assertTrue(graph.remove(tripleAlice));
- Assert.assertFalse(graph.remove(tripleAlice));
- Assert.assertEquals(1, graph.size());
- }
-
- @Test
- public void testGetSameBNode() throws Exception {
- MGraph graph = getEmptyMGraph();
- BNode bNode = new BNode();
- final UriRef HAS_NAME = new UriRef("http://example.org/ontology/hasName");
- final PlainLiteralImpl name = new PlainLiteralImpl("http://example.org/people/alice");
- final PlainLiteralImpl name2 = new PlainLiteralImpl("http://example.org/people/bob");
- final Triple tripleAlice = new TripleImpl(bNode, HAS_NAME, name);
- final Triple tripleBob = new TripleImpl(bNode, HAS_NAME, name2);
- Assert.assertTrue(graph.add(tripleAlice));
- Assert.assertTrue(graph.add(tripleBob));
- Iterator<Triple> result = graph.filter(null, HAS_NAME, name);
- Assert.assertEquals(bNode, result.next().getSubject());
- }
-
- @Test
- public void testContainsIfContained() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple triple= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- Assert.assertTrue(graph.add(triple));
- Assert.assertTrue(graph.contains(triple));
- }
-
-
- @Test
- public void testContainsIfEmpty() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple triple= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- Assert.assertFalse(graph.contains(triple));
- }
-
-
- @Test
- public void testContainsIfNotContained() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple tripleAdd= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- final Triple tripleTest= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/bob");
- Assert.assertTrue(graph.add(tripleAdd));
- Assert.assertFalse(graph.contains(tripleTest));
- }
-
-
- @Test
- public void testFilterEmptyGraph() throws Exception {
- MGraph graph = getEmptyMGraph();
- Iterator<Triple> i = graph.filter(null, null, null);
- Assert.assertFalse(i.hasNext());
- }
-
-
- @Test
- public void testFilterSingleEntry() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple triple= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- Assert.assertTrue(graph.add(triple));
-
- Iterator<Triple> i = graph.filter(null, null, null);
- Collection<Triple> resultSet= toCollection(i);
- Assert.assertEquals(1, resultSet.size());
- Assert.assertTrue(resultSet.contains(triple));
- }
-
-
- @Test
- public void testFilterByObject() throws Exception {
- MGraph graph = getEmptyMGraph();
- final Triple tripleAlice= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/alice");
- final Triple tripleBob= createTriple(
- "http://example.org/ontology/Person",
- "http://example.org/ontology/hasName",
- "http://example.org/people/bob");
- Assert.assertTrue(graph.add(tripleAlice));
- Assert.assertTrue(graph.add(tripleBob));
-
- Iterator<Triple> iterator;
- Collection<Triple> resultSet;
-
- // Find bob
- iterator = graph.filter(null, null,
- new UriRef("http://example.org/people/bob"));
- resultSet= toCollection(iterator);
- Assert.assertEquals(1, resultSet.size());
- Assert.assertTrue(resultSet.contains(tripleBob));
-
- // Find alice
- iterator = graph.filter(null, null,
- new UriRef("http://example.org/people/alice"));
- resultSet= toCollection(iterator);
- Assert.assertEquals(1, resultSet.size());
- Assert.assertTrue(resultSet.contains(tripleAlice));
-
- // Find both
- iterator = graph.filter(null, null, null);
- resultSet= toCollection(iterator);
- Assert.assertEquals(2, resultSet.size());
- Assert.assertTrue(resultSet.contains(tripleAlice));
- Assert.assertTrue(resultSet.contains(tripleBob));
- }
-
- @Test
- public void graphEventTestAddRemove() {
- MGraph mGraph = getEmptyMGraph();
- TestGraphListener listener = new TestGraphListener();
- mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
- mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
- mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
- mGraph.add(trpl1);
- Assert.assertNull(listener.getEvents());
- mGraph.add(trpl2);
- Assert.assertEquals(1, listener.getEvents().size());
- Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple());
- Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
- listener.resetEvents();
- mGraph.remove(trpl2);
- Assert.assertEquals(1, listener.getEvents().size());
- Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple());
- Assert.assertTrue(listener.getEvents().get(0) instanceof RemoveEvent);
- listener.resetEvents();
- mGraph.add(trpl3);
- Assert.assertEquals(1, listener.getEvents().size());
- Assert.assertEquals(trpl3, listener.getEvents().get(0).getTriple());
- Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
- listener.resetEvents();
- mGraph.remove(trpl4);
- Assert.assertNull(listener.getEvents());
- }
-
- @Test
- public void graphEventTestAddAllRemoveAll() {
- MGraph mGraph = getEmptyMGraph();
- TestGraphListener listener = new TestGraphListener();
- mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
- mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
- mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
- MGraph triples = new SimpleMGraph();
- triples.add(trpl1);
- triples.add(trpl2);
- triples.add(trpl3);
- triples.add(trpl4);
- mGraph.addAll(triples);
- List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents();
- Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents);
- Assert.assertEquals(3, cumulatedEvents.size());
- Assert.assertTrue(cumulatedEvents.get(0) instanceof AddEvent);
- Assert.assertTrue(cumulatedTriples.contains(trpl2));
- Assert.assertTrue(cumulatedTriples.contains(trpl3));
- Assert.assertTrue(cumulatedTriples.contains(trpl4));
- listener.resetCumulatedEvents();
- mGraph.removeAll(triples);
- cumulatedEvents = listener.getCumulatedEvents();
- cumulatedTriples = getCumulatedTriples(cumulatedEvents);
- Assert.assertEquals(3, cumulatedEvents.size());
- Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent);
- Assert.assertTrue(cumulatedTriples.contains(trpl2));
- Assert.assertTrue(cumulatedTriples.contains(trpl3));
- Assert.assertTrue(cumulatedTriples.contains(trpl4));
- }
-
- @Test
- public void graphEventTestFilterRemove() {
- MGraph mGraph = getEmptyMGraph();
- TestGraphListener listener = new TestGraphListener();
- mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
- mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
- mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
- mGraph.add(trpl1);
- mGraph.add(trpl2);
- mGraph.add(trpl3);
- mGraph.add(trpl4);
- listener.resetCumulatedEvents();
- Iterator<Triple> result = mGraph.filter(null, uriRef2, null);
- while (result.hasNext()) {
- result.next();
- result.remove();
- }
- List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents();
- Assert.assertEquals(1, cumulatedEvents.size());
- Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent);
- Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple());
- }
-
- @Test
- public void graphEventTestIteratorRemove() {
- MGraph mGraph = getEmptyMGraph();
- TestGraphListener listener = new TestGraphListener();
- mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
- mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
- mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
- mGraph.add(trpl1);
- mGraph.add(trpl2);
- mGraph.add(trpl3);
- mGraph.add(trpl4);
- listener.resetCumulatedEvents();
- Iterator<Triple> result = mGraph.iterator();
- while (result.hasNext()) {
- result.next();
- result.remove();
- }
- List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents();
- Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents);
- Assert.assertEquals(3, cumulatedEvents.size());
- Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent);
- Assert.assertTrue(cumulatedTriples.contains(trpl2));
- Assert.assertTrue(cumulatedTriples.contains(trpl3));
- Assert.assertTrue(cumulatedTriples.contains(trpl4));
- }
-
- @Test
- public void graphEventTestClear() {
- MGraph mGraph = getEmptyMGraph();
- TestGraphListener listener = new TestGraphListener();
- mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null));
- mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2));
- mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2));
- mGraph.add(trpl1);
- mGraph.add(trpl2);
- mGraph.add(trpl3);
- mGraph.add(trpl4);
- listener.resetCumulatedEvents();
- mGraph.clear();
- List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents();
- Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents);
- Assert.assertEquals(3, cumulatedEvents.size());
- Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent);
- Assert.assertTrue(cumulatedTriples.contains(trpl2));
- Assert.assertTrue(cumulatedTriples.contains(trpl3));
- Assert.assertTrue(cumulatedTriples.contains(trpl4));
- }
-
- private Set<Triple> getCumulatedTriples(List<GraphEvent> cumulatedEvents) {
- Set<Triple> triples = new HashSet<Triple>();
- for(GraphEvent event: cumulatedEvents) {
- triples.add(event.getTriple());
- }
- return triples;
- }
-
- @Test
- public void graphEventTestWithDelay() throws Exception{
- MGraph mGraph = getEmptyMGraph();
- TestGraphListener listener = new TestGraphListener();
- mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null),
- 1000);
-
- Triple triple0 = new TripleImpl(uriRef2, uriRef2, literal1);
- Triple triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1);
- Triple triple2 = new TripleImpl(uriRef1, uriRef2, literal1);
- Triple triple3 = new TripleImpl(uriRef1, uriRef2, bnode1);
- mGraph.add(triple0);
- mGraph.add(triple1);
- mGraph.add(triple2);
- mGraph.add(triple3);
- Thread.sleep(1500);
- Assert.assertEquals(3, listener.getEvents().size());
- Assert.assertEquals(triple1, listener.getEvents().get(0).getTriple());
- Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
- Assert.assertEquals(triple2, listener.getEvents().get(1).getTriple());
- Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
- Assert.assertEquals(triple3, listener.getEvents().get(2).getTriple());
- Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent);
- }
-
- private static class TestGraphListener implements GraphListener {
- private List<GraphEvent> events = null;
- private List<GraphEvent> cumulatedEvents = new ArrayList<GraphEvent>();
-
- @Override
- public void graphChanged(List<GraphEvent> events) {
- this.events = events;
- Iterator<GraphEvent> eventIter = events.iterator();
- while (eventIter.hasNext()) {
- GraphEvent graphEvent = eventIter.next();
- this.cumulatedEvents.add(graphEvent);
- }
- }
-
- public List<GraphEvent> getEvents() {
- return events;
- }
-
- public List<GraphEvent> getCumulatedEvents() {
- return cumulatedEvents;
- }
-
- public void resetEvents() {
- events = null;
- }
-
- public void resetCumulatedEvents() {
- cumulatedEvents = new ArrayList<GraphEvent>();
- }
- }
-
- private Collection<Triple> toCollection(Iterator<Triple> iterator) {
- Collection<Triple> result = new ArrayList<Triple>();
- while (iterator.hasNext()) {
- result.add(iterator.next());
- }
- return result;
- }
-
- /**
- * Creates a new <code>Triple</code>.
- * @param subject the subject.
- * @param predicate the predicate.
- * @param object the object.
- * @throws IllegalArgumentException If an attribute is <code>null</code>.
- */
- private Triple createTriple(String subject, String predicate,
- String object) {
- return new TripleImpl(new UriRef(subject), new UriRef(predicate),
- new UriRef(object));
- }
-
-}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java
----------------------------------------------------------------------
diff --git a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java
deleted file mode 100644
index 0eedb0d..0000000
--- a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/MGraphWrapper.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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.clerezza.rdf.core.test;
-
-import org.apache.clerezza.rdf.core.Graph;
-import org.apache.clerezza.rdf.core.MGraph;
-
-/**
- *
- * @author mir
- */
-class MGraphWrapper extends TripleCollectionWrapper implements MGraph{
-
- public MGraphWrapper(MGraph mGraph) {
- super(mGraph);
- }
-
- @Override
- public Graph getGraph() {
- throw new UnsupportedOperationException("Not supported yet.");
- }
-
-}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java
----------------------------------------------------------------------
diff --git a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java
new file mode 100644
index 0000000..b478b4e
--- /dev/null
+++ b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomGraph.java
@@ -0,0 +1,237 @@
+/*
+ * 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.clerezza.rdf.core.test;
+
+import java.util.Iterator;
+import java.util.UUID;
+import org.apache.commons.rdf.BlankNode;
+import org.apache.commons.rdf.Graph;
+import org.apache.commons.rdf.BlankNodeOrIri;
+import org.apache.commons.rdf.RdfTerm;
+import org.apache.commons.rdf.Triple;
+import org.apache.commons.rdf.Iri;
+import org.apache.commons.rdf.impl.utils.PlainLiteralImpl;
+import org.apache.commons.rdf.impl.utils.TripleImpl;
+import org.apache.commons.lang.RandomStringUtils;
+
+/**
+ * A <code>Graph</code> wrapper that allows growing and shrinking of
+ * the wrapped mgraph.
+ *
+ * @author mir
+ */
+public class RandomGraph extends GraphWrapper {
+
+ private int interconnectivity = 2;
+
+ public RandomGraph(Graph mGraph, int interconnectivity) {
+ super(mGraph);
+ this.interconnectivity = interconnectivity;
+ }
+
+ /**
+ * Creates a new random mutual graph.
+ *
+ * @param initialSize Determines the initial size of the content graph
+ * @param interconnectivity Determines the probability of using already existing
+ * resource when creating a new triple. The probability of using an existing
+ * resource over creating a new resouce is 1-(1/interconnectivity).
+ * @param mGraph
+ */
+ public RandomGraph(int initialSize, int interconnectivity, Graph mGraph) {
+ super(mGraph);
+ if (interconnectivity <= 0) {
+ throw new IllegalArgumentException("growth speed and the interconnectivity "
+ + "value have to be equals or highter one");
+ }
+ this.interconnectivity = interconnectivity;
+
+ setupInitialSize(initialSize);
+ }
+
+ /**
+ * Add or removes randomly a triple.
+ *
+ * @return the triple that was added or removed.
+ */
+ public Triple evolve() {
+ Triple triple;
+ int random = rollDice(2);
+ if (random == 0 && size() != 0) {
+ triple = getRandomTriple();
+ remove(triple);
+ } else {
+ triple = createRandomTriple();
+ add(triple);
+ }
+ return triple;
+ }
+
+ /**
+ * Removes a random triple.
+ *
+ * @return the triple that was removed.
+ */
+ public Triple removeRandomTriple() {
+ Triple randomTriple = getRandomTriple();
+ remove(randomTriple);
+ return randomTriple;
+ }
+
+ /**
+ * Adds a random triple.
+ *
+ * @return the triple that was added.
+ */
+ public Triple addRandomTriple() {
+ Triple randomTriple;
+ do {
+ randomTriple = createRandomTriple();
+ } while(contains(randomTriple));
+
+ add(randomTriple);
+ return randomTriple;
+ }
+
+ private Triple createRandomTriple() {
+ return new TripleImpl(getSubject(), getPredicate(), getObject());
+ }
+
+ private BlankNodeOrIri getSubject() {
+ int random = rollDice(interconnectivity);
+ if (size() == 0) {
+ random = 0;
+ }
+ switch (random) {
+ case 0: // create new BlankNodeOrIri
+ RdfTerm newRdfTerm;
+ do {
+ newRdfTerm = createRandomRdfTerm();
+ } while (!(newRdfTerm instanceof BlankNodeOrIri));
+ return (BlankNodeOrIri) newRdfTerm;
+ default: // get existing BlankNodeOrIri
+ RdfTerm existingRdfTerm;
+ do {
+ existingRdfTerm = getExistingRdfTerm();
+ if (existingRdfTerm == null) {
+ random = 0;
+ }
+ } while (!(existingRdfTerm instanceof BlankNodeOrIri));
+
+ return (BlankNodeOrIri) existingRdfTerm;
+ }
+ }
+
+ private Iri getPredicate() {
+ int random = rollDice(interconnectivity);
+ if (size() == 0) {
+ random = 0;
+ }
+ switch (random) {
+ case 0: // create new Iri
+ return createRandomIri();
+ default: // get existing Iri
+ RdfTerm existingRdfTerm;
+ do {
+ existingRdfTerm = getExistingRdfTerm();
+ if (existingRdfTerm == null) {
+ random = 0;
+ }
+ } while (!(existingRdfTerm instanceof Iri));
+ return (Iri) existingRdfTerm;
+ }
+ }
+
+ private RdfTerm getObject() {
+ int random = rollDice(interconnectivity);
+ if (size() == 0) {
+ random = 0;
+ }
+ switch (random) {
+ case 0: // create new resource
+ return createRandomRdfTerm();
+ default: // get existing resource
+ RdfTerm existingRdfTerm = getExistingRdfTerm();
+ if (existingRdfTerm == null) {
+ random = 0;
+ }
+ return existingRdfTerm;
+ }
+ }
+
+ private static int rollDice(int faces) {
+ return Double.valueOf(Math.random() * faces).intValue();
+ }
+
+ private RdfTerm createRandomRdfTerm() {
+ switch (rollDice(3)) {
+ case 0:
+ return new BlankNode();
+ case 1:
+ return createRandomIri();
+ case 2:
+ return new PlainLiteralImpl(RandomStringUtils.random(rollDice(100) + 1));
+ }
+ throw new RuntimeException("in createRandomRdfTerm()");
+ }
+
+ private RdfTerm getExistingRdfTerm() {
+ Triple triple = getRandomTriple();
+ if (triple == null) {
+ return null;
+ }
+ switch (rollDice(3)) {
+ case 0:
+ return triple.getSubject();
+ case 1:
+ return triple.getPredicate();
+ case 2:
+ return triple.getObject();
+ }
+ return null;
+ }
+
+ private Iri createRandomIri() {
+ return new Iri("http://" + UUID.randomUUID().toString());
+ }
+
+ /**
+ * Returns a random triple contained in the Graph.
+ */
+ public Triple getRandomTriple() {
+ int size = this.size();
+ if (size == 0) {
+ return null;
+ }
+ Iterator<Triple> triples = iterator();
+ while (triples.hasNext()) {
+ Triple triple = triples.next();
+ if (rollDice(this.size()) == 0) {
+ return triple;
+ }
+ }
+ return getRandomTriple();
+ }
+
+ private void setupInitialSize(int initialSize) {
+ for (int i = 0; i < initialSize; i++) {
+ addRandomTriple();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/4bc320a8/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomMGraph.java
----------------------------------------------------------------------
diff --git a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomMGraph.java b/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomMGraph.java
deleted file mode 100644
index 4d805ca..0000000
--- a/rdf.core.test/src/main/java/org/apache/clerezza/rdf/core/test/RandomMGraph.java
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * 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.clerezza.rdf.core.test;
-
-import java.util.Iterator;
-import java.util.UUID;
-import org.apache.clerezza.rdf.core.BNode;
-import org.apache.clerezza.rdf.core.MGraph;
-import org.apache.clerezza.rdf.core.NonLiteral;
-import org.apache.clerezza.rdf.core.Resource;
-import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
-import org.apache.clerezza.rdf.core.impl.TripleImpl;
-import org.apache.commons.lang.RandomStringUtils;
-
-/**
- * A <code>MGraph</code> wrapper that allows growing and shrinking of
- * the wrapped mgraph.
- *
- * @author mir
- */
-public class RandomMGraph extends MGraphWrapper {
-
- private int interconnectivity = 2;
-
- public RandomMGraph(MGraph mGraph, int interconnectivity) {
- super(mGraph);
- this.interconnectivity = interconnectivity;
- }
-
- /**
- * Creates a new random mutual graph.
- *
- * @param initialSize Determines the initial size of the content graph
- * @param interconnectivity Determines the probability of using already existing
- * resource when creating a new triple. The probability of using an existing
- * resource over creating a new resouce is 1-(1/interconnectivity).
- * @param mGraph
- */
- public RandomMGraph(int initialSize, int interconnectivity, MGraph mGraph) {
- super(mGraph);
- if (interconnectivity <= 0) {
- throw new IllegalArgumentException("growth speed and the interconnectivity "
- + "value have to be equals or highter one");
- }
- this.interconnectivity = interconnectivity;
-
- setupInitialSize(initialSize);
- }
-
- /**
- * Add or removes randomly a triple.
- *
- * @return the triple that was added or removed.
- */
- public Triple evolve() {
- Triple triple;
- int random = rollDice(2);
- if (random == 0 && size() != 0) {
- triple = getRandomTriple();
- remove(triple);
- } else {
- triple = createRandomTriple();
- add(triple);
- }
- return triple;
- }
-
- /**
- * Removes a random triple.
- *
- * @return the triple that was removed.
- */
- public Triple removeRandomTriple() {
- Triple randomTriple = getRandomTriple();
- remove(randomTriple);
- return randomTriple;
- }
-
- /**
- * Adds a random triple.
- *
- * @return the triple that was added.
- */
- public Triple addRandomTriple() {
- Triple randomTriple;
- do {
- randomTriple = createRandomTriple();
- } while(contains(randomTriple));
-
- add(randomTriple);
- return randomTriple;
- }
-
- private Triple createRandomTriple() {
- return new TripleImpl(getSubject(), getPredicate(), getObject());
- }
-
- private NonLiteral getSubject() {
- int random = rollDice(interconnectivity);
- if (size() == 0) {
- random = 0;
- }
- switch (random) {
- case 0: // create new NonLiteral
- Resource newResource;
- do {
- newResource = createRandomResource();
- } while (!(newResource instanceof NonLiteral));
- return (NonLiteral) newResource;
- default: // get existing NonLiteral
- Resource existingResource;
- do {
- existingResource = getExistingResource();
- if (existingResource == null) {
- random = 0;
- }
- } while (!(existingResource instanceof NonLiteral));
-
- return (NonLiteral) existingResource;
- }
- }
-
- private UriRef getPredicate() {
- int random = rollDice(interconnectivity);
- if (size() == 0) {
- random = 0;
- }
- switch (random) {
- case 0: // create new UriRef
- return createRandomUriRef();
- default: // get existing UriRef
- Resource existingResource;
- do {
- existingResource = getExistingResource();
- if (existingResource == null) {
- random = 0;
- }
- } while (!(existingResource instanceof UriRef));
- return (UriRef) existingResource;
- }
- }
-
- private Resource getObject() {
- int random = rollDice(interconnectivity);
- if (size() == 0) {
- random = 0;
- }
- switch (random) {
- case 0: // create new resource
- return createRandomResource();
- default: // get existing resource
- Resource existingResource = getExistingResource();
- if (existingResource == null) {
- random = 0;
- }
- return existingResource;
- }
- }
-
- private static int rollDice(int faces) {
- return Double.valueOf(Math.random() * faces).intValue();
- }
-
- private Resource createRandomResource() {
- switch (rollDice(3)) {
- case 0:
- return new BNode();
- case 1:
- return createRandomUriRef();
- case 2:
- return new PlainLiteralImpl(RandomStringUtils.random(rollDice(100) + 1));
- }
- throw new RuntimeException("in createRandomResource()");
- }
-
- private Resource getExistingResource() {
- Triple triple = getRandomTriple();
- if (triple == null) {
- return null;
- }
- switch (rollDice(3)) {
- case 0:
- return triple.getSubject();
- case 1:
- return triple.getPredicate();
- case 2:
- return triple.getObject();
- }
- return null;
- }
-
- private UriRef createRandomUriRef() {
- return new UriRef("http://" + UUID.randomUUID().toString());
- }
-
- /**
- * Returns a random triple contained in the MGraph.
- */
- public Triple getRandomTriple() {
- int size = this.size();
- if (size == 0) {
- return null;
- }
- Iterator<Triple> triples = iterator();
- while (triples.hasNext()) {
- Triple triple = triples.next();
- if (rollDice(this.size()) == 0) {
- return triple;
- }
- }
- return getRandomTriple();
- }
-
- private void setupInitialSize(int initialSize) {
- for (int i = 0; i < initialSize; i++) {
- addRandomTriple();
- }
- }
-}