Beispiel #1
0
 public static Parts Split(string uri)
 {
     return(new OwlName()
     {
         iri = IRI.create(uri)
     }.Split());
 }
Beispiel #2
0
 public IRI getIRIFromId(string qname, EntityKind makeFor)
 {
     if (InvUriMappings.ContainsKey(qname))
     {
         return(IRI.create(InvUriMappings[qname]));
     }
     else
     {
         DlName dl = new DlName()
         {
             id = qname
         };
         var dlp = dl.Split();
         if (InvUriMappings.ContainsKey(dlp.name) && dlp.term == ns2pfx(IRI.create(InvUriMappings[dlp.name]).getNamespace()))
         {
             return(IRI.create(InvUriMappings[dlp.name]));
         }
         else
         {
             return(owlNameingConvention.FromDL(new DlName()
             {
                 id = qname
             }, lex, pfx2ns, makeFor).iri);
         }
     }
 }
Beispiel #3
0
        public DlName ToDL(string uri, ARS.EntityKind makeFor)
        {
            var owlName = new CogniPy.ARS.OwlName()
            {
                iri = IRI.create(uri)
            };

            return(owlNameingConvention.ToDL(owlName, lex, ns2pfx, makeFor));
        }
Beispiel #4
0
            public OwlName Combine()
            {
                string sep = "";

                if (!ns.EndsWith("/") && !ns.EndsWith("#") && !ns.Contains("#"))
                {
                    sep = "#";
                }

                if (name.Contains("/") && ns.EndsWith("/"))
                {
                    ns = ns.Substring(0, ns.Length - 1) + "#";
                }

                return(new OwlName()
                {
                    iri = IRI.create(ns + sep + encodedName)
                });
            }
        public static void Initialize(string ontologyProfilePath)
        {
            try
            {
                Manager = OWLManager.createOWLOntologyManager();
                //tento řádek je potřebný kvůli nedokonale převedené knihovně z javy, kvůli které se načítá owl parser
                [email protected] s = new [email protected]();


                // načtení owl souboru, který obsahuje metamodel a který každá vložená báze individuí importuje
                byte[] owlResourceUPMM = Properties.Resources.UPMM;
                java.io.ByteArrayInputStream upmmInputStream = new java.io.ByteArrayInputStream(owlResourceUPMM);
                OWLOntology ontologyUPMM = Manager.loadOntologyFromOntologyDocument(upmmInputStream);
                MetamodelIRI = ontologyUPMM.getOntologyID().getOntologyIRI();
                Trace.WriteLine("Loaded Metamodel Ontology : " + MetamodelIRI);

                // načtení owl báze konkrétního profilu
                java.io.File processModel     = new java.io.File(ontologyProfilePath);
                OWLOntology  knowledgeProfile = Manager.loadOntologyFromOntologyDocument(processModel);
                ProfileIRI = knowledgeProfile.getOntologyID().getOntologyIRI();
                Trace.WriteLine("Loaded Profile Ontology : " + ProfileIRI);


                // vytvoření reasoneru nad profilem
                Reasoner       = new StructuralReasonerFactory().createReasoner(knowledgeProfile, new SimpleConfiguration());
                PelletReasoner = new PelletReasonerFactory().createReasoner(knowledgeProfile, new SimpleConfiguration());
                //Reasoner = new Reasoner.ReasonerFactory().createReasoner(knowledgeProfile); HermiT
                Trace.WriteLine("Reasoner is running!");

                DataFactory = Manager.getOWLDataFactory();
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Ontology loading failed!", ex);
            }
        }
Beispiel #6
0
        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));
        }
Beispiel #7
0
 static public string IRI2Path(IRI iri)
 {
     return(iri.toURI().toString().Replace('\\', '/'));
 }
Beispiel #8
0
 static public IRI Path2IRI(string fileName)
 {
     return(IRI.create(fileName.Replace('\\', '/')));
 }
Beispiel #9
0
        /// <summary>Creates an array of RDF triples for the given graph.</summary>
        /// <remarks>Creates an array of RDF triples for the given graph.</remarks>
        /// <param name="graph">the graph to create RDF triples for.</param>
        internal virtual void GraphToRDF(string graphName,
                                         JObject graph
                                         )
        {
            // 4.2)
            IList <Quad> triples = new List <Quad>();

            // 4.3)
            var subjects = graph.GetKeys();

            // Collections.sort(subjects);
            foreach (var id in subjects)
            {
                if (JsonLdUtils.IsRelativeIri(id))
                {
                    continue;
                }
                var node       = (JObject)graph[id];
                var properties = new JArray(node.GetKeys());
                properties.SortInPlace();
                foreach (string property in properties)
                {
                    var    localProperty = property;
                    JArray values;

                    // 4.3.2.1)
                    if ("@type".Equals(localProperty))
                    {
                        values        = (JArray)node["@type"];
                        localProperty = JsonLdConsts.RdfType;
                    }
                    else
                    {
                        // 4.3.2.2)
                        if (JsonLdUtils.IsKeyword(localProperty))
                        {
                            continue;
                        }

                        // 4.3.2.3)
                        if (localProperty.StartsWith("_:") && !api.opts.GetProduceGeneralizedRdf())
                        {
                            continue;
                        }

                        // 4.3.2.4)
                        if (JsonLdUtils.IsRelativeIri(localProperty))
                        {
                            continue;
                        }
                        values = (JArray)node[localProperty];
                    }

                    Node subject;
                    if (id.IndexOf("_:") == 0)
                    {
                        subject = new BlankNode(id);
                    }
                    else
                    {
                        subject = new IRI(id);
                    }

                    // RDF predicates
                    Node predicate;
                    if (localProperty.StartsWith("_:"))
                    {
                        predicate = new BlankNode(localProperty);
                    }
                    else
                    {
                        predicate = new IRI(localProperty);
                    }
                    foreach (var item in values)
                    {
                        // convert @list to triples
                        if (JsonLdUtils.IsList(item))
                        {
                            var  list       = (JArray)((JObject)item)["@list"];
                            Node last       = null;
                            var  firstBNode = nil;
                            if (!list.IsEmpty())
                            {
                                last       = ObjectToRDF(list[list.Count - 1]);
                                firstBNode = new BlankNode(api.GenerateBlankNodeIdentifier());
                            }

                            triples.Add(new Quad(subject, predicate, firstBNode, graphName));
                            for (var i = 0; i < list.Count - 1; i++)
                            {
                                var @object = ObjectToRDF(list[i]);
                                triples.Add(new Quad(firstBNode, first, @object, graphName));
                                Node restBNode = new BlankNode(api.GenerateBlankNodeIdentifier
                                                                   ());
                                triples.Add(new Quad(firstBNode, rest, restBNode, graphName));
                                firstBNode = restBNode;
                            }

                            if (last != null)
                            {
                                triples.Add(new Quad(firstBNode, first, last, graphName));
                                triples.Add(new Quad(firstBNode, rest, nil, graphName));
                            }
                        }
                        else
                        {
                            // convert value or node object to triple
                            var @object = ObjectToRDF(item);
                            if (@object != null)
                            {
                                triples.Add(new Quad(subject, predicate, @object, graphName));
                            }
                        }
                    }
                }
            }

            this[graphName] = triples;
        }
Beispiel #10
0
        private IRI decode_IRI()
        {
            push("IRI");

            bool decoded = true;
            int s0 = index;
            var e0 = new List<Rule>();
            Rule rule;

            decoded = false;
            if (!decoded)
            {
                {
                    var e1 = new List<Rule>();
                    int s1 = index;
                    decoded = true;
                    if (decoded)
                    {
                        bool f1 = true;
                        int c1 = 0;
                        for (int i1 = 0; i1 < 1 && f1; i1++)
                        {
                            rule = decode_scheme();
                            if ((f1 = rule != null))
                            {
                                e1.Add(rule);
                                c1++;
                            }
                        }
                        decoded = c1 == 1;
                    }
                    if (decoded)
                    {
                        bool f1 = true;
                        int c1 = 0;
                        for (int i1 = 0; i1 < 1 && f1; i1++)
                        {
                            rule = decode_StringValue(":");
                            if ((f1 = rule != null))
                            {
                                e1.Add(rule);
                                c1++;
                            }
                        }
                        decoded = c1 == 1;
                    }
                    if (decoded)
                    {
                        bool f1 = true;
                        int c1 = 0;
                        for (int i1 = 0; i1 < 1 && f1; i1++)
                        {
                            rule = decode_ihier_part();
                            if ((f1 = rule != null))
                            {
                                e1.Add(rule);
                                c1++;
                            }
                        }
                        decoded = c1 == 1;
                    }
                    if (decoded)
                    {
                        bool f1 = true;
                        int c1 = 0;
                        for (int i1 = 0; i1 < 1 && f1; i1++)
                        {
                            decoded = false;
                            if (!decoded)
                            {
                                {
                                    var e2 = new List<Rule>();
                                    int s2 = index;
                                    decoded = true;
                                    if (decoded)
                                    {
                                        bool f2 = true;
                                        int c2 = 0;
                                        for (int i2 = 0; i2 < 1 && f2; i2++)
                                        {
                                            rule = decode_StringValue("?");
                                            if ((f2 = rule != null))
                                            {
                                                e2.Add(rule);
                                                c2++;
                                            }
                                        }
                                        decoded = c2 == 1;
                                    }
                                    if (decoded)
                                    {
                                        bool f2 = true;
                                        int c2 = 0;
                                        for (int i2 = 0; i2 < 1 && f2; i2++)
                                        {
                                            rule = decode_iquery();
                                            if ((f2 = rule != null))
                                            {
                                                e2.Add(rule);
                                                c2++;
                                            }
                                        }
                                        decoded = c2 == 1;
                                    }
                                    if (decoded)
                                        e1.AddRange(e2);
                                    else
                                        index = s2;
                                }
                            }
                            if (decoded)
                                c1++;
                            f1 = decoded;
                        }
                        decoded = true;
                    }
                    if (decoded)
                    {
                        bool f1 = true;
                        int c1 = 0;
                        for (int i1 = 0; i1 < 1 && f1; i1++)
                        {
                            decoded = false;
                            if (!decoded)
                            {
                                {
                                    var e2 = new List<Rule>();
                                    int s2 = index;
                                    decoded = true;
                                    if (decoded)
                                    {
                                        bool f2 = true;
                                        int c2 = 0;
                                        for (int i2 = 0; i2 < 1 && f2; i2++)
                                        {
                                            rule = decode_StringValue("#");
                                            if ((f2 = rule != null))
                                            {
                                                e2.Add(rule);
                                                c2++;
                                            }
                                        }
                                        decoded = c2 == 1;
                                    }
                                    if (decoded)
                                    {
                                        bool f2 = true;
                                        int c2 = 0;
                                        for (int i2 = 0; i2 < 1 && f2; i2++)
                                        {
                                            rule = decode_ifragment();
                                            if ((f2 = rule != null))
                                            {
                                                e2.Add(rule);
                                                c2++;
                                            }
                                        }
                                        decoded = c2 == 1;
                                    }
                                    if (decoded)
                                        e1.AddRange(e2);
                                    else
                                        index = s2;
                                }
                            }
                            if (decoded)
                                c1++;
                            f1 = decoded;
                        }
                        decoded = true;
                    }
                    if (decoded)
                        e0.AddRange(e1);
                    else
                        index = s1;
                }
            }

            rule = null;
            if (decoded)
                rule = new IRI(text.Substring(s0, index - s0), e0);
            else
                index = s0;

            pop("IRI", decoded, index - s0);

            return (IRI)rule;
        }
Beispiel #11
0
 public IRI(IRI rule)
     : base(rule.spelling, rule.rules)
 {
 }