public static string GetOWLXML(CNL.DL.Paragraph para, bool owlXml, string externext, Dictionary <string, string> invUriMappings, AnnotationManager annotMan, string ontologyBase = null, Dictionary <string, Tuple <string, string> > prefixes = null, string defaultGuid = null, Dictionary <string, List <string> > owlOntologyAnnotation = null, string generatedBy = "CogniPy") { prefixes = prefixes ?? new Dictionary <string, Tuple <string, string> >(); if (ontologyBase == null) { ontologyBase = "http://www.ontorion.com/ontologies/Ontology" + Guid.NewGuid().ToString("N"); } //var ontologyBase = (ontologyNs ?? "http://www.ontorion.com/ontologies/Ontology" + Guid.NewGuid().ToString("N")); //if(!String.IsNullOrEmpty(defaultGuid)) // ontologyBase = (ontologyNs ?? "http://www.ontorion.com/ontologies/Ontology" + defaultGuid); if (!ontologyBase.EndsWith("/") && !ontologyBase.EndsWith("#") && !ontologyBase.Contains("#")) { ontologyBase += "#"; } OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); CogniPy.ARS.Transform transform = new CogniPy.ARS.Transform(); transform.InvUriMappings = invUriMappings; var df = manager.getOWLDataFactory(); ontology = manager.createOntology(IRI.create(ontologyBase)); org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat owlxmlFormat = null; if (owlXml) { owlxmlFormat = new org.semanticweb.owlapi.io.OWLXMLOntologyFormat(); } else { owlxmlFormat = new org.semanticweb.owlapi.io.RDFXMLOntologyFormat(); } owlxmlFormat.setDefaultPrefix(ontologyBase); foreach (var kv in prefixes) { owlxmlFormat.setPrefix(kv.Key.Replace("$", "."), kv.Value.Item1); // should we put here the Item2 (the location) and not the inner namespace??? if (!String.IsNullOrEmpty(kv.Value.Item2) && !(kv.Value.Item2.EndsWith(".encnl") || kv.Value.Item2.EndsWith(".encnl#"))) // do not export cnl imports (not in OWL!) { // here we need to use Item1 because we cannot export into OWL the specific location of the ontology ---> this is bad practice as in this way we loose the generality of the owl file // imagine that the location is C://mydirectory/... and I add into an owl file: owl:import "C:/mydirectory/". This mean that only on my computer I will be able to import this file. // On the other hand if we write owl:import "namespace of the file" there is a good chance that when someone else will open the file, the file will be imported from internet. var decl = manager.getOWLDataFactory().getOWLImportsDeclaration(OWLPathUriTools.Path2IRI(kv.Value.Item1.TrimEnd('#'))); manager.applyChange(new AddImport(ontology, decl)); } } manager.setOntologyFormat(ontology, owlxmlFormat); if (owlOntologyAnnotation != null) { foreach (var keyVal in owlOntologyAnnotation) { foreach (string val in keyVal.Value) { manager.applyChange(new AddOntologyAnnotation( ontology, manager.getOWLDataFactory().getOWLAnnotation( manager.getOWLDataFactory().getOWLAnnotationProperty(IRI.create(keyVal.Key)), manager.getOWLDataFactory().getOWLLiteral(val)))); } } } transform.setOWLDataFactory(false, ontologyBase, df, owlxmlFormat, CNL.EN.CNLFactory.lex); var paraFromAnnotStatements = annotMan.getDLAnnotationAxioms(); var stmts = para.Statements; foreach (var p in paraFromAnnotStatements) { stmts.AddRange(p.Value); } var conv = transform.Convert(new CogniPy.CNL.DL.Paragraph(null) { Statements = stmts }); var om = new org.coode.xml.OWLOntologyXMLNamespaceManager(manager, ontology); foreach (var axiom in conv.axioms) { if (axiom.comment != null) { var dp = axiom.comment.IndexOf(':'); var x = axiom.comment.Substring(0, dp); if (x.Trim() == "Namespace") { var ontologyIri = axiom.comment.Substring(dp + 1).Trim(); if (ontologyIri.EndsWith(".")) { ontologyIri = ontologyIri.Substring(0, ontologyIri.Length - 1); } if (ontologyIri.StartsWith("\'") && ontologyIri.Length > 2) { ontologyIri = ontologyIri.Substring(1, ontologyIri.Length - 2).Replace("\'\'", "\'"); } manager.removeOntology(ontology); ontology = manager.createOntology(IRI.create(ontologyIri)); om.setDefaultNamespace(ontologyIri + "#"); transform.setOWLDataFactory(true, ontologyBase, df, owlxmlFormat, CNL.EN.CNLFactory.lex); } else if (x.Trim() == "References") { var refs = CNLTools.ParseReferences(axiom.comment.Substring(dp)); foreach (Match match in refs) { var onto = match.Groups["ont"].Value; if (onto.StartsWith("\'") && onto.Length > 2) { onto = onto.Substring(1, onto.Length - 2).Replace("\'\'", "\'").Trim(); } if (!string.IsNullOrEmpty(onto)) { if (onto.ToLower().EndsWith(".encnl")) { onto = OWLConverter.PathToIRIString(onto.Substring(0, onto.Length - ".encnl".Length) + externext); } var ns = match.Groups["ns"].Value; if (ns.StartsWith("\'") && ns.Length > 2) { ns = ns.Substring(1, ns.Length - 2).Replace("\'\'", "\'").Trim(); } else if (string.IsNullOrEmpty(ns)) { ns = onto; } om.setPrefix(match.Groups["pfx"].Value, ns); owlxmlFormat.setPrefix(match.Groups["pfx"].Value, ns); var decl = manager.getOWLDataFactory().getOWLImportsDeclaration(OWLPathUriTools.Path2IRI(onto)); manager.applyChange(new AddImport(ontology, decl)); } } } else { //manager.applyChange(new AddOntologyAnnotation( // ontology, // manager.getOWLDataFactory().getOWLAnnotation( // manager.getOWLDataFactory().getOWLAnnotationProperty(IRI.create(ontologyBase + x)), // manager.getOWLDataFactory().getOWLLiteral(axiom.comment.Substring(dp))))); } } else if (axiom.axiom != null) { manager.addAxiom(ontology, axiom.axiom); } } foreach (var axiom in conv.additions) { manager.addAxiom(ontology, axiom); } var ontout = new org.semanticweb.owlapi.io.StringDocumentTarget(); manager.saveOntology(ontology, owlxmlFormat, ontout); manager.removeOntology(ontology); var retdoc = new XmlDocument(); retdoc.LoadXml(ontout.toString()); foreach (var elem in retdoc.ChildNodes) { if (elem is XmlComment) { retdoc.RemoveChild(elem as XmlComment); } } retdoc.AppendChild(retdoc.CreateComment("Generated by " + generatedBy + ", (with support from OwlApi)")); return(SerializeDoc(retdoc)); }
public static string PathToIRIString(string path) { return(OWLPathUriTools.Path2IRI(path).toString()); }