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.assertTrue;
023
024 import java.io.File;
025 import java.net.MalformedURLException;
026 import java.util.LinkedList;
027 import java.util.List;
028 import java.util.SortedSet;
029
030 import org.apache.log4j.Logger;
031 import org.dllearner.core.ComponentInitException;
032 import org.dllearner.core.ComponentManager;
033 import org.dllearner.core.KnowledgeSource;
034 import org.dllearner.core.ReasonerComponent;
035 import org.dllearner.core.owl.Description;
036 import org.dllearner.core.owl.Individual;
037 import org.dllearner.core.owl.KB;
038 import org.dllearner.kb.KBFile;
039 import org.dllearner.kb.OWLFile;
040 import org.dllearner.parser.KBParser;
041 import org.dllearner.parser.ParseException;
042 import org.dllearner.reasoning.FastInstanceChecker;
043 import org.dllearner.reasoning.OWLAPIReasoner;
044 import org.junit.Test;
045
046 /**
047 * A suite of JUnit tests related to the DL-Learner reasoning.
048 *
049 * @author Jens Lehmann
050 *
051 */
052 public class ReasonerTests {
053
054 private static Logger logger = Logger.getLogger(ReasonerTests.class);
055
056 private String baseURI;
057
058 public KB getSimpleKnowledgeBase() {
059 String kb = "person SUB TOP.";
060 kb += "man SUB person.";
061 kb += "man SUB male.";
062 kb += "woman SUB person.";
063 kb += "woman SUB female.";
064 kb += "(male AND female) = BOTTOM.";
065 kb += "man(stephen).";
066 kb += "woman(maria).";
067 kb += "hasChild(stephen,maria).";
068 KB kbObject = null;
069 try {
070 kbObject = KBParser.parseKBFile(kb);
071 } catch (ParseException e) {
072 e.printStackTrace();
073 }
074 return kbObject;
075 }
076
077 /**
078 * Performs an instance checks on all reasoner components to verify that
079 * they all return the correct result.
080 */
081 // @Test
082 public void instanceCheckTest() {
083 try {
084 ComponentManager cm = ComponentManager.getInstance();
085 KB kb = getSimpleKnowledgeBase();
086 KnowledgeSource ks = new KBFile(kb);
087 ks.init();
088 Description d;
089 // d = KBParser.parseConcept("man");
090 d = KBParser.parseConcept("(person AND EXISTS hasChild.female)");
091 Individual i = new Individual(KBParser.getInternalURI("stephen"));
092 List<Class<? extends ReasonerComponent>> reasonerClasses = cm.getReasonerComponents();
093 for (Class<? extends ReasonerComponent> reasonerClass : reasonerClasses) {
094 ReasonerComponent reasoner = cm.reasoner(reasonerClass, ks);
095 reasoner.init();
096 // long startTime = System.nanoTime();
097 boolean result = false;
098 // for(int n=0; n<10000; n++) {
099 result = reasoner.hasType(d, i);
100 // }
101 // long time = System.nanoTime() - startTime;
102 logger.debug("instance check: " + reasoner + " " + d + " " + i + " " + result);
103 assertTrue(result);
104 }
105 } catch (ParseException e) {
106 e.printStackTrace();
107 // } catch (ReasoningMethodUnsupportedException e) {
108 // e.printStackTrace();
109 } catch (ComponentInitException e) {
110 e.printStackTrace();
111 }
112 }
113
114 /**
115 * Test of fast instance check algorithm on carcinogenesis data set.
116 * @throws ComponentInitException
117 * @throws ParseException
118 */
119 // @Test
120 public void fastInstanceCheckTest() throws ComponentInitException, ParseException {
121 String file = "examples/carcinogenesis/carcinogenesis.owl";
122 ComponentManager cm = ComponentManager.getInstance();
123 KnowledgeSource ks = cm.knowledgeSource(OWLFile.class);
124 try {
125 cm.applyConfigEntry(ks, "url", new File(file).toURI().toURL());
126 } catch (MalformedURLException e) {
127 // should never happen
128 e.printStackTrace();
129 }
130 ks.init();
131 ReasonerComponent reasoner = cm.reasoner(OWLAPIReasoner.class, ks);
132 reasoner.init();
133 baseURI = reasoner.getBaseURI();
134
135 List<Description> testDescriptions = new LinkedList<Description>();
136 List<List<Individual>> posIndividuals = new LinkedList<List<Individual>>();
137 List<List<Individual>> negIndividuals = new LinkedList<List<Individual>>();
138
139 // TODO manually verify that the results are indeed correct
140 testDescriptions.add(KBParser.parseConcept("\"http://dl-learner.org/carcinogenesis#Compound\" AND (\"http://dl-learner.org/carcinogenesis#amesTestPositive\" = true OR >= 2 \"http://dl-learner.org/carcinogenesis#hasStructure\" \"http://dl-learner.org/carcinogenesis#Ar_halide\"))"));
141 posIndividuals.add(getIndSet("d113","d133","d171","d262","d265","d294","d68","d77","d79"));
142 negIndividuals.add(getIndSet("d139","d199","d202","d203","d283","d42"));
143
144 // TODO add more descriptions and instances
145
146 // make the specified assertions
147 for(int i=0; i<testDescriptions.size(); i++) {
148 Description description = testDescriptions.get(i);
149 List<Individual> pos = posIndividuals.get(i);
150 List<Individual> neg = negIndividuals.get(i);
151
152 for(Individual ind : pos) {
153 assertTrue(reasoner.hasType(description, ind));
154 }
155
156 for(Individual ind : neg) {
157 assertTrue(!reasoner.hasType(description, ind));
158 }
159 }
160 }
161
162 // @Test
163 public void fastInstanceCheck2() throws ComponentInitException, ParseException {
164 String file = "examples/epc/sap_epc.owl";
165 ComponentManager cm = ComponentManager.getInstance();
166 KnowledgeSource ks = cm.knowledgeSource(OWLFile.class);
167 try {
168 cm.applyConfigEntry(ks, "url", new File(file).toURI().toURL());
169 } catch (MalformedURLException e) {
170 // should never happen
171 e.printStackTrace();
172 }
173 ks.init();
174 ReasonerComponent reasoner = cm.reasoner(FastInstanceChecker.class, ks);
175 reasoner.init();
176 baseURI = reasoner.getBaseURI();
177
178 Description description = KBParser.parseConcept("(\"http://localhost/aris/sap_model.owl#EPC\" AND EXISTS \"http://localhost/aris/sap_model.owl#hasModelElements\".(\"http://localhost/aris/sap_model.owl#Event\" AND >= 2 \"http://localhost/aris/sap_model.owl#previousObjects\".TOP))");
179 Individual ind = new Individual("http://localhost/aris/sap_model.owl#e4j0__6_____u__");
180 boolean result = reasoner.hasType(description, ind);
181 System.out.println(result);
182 }
183
184 // simple unit test for new retrieval algorithm
185 @Test
186 public void fastInstanceCheck3() throws MalformedURLException, ComponentInitException, ParseException {
187 String file = "examples/family/father_oe.owl";
188 ComponentManager cm = ComponentManager.getInstance();
189 KnowledgeSource ks = cm.knowledgeSource(OWLFile.class);
190 cm.applyConfigEntry(ks, "url", new File(file).toURI().toURL());
191 ks.init();
192 ReasonerComponent reasoner = cm.reasoner(FastInstanceChecker.class, ks);
193 reasoner.init();
194 baseURI = reasoner.getBaseURI();
195 Description description = KBParser.parseConcept("(\"http://example.com/father#male\" AND EXISTS \"http://example.com/father#hasChild\".TOP)");
196 // Description description = KBParser.parseConcept("EXISTS \"http://example.com/father#hasChild\".TOP");
197 SortedSet<Individual> result = reasoner.getIndividuals(description);
198 assertTrue(result.size()==3);
199 assertTrue(result.contains(new Individual("http://example.com/father#markus")));
200 assertTrue(result.contains(new Individual("http://example.com/father#martin")));
201 assertTrue(result.contains(new Individual("http://example.com/father#stefan")));
202 // System.out.println(result);
203
204 Description description2 = KBParser.parseConcept("(\"http://example.com/father#male\" AND ALL \"http://example.com/father#hasChild\".\"http://example.com/father#father\")");
205 SortedSet<Individual> result2 = reasoner.getIndividuals(description2);
206 assertTrue(result2.size()==2);
207 assertTrue(result2.contains(new Individual("http://example.com/father#heinz")));
208 assertTrue(result2.contains(new Individual("http://example.com/father#stefan")));
209 }
210
211 private List<Individual> getIndSet(String... inds) {
212 List<Individual> individuals = new LinkedList<Individual>();
213 for(String ind : inds) {
214 individuals.add(new Individual(uri(ind)));
215 }
216 return individuals;
217 }
218
219 private String uri(String name) {
220 return "\""+baseURI+name+"\"";
221 }
222
223 }