001package org.dllearner.scripts;
002
003import org.apache.jena.rdf.model.InfModel;
004import org.apache.jena.rdf.model.Model;
005import org.apache.jena.rdf.model.ModelFactory;
006import org.apache.jena.riot.RDFDataMgr;
007import org.dllearner.utilities.OwlApiJenaUtils;
008import org.mindswap.pellet.jena.PelletReasonerFactory;
009import org.semanticweb.owlapi.apibinding.OWLManager;
010import org.semanticweb.owlapi.formats.TurtleDocumentFormat;
011import org.semanticweb.owlapi.model.*;
012import org.semanticweb.owlapi.reasoner.OWLReasoner;
013import org.semanticweb.owlapi.util.*;
014
015import java.io.*;
016import java.util.ArrayList;
017import java.util.List;
018import java.util.Set;
019
020public class Infgen {
021        public static Model getModel(final OWLOntology ontology) {
022                Model model = ModelFactory.createDefaultModel();
023
024                try (PipedInputStream is = new PipedInputStream(); PipedOutputStream os = new PipedOutputStream(is)) {
025                        new Thread(new Runnable() {
026                                @Override
027                                public void run() {
028                                        try {
029                                                ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), os);
030                                                os.close();
031                                        } catch (OWLOntologyStorageException | IOException e) {
032                                                e.printStackTrace();
033                                        }
034                                }
035                        }).start();
036                        model.read(is, null, "TURTLE");
037                        return model;
038                } catch (Exception e) {
039                        throw new RuntimeException("Could not convert OWL API ontology to JENA API model.", e);
040                }
041        }
042        
043        
044
045//      private static void reasonWithPellet2(String in, String out) throws FileNotFoundException {
046//              OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC );
047//              model.read("file:///"+in);
048//              System.out.println("rwp2; size:"+model.size());
049//              model.write(new FileOutputStream(out));
050//      }
051        private static void reasonWithPellet(String in, String out) throws FileNotFoundException {
052                //OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC );
053                Model model = ModelFactory.createDefaultModel();
054                model.read("file:///"+in);
055                System.out.println("rwp; size:"+model.size());
056//              InfModel inf = ModelFactory.createInfModel(PelletReasonerFactory.theInstance().create(), model);
057//              inf.write(new FileOutputStream(out));
058        }
059        
060        private static void reasonWithHermit(String in, String out, boolean copy) throws IOException, OWLOntologyStorageException, OWLOntologyCreationException {
061        OWLOntologyManager manager=OWLManager.createOWLOntologyManager();
062        File inputOntologyFile = new File(in);
063        OWLOntology ontology=manager.loadOntologyFromOntologyDocument(inputOntologyFile);
064        org.semanticweb.HermiT.ReasonerFactory factory
065        = new org.semanticweb.HermiT.ReasonerFactory();
066        // The factory can now be used to obtain an instance of HermiT as an OWLReasoner.
067        org.semanticweb.HermiT.Configuration c
068        = new org.semanticweb.HermiT.Configuration();
069        OWLReasoner reasoner=factory.createReasoner(ontology, c);
070        List<InferredAxiomGenerator<? extends OWLAxiom>> generators= new ArrayList<>();
071        generators.add(new InferredSubClassAxiomGenerator());
072        generators.add(new InferredClassAssertionAxiomGenerator());
073        generators.add(new InferredDisjointClassesAxiomGenerator() {
074            boolean precomputed=false;
075            @Override
076                        protected void addAxioms(OWLClass entity, OWLReasoner reasoner, OWLDataFactory dataFactory, Set<OWLDisjointClassesAxiom> result) {
077                if (!precomputed) {
078                    reasoner.precomputeInferences(org.semanticweb.owlapi.reasoner.InferenceType.DISJOINT_CLASSES);
079                    precomputed=true;
080                }
081                for (OWLClass cls : reasoner.getDisjointClasses(entity).getFlattened()) {
082                    result.add(dataFactory.getOWLDisjointClassesAxiom(entity, cls));
083                }
084            }
085        });
086        generators.add(new InferredDataPropertyCharacteristicAxiomGenerator());
087        generators.add(new InferredEquivalentClassAxiomGenerator());
088        generators.add(new InferredEquivalentDataPropertiesAxiomGenerator());
089        generators.add(new InferredEquivalentObjectPropertyAxiomGenerator());
090        generators.add(new InferredInverseObjectPropertiesAxiomGenerator());
091        generators.add(new InferredObjectPropertyCharacteristicAxiomGenerator());
092        generators.add(new InferredPropertyAssertionGenerator());
093        generators.add(new InferredSubDataPropertyAxiomGenerator());
094        generators.add(new InferredSubObjectPropertyAxiomGenerator());
095
096        InferredOntologyGenerator iog=new InferredOntologyGenerator(reasoner,generators);
097        OWLOntology inferredAxiomsOntology=manager.createOntology();
098        iog.fillOntology(manager.getOWLDataFactory(), inferredAxiomsOntology);
099        if (copy) {
100                manager.addAxioms(inferredAxiomsOntology, ontology.getAxioms());
101                Model m1 = OwlApiJenaUtils.getModel(inferredAxiomsOntology);
102                Model m0 = RDFDataMgr.loadModel("file://"+in);
103                        m0.add(m1.listStatements());
104                        m0.write(new FileOutputStream(out));
105        } else {
106        File inferredOntologyFile=new File(out);
107        if (!inferredOntologyFile.exists())
108            inferredOntologyFile.createNewFile();
109        inferredOntologyFile=inferredOntologyFile.getAbsoluteFile();
110        OutputStream outputStream=new FileOutputStream(inferredOntologyFile);
111        manager.saveOntology(inferredAxiomsOntology, manager.getOntologyFormat(ontology), outputStream);
112        }
113        System.out.println("The ontology in "+out+" should now contain all inferred axioms ");
114        }
115
116        private static void loadThroughJena(String in, String out) throws OWLOntologyCreationException, FileNotFoundException {
117                OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
118                OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File(in));
119                Model model = OwlApiJenaUtils.getModel(ontology);
120                System.out.println("ltj; size:"+model.size());
121                model.write(new FileOutputStream(out));
122        }
123
124        private static void loadThroughJena2(String in, String out) throws FileNotFoundException {
125                
126                Model model = RDFDataMgr.loadModel("file://"+in);
127                System.out.println("ltj; size:"+model.size());
128                model.write(new FileOutputStream(out));
129        }
130
131        public static void main(String[] args) throws Exception {
132                String in = args.length > 0 ? args[0] : "../examples/carcinogenesis/carcinogenesis.owl";
133                if (args.length <= 1) {
134                        loadThroughJena(in, in+".jena1");
135                        reasonWithHermit(in, in+".her0", false);
136                }
137                else {
138                        for (int i = 0; i < args[1].length(); ++i) {
139                                String step = in;
140                                String next = null;
141                                switch (args[1].charAt(i)) {
142                                case 'h':
143                                        next = in + "." + i + "." + "her";
144                                        reasonWithHermit(step, next, false);
145                                        break;
146                                case 'H':
147                                        next = in + "." + i + "." + "her2";
148                                        reasonWithHermit(step, next, true);
149                                        break;
150                                case 'j':
151                                        next = in + "." + i + "." + "jena";
152                                        loadThroughJena(step, next);
153                                        break;
154                                case 'J':
155                                        next = in + "." + i + "." + "jena2";
156                                        loadThroughJena2(step, next);
157                                        break;
158                                case 'p':
159                                        next = in + "." + i + "." + "pel";
160                                        reasonWithPellet(step, next);
161                                        break;
162                                default:
163                                        System.err.println("Unknown mode: " + args[1].charAt(i));
164                                }
165                                if (next != null) {
166                                        step = next;
167                                        next = null;
168                                }
169                        }
170                }
171
172        }
173
174}