001 /**
002 * Copyright (C) 2007-2008, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program. If not, see <http://www.gnu.org/licenses/>.
018 *
019 */
020 package org.dllearner.test.junit;
021
022 import static org.junit.Assert.*;
023
024 import java.util.TreeSet;
025
026 import org.dllearner.algorithms.el.ELDescriptionNode;
027 import org.dllearner.algorithms.el.ELDescriptionTree;
028 import org.dllearner.algorithms.el.ELDescriptionTreeComparator;
029 import org.dllearner.algorithms.el.Simulation;
030 import org.dllearner.algorithms.el.TreeTuple;
031 import org.dllearner.core.ComponentInitException;
032 import org.dllearner.core.ReasonerComponent;
033 import org.dllearner.core.owl.Description;
034 import org.dllearner.core.owl.NamedClass;
035 import org.dllearner.core.owl.ObjectProperty;
036 import org.dllearner.parser.KBParser;
037 import org.dllearner.parser.ParseException;
038 import org.dllearner.test.junit.TestOntologies.TestOntology;
039 import org.dllearner.utilities.Helper;
040 import org.dllearner.utilities.owl.ConceptTransformation;
041 import org.junit.Test;
042
043 /**
044 * Tests related to EL description tree including operations on
045 * them, simulations, equivalence checks, minimisation etc.
046 *
047 * @author Jens Lehmann
048 *
049 */
050 public final class ELDescriptionTreeTests {
051
052 @Test
053 public void simulationTest() {
054 ReasonerComponent rs = TestOntologies.getTestOntology(TestOntology.EMPTY);
055 Simulation s = new Simulation();
056 ELDescriptionTree tree1 = new ELDescriptionTree(rs);
057 ELDescriptionTree tree2 = new ELDescriptionTree(rs);
058 ELDescriptionNode t1 = new ELDescriptionNode(tree1);
059 ELDescriptionNode t2 = new ELDescriptionNode(tree2);
060 TreeTuple tuple1 = new TreeTuple(t1,t2);
061 s.addTuple(tuple1);
062 assertTrue(s.in(t2).size() == 1);
063 // assertTrue(s.out(t2).size() == 0);
064 ObjectProperty p = new ObjectProperty("p");
065 TreeSet<NamedClass> l3 = new TreeSet<NamedClass>();
066 ELDescriptionNode t3 = new ELDescriptionNode(t1,p,l3);
067 assertTrue(t3.getLevel() == 2);
068 assertTrue(tree1.getMaxLevel() == 2);
069 }
070
071 @Test
072 public void minimalityTest() throws ParseException, ComponentInitException {
073 ReasonerComponent rs = TestOntologies.getTestOntology(TestOntology.SIMPLE);
074 // the following should be recognized as non-minimal
075 Description d = KBParser.parseConcept("(human AND (EXISTS has.animal AND EXISTS has.TOP))");
076 ConceptTransformation.cleanConcept(d);
077 ELDescriptionTree tree = new ELDescriptionTree(rs, d);
078 assertFalse(tree.isMinimal());
079 }
080
081 @Test
082 public void cloneTest() throws ParseException {
083 ReasonerComponent rs = TestOntologies.getTestOntology(TestOntology.EMPTY);
084 Description d = KBParser.parseConcept("(male AND (human AND EXISTS hasChild.(female AND EXISTS hasChild.male)))");
085 ConceptTransformation.cleanConcept(d);
086 ELDescriptionTree tree = new ELDescriptionTree(rs, d);
087 // clone performance (false for simple unit test, true for clone performance test)
088 boolean testPerformance = true;
089 ELDescriptionTree treeCloned = null;
090 if(testPerformance) {
091 int runs = 1000000;
092 long startTime = System.nanoTime();
093 for(int i=0; i<runs; i++) {
094 treeCloned = tree.clone();
095 }
096 long runTime = System.nanoTime() - startTime;
097 System.out.println(Helper.prettyPrintNanoSeconds(runTime/runs, true, true) + " per clone operation");
098 } else {
099 treeCloned = tree.clone();
100 }
101
102 ELDescriptionTreeComparator comparator = new ELDescriptionTreeComparator();
103 assertTrue(comparator.compare(tree, treeCloned) == 0);
104 }
105
106 }