Пример #1
0
        public void test2()
        {
            OwlGraph ontology = new OwlGraph();
            ontology.NameSpaces["xmlns:" + OwlNamespaceCollection.OwlNamespacePrefix] = OwlNamespaceCollection.OwlNamespace;
            ontology.NameSpaces["xmlns:" + OwlNamespaceCollection.RdfSchemaNamespacePrefix] = OwlNamespaceCollection.RdfSchemaNamespace;
            ontology.NameSpaces["xmlns:daml"] = "http://www.daml.org/2001/03/daml+oil#";
            ontology.NameSpaces["xmlns:dc"] = "http://purl.org/dc/elements/1.1/";
            ontology.NameSpaces["xmlns"] = "http://www.owl-ontologies.com/test.owl#";
            ontology.NameSpaces["xml:base"] = "http://www.owl-ontologies.com/test.owl";

            string baseUri = "http://www.owl-ontologies.com/test.owl#";

            OwlOntology o = new OwlOntology(baseUri + "testOntology");
            ontology.Nodes.Add(o);

            OwlClass a = new OwlClass(baseUri + "ClassA");
            ontology.Nodes.Add(a);

            OwlClass b = new OwlClass(baseUri + "ClassB");
            ontology.Nodes.Add(b);

            OwlEdge relation = new OwlEdge(OwlNamespaceCollection.RdfSchemaNamespace + "subClassOf");
            relation.AttachParentNode(a);
            relation.AttachChildNode(b);
            ontology.Edges.Add(relation);

            IOwlGenerator generator = new OwlXmlGenerator();
            generator.GenerateOwl(ontology, @"c:\example2.owl");
        }
Пример #2
0
        /// <summary>
        /// Parses the OWL from the given XmlDocument, into an existing graph using the given xml:base uri
        /// </summary>
        /// <param name="doc">The XmlDocument to use as the source of the XML data</param>
        /// <param name="graph">An object that implements the IOwlGraph interface</param>
        /// <param name="xmlbaseUri">The xml:base Uri to use incase one is not found in the XML data or the graph</param>
        /// <returns>An object that implements the IOwlGraph interface</returns>
        public IOwlGraph ParseOwl(XmlDocument doc, IOwlGraph graph, string xmlbaseUri)
        {
            //parses from the xml document
            //if doc is null throws an ArgumentNullException
            //looks for xml:base in doc
            //if xml:base is not found in doc then uses the xmlbaseUri
            //if xmlbaseUri is not a valid Uri it defaults to http://unknown.org/

            if(doc == null)
                throw(new ArgumentNullException("The specified XmlDocument object is a null reference"));

            Warnings.Clear();
            Errors.Clear();
            //Start with the root
            XmlElement root = doc.DocumentElement;

            if(root.Name != "rdf:RDF")
            {
                if(root.Name.ToLower() == "rdf")
                    OnWarning("Unqualified use of rdf as the root element name.");
                else
                    OnWarning("Root element of an OWL document must be rdf:RDF");
            }
            string oldXmlbaseUri = null;
            if(graph == null)
            {
                //Now create the OwlGraph
                _owlGraph = new OwlGraph();
            }
            else
            {
                oldXmlbaseUri = graph.NameSpaces["xml:base"];
                graph.NameSpaces.Remove("xml:base");
                _owlGraph = graph;
            }

            //its an OWL Document so now get the namespace info
            int count = root.Attributes.Count;
            for(int i=0;i<count;i++)
            {
                try
                {
                    string nsName = root.Attributes[i].Name;
                    if(_owlGraph.NameSpaces[nsName] != null)
                        OnWarning("Redefinition of namespace "+nsName);
                    _owlGraph.NameSpaces[nsName] = root.Attributes[i].Value;
                }
                catch(ArgumentException ine)
                {
                    OnWarning(ine.Message);
                }
            }

            string xbUri = _owlGraph.NameSpaces["xml:base"];
            if(xbUri == null)
            {
                xbUri = doc.BaseURI;
                if(!IsValidUri(xbUri))
                {
                    xbUri = xmlbaseUri;
                    if(!IsValidUri(xbUri))
                    {
                        if(oldXmlbaseUri != null)
                            xbUri = oldXmlbaseUri;
                        else
                        {
                            OnWarning("Valid xml:base URI not found. Using http://unknown.org/");
                            xbUri = "http://unknown.org/";
                        }
                    }
                }
            }

            //ignore and discard everything after the first # character
            int pos = xbUri.IndexOf('#');
            if(pos != -1)
            {
                xbUri = xbUri.Remove(pos,xbUri.Length-pos);
            }
            //Now finally set the value of the xml:base Uri
            _owlGraph.NameSpaces["xml:base"] = xbUri;

            if(root.HasChildNodes)
            {
                count = root.ChildNodes.Count;
                for(int i=0;i<count;i++)
                {
                    ProcessNode(root.ChildNodes[i], null);
                }
            }

            return _owlGraph;
        }