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 14:46:17 UTC

[3/3] clerezza-rdf-core git commit: CLEREZZA-959: moved graph isomorphisms tests to here

CLEREZZA-959: moved graph isomorphisms tests to here


Project: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/commit/df00e82d
Tree: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/tree/df00e82d
Diff: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/diff/df00e82d

Branch: refs/heads/master
Commit: df00e82df710bc6c29e8e9a9353a92131217af89
Parents: 94a9a13
Author: Reto Gmuer <re...@apache.org>
Authored: Thu Mar 19 13:45:35 2015 +0000
Committer: Reto Gmuer <re...@apache.org>
Committed: Thu Mar 19 13:45:35 2015 +0000

----------------------------------------------------------------------
 .../utils/graphmatching/GraphMatcherTest.java   | 211 +++++++++++++++++++
 .../utils/graphmatching/HashMatchingTest.java   |  51 +++++
 .../graphmatching/PermutationIteratorTest.java  |  78 +++++++
 .../impl/utils/graphmatching/Utils4Testing.java |  51 +++++
 4 files changed, 391 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/df00e82d/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/GraphMatcherTest.java
----------------------------------------------------------------------
diff --git a/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/GraphMatcherTest.java b/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/GraphMatcherTest.java
new file mode 100644
index 0000000..5c43c1b
--- /dev/null
+++ b/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/GraphMatcherTest.java
@@ -0,0 +1,211 @@
+/*
+ * 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.impl.utils.graphmatching;
+
+import org.apache.commons.rdf.impl.utils.graphmatching.GraphMatcher;
+import java.util.Map;
+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.Graph;
+import org.apache.commons.rdf.Iri;
+import org.apache.commons.rdf.impl.utils.simple.SimpleMGraph;
+import org.apache.commons.rdf.impl.utils.TripleImpl;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * @author reto
+ */
+public class GraphMatcherTest {
+
+    final static Iri u1 = new Iri("http://example.org/u1");
+
+    @Test
+    public void testEmpty() {
+        Graph tc1 = new SimpleMGraph();
+        Graph tc2 = new SimpleMGraph();
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        Assert.assertEquals(0, mapping.size());
+    }
+
+    @Test
+    public void test2() {
+        Graph tc1 = new SimpleMGraph();
+        tc1.add(new TripleImpl(u1, u1, u1));
+        Graph tc2 = new SimpleMGraph();
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNull(mapping);
+    }
+
+    @Test
+    public void test3() {
+        Graph tc1 = new SimpleMGraph();
+        tc1.add(new TripleImpl(u1, u1, u1));
+        Graph tc2 = new SimpleMGraph();
+        tc2.add(new TripleImpl(u1, u1, u1));
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        Assert.assertEquals(0, mapping.size());
+    }
+
+    @Test
+    public void test4() {
+        Graph tc1 = new SimpleMGraph();
+        tc1.add(new TripleImpl(u1, u1, new BlankNode()));
+        Graph tc2 = new SimpleMGraph();
+        tc2.add(new TripleImpl(u1, u1, new BlankNode()));
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        Assert.assertEquals(1, mapping.size());
+    }
+
+    @Test
+    public void test5() {
+        Graph tc1 = new SimpleMGraph();
+        tc1.add(new TripleImpl(new BlankNode(), u1, new BlankNode()));
+        Graph tc2 = new SimpleMGraph();
+        tc2.add(new TripleImpl(new BlankNode(), u1, new BlankNode()));
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        Assert.assertEquals(2, mapping.size());
+    }
+
+    @Test
+    public void test6() {
+        Graph tc1 = new SimpleMGraph();
+        final BlankNode b11 = new BlankNode();
+        tc1.add(new TripleImpl(new BlankNode(), u1,b11));
+        tc1.add(new TripleImpl(new BlankNode(), u1,b11));
+        Graph tc2 = new SimpleMGraph();
+        tc2.add(new TripleImpl(new BlankNode(), u1, new BlankNode()));
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNull(mapping);
+    }
+
+    private Graph generateCircle(int size) {
+        return generateCircle(size, new BlankNode());
+    }
+
+    private Graph generateCircle(int size, final BlankNodeOrIri firstNode) {
+        if (size < 1) {
+            throw new IllegalArgumentException();
+        }
+        Graph result = new SimpleMGraph();
+        BlankNodeOrIri lastNode = firstNode;
+        for (int i = 0; i < (size-1); i++) {
+            final BlankNode newNode = new BlankNode();
+            result.add(new TripleImpl(lastNode, u1, newNode));
+            lastNode = newNode;
+        }
+        result.add(new TripleImpl(lastNode, u1, firstNode));
+        return result;
+    }
+
+    @Test
+    public void test7() {
+        Graph tc1 = generateCircle(2);
+        Graph tc2 = generateCircle(2);
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        Assert.assertEquals(2, mapping.size());
+    }
+
+    @Test
+    public void test8() {
+        Graph tc1 = generateCircle(5);
+        Graph tc2 = generateCircle(5);
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        Assert.assertEquals(5, mapping.size());
+    }
+
+    @Test
+    public void test9() {
+        BlankNodeOrIri crossing = new Iri("http://example.org/");
+        Graph tc1 = generateCircle(2,crossing);
+        tc1.addAll(generateCircle(3,crossing));
+        Graph tc2 = generateCircle(2,crossing);
+        tc2.addAll(generateCircle(3,crossing));
+        Assert.assertEquals(5, tc1.size());
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        //a circle of 2 with 1 bnode and one of 2 bnodes
+        Assert.assertEquals(3, mapping.size());
+    }
+
+    @Test
+    public void test10() {
+        BlankNodeOrIri crossing1 = new BlankNode();
+        Graph tc1 = generateCircle(2,crossing1);
+        tc1.addAll(generateCircle(3,crossing1));
+        BlankNodeOrIri crossing2 = new BlankNode();
+        Graph tc2 = generateCircle(2,crossing2);
+        tc2.addAll(generateCircle(3,crossing2));
+        Assert.assertEquals(5, tc1.size());
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        //a circle of 2 and one of 3 with one common node
+        Assert.assertEquals(4, mapping.size());
+    }
+
+    @Test
+    public void test11() {
+        BlankNodeOrIri crossing1 = new BlankNode();
+        Graph tc1 = generateCircle(2,crossing1);
+        tc1.addAll(generateCircle(4,crossing1));
+        BlankNodeOrIri crossing2 = new BlankNode();
+        Graph tc2 = generateCircle(3,crossing2);
+        tc2.addAll(generateCircle(3,crossing2));
+        Assert.assertEquals(6, tc1.size());
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNull(mapping);
+    }
+
+    @Test
+    public void test12() {
+        BlankNodeOrIri start1 = new BlankNode();
+        Graph tc1 = Utils4Testing.generateLine(4,start1);
+        tc1.addAll(Utils4Testing.generateLine(5,start1));
+        BlankNodeOrIri start2 = new BlankNode();
+        Graph tc2 = Utils4Testing.generateLine(5,start2);
+        tc2.addAll(Utils4Testing.generateLine(4,start2));
+        Assert.assertEquals(9, tc1.size());
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNotNull(mapping);
+        Assert.assertEquals(10, mapping.size());
+    }
+
+    @Test
+    public void test13() {
+        BlankNodeOrIri start1 = new BlankNode();
+        Graph tc1 = Utils4Testing.generateLine(4,start1);
+        tc1.addAll(Utils4Testing.generateLine(5,start1));
+        BlankNodeOrIri start2 = new BlankNode();
+        Graph tc2 = Utils4Testing.generateLine(3,start2);
+        tc2.addAll(Utils4Testing.generateLine(3,start2));
+        Assert.assertEquals(9, tc1.size());
+        final Map<BlankNode, BlankNode> mapping = GraphMatcher.getValidMapping(tc1, tc2);
+        Assert.assertNull(mapping);
+    }
+}

http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/df00e82d/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/HashMatchingTest.java
----------------------------------------------------------------------
diff --git a/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/HashMatchingTest.java b/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/HashMatchingTest.java
new file mode 100644
index 0000000..baac5b9
--- /dev/null
+++ b/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/HashMatchingTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.impl.utils.graphmatching;
+
+
+import java.util.Map;
+
+import org.apache.commons.rdf.BlankNode;
+import org.apache.commons.rdf.Graph;
+import org.apache.commons.rdf.BlankNodeOrIri;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * @author reto
+ */
+public class HashMatchingTest {
+
+    @Test
+    public void twoLine() throws GraphNotIsomorphicException {
+        BlankNodeOrIri start1 = new BlankNode();
+        Graph tc1 = Utils4Testing.generateLine(4,start1);
+        tc1.addAll(Utils4Testing.generateLine(5,start1));
+        BlankNodeOrIri start2 = new BlankNode();
+        Graph tc2 = Utils4Testing.generateLine(5,start2);
+        tc2.addAll(Utils4Testing.generateLine(4,start2));
+        Assert.assertEquals(9, tc1.size());
+        final Map<BlankNode, BlankNode> mapping = new HashMatching(tc1, tc2).getMatchings();
+        Assert.assertNotNull(mapping);
+        Assert.assertEquals(10, mapping.size());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/df00e82d/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/PermutationIteratorTest.java
----------------------------------------------------------------------
diff --git a/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/PermutationIteratorTest.java b/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/PermutationIteratorTest.java
new file mode 100644
index 0000000..6616060
--- /dev/null
+++ b/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/PermutationIteratorTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.impl.utils.graphmatching;
+
+import org.apache.commons.rdf.impl.utils.graphmatching.PermutationIterator;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * @author reto
+ */
+public class PermutationIteratorTest {
+
+    @Test
+    public void simple() {
+        List<String> list = new ArrayList<String>();
+        PermutationIterator<String> pi = new PermutationIterator<String>(list);
+        Assert.assertFalse(pi.hasNext());
+    }
+
+    @Test
+    public void lessSimple() {
+        List<String> list = new ArrayList<String>();
+        list.add("Hasan");
+        PermutationIterator<String> pi = new PermutationIterator<String>(list);
+        Assert.assertTrue(pi.hasNext());
+    }
+
+    @Test
+    public void regular() {
+        List<String> list = new ArrayList<String>();
+        list.add("Hasan");
+        list.add("Tsuy");
+        PermutationIterator<String> pi = new PermutationIterator<String>(list);
+        Set<List<String>> permutations = new HashSet<List<String>>();
+        while (pi.hasNext()) {
+            permutations.add(pi.next());
+        }
+        Assert.assertEquals(2, permutations.size());
+    }
+
+    @Test
+    public void extended() {
+        List<String> list = new ArrayList<String>();
+        list.add("Hasan");
+        list.add("Tsuy");
+        list.add("Llena");
+        PermutationIterator<String> pi = new PermutationIterator<String>(list);
+        Set<List<String>> permutations = new HashSet<List<String>>();
+        while (pi.hasNext()) {
+            permutations.add(pi.next());
+        }
+        Assert.assertEquals(6, permutations.size());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/df00e82d/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/Utils4Testing.java
----------------------------------------------------------------------
diff --git a/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/Utils4Testing.java b/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/Utils4Testing.java
new file mode 100644
index 0000000..3246575
--- /dev/null
+++ b/impl.utils/src/test/java/org/apache/commons/rdf/impl/utils/graphmatching/Utils4Testing.java
@@ -0,0 +1,51 @@
+/*
+ * 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.impl.utils.graphmatching;
+
+import org.apache.commons.rdf.BlankNode;
+import org.apache.commons.rdf.Graph;
+import org.apache.commons.rdf.BlankNodeOrIri;
+import org.apache.commons.rdf.Iri;
+import org.apache.commons.rdf.impl.utils.simple.SimpleMGraph;
+import org.apache.commons.rdf.impl.utils.TripleImpl;
+
+/**
+ *
+ * @author reto
+ */
+public class Utils4Testing {
+
+    static Graph generateLine(int size, final BlankNodeOrIri firstNode) {
+        if (size < 1) {
+            throw new IllegalArgumentException();
+        }
+        Graph result = new SimpleMGraph();
+        BlankNodeOrIri lastNode = firstNode;
+        for (int i = 0; i < size; i++) {
+            final BlankNode newNode = new BlankNode();
+            result.add(new TripleImpl(lastNode, u1, newNode));
+            lastNode = newNode;
+        }
+        return result;
+    }
+
+    final static Iri u1 = new Iri("http://example.org/u1");
+
+}