From: <jen...@us...> - 2008-11-04 17:03:02
|
Revision: 1491 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1491&view=rev Author: jenslehmann Date: 2008-11-04 17:02:56 +0000 (Tue, 04 Nov 2008) Log Message: ----------- - three EL description tree simulation tests - started alternative tree clone method Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/algorithms/el/ELDescriptionNode.java trunk/src/dl-learner/org/dllearner/algorithms/el/ELDescriptionTree.java trunk/src/dl-learner/org/dllearner/test/junit/TestOntologies.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/test/junit/SimulationTests.java Modified: trunk/src/dl-learner/org/dllearner/algorithms/el/ELDescriptionNode.java =================================================================== --- trunk/src/dl-learner/org/dllearner/algorithms/el/ELDescriptionNode.java 2008-11-04 16:27:07 UTC (rev 1490) +++ trunk/src/dl-learner/org/dllearner/algorithms/el/ELDescriptionNode.java 2008-11-04 17:02:56 UTC (rev 1491) @@ -20,6 +20,7 @@ package org.dllearner.algorithms.el; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -57,17 +58,17 @@ public class ELDescriptionNode { // the reference tree for storing values, must not be null - private ELDescriptionTree tree; + protected ELDescriptionTree tree; - private NavigableSet<NamedClass> label; + protected TreeSet<NamedClass> label; - private List<ELDescriptionEdge> edges; + protected List<ELDescriptionEdge> edges; - private int level; + protected int level; // parent node in the tree; // null indicates that this node is a root node - private ELDescriptionNode parent = null; + protected ELDescriptionNode parent = null; // simulation information (list or set?) protected Set<ELDescriptionNode> in = new HashSet<ELDescriptionNode>(); @@ -78,6 +79,13 @@ protected Set<ELDescriptionNode> outSC2 = new HashSet<ELDescriptionNode>(); /** + * Internal constructor used for cloning nodes. + */ + protected ELDescriptionNode() { + + } + + /** * Constructs an EL description tree with empty root label. */ public ELDescriptionNode(ELDescriptionTree tree) { @@ -88,7 +96,7 @@ * Constructs an EL description tree given its root label. * @param label Label of the root node. */ - public ELDescriptionNode(ELDescriptionTree tree, NavigableSet<NamedClass> label) { + public ELDescriptionNode(ELDescriptionTree tree, TreeSet<NamedClass> label) { this.label = label; this.edges = new LinkedList<ELDescriptionEdge>(); this.tree = tree; @@ -97,11 +105,14 @@ // this is the root node of the overall tree tree.rootNode = this; tree.addNodeToLevel(this, level); - - // TODO simulation initialization } - public ELDescriptionNode(ELDescriptionNode parentNode, ObjectProperty parentProperty, NavigableSet<NamedClass> label) { + // convenience constructor + public ELDescriptionNode(ELDescriptionNode parentNode, ObjectProperty parentProperty, NamedClass... label) { + this(parentNode, parentProperty, new TreeSet<NamedClass>(Arrays.asList(label))); + } + + public ELDescriptionNode(ELDescriptionNode parentNode, ObjectProperty parentProperty, TreeSet<NamedClass> label) { this.label = label; this.edges = new LinkedList<ELDescriptionEdge>(); parent = parentNode; @@ -394,4 +405,46 @@ public ELDescriptionNode getParent() { return parent; } + + /** + * @return the in + */ + public Set<ELDescriptionNode> getIn() { + return in; + } + + /** + * @return the inSC1 + */ + public Set<ELDescriptionNode> getInSC1() { + return inSC1; + } + + /** + * @return the inSC2 + */ + public Set<ELDescriptionNode> getInSC2() { + return inSC2; + } + + /** + * @return the out + */ + public Set<ELDescriptionNode> getOut() { + return out; + } + + /** + * @return the outSC1 + */ + public Set<ELDescriptionNode> getOutSC1() { + return outSC1; + } + + /** + * @return the outSC2 + */ + public Set<ELDescriptionNode> getOutSC2() { + return outSC2; + } } Modified: trunk/src/dl-learner/org/dllearner/algorithms/el/ELDescriptionTree.java =================================================================== --- trunk/src/dl-learner/org/dllearner/algorithms/el/ELDescriptionTree.java 2008-11-04 16:27:07 UTC (rev 1490) +++ trunk/src/dl-learner/org/dllearner/algorithms/el/ELDescriptionTree.java 2008-11-04 17:02:56 UTC (rev 1491) @@ -26,7 +26,9 @@ import java.util.Map; import java.util.NavigableSet; import java.util.Set; +import java.util.TreeMap; import java.util.TreeSet; +import java.util.Map.Entry; import org.dllearner.core.ReasoningService; import org.dllearner.core.owl.Description; @@ -336,6 +338,62 @@ node2.inSC2.remove(node1); } + @SuppressWarnings("unchecked") + public ELDescriptionTree cloneNew() { + // clone "global" tree + ELDescriptionTree treeClone = new ELDescriptionTree(rs); + + // a mapping between "old" and "new" nodes + // (hash map should be fast here, but one could also + // experiment with TreeMap) + Map<ELDescriptionNode, ELDescriptionNode> cloneMap = + new HashMap<ELDescriptionNode, ELDescriptionNode>(); + + // create a new (empty) node for each node in the tree + // (we loop through the level mapping, because it is cheaper + // than creating a set of all nodes) + for(int i=1; i<=maxLevel; i++) { + Set<ELDescriptionNode> tmp = levelNodeMapping.get(i); + for(ELDescriptionNode node : tmp) { + ELDescriptionNode nodeNew = new ELDescriptionNode(); + cloneMap.put(node, nodeNew); + } + } + + ELDescriptionNode newRoot = null; + + // loop through all nodes and perform copy operations + for(Entry<ELDescriptionNode, ELDescriptionNode> entry : cloneMap.entrySet()) { + ELDescriptionNode oldNode = entry.getKey(); + ELDescriptionNode newNode = entry.getValue(); + + newNode.tree = treeClone; + newNode.level = oldNode.level; + newNode.label = (TreeSet<NamedClass>) oldNode.label.clone(); + if(oldNode.parent != null) { + newNode.parent = cloneMap.get(oldNode.parent); + } else { + newRoot = newNode; + } + + // TODO: edges, simulation information ... + } + + // update global tree + treeClone.rootNode = newRoot; + treeClone.maxLevel = maxLevel; + for(int i=1; i<=maxLevel; i++) { + Set<ELDescriptionNode> oldNodes = levelNodeMapping.get(i); + Set<ELDescriptionNode> newNodes = new HashSet<ELDescriptionNode>(); + for(ELDescriptionNode oldNode : oldNodes) { + newNodes.add(cloneMap.get(oldNode)); + } + treeClone.levelNodeMapping.put(i, newNodes); + } + + return treeClone; + } + @Override public ELDescriptionTree clone() { // create a new reference tree Added: trunk/src/dl-learner/org/dllearner/test/junit/SimulationTests.java =================================================================== --- trunk/src/dl-learner/org/dllearner/test/junit/SimulationTests.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/test/junit/SimulationTests.java 2008-11-04 17:02:56 UTC (rev 1491) @@ -0,0 +1,213 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.test.junit; + +import static org.junit.Assert.*; + +import java.util.TreeSet; + +import org.dllearner.algorithms.el.ELDescriptionNode; +import org.dllearner.algorithms.el.ELDescriptionTree; +import org.dllearner.core.ReasoningService; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.test.junit.TestOntologies.TestOntology; +import org.junit.Test; + +/** + * We test whether level-restricted, non-reflexive simulations on + * EL description trees are correctly computed. + * + * @author Jens Lehmann + * + */ +public class SimulationTests { + + @Test + public void test1() { + // perform test with empty background knowledge and TOP concept + ReasoningService rs = TestOntologies.getTestOntology(TestOntology.EMPTY); + ELDescriptionTree tree = new ELDescriptionTree(rs); + ELDescriptionNode root = new ELDescriptionNode(tree); + + // simulation relation should be empty + assertEmpty(root); + } + + /** + * v1:{A1} + * / \ + * r1 r2 + * / \ + * v2:{} v3:{} + * + * v1: - + * v2: in=inSC1=inSC2=out=outSC1=outSC2={v3} + * v3: in=inSC1=inSC2=out=outSC1=outSC2={v2} + */ + @Test + public void test2() { + // perform test with empty background knowledge and A1 AND EXISTS r1.TOP AND EXISTS r2.TOP + ReasoningService rs = TestOntologies.getTestOntology(TestOntology.EMPTY); + ELDescriptionTree tree = new ELDescriptionTree(rs); + ELDescriptionNode v1 = new ELDescriptionNode(tree); + ObjectProperty r1 = new ObjectProperty("r1"); + ELDescriptionNode v2 = new ELDescriptionNode(v1, r1, new TreeSet<NamedClass>()); + ObjectProperty r2 = new ObjectProperty("r2"); + ELDescriptionNode v3 = new ELDescriptionNode(v1, r2, new TreeSet<NamedClass>()); + + assertEmpty(v1); + assertAll(v2, v3); + assertAll(v3, v2); + } + + /** + * K: r1 \sqsubset r2 + * + * v1:{} + * / | \ + * r1 r1 r2 + * / | \ + * v2:{A1,A2} v3:{A2} v4:{A1} + * + * v1: - + * v2: in=inSC1=inSC2=outSC2={v3,v4} + * v3: inSC2=outSC2={v2,v4} + * v4: inSC2=outSC2={v2,v3} + */ + @Test + public void test3() { + ReasoningService rs = TestOntologies.getTestOntology(TestOntology.R1SUBR2); + ELDescriptionTree tree = new ELDescriptionTree(rs); + ELDescriptionNode v1 = new ELDescriptionNode(tree); + ObjectProperty r1 = new ObjectProperty("r1"); + NamedClass a1 = new NamedClass("a1"); + NamedClass a2 = new NamedClass("a2"); + ELDescriptionNode v2 = new ELDescriptionNode(v1, r1, a1, a2); + ELDescriptionNode v3 = new ELDescriptionNode(v1, r1, a2); + ObjectProperty r2 = new ObjectProperty("r2"); + ELDescriptionNode v4 = new ELDescriptionNode(v1, r2, a1); + + assertEmpty(v1); + + assertAllIn(v2, v3, v4); + assertOutSC2(v2, v3, v4); + assertOutSC1(v2); + assertOut(v2); + + assertSC2(v3, v2, v4); + assertSC1(v3); + assertSC(v3); + + assertSC2(v4, v2, v3); + assertSC1(v4); + assertSC(v4); + } + + // all relations (in, inSC1, inSC2) should have the + // the specified node set as value + private void assertAll(ELDescriptionNode node, ELDescriptionNode... nodes) { + assertAllIn(node, nodes); + assertAllOut(node, nodes); + } + + // all in relations (in, inSC1, inSC2) should have the + // specified node set as value + private void assertAllIn(ELDescriptionNode node, ELDescriptionNode... nodesIn) { + assertIn(node, nodesIn); + assertInSC1(node, nodesIn); + assertInSC2(node, nodesIn); + } + + // all out relations (out, outSC1, outSC2) should have the + // specified node set as value + private void assertAllOut(ELDescriptionNode node, ELDescriptionNode... nodesOut) { + assertOut(node, nodesOut); + assertOutSC1(node, nodesOut); + assertOutSC2(node, nodesOut); + } + + private void assertSC(ELDescriptionNode node, ELDescriptionNode... nodesOut) { + assertIn(node, nodesOut); + assertOut(node, nodesOut); + } + + private void assertSC1(ELDescriptionNode node, ELDescriptionNode... nodesOut) { + assertInSC1(node, nodesOut); + assertOutSC1(node, nodesOut); + } + + private void assertSC2(ELDescriptionNode node, ELDescriptionNode... nodesOut) { + assertInSC2(node, nodesOut); + assertOutSC2(node, nodesOut); + } + + private void assertInSC1(ELDescriptionNode node, ELDescriptionNode... nodes) { + for(ELDescriptionNode nodeTmp : nodes) { + assertTrue(node.getInSC1().contains(nodeTmp)); + } + assertTrue(node.getInSC1().size() == nodes.length); + } + + private void assertInSC2(ELDescriptionNode node, ELDescriptionNode... nodes) { + for(ELDescriptionNode nodeTmp : nodes) { + assertTrue(node.getInSC2().contains(nodeTmp)); + } + assertTrue(node.getInSC2().size() == nodes.length); + } + + private void assertIn(ELDescriptionNode node, ELDescriptionNode... nodes) { + for(ELDescriptionNode nodeTmp : nodes) { + assertTrue(node.getIn().contains(nodeTmp)); + } + assertTrue(node.getIn().size() == nodes.length); + } + + private void assertOutSC2(ELDescriptionNode node, ELDescriptionNode... nodes) { + for(ELDescriptionNode nodeTmp : nodes) { + assertTrue(node.getOutSC2().contains(nodeTmp)); + } + assertTrue(node.getOutSC2().size() == nodes.length); + } + + private void assertOutSC1(ELDescriptionNode node, ELDescriptionNode... nodes) { + for(ELDescriptionNode nodeTmp : nodes) { + assertTrue(node.getOutSC1().contains(nodeTmp)); + } + assertTrue(node.getOutSC1().size() == nodes.length); + } + + private void assertOut(ELDescriptionNode node, ELDescriptionNode... nodes) { + for(ELDescriptionNode nodeTmp : nodes) { + assertTrue(node.getOut().contains(nodeTmp)); + } + assertTrue(node.getOut().size() == nodes.length); + } + + // all simulation relations should be empty for this node + private void assertEmpty(ELDescriptionNode node) { + assertTrue(node.getIn().isEmpty()); + assertTrue(node.getInSC1().isEmpty()); + assertTrue(node.getInSC2().isEmpty()); + assertTrue(node.getOut().isEmpty()); + assertTrue(node.getOutSC1().isEmpty()); + assertTrue(node.getOutSC2().isEmpty()); + } +} Modified: trunk/src/dl-learner/org/dllearner/test/junit/TestOntologies.java =================================================================== --- trunk/src/dl-learner/org/dllearner/test/junit/TestOntologies.java 2008-11-04 16:27:07 UTC (rev 1490) +++ trunk/src/dl-learner/org/dllearner/test/junit/TestOntologies.java 2008-11-04 17:02:56 UTC (rev 1491) @@ -37,7 +37,7 @@ */ public final class TestOntologies { - public enum TestOntology { EMPTY, SIMPLE }; + public enum TestOntology { EMPTY, SIMPLE, R1SUBR2 }; public static ReasoningService getTestOntology(TestOntology ont) { String kbString = ""; @@ -55,6 +55,8 @@ kbString += "bird SUB animal.\n"; kbString += "cat SUB animal.\n"; kbString += "(human AND animal) = BOTTOM.\n"; + } else if(ont.equals(TestOntology.R1SUBR2)) { + kbString += "Subrole(r1,r2).\n"; } try { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |