Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
CLEREZZA-959: moved graph isomorphisms tests to here
  • Loading branch information
retog committed Mar 19, 2015
1 parent 94a9a13 commit df00e82df710bc6c29e8e9a9353a92131217af89
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 0 deletions.
@@ -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);
}
}
@@ -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());
}

}
@@ -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());
}

}
@@ -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");

}

0 comments on commit df00e82

Please sign in to comment.