comparison OPPL/Tool.java @ 3:5255f1333cc4

Version 1.0.1 Added output choice: OBO or OWL(RDF/XML)
author Mikel Egaña Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu>
date Wed, 07 Sep 2011 10:46:29 +0200
parents
children 4f60202c58d9
comparison
equal deleted inserted replaced
2:0a374d0b7759 3:5255f1333cc4
1 /**
2 * Copyright (C) 2011, Mikel Egaña Aranguren
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package es.upm.fi.dia.oeg.oppl.galaxy;
19
20 import java.io.BufferedOutputStream;
21 import java.io.BufferedWriter;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FilterOutputStream;
25 import java.io.OutputStream;
26 import java.io.PipedOutputStream;
27 import java.io.PrintStream;
28 import java.util.List;
29 import java.util.Scanner;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32
33 import org.coode.oppl.ChangeExtractor;
34 import org.coode.oppl.OPPLParser;
35 import org.coode.oppl.OPPLScript;
36 import org.coode.oppl.ParserFactory;
37 import org.coode.oppl.exceptions.QuickFailRuntimeExceptionHandler;
38 import org.coode.oppl.log.Logging;
39 import org.coode.owlapi.obo.parser.OBOOntologyFormat;
40 import org.coode.parsers.ErrorListener;
41 import org.coode.parsers.LoggerErrorListener;
42 import org.semanticweb.owlapi.apibinding.OWLManager;
43 import org.semanticweb.owlapi.io.OWLOntologyDocumentTarget;
44 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
45 import org.semanticweb.owlapi.io.SystemOutDocumentTarget;
46 import org.semanticweb.owlapi.model.IRI;
47 import org.semanticweb.owlapi.model.OWLAxiomChange;
48 import org.semanticweb.owlapi.model.OWLOntology;
49 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
50 import org.semanticweb.owlapi.model.OWLOntologyManager;
51 import org.semanticweb.owlapi.model.OWLOntologyStorageException;
52 import org.semanticweb.owlapi.reasoner.OWLReasoner;
53 import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
54
55 import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;
56
57
58 /**
59 * @author Mikel Egaña Aranguren
60 *
61 */
62 public class Tool {
63
64 /**
65 * @param OWL file
66 * @param OPPL script
67 * @throws FileNotFoundException
68 * @throws OWLOntologyCreationException
69 * @throws OWLOntologyStorageException
70 */
71 public static void main(String[] args) throws FileNotFoundException, OWLOntologyCreationException, OWLOntologyStorageException {
72
73 // Get the arguments from command-line
74 String OWLFilePath = args [0];
75 String OPPL_script_file = args [1];
76 String Output_format = args [2];
77 String OPPL_script_source = "";
78
79 // Load the flat file with script in memory
80 File file = new File(OPPL_script_file);
81 Scanner input = new Scanner(file);
82 while(input.hasNext()) {
83 String nextToken = input.next();
84 OPPL_script_source = OPPL_script_source + " " + nextToken;
85 }
86 input.close();
87
88 // Load the OWL ontology
89 File owl_file = new File(OWLFilePath);
90 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
91 OWLOntology OWL_ontology = manager.loadOntologyFromOntologyDocument(owl_file);
92
93 // Sync reasoner and check consistency
94 OWLReasonerFactory reasonerFactory = new PelletReasonerFactory();
95 OWLReasoner reasoner = reasonerFactory.createReasoner(OWL_ontology);
96
97 // Parse the OPPL script
98 ParserFactory parserFactory = new ParserFactory(manager, OWL_ontology, reasoner);
99 Logger logger = Logger.getLogger(Tool.class.getName());
100 Logging.getQueryLogger().setLevel(Level.OFF); // The normal messages are errors for galaxy
101 ErrorListener errorListener = (ErrorListener)new LoggerErrorListener(logger);
102 OPPLParser opplparser = parserFactory.build(errorListener);
103 OPPLScript OPPLscript = opplparser.parse(OPPL_script_source);
104
105 // Execute the script
106 ChangeExtractor extractor = new ChangeExtractor(new QuickFailRuntimeExceptionHandler(), true);
107 List<OWLAxiomChange> changes = extractor.visit(OPPLscript);
108 manager.applyChanges(changes);
109
110 // Print the ontology to the standard output so other galaxy tools can operate on the output
111 OWLOntologyDocumentTarget documentTarget = new SystemOutDocumentTarget();
112
113 if(Output_format.equals("OBO")){
114 manager.saveOntology(OWL_ontology, new OBOOntologyFormat(), new SystemOutDocumentTarget());
115 }
116 else{
117 manager.saveOntology(OWL_ontology, new RDFXMLOntologyFormat(), new SystemOutDocumentTarget());
118 }
119
120 }
121 }