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 java.io.File;
023    import java.net.MalformedURLException;
024    
025    import org.dllearner.core.ComponentInitException;
026    import org.dllearner.core.ComponentManager;
027    import org.dllearner.core.KnowledgeSource;
028    import org.dllearner.core.ReasonerComponent;
029    import org.dllearner.core.owl.KB;
030    import org.dllearner.kb.KBFile;
031    import org.dllearner.kb.OWLFile;
032    import org.dllearner.parser.KBParser;
033    import org.dllearner.parser.ParseException;
034    import org.dllearner.reasoning.FastInstanceChecker;
035    
036    /**
037     * Some ontologies to simplify unit tests.
038     * 
039     * @author Jens Lehmann
040     *
041     */
042    public final class TestOntologies {
043    
044            public enum TestOntology { EMPTY, SIMPLE, SIMPLE_NO_DR, SIMPLE_NO_DISJOINT, SIMPLE_NO_DR_DISJOINT, SIMPLE2, SIMPLE3, R1SUBR2, DATA1, FIVE_ROLES, FATHER_OE, CARCINOGENESIS, EPC_OE };
045            
046            public static ReasonerComponent getTestOntology(TestOntology ont) {
047                    String kbString = "";
048                    String owlFile = "";
049                    
050                    if(ont.equals(TestOntology.EMPTY)) {
051                            // no background knowledge
052                    } else if(ont.equals(TestOntology.SIMPLE)) {
053                            // background knowledge used in EL paper
054                            kbString += "OPDOMAIN(hasChild) = human.\n";
055                            kbString += "OPRANGE(hasChild) = human.\n";
056                            kbString += "OPDOMAIN(hasPet) = human.\n";
057                            kbString += "OPRANGE(hasPet) = animal.\n";
058                            kbString += "Subrole(hasChild, has).\n";
059                            kbString += "Subrole(hasPet, has).\n";
060                            kbString += "bird SUB animal.\n";
061                            kbString += "cat SUB animal.\n";
062                            kbString += "(human AND animal) = BOTTOM.\n";
063                    } else if(ont.equals(TestOntology.SIMPLE_NO_DR)) {
064                            kbString += "Subrole(hasChild, has).\n";
065                            kbString += "Subrole(hasPet, has).\n";
066                            kbString += "bird SUB animal.\n";
067                            kbString += "cat SUB animal.\n";
068                            kbString += "(human AND animal) = BOTTOM.\n";
069                    } else if(ont.equals(TestOntology.SIMPLE_NO_DISJOINT)) {
070                            kbString += "OPDOMAIN(hasChild) = human.\n";
071                            kbString += "OPRANGE(hasChild) = human.\n";
072                            kbString += "OPDOMAIN(hasPet) = human.\n";
073                            kbString += "OPRANGE(hasPet) = animal.\n";
074                            kbString += "Subrole(hasChild, has).\n";
075                            kbString += "Subrole(hasPet, has).\n";
076                            kbString += "bird SUB animal.\n";
077                            kbString += "cat SUB animal.\n";
078                            kbString += "human SUB TOP.\n";
079                    } else if(ont.equals(TestOntology.SIMPLE_NO_DR_DISJOINT)) {
080                            kbString += "Subrole(hasChild, has).\n";
081                            kbString += "Subrole(hasPet, has).\n";
082                            kbString += "bird SUB animal.\n";
083                            kbString += "cat SUB animal.\n";
084                            kbString += "human SUB TOP.\n";                 
085                    } else if(ont.equals(TestOntology.SIMPLE2)) {
086                            kbString += "Subrole(r2,r3).\n";
087                            kbString += "a1 SUB TOP.\n";
088                            kbString += "a2 SUB a3.\n";
089                            kbString += "r1(a,b).\n"; // we have to declare r1
090                    } else if(ont.equals(TestOntology.SIMPLE3)) {
091                            kbString += "a1 SUB a2.\n";
092                            kbString += "Subrole(r1,r2).\n";
093                    } else if(ont.equals(TestOntology.R1SUBR2)) {
094                            kbString += "Subrole(r1,r2).\n";
095                            kbString += "a1 SUB TOP.\n";
096                            kbString += "a2 SUB TOP.\n";
097                    } else if(ont.equals(TestOntology.DATA1)) {
098                            kbString += "man SUB person.\n";
099                            kbString += "woman SUB person.\n";
100                            kbString += "man(eric).\n";
101                            kbString += "woman(diana).\n";
102                            kbString += "married(eric,diana).\n";
103                            kbString += "hasChild(eric,frank).\n";
104                            kbString += "hasChild(eric,tim).\n";
105                    } else if(ont.equals(TestOntology.FIVE_ROLES)) {
106                            // we only define five roles, no TBox
107                            kbString += "r1(a,b).\n";
108                            kbString += "r2(a,b).\n";
109                            kbString += "r3(a,b).\n";
110                            kbString += "r4(a,b).\n";
111                            kbString += "r5(a,b).\n";
112                    } else if(ont.equals(TestOntology.FATHER_OE)) {
113                            owlFile = "examples/family/father_oe.owl";
114                    } else if(ont.equals(TestOntology.CARCINOGENESIS)) {
115                            owlFile = "examples/carcinogenesis/carcinogenesis.owl";
116                    } else if(ont.equals(TestOntology.EPC_OE)) {
117                            owlFile = "examples/epc/sap_epc_oe.owl";
118                    }
119                    
120                    try {   
121                            ComponentManager cm = ComponentManager.getInstance();
122                            KnowledgeSource source;
123                            
124                            // parse KB string if one has been specified
125                            if(!kbString.isEmpty()) {
126                                    KB kb = KBParser.parseKBFile(kbString);
127                                    source = new KBFile(kb);
128                            // parse OWL file otherwise
129                            } else {
130                                    source = cm.knowledgeSource(OWLFile.class);
131                                    try {
132                                            cm.applyConfigEntry(source, "url", new File(owlFile).toURI().toURL());
133                                    } catch (MalformedURLException e) {
134                                            e.printStackTrace();
135                                    }                       
136                            }
137                            
138                            ReasonerComponent rc = cm.reasoner(FastInstanceChecker.class, source);
139                            source.init();
140                            rc.init();
141                            return rc;      
142                    } catch(ParseException e) {
143                            e.printStackTrace();
144                    } catch (ComponentInitException e) {
145                            e.printStackTrace();
146                    }
147                    
148                    throw new Error("Test ontology could not be created."); 
149            }
150            
151    }