OWL2Parser.java revision 4efb09a1f46a0b322633b729c9a9fd50bf270125
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;
2578a6d217296401d347302c9104fee179bd0470Francisc Nicolae Bungiuimport uk.ac.manchester.cs.owl.owlapi.mansyntaxrenderer.ManchesterOWLSyntaxRenderer;
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder private static enum OPTION {OWL_XML, MANCHESTER, RDF_XML}
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 System.out.println("Usage: processor <URI> [FILENAME] <OPTION>");
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder // A simple example of how to load and save an ontology
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
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 out = new BufferedWriter(new FileWriter(filename));
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 // 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 out = new BufferedWriter(new FileWriter(filename));
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder /* Load an ontology from a physical IRI */
ebe1b5c42cea5468e66f088648362125cb3c2818Christian Maeder URLConnection con = physicalUrl.openConnection();
ebe1b5c42cea5468e66f088648362125cb3c2818Christian Maeder con.addRequestProperty("Accept", "text/plain");
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder StreamDocumentSource sds = new StreamDocumentSource(con.getInputStream());
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 OWLOntologyMerger merger = new OWLOntologyMerger(manager);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder String str = ontology.getOntologyID().getOntologyIRI().toString();
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder String mrg = aux_ont.getOntologyID().getOntologyIRI().toString();
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");
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder OWLOntology merged = merger.createMergedOntology(manager, mergedOntologyIRI);
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.err.println("OWL parse error: " + ex.getMessage());
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 for (OWLOntology imported : ontology.getDirectImports())
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder if (next.contains(imported)) cyclic = true;
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder Set<OWLOntology> i = getImports(imported, next);
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder private static void exportImports(BufferedWriter out) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder if (exported.containsAll(onto.getDirectImports())) {
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder private static void renderUsingOption(OWLOntology onto, BufferedWriter out) {
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder private static void renderAsOmn(OWLOntology onto, BufferedWriter out) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder ManchesterOWLSyntaxRenderer rendi = new ManchesterOWLSyntaxRenderer();
d25c69509848853b0b15b93e3cc180cab81047acChristian Maeder System.err.println("Error by ManchesterParser!");
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder private static void renderAsXml(OWLOntology onto, BufferedWriter out) {
4efb09a1f46a0b322633b729c9a9fd50bf270125Christian Maeder out.append("<Loaded name=\"").append(manager.getOntologyDocumentIRI(onto));
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder out.append("\" ontiri=\"").append(onto.getOntologyID().getOntologyIRI()).append("\"/>\n");
8c67d928dcfe67b665392c4d52eab299f7fabfc6Christian Maeder private static void renderAsRdf(OWLOntology onto, BufferedWriter out) {