001package org.dllearner.examples;
002
003import org.apache.log4j.Level;
004import org.apache.log4j.Logger;
005import org.dllearner.algorithms.celoe.CELOE;
006import org.dllearner.algorithms.celoe.OEHeuristicRuntime;
007import org.dllearner.core.KnowledgeSource;
008import org.dllearner.core.StringRenderer;
009import org.dllearner.core.StringRenderer.Rendering;
010import org.dllearner.kb.OWLAPIOntology;
011import org.dllearner.learningproblems.ClassAsInstanceLearningProblem;
012import org.dllearner.reasoning.OWLAPIReasoner;
013import org.dllearner.reasoning.ReasonerImplementation;
014import org.dllearner.refinementoperators.RhoDRDown;
015import org.semanticweb.owlapi.apibinding.OWLManager;
016import org.semanticweb.owlapi.model.*;
017import org.semanticweb.owlapi.profiles.OWL2ELProfile;
018import org.semanticweb.owlapi.profiles.OWLProfileReport;
019import org.semanticweb.owlapi.profiles.OWLProfileViolation;
020import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl;
021import uk.ac.manchester.cs.owlapi.modularity.ModuleType;
022import uk.ac.manchester.cs.owlapi.modularity.SyntacticLocalityModuleExtractor;
023
024import java.io.BufferedReader;
025import java.io.File;
026import java.io.FileReader;
027import java.io.IOException;
028import java.util.HashSet;
029import java.util.Set;
030import java.util.TreeSet;
031
032public class MouseDiabetesExp3CaI {
033
034    private static final Logger logger = Logger.getLogger(MouseDiabetesExp3CaI.class);
035    private static final String dir = "/tmp/";
036    private static final String kbFilePath = dir + "go.owl";
037    // http://pwestphal.aksw.org/smallis/telco_20150429/pos_ont_cai_intersection_curated.xml
038    private static final String posOntFilePath = dir + "pos_ont_cai_intersection_curated.xml";
039    // http://pwestphal.aksw.org/smallis/telco_20150429/neg_ont_cai_intersection_curated.xml
040    private static final String negOntFilePath = dir + "neg_ont_cai_intersection_curated.xml";
041    // http://pwestphal.aksw.org/smallis/telco_20150429/pos_uris_cai_intersection_curated.txt
042    public static final String posExamplesFilePath = dir + "pos_uris_cai_intersection_curated.txt";
043    // http://pwestphal.aksw.org/smallis/telco_20150429/neg_uris_cai_intersection_curated.txt
044    public static final String negExamplesFilePath = dir + "neg_uris_cai_intersection_curated.txt";
045
046    static boolean useCBD = false;
047
048    public static void main(String[] args) throws Exception {
049        setUp();
050        logger.info("starting...");
051        OWLOntology ontology;
052        ontology = readDumpFiles();
053
054        logger.info("reading positive and negative examples...");
055        Set<OWLClass> posExamples = readExamples(posExamplesFilePath);
056        Set<OWLClass> negExamples = readExamples(negExamplesFilePath);
057        logger.info("finished reading examples");
058
059        logger.info("Building locality based module...");
060        /* This is done to narrow down the search space and avoid infeasible
061         * number of concept combinations*/
062        OWLOntology module = buildLBM(posExamples, negExamples, ontology);
063        logger.info("-Done-");
064
065        logger.info("initializing knowledge source...");
066        KnowledgeSource ks = new OWLAPIOntology(module);
067        ks.init();
068        logger.info("finished initializing knowledge source");
069
070        logger.info("initializing reasoner...");
071        OWLAPIReasoner baseReasoner = new OWLAPIReasoner(ks);
072        baseReasoner.setReasonerImplementation(ReasonerImplementation.HERMIT);
073        baseReasoner.setUseFallbackReasoner(true);
074        baseReasoner.init();
075        logger.info("finished initializing reasoner");
076
077        logger.info("initializing learning problem...");
078        ClassAsInstanceLearningProblem lp = new ClassAsInstanceLearningProblem();
079        lp.setNegativeExamples(negExamples);
080        lp.setPositiveExamples(posExamples);
081        lp.setReasoner(baseReasoner);
082        lp.init();
083        logger.info("finished initializing learning problem");
084
085        logger.info("initializing learning algorithm...");
086        CELOE la;
087        OEHeuristicRuntime heuristic = new OEHeuristicRuntime();
088        heuristic.setExpansionPenaltyFactor(0.01);
089
090        la = new CELOE(lp, baseReasoner);
091        la.setHeuristic(heuristic);
092        la.setMaxExecutionTimeInSeconds(36000);
093        la.setMaxDepth(30);
094        la.setNoisePercentage(20);
095        la.setMaxNrOfResults(70);
096        la.setWriteSearchTree(true);
097        la.setReplaceSearchTree(true);
098        la.setSearchTreeFile("log/mouse-diabetis.log");
099//        OWLDataFactory factory = OWLManager.getOWLDataFactory();
100//        OWLClassExpression startClass = factory.getOWLObjectUnionOf(
101//                new OWLClassImpl(IRI.create("http://purl.obolibrary.org/obo/GO_0003674")),
102//                new OWLClassImpl(IRI.create("http://purl.obolibrary.org/obo/GO_0005575")),
103//                new OWLClassImpl(IRI.create("http://purl.obolibrary.org/obo/GO_0008150"))
104//                );
105//        OWLClassExpression startClass = new OWLClassImpl(IRI.create("http://dl-learner.org/smallis/Sample"));
106//        OWLClassExpression startClass = new OWLClassImpl(IRI.create("http://purl.obolibrary.org/obo/GO_TOP"));
107
108//        OWLClassExpression startClass = factory.getOWLObjectIntersectionOf(
109//              new OWLClassImpl(IRI.create("http://dl-learner.org/smallis/Sample")),
110//              new OWLObjectSomeValuesFromImpl(
111//                      new OWLObjectPropertyImpl(IRI.create("http://dl-learner.org/smallis/has_gene_association")),
112//                      factory.getOWLThing()));
113//      la.setStartClass(startClass);
114        logger.info("finished initializing learning algorithm");
115
116        logger.info("initializing operator...");
117        RhoDRDown op = new RhoDRDown();
118        op.setInstanceBasedDisjoints(true);
119        op.setUseNegation(false);
120        op.setUseHasValueConstructor(true);
121        op.setUseAllConstructor(false);
122//        op.setStartClass(startClass);
123        op.setReasoner(baseReasoner);
124        op.setSubHierarchy(baseReasoner.getClassHierarchy());
125        op.setObjectPropertyHierarchy(baseReasoner.getObjectPropertyHierarchy());
126        op.setDataPropertyHierarchy(baseReasoner.getDatatypePropertyHierarchy());
127        op.init();
128        logger.info("finished initializing operator");
129        la.setOperator(op);
130
131        la.init();
132        la.start();
133    }
134
135    public static Set<OWLClass> loadPosExamples() throws IOException {
136        return readExamples(posExamplesFilePath);
137    }
138
139    public static Set<OWLClass> loadNegExamples() throws IOException {
140        return readExamples(negExamplesFilePath);
141    }
142
143    public static Set<OWLClass> readExamples(String filePath) throws IOException {
144        Set<OWLClass> classes = new TreeSet<>();
145        try(BufferedReader buffRead = new BufferedReader(new FileReader(new File(filePath)))){
146            String line;
147            while ((line = buffRead.readLine()) != null) {
148                line = line.trim();
149//              line = line.substring(1, line.length()-1);  // strip off angle brackets
150                classes.add(new OWLClassImpl(IRI.create(line)));
151            }
152        }
153        return classes;
154    }
155
156    private static void setUp() {
157        logger.setLevel(Level.DEBUG);
158//        Logger.getLogger(AbstractReasonerComponent.class).setLevel(Level.OFF);
159        StringRenderer.setRenderer(Rendering.DL_SYNTAX);
160    }
161
162    public static OWLOntology readDumpFiles() throws
163            Exception {
164
165        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
166        OWLOntology ont = man.createOntology();
167
168        logger.info("reading main knowledge base (" + kbFilePath + ")...");
169        OWLOntology kb = readOWLOntology(kbFilePath);
170
171        logger.info("reading positive examples data (" + posOntFilePath + ")...");
172        OWLOntology posExmpls = readOWLOntology(posOntFilePath);
173
174        logger.info("reading negative examples data (" + negOntFilePath + ")...");
175        OWLOntology negExmpls = readOWLOntology(negOntFilePath);
176
177        logger.info("finished reading files");
178
179        logger.info("Merging ontologies...");
180        man.addAxioms(ont, kb.getAxioms());
181        man.addAxioms(ont, posExmpls.getAxioms());
182        man.addAxioms(ont, negExmpls.getAxioms());
183        logger.info("finished conversion");
184
185        // sanity check
186        if(containsErrorNamedClasses(ont)){
187            logger.fatal("Ontology contains errors!!! Exiting...");
188            System.exit(0);
189        }
190
191        return ont;
192    }
193
194    private static boolean containsErrorNamedClasses(OWLOntology ontology){
195        for (OWLClass cls : ontology.getClassesInSignature()) {
196            if(cls.toStringID().startsWith("http://org.semanticweb.owlapi/error#")){
197                return true;
198            }
199        }
200        return false;
201    }
202
203    public static OWLOntology readOWLELOntology(String filePath) throws
204            OWLOntologyCreationException {
205
206        OWLOntology ont = readOWLOntology(filePath);
207        OWLOntologyManager elMan = OWLManager.createOWLOntologyManager();
208
209        OWLOntology elOnt = elMan.createOntology();
210
211        OWL2ELProfile el = new OWL2ELProfile();
212        OWLProfileReport report = el.checkOntology(ont);
213        HashSet<OWLAxiom> violatingAxioms = new HashSet<>();
214
215        for (OWLProfileViolation violation : report.getViolations()) {
216            violatingAxioms.add(violation.getAxiom());
217        }
218
219        for (OWLAxiom axiom : ont.getLogicalAxioms()) {
220            if (!violatingAxioms.contains(axiom)) {
221                elMan.addAxiom(elOnt, axiom);
222            }
223        }
224//        IRI physicalModuleIRI = IRI.create(new File("/tmp/smallis/el_module.owl"));
225//        elMan.saveOntology(elOnt, new RDFXMLOntologyFormat(), physicalModuleIRI);
226        logger.info("-Done-");
227        return elOnt;
228    }
229
230    public static OWLOntology readOWLOntology(String filePath) throws OWLOntologyCreationException {
231        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
232        OWLOntology ontology = man.loadOntologyFromOntologyDocument(new File(filePath));
233
234        return ontology;
235    }
236
237    private static OWLOntology buildLBM(Set<OWLClass> posSamples,
238            Set<OWLClass> negSamples, OWLOntology ont) throws OWLOntologyCreationException {
239
240        Set<OWLEntity> allClasses = new HashSet<>();
241        allClasses.addAll(posSamples);
242        allClasses.addAll(negSamples);
243
244        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
245
246        SyntacticLocalityModuleExtractor extractor =
247                new SyntacticLocalityModuleExtractor(man, ont, ModuleType.BOT);
248
249        IRI moduleIRI = IRI.create("http://dl-learner.org/modules/mouse_diabetes/exp3.owl");
250//        IRI moduleIRI = IRI.create(new File("/tmp/module.owl"));
251        OWLOntology module = extractor.extractAsOntology(allClasses, moduleIRI);
252
253        return module;
254    }
255}