OWL2Parser.java revision c50adef8a083be353c6f07af011359ed16a98d15
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport org.coode.owlapi.owlxml.renderer.OWLXMLRenderer;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport org.coode.owlapi.rdf.rdfxml.RDFXMLRenderer;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport org.semanticweb.owlapi.apibinding.OWLManager;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport org.semanticweb.owlapi.io.OWLRendererException;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport org.semanticweb.owlapi.io.StreamDocumentSource;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport org.semanticweb.owlapi.model.*;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport org.semanticweb.owlapi.util.OWLOntologyMerger;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxRenderer;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport java.io.*;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport java.net.URL;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport java.net.URLConnection;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartreimport java.util.*;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartrepublic class OWL2Parser {
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre private static enum OPTION {OWL_XML, MANCHESTER, RDF_XML}
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre private static OPTION op;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre private static Boolean cyclic = false;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre private static Set<OWLOntology> ontologies;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre private static final Set<OWLOntology> exported = new HashSet<OWLOntology>();
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre private static final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre public static void main(String[] args) {
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre if (args.length < 1) {
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre System.out.println("Usage: processor <URI> [FILENAME] <OPTION>");
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre System.exit(1);
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre }
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // A simple example of how to load and save an ontology
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre try {
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre op = OPTION.MANCHESTER;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre if (args.length > 1) {
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // args[0]: IRI
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // args[1]: name of output file
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // args[2]: type of output file: xml, rdf, or otherwise assume Manchester syntax
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre String filename = args[1];
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre if (args.length == 3) {
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre out = new BufferedWriter(new FileWriter(filename));
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre if (args[2].equals("xml"))
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre op = OPTION.OWL_XML;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre else if (args[2].equals("rdf"))
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre op = OPTION.RDF_XML;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre } else
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // args[0]: IRI
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // args[1]: type of output (or output file for Manchester syntax)
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // xml (OWL XML),
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // rdf (RDF/XML),
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // or otherwise use argument as file name for Manchester syntax
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // for xml and rdf output goes to standard output, i.e. System.out
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre if (args[1].equals("xml"))
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre op = OPTION.OWL_XML;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre else if (args[1].equals("rdf"))
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre op = OPTION.RDF_XML;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre else
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre out = new BufferedWriter(new FileWriter(filename));
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre }
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre out.write("<Ontologies>\n");
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre /* Load an ontology from a physical IRI */
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre URL physicalUrl = new URL(args[0]);
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre URLConnection con = physicalUrl.openConnection();
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre con.addRequestProperty("Accept", "text/plain");
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre StreamDocumentSource sds = new StreamDocumentSource(con.getInputStream());
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration();
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre config = config.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre OWLOntology ontology = manager.loadOntologyFromOntologyDocument(sds, config);
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre ontologies = getImports(ontology, new HashSet<OWLOntology>());
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre if (cyclic) {
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre OWLOntologyMerger merger = new OWLOntologyMerger(manager);
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre String str = ontology.getOntologyID().getOntologyIRI().toString();
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre String merged_name = "";
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre for (OWLOntology aux_ont : ontologies) {
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre String mrg = aux_ont.getOntologyID().getOntologyIRI().toString();
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre mrg = mrg.replaceAll("[<>\\(\\) ]", "");
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre mrg = mrg.replaceAll(".*/", "");
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre mrg = mrg.replaceAll("\\[.*]", "");
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre merged_name = merged_name + mrg;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre }
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre merged_name = str + merged_name;
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // System.out.println("NAME: " + merged_name + "\n");
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre IRI mergedOntologyIRI = IRI.create(merged_name);
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre // System.out.println("MERGED_IRI " + mergedOntologyIRI + "\n");
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre OWLOntology merged = merger.createMergedOntology(manager, mergedOntologyIRI);
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre renderUsingOption(merged, out);
49bfb42b00abac0958a1308f4233e366fd083366Alexandre Chartre } else {
ontologies.add(ontology);
exportImports(out);
}
out.write("\n</Ontologies>\n");
out.flush();
out.close();
} catch (Exception ex) {
System.err.println("OWL parse error: " + ex.getMessage());
ex.printStackTrace();
}
}
private static Set<OWLOntology> getImports(OWLOntology ontology, Set<OWLOntology> stop) {
Set<OWLOntology> s = new HashSet<OWLOntology>();
Set<OWLOntology> next = new HashSet<OWLOntology>(stop);
next.add(ontology);
for (OWLOntology imported : ontology.getDirectImports())
if (cyclic || next.contains(imported)) cyclic = true;
else if (!s.contains(imported)) {
Set<OWLOntology> i = getImports(imported, next);
s.add(imported);
s.addAll(i);
}
return s;
}
private static void exportImports(BufferedWriter out) {
Boolean changed;
do {
changed = false;
for (OWLOntology onto : ontologies)
if (exported.containsAll(onto.getDirectImports())) {
if (!exported.contains(onto)) {
changed = exported.add(onto);
renderUsingOption(onto, out);
}
}
} while (changed);
}
private static void renderUsingOption(OWLOntology onto, BufferedWriter out) {
switch (op) {
case OWL_XML:
renderAsXml(onto, out);
break;
case MANCHESTER:
renderAsOmn(onto, out);
break;
case RDF_XML:
renderAsRdf(onto, out);
break;
}
}
private static void renderAsOmn(OWLOntology onto, BufferedWriter out) {
try {
ManchesterOWLSyntaxRenderer rendi = new ManchesterOWLSyntaxRenderer();
rendi.render(onto, out);
} catch (OWLRendererException ex) {
System.err.println("Error by ManchesterParser!");
ex.printStackTrace();
}
}
private static void renderAsXml(OWLOntology onto, BufferedWriter out) {
try {
OWLXMLRenderer ren = new OWLXMLRenderer();
ren.render(onto, out);
out.append("<Loaded name=\"").append(manager.getOntologyDocumentIRI(onto));
out.append("\" ontiri=\"").append(onto.getOntologyID().getOntologyIRI()).append("\"/>\n");
} catch (Exception ex) {
System.err.println("Error by XMLParser!");
ex.printStackTrace();
}
}
private static void renderAsRdf(OWLOntology onto, BufferedWriter out) {
try {
RDFXMLRenderer rdfrend = new RDFXMLRenderer(onto, out);
rdfrend.render();
} catch (IOException ex) {
System.err.println("Error by RDFParser!");
ex.printStackTrace();
}
}
}