OWL2Parser.java revision 47bae6b7cf4f66223e52d0e1fd547fd620b3209d
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;
9a15a025b733a3913ea347ddabc837bb1a0a3b90Christian Maederimport org.semanticweb.owlapi.model.*;
478b7abe044f6dcc5bb0db7cd9155ddc1730ed1bFrancisc Nicolae Bungiuimport org.semanticweb.owlapi.util.OWLOntologyMerger;
2578a6d217296401d347302c9104fee179bd0470Francisc Nicolae Bungiuimport uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxRenderer;
2578a6d217296401d347302c9104fee179bd0470Francisc Nicolae Bungiu
9a15a025b733a3913ea347ddabc837bb1a0a3b90Christian Maederimport java.io.*;
8890c291ae8120c275f8a88594ecd23f78b80013Christian Maederimport java.net.*;
91e24fc45834b35f2a3830d72565640251149bf3Christian Maederimport java.util.HashSet;
91e24fc45834b35f2a3830d72565640251149bf3Christian Maederimport java.util.Set;
2578a6d217296401d347302c9104fee179bd0470Francisc Nicolae Bungiu
2578a6d217296401d347302c9104fee179bd0470Francisc Nicolae Bungiupublic class OWL2Parser {
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder private static enum OPTION {QUICK_OWL_XML, OWL_XML, MANCHESTER, RDF_XML}
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder private static OPTION op;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder private static Boolean cyclic = false;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder private static Set<OWLOntology> ontologies;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder private static final Set<OWLOntology> exported = new HashSet<OWLOntology>();
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder private static final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder public static void main(String[] args) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder if (args.length < 1) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.out.println("Usage: processor <URI> [FILENAME] <OPTION>");
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.exit(1);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // A simple example of how to load and save an ontology
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder try {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder op = OPTION.MANCHESTER;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder if (args.length > 1) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // args[0]: IRI
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // args[1]: name of output file
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // args[2]: type of output file: xml, rdf, or otherwise assume Manchester syntax
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder String filename = args[1];
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder if (args.length == 3) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder out = new BufferedWriter(new FileWriter(filename));
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder if (args[2].equals("xml"))
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder op = OPTION.OWL_XML;
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder else if (args[2].equals("quick"))
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder op = OPTION.QUICK_OWL_XML;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder else if (args[2].equals("rdf"))
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder op = OPTION.RDF_XML;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder } else
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // args[0]: IRI
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // args[1]: type of output (or output file for Manchester syntax)
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // xml (OWL XML),
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // rdf (RDF/XML),
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // or otherwise use argument as file name for Manchester syntax
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // for xml and rdf output goes to standard output, i.e. System.out
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder if (args[1].equals("xml"))
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder op = OPTION.OWL_XML;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder else if (args[1].equals("rdf"))
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder op = OPTION.RDF_XML;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder else
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder out = new BufferedWriter(new FileWriter(filename));
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
04fceeab0047ae1614c04847e6ab01b3323482feChristian Maeder out.write("<Ontologies>\n");
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder /* Load an ontology from a physical IRI */
8890c291ae8120c275f8a88594ecd23f78b80013Christian Maeder String inp = args[0];
8890c291ae8120c275f8a88594ecd23f78b80013Christian Maeder URI uri;
7d9a0481d5d814e945e4b01599e177f79f69fd3dChristian Maeder try { uri = new URI(inp);
7d9a0481d5d814e945e4b01599e177f79f69fd3dChristian Maeder } catch (Exception ex)
7d9a0481d5d814e945e4b01599e177f79f69fd3dChristian Maeder {
7d9a0481d5d814e945e4b01599e177f79f69fd3dChristian Maeder uri = new File(inp).toURI();
8890c291ae8120c275f8a88594ecd23f78b80013Christian Maeder }
8890c291ae8120c275f8a88594ecd23f78b80013Christian Maeder URL url = uri.toURL();
8890c291ae8120c275f8a88594ecd23f78b80013Christian Maeder URLConnection con = url.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);
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder OWLOntology ontology = manager.loadOntologyFromOntologyDocument(sds, config);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder ontologies = getImports(ontology, new HashSet<OWLOntology>());
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder if (cyclic) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder String str = ontology.getOntologyID().getOntologyIRI().toString();
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder String merged_name = "";
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder for (OWLOntology aux_ont : ontologies) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder String mrg = aux_ont.getOntologyID().getOntologyIRI().toString();
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder mrg = mrg.replaceAll("[<>\\(\\) ]", "");
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder mrg = mrg.replaceAll(".*/", "");
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder mrg = mrg.replaceAll("\\[.*]", "");
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder merged_name = merged_name + mrg;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder merged_name = str + merged_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");
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder OWLOntology merged;
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder if (op == OPTION.QUICK_OWL_XML)
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder merged = manager.createOntology(mergedOntologyIRI);
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder else {
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder OWLOntologyMerger merger = new OWLOntologyMerger(manager);
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder merged = merger.createMergedOntology(manager, mergedOntologyIRI);
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder }
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder renderUsingOption(merged, out);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder } else {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder ontologies.add(ontology);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder exportImports(out);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
04fceeab0047ae1614c04847e6ab01b3323482feChristian Maeder out.write("\n</Ontologies>\n");
04fceeab0047ae1614c04847e6ab01b3323482feChristian Maeder out.flush();
04fceeab0047ae1614c04847e6ab01b3323482feChristian Maeder out.close();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder } catch (Exception ex) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.err.println("OWL parse error: " + ex.getMessage());
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder ex.printStackTrace();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder 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 next.add(ontology);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder for (OWLOntology imported : ontology.getDirectImports())
c50adef8a083be353c6f07af011359ed16a98d15Christian Maeder if (cyclic || next.contains(imported)) cyclic = true;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder else if (!s.contains(imported)) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder Set<OWLOntology> i = getImports(imported, next);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder s.add(imported);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder s.addAll(i);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder return s;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder private static void exportImports(BufferedWriter out) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder Boolean changed;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder do {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder changed = false;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder for (OWLOntology onto : ontologies)
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder if (exported.containsAll(onto.getDirectImports())) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder if (!exported.contains(onto)) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder changed = exported.add(onto);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder renderUsingOption(onto, out);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder } while (changed);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder private static void renderUsingOption(OWLOntology onto, BufferedWriter out) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder switch (op) {
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder case QUICK_OWL_XML:
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder renderAsXml(true, onto, out);
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder break;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder case OWL_XML:
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder renderAsXml(false, onto, out);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder break;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder case MANCHESTER:
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder renderAsOmn(onto, out);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder break;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder case RDF_XML:
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder renderAsRdf(onto, out);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder break;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder private static void renderAsOmn(OWLOntology onto, BufferedWriter out) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder try {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder ManchesterOWLSyntaxRenderer rendi = new ManchesterOWLSyntaxRenderer();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder rendi.render(onto, out);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder } catch (OWLRendererException ex) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.err.println("Error by ManchesterParser!");
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder ex.printStackTrace();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder private static void renderAsXml(boolean quick, OWLOntology onto, BufferedWriter out) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder try {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder OWLXMLRenderer ren = new OWLXMLRenderer();
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder File tempFile = File.createTempFile("owlTemp", ".xml");
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder FileWriter buf = new FileWriter(tempFile);
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder if (quick) {
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder onto.getOWLOntologyManager().removeAxioms(onto, onto.getAxioms());
91e24fc45834b35f2a3830d72565640251149bf3Christian Maeder }
4ba4ec250c14dcdee866a16eeabb61917f7cb028Christian Maeder ren.render(onto, buf);
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder buf.flush();
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder buf.close();
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");
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder rBuf.close();
f792c718a71c2e62f9a22b6ed5e9146704fc08b9Christian Maeder tempFile.deleteOnExit();
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder out.append("<Loaded name=\"").append(manager.getOntologyDocumentIRI(onto));
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder out.append("\" ontiri=\"").append(onto.getOntologyID().getOntologyIRI()).append("\"/>\n");
b9a7827caf331ae5b7eb491455d7e647c83f1345Christian Maeder } catch (Exception ex) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.err.println("Error by XMLParser!");
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder ex.printStackTrace();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder private static void renderAsRdf(OWLOntology onto, BufferedWriter out) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder try {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder RDFXMLRenderer rdfrend = new RDFXMLRenderer(onto, out);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder rdfrend.render();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder } catch (IOException ex) {
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.err.println("Error by RDFParser!");
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder ex.printStackTrace();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder }
2578a6d217296401d347302c9104fee179bd0470Francisc Nicolae Bungiu}