OWL2Parser.java revision ae0124755bed317b3e11afe1c6275fdb488807ac
d25c69509848853b0b15b93e3cc180cab81047acChristian Maederimport org.coode.owlapi.owlxml.renderer.OWLXMLRenderer;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maederimport org.coode.owlapi.rdf.rdfxml.RDFXMLRenderer;
2578a6d217296401d347302c9104fee179bd0470Francisc Nicolae Bungiuimport org.semanticweb.owlapi.apibinding.OWLManager;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maederimport org.semanticweb.owlapi.io.OWLRendererException;
6f52b2f195d946649597c21e5888c70ccfeeb81eChristian Maederimport org.semanticweb.owlapi.io.StreamDocumentSource;
478b7abe044f6dcc5bb0db7cd9155ddc1730ed1bFrancisc Nicolae Bungiuimport org.semanticweb.owlapi.util.OWLOntologyMerger;
43f8464f7a29c9609509b9c58925f0432263803dSimon Ulbrichtimport org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration;
2578a6d217296401d347302c9104fee179bd0470Francisc Nicolae Bungiuimport uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxRenderer;
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist//import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxEditorParser;
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist//import org.coode.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat;
43f8464f7a29c9609509b9c58925f0432263803dSimon Ulbrichtimport uk.ac.manchester.cs.owl.owlapi.OWLOntologyIRIMapperImpl;
25f163f5755534f518718981c8b160fdf5097773notanartist private static enum OPTION {OWL_XML, MANCHESTER, RDF_XML, OBO, TURTLE, DOL}
7ca6e1b0867fc982f00128017f530b433ea8a37fChristian Maeder private static final Set<IRI> missingImports = new HashSet<IRI>();
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder private static Set<OWLOntology> ontologies;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder private static final Set<OWLOntology> exported = new HashSet<OWLOntology>();
2e08f62eab7a9bd9becf08ce32871a39306e4798Szaben private static OWLOntologyManager manager = setupManagerWithMissingImportListener();
43f8464f7a29c9609509b9c58925f0432263803dSimon Ulbricht private static OWLOntologyIRIMapperImpl mapper = new OWLOntologyIRIMapperImpl();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // A simple example of how to load and save an ontology
a933696a057bd1ad025d2d8e6b7079cc8e979d6eChristian Maeder URLConnection con = uri.toURL().openConnection();
ebe1b5c42cea5468e66f088648362125cb3c2818Christian Maeder con.addRequestProperty("Accept", "text/plain");
47bae6b7cf4f66223e52d0e1fd547fd620b3209dChristian Maeder StreamDocumentSource sds = new StreamDocumentSource(con.getInputStream(), IRI.create(uri));
6f52b2f195d946649597c21e5888c70ccfeeb81eChristian Maeder OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration();
6f52b2f195d946649597c21e5888c70ccfeeb81eChristian Maeder config = config.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
c1cca90cf3d763a48705ddaeeffb3c2d82e9e37eSimon Ulbricht OWLOntology ontology = manager.loadOntologyFromOntologyDocument(sds, config);
c1cca90cf3d763a48705ddaeeffb3c2d82e9e37eSimon Ulbricht IRI ontohub = IRI.create("https://ontohub.org/external/");
c1cca90cf3d763a48705ddaeeffb3c2d82e9e37eSimon Ulbricht mapper.addMapping(mi, ontohub.resolve(mi.toURI().getHost() + mi.toURI().getPath()));
2e08f62eab7a9bd9becf08ce32871a39306e4798Szaben // reset the manager. clear out imports to avoid duplicates
c1cca90cf3d763a48705ddaeeffb3c2d82e9e37eSimon Ulbricht // collect and report missing imports again.
c1cca90cf3d763a48705ddaeeffb3c2d82e9e37eSimon Ulbricht ontology = manager.loadOntologyFromOntologyDocument(sds, config);
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist out.writeXml("<Missing>" + mi + "</Missing>\n");
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder ontologies = getImports(ontology, new HashSet<OWLOntology>());
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder String str = ontology.getOntologyID().getOntologyIRI().toString();
7cfebf9e7fdb17e6977a47a593f5dae3bc3d15b0Christian Maeder String merged_name = str + ".merged.owl"; // we must make a new name!
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder // System.out.println("NAME: " + merged_name + "\n");
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder IRI mergedOntologyIRI = IRI.create(merged_name);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder // System.out.println("MERGED_IRI " + mergedOntologyIRI + "\n");
c7d44bc923b7eb1a01abd095cd004030fee589eanotanartist // Axioms can be excluded when 'quick' Option selected
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder merged = manager.createOntology(mergedOntologyIRI);
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder OWLOntologyMerger merger = new OWLOntologyMerger(manager);
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder merged = merger.createMergedOntology(manager, mergedOntologyIRI);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.err.println("OWL parse error: " + ex.getMessage());
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist // print usage information screen
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist private static void showHelpScreen() {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist System.out.println ("Usage: processor [<options..>] <URI>\n"
25f163f5755534f518718981c8b160fdf5097773notanartist + "_>_options_<______________\n"
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist + " | -o <tp> <fn> ..write output to file\n"
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist + " - - - tp <- type [: owl xml omn rdf obo dol ttl]\n"
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist + " - - - fn <- filename\n"
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist + " | -o-sys <tp> ..write output to system.out\n"
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist + " - - - - - tp <- type\n"
25f163f5755534f518718981c8b160fdf5097773notanartist + " | -qk ..internal(!) sets 'quick' option\n"
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist + " | -h ..this helptext"
c7d44bc923b7eb1a01abd095cd004030fee589eanotanartist // parse arguments according to option set
b1854c6c6cbc17bd15e317b7b787b29b0b788e8cnotanartist // fails for unknown or incomplete arguments, or when IRI is not set
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist private static void parseArgs
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist (String[] args, OWLOutputHandler out) throws Exception
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist String inp = "", msg = "OWL2Parser.parseArgs: ";
b1854c6c6cbc17bd15e317b7b787b29b0b788e8cnotanartist throw new Exception (msg + "no arguments provided");
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist if ( ! (i < args.length-2)) throw new Exception (msg
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist + "insufficient arguments (-o [format] [filename])");
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist out.add (parseOption (args[++i].toLowerCase(), msg), args[++i]);
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist if ( ! (i < args.length-1)) throw new Exception (msg
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist + "insufficient arguments (-o-sys [format])");
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist out.add (parseOption (args[++i].toLowerCase(), msg));
25f163f5755534f518718981c8b160fdf5097773notanartist } else if (arg.equals("-qk") || arg.equals("-q")) {
25f163f5755534f518718981c8b160fdf5097773notanartist } else if (arg.equals("-h") || arg.equals("--help")) {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist } else throw new Exception (msg + "unknown command <"+arg+">");
b1854c6c6cbc17bd15e317b7b787b29b0b788e8cnotanartist inp = args[i]; // read again to undo earlier 'toLowerCase'
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist if (arg.startsWith("http:") || arg.startsWith("https:"))
b1854c6c6cbc17bd15e317b7b787b29b0b788e8cnotanartist if (inp.equals("")) throw new Exception (msg + "IRI not set");
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist private static OPTION parseOption (String opt, String err) throws Exception {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist if (opt.equals("xml") || opt.equals("owl")) return OPTION.OWL_XML;
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist else if (opt.equals("omn")) return OPTION.MANCHESTER;
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist else if (opt.equals("rdf")) return OPTION.RDF_XML;
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist else if (opt.equals("ttl")) return OPTION.TURTLE;
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist throw new Exception (err + "unrecognized owl-format: " + opt);
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist // custem OntolgyWriter bundles a BufferedWriter with an OWL output format
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist protected static class OWLOntologyWriter extends BufferedWriter {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist OWLOntologyWriter (String fn, OPTION op) throws Exception {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist OWLOntologyWriter (OPTION op) throws Exception {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist // conveinience method to avoid xml-tags in other-format output
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist // output handler allows and handles a list of output-requests
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist protected static class OWLOutputHandler {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist void add (OPTION op, String filename) throws Exception {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist writer.add (new OWLOntologyWriter (filename, op));
2e08f62eab7a9bd9becf08ce32871a39306e4798Szaben private static OWLOntologyManager setupManagerWithMissingImportListener () {
2e08f62eab7a9bd9becf08ce32871a39306e4798Szaben OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
2e08f62eab7a9bd9becf08ce32871a39306e4798Szaben mgr.addMissingImportListener(new HasMissingImports());
7ca6e1b0867fc982f00128017f530b433ea8a37fChristian Maeder private static class HasMissingImports implements MissingImportListener {
7ca6e1b0867fc982f00128017f530b433ea8a37fChristian Maeder public void importMissing(MissingImportEvent event) {
7ca6e1b0867fc982f00128017f530b433ea8a37fChristian Maeder missingImports.add(event.getImportedOntologyURI());
25f163f5755534f518718981c8b160fdf5097773notanartist private static Set<OWLOntology> getImports (OWLOntology ontology, Set<OWLOntology> stop) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder Set<OWLOntology> s = new HashSet<OWLOntology>();
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder Set<OWLOntology> next = new HashSet<OWLOntology>(stop);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder for (OWLOntology imported : ontology.getDirectImports())
c50adef8a083be353c6f07af011359ed16a98d15Christian Maeder if (cyclic || next.contains(imported)) cyclic = true;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder Set<OWLOntology> i = getImports(imported, next);
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist private static void exportImports (OWLOutputHandler out) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder if (exported.containsAll(onto.getDirectImports())) {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist private static void renderUsingOption (OWLOntology onto, OWLOutputHandler outmgr) {
f79175f2c52a2d588934ba75c1994892c6fa4918notanartist // TODO: the below still need implementation!
25f163f5755534f518718981c8b160fdf5097773notanartist private static void renderAsOmn (OWLOntology onto, BufferedWriter out) {
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist // ManchesterOWLSyntaxOntologyFormat omn = new ManchesterOWLSyntaxOntologyFormat();
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist // manager.setOntologyFormat(onto, omn);
ae0124755bed317b3e11afe1c6275fdb488807acnotanartist // manager.saveOntology(onto, omn, out);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder ManchesterOWLSyntaxRenderer rendi = new ManchesterOWLSyntaxRenderer();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.err.println("Error by ManchesterParser!");
25f163f5755534f518718981c8b160fdf5097773notanartist private static void renderAsXml (OWLOntology onto, BufferedWriter out) {
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder File tempFile = File.createTempFile("owlTemp", ".xml");
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder onto.getOWLOntologyManager().removeAxioms(onto, onto.getAxioms());
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder BufferedReader rBuf = new BufferedReader(new FileReader(tempFile));
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder rBuf.readLine(); // ignore the first line containing <?xml version="1.0"?>
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder while (rBuf.ready()) out.append(rBuf.readLine()).append("\n");
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder out.append("<Loaded name=\"").append(manager.getOntologyDocumentIRI(onto));
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder out.append("\" ontiri=\"").append(onto.getOntologyID().getOntologyIRI()).append("\"/>\n");
25f163f5755534f518718981c8b160fdf5097773notanartist private static void renderAsRdf (OWLOntology onto, BufferedWriter out) {