Mercurial > repos > mikel-egana-aranguren > oppl
view OPPL/Tool.java @ 4:4f60202c58d9
Added option to push inferred subsumption axioms into output ontology
author | Mikel Egaña Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu> |
---|---|
date | Wed, 14 Sep 2011 19:52:06 +0200 |
parents | 5255f1333cc4 |
children | 68935f90c2db |
line wrap: on
line source
/** * Copyright (C) 2011, Mikel Egaña Aranguren * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package es.upm.fi.dia.oeg.oppl.galaxy; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FilterOutputStream; import java.io.OutputStream; import java.io.PipedOutputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import org.coode.oppl.ChangeExtractor; import org.coode.oppl.OPPLParser; import org.coode.oppl.OPPLScript; import org.coode.oppl.ParserFactory; import org.coode.oppl.exceptions.QuickFailRuntimeExceptionHandler; import org.coode.oppl.log.Logging; import org.coode.owlapi.obo.parser.OBOOntologyFormat; import org.coode.parsers.ErrorListener; import org.coode.parsers.LoggerErrorListener; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.OWLOntologyDocumentTarget; import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; import org.semanticweb.owlapi.io.SystemOutDocumentTarget; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLAxiomChange; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; import org.semanticweb.owlapi.model.OWLOntologyStorageException; import org.semanticweb.owlapi.reasoner.InferenceType; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; import org.semanticweb.owlapi.util.InferredAxiomGenerator; import org.semanticweb.owlapi.util.InferredOntologyGenerator; import org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator; import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory; /** * @author Mikel Egaña Aranguren * */ public class Tool { /** * @param OWL file * @param OPPL script * @throws FileNotFoundException * @throws OWLOntologyCreationException * @throws OWLOntologyStorageException */ public static void main(String[] args) throws FileNotFoundException, OWLOntologyCreationException, OWLOntologyStorageException { // Get the arguments from command-line String OWLFilePath = args [0]; String OPPL_script_file = args [1]; String Output_format = args [2]; String Add_inferred = args [3]; String OPPL_script_source = ""; // Load the flat file with script in memory File file = new File(OPPL_script_file); Scanner input = new Scanner(file); while(input.hasNext()) { String nextToken = input.next(); OPPL_script_source = OPPL_script_source + " " + nextToken; } input.close(); // Load the OWL ontology File owl_file = new File(OWLFilePath); OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); OWLOntology OWL_ontology = manager.loadOntologyFromOntologyDocument(owl_file); // Sync reasoner and check consistency OWLReasonerFactory reasonerFactory = new PelletReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(OWL_ontology); // Parse the OPPL script ParserFactory parserFactory = new ParserFactory(manager, OWL_ontology, reasoner); Logger logger = Logger.getLogger(Tool.class.getName()); Logging.getQueryLogger().setLevel(Level.OFF); // The normal messages are errors for galaxy ErrorListener errorListener = (ErrorListener)new LoggerErrorListener(logger); OPPLParser opplparser = parserFactory.build(errorListener); OPPLScript OPPLscript = opplparser.parse(OPPL_script_source); // Execute the script ChangeExtractor extractor = new ChangeExtractor(new QuickFailRuntimeExceptionHandler(), true); List<OWLAxiomChange> changes = extractor.visit(OPPLscript); manager.applyChanges(changes); // Add the inferred axioms as asserted axioms to the original ontology if(Add_inferred.equals("Add_inferred")){ reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>(); gens.add(new InferredSubClassAxiomGenerator()); InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); iog.fillOntology(manager, OWL_ontology); } // Print the ontology to the standard output so other galaxy tools can operate on the output OWLOntologyDocumentTarget documentTarget = new SystemOutDocumentTarget(); if(Output_format.equals("OBO")){ manager.saveOntology(OWL_ontology, new OBOOntologyFormat(), new SystemOutDocumentTarget()); } else{ manager.saveOntology(OWL_ontology, new RDFXMLOntologyFormat(), new SystemOutDocumentTarget()); } } }