/// <summary>
        /// Merges the srcGraph into this graph object
        /// </summary>
        /// <param name="srcGraph">An object that implements the IOwlGraph interace</param>
        /// <param name="skipDuplicateEdges">A flag that indicates whether duplicate edges present in both graphs should be skipped during the merge process.</param>
        public void Merge(IOwlGraph srcGraph, bool skipDuplicateEdges)
        {
            if (srcGraph == null)
            {
                return;
            }
            Hashtable literalsAdded = new Hashtable();
            //go through all the nodes in the source graph
            IDictionaryEnumerator enumerator = (IDictionaryEnumerator)srcGraph.Nodes.GetEnumerator();

            while (enumerator.MoveNext())
            {
                //Console.WriteLine(((IOwlNode)(enumerator.Value)).ID);
                //add this node to the graph
                IOwlNode srcParentNode  = (IOwlNode)enumerator.Value;
                IOwlNode destParentNode = AddNode(srcParentNode.ID);
                //go through all of the src node's child edges
                foreach (IOwlEdge srcChildEdge in srcParentNode.ChildEdges)
                {
                    //for each of the src node's child edges do...
                    IOwlNode destChildNode;
                    if (srcChildEdge.ChildNode is IOwlLiteral)
                    {
                        IOwlLiteral srcChildLiteral = srcChildEdge.ChildNode as IOwlLiteral;
                        literalsAdded[srcChildLiteral] = srcChildLiteral;
                        destChildNode = AddLiteral(srcChildLiteral.Value, srcChildLiteral.LangID, srcChildLiteral.Datatype);
                    }
                    else
                    {
                        destChildNode = AddNode(srcChildEdge.ChildNode.ID);
                    }

                    //Now we have the parent and the child nodes added to the graph..

                    bool edgeExists = false;
                    if (skipDuplicateEdges)
                    {
                        //does the new parent node and the new child node have an edge with the same ID as srcChildEdge?
                        //go through all the child edges of destParentNode
                        foreach (OwlEdge tempEdge in destParentNode.ChildEdges)
                        {
                            if ((tempEdge.ChildNode == destChildNode) && (tempEdge.ID == srcChildEdge.ID))
                            {
                                edgeExists = true;
                                break;
                            }
                        }
                    }
                    if (!skipDuplicateEdges || (skipDuplicateEdges && !edgeExists))
                    {
                        OwlEdge destChildEdge = new OwlEdge(srcChildEdge.ID);
                        destParentNode.AttachChildEdge(destChildEdge);
                        destChildEdge.AttachChildNode(destChildNode);
                        //add the edge to the graph
                        AddEdge(destChildEdge);
                    }
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the OwlClass class with the given Uri and TypeNode
 /// </summary>
 /// <param name="nodeUri">A string representing the Uri of this Resource</param>
 /// <param name="typeNode">The OwlNode object to attach to the edge specifying the type. This is usually a node with ID owl:Class.</param>
 /// <exception cref="ArgumentNullException">typeNode is a null reference</exception>
 public OwlClass(string nodeUri, OwlNode typeNode)
 {
     if(typeNode == null)
         throw(new ArgumentNullException());
     ID = nodeUri;
     _typeEdge = new OwlEdge(OwlNamespaceCollection.RdfNamespace+"type");
     _typeEdge.AttachChildNode(typeNode);
     AttachChildEdge(_typeEdge);
 }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the OwlRestriction class with the given Uri and TypeNode
 /// </summary>
 /// <param name="nodeUri">A string representing the Uri of this Resource</param>
 /// <param name="typeNode">The OwlNode object to attach to the edge specifying the type. This is usually a node with ID owl:Restriction.</param>
 /// <exception cref="ArgumentNullException">typeNode is a null reference</exception>
 public OwlRestriction(string nodeUri, OwlNode typeNode)
 {
     if (typeNode == null)
     {
         throw(new ArgumentNullException());
     }
     ID        = nodeUri;
     _typeEdge = new OwlEdge(OwlNamespaceCollection.RdfNamespace + "type");
     _typeEdge.AttachChildNode(typeNode);
     AttachChildEdge(_typeEdge);
 }
 /// <summary>
 /// Adds an OWL Resource of type owl:TransitiveProperty to the graph</summary>
 /// <param name="nodeUri">The Uri of the resource.</param>
 /// <returns>Returns a reference to the newly added resource.</returns>
 /// <exception cref="UriFormatException">The specified nodeUri is not a well formed Uri.</exception>
 private OwlNode AddTransitivePropertyToGraph(string nodeUri)
 {
     OwlNode node = AddObjectPropertyToGraph(nodeUri);
     OwlNode typeNode = (OwlNode)_owlGraph.AddNode(OwlNamespaceCollection.OwlNamespace+"TransitiveProperty");
     OwlEdge typeEdge = new OwlEdge(OwlNamespaceCollection.RdfNamespace+"type");
     _owlGraph.AddEdge(typeEdge);
     typeEdge.AttachChildNode(typeNode);
     node.AttachChildEdge(typeEdge);
     return node;
 }
 /// <summary>
 /// Create a node from the rdf:resource attribute and attach it to a given edge as a child
 /// </summary>
 /// <param name="node">The XmlNode that contains the rdf:resource attribute.</param>
 /// <param name="rEdge">The edge to which the new childnode must be added.</param>
 /// <param name="resourceUri">The URI specified by the rdf:resource attribute.</param>
 /// <remarks>If the specified Uri is null or empty a new blank node URI is created. 
 /// If it is a relative URI then it is converted to an absolute URI by prefixing it with the value given by xml:base</remarks>
 private void ProcessRdfResource(XmlNode node, OwlEdge rEdge, string resourceUri)
 {
     string nodeID = QualifyResource(resourceUri,GetXmlBase(node));
     OwlNode rNode = (OwlNode)_owlGraph.AddNode(nodeID);
     rEdge.AttachChildNode(rNode);
 }
        /// <summary>
        /// Processes the rdf:parseType attribute
        /// </summary>
        /// <param name="node">The XML Node on which the attribute appears</param>
        /// <param name="rEdge">The edge to which the attribute must be applied</param>
        /// <param name="parseType">The parse type as specified by the value of the attribute</param>
        private void ProcessRdfParseType(XmlNode node, OwlEdge rEdge, string parseType)
        {
            this.OnMessage("Parsing a parseType");
            if(parseType == "Resource")
            {
                //its an rdf:parseType="Resource" so we must now parse its children
                //the children of this xmlNode are now EDGES in the RDF graph that
                //extend out from the dest node of this edge i.e. the rdfNode created above

                //create a new node that will be the child of this edge
                //create a new node ID
                OwlNode rNode = (OwlNode)_owlGraph.AddNode(GetBlankNodeUri(null));
                //attach this node to the edge
                rEdge.AttachChildNode(rNode);
                this.OnMessage("Parsing a parseType: Resource");
                if(node.HasChildNodes)
                {
                    int count = node.ChildNodes.Count;
                    for(int i=0;i<count;i++)
                    {
                        this.OnMessage("Going to parse the first child: " + node.ChildNodes[i].OuterXml.ToString());
                        ProcessEdge(node.ChildNodes[i], rNode);
                    }
                }
            }
            else if(parseType == "Literal")
            {
                //its an rdf:parseType="Literal" so all the xml below this node
                //will be the content of the dest node i.e. rdfNode
                //create a new node that will be the child of this edge
                //create a new node ID
                string literalValue = node.InnerXml;
                string datatypeUri = GetDatatypeUri(node);
                if(datatypeUri == null)
                    datatypeUri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral";
                OwlLiteral literal = (OwlLiteral)_owlGraph.AddLiteral(literalValue,rEdge.LangID,datatypeUri);
                //attach this node to the edge
                rEdge.AttachChildNode(literal);
            }
            else if(parseType == "Collection")
            {
                //its a Collection so make a cons list
                //get the children of this node
                OwlNode rNode = BuildCollection(node);
                //connect the collection to the edge
                rEdge.AttachChildNode(rNode);
            }
            else
            {
                OnError("Unknown parseType "+parseType);
            }
        }
 /// <summary>
 /// Process the rdf:nodeID attribute found on the OWL edge
 /// </summary>
 /// <param name="rEdge">The edge to which the rdf:nodeID attribute must be applied</param>
 /// <param name="baseNodeID">The ID specified by the rdf:nodeID attribute</param>
 private void ProcessRdfNodeID(OwlEdge rEdge, string baseNodeID)
 {
     if(!IsXmlName(baseNodeID))
         OnError(baseNodeID+" is not an XML name");
     OwlNode rNode = (OwlNode)_owlGraph.AddNode(GetBlankNodeUri(baseNodeID));
     rEdge.AttachChildNode(rNode);
 }
        /// <summary>
        /// Processes an edge in the XML document. 
        /// </summary>
        /// <param name="node">The XmlNode to process.</param>
        /// <param name="parent">The parent of the current node.</param>
        /// <returns>Returns a reference to the new edge created</returns>
        private Object ProcessEdge(XmlNode node,  Object parent)
        {
            //if the node is a comment or anything else then totally ignore
            if((node.NodeType == XmlNodeType.Comment) || (node.NodeType == XmlNodeType.None) || (node.NodeType == XmlNodeType.Whitespace) || (node.NodeType == XmlNodeType.SignificantWhitespace))
                return true;
            OwlEdge rEdge = null;

            if(node.NodeType == XmlNodeType.Element)
            {
                //get the xml:base attribute...
                XmlAttribute xmlBaseAttr = node.Attributes["xml:base"];
                if((xmlBaseAttr == null) && (parent != null))
                {
                    //ok the child does not have an xml:base... and the parent is not null i.e. this node is not a child of the rdf:RDF root
                    xmlBaseAttr = node.ParentNode.Attributes["xml:base"];
                    if(xmlBaseAttr != null)
                        node.Attributes.Append(xmlBaseAttr);
                }
            }

            rEdge = new OwlEdge();

            OwlNode parentOwlNode = (OwlNode)parent;
            //process the xml:lang attribute if applicable
            //rEdge.LangID = parentOwlNode.LangID;

            if(ParseEdgeRdfAttributes(node,rEdge)) //if the process attributes method returns true then process the children of this node
            {
                if(node.HasChildNodes)
                {
                    int count = node.ChildNodes.Count;
                    for(int i=0;i<count;i++)
                        ProcessNode(node.ChildNodes[i], rEdge);
                }
            }

            if(!ParseEdgeSyntax(node,rEdge, parentOwlNode))
            {
                rEdge.ID = node.NamespaceURI+node.LocalName;
                rEdge.AttachParentNode(parentOwlNode);

            }

            //coming back up the tree
            ParseEdgeAttributes(node,rEdge);

            //if the edge is dangling then put an empty Literal on it.
            if(rEdge.ChildNode == null)
                rEdge.AttachChildNode(_owlGraph.AddLiteral("",null,null));

            //add the edge to the Graph
            _owlGraph.AddEdge(rEdge);

            return rEdge;
        }
        /// <summary>
        /// Parses the rdf:value, rdf:type attributes as well as any attributes not part of the OWL, RDF or XML namespace
        /// </summary>
        /// <param name="node">The XmlNode on which the attributes appear</param>
        /// <param name="rNode">The OwlNode to which the attributes must be applied</param>
        private void ParseNodeAttributes(XmlNode node, OwlNode rNode)
        {
            int count = node.Attributes.Count;
            for(int i=0;i<count;i++)
            {
                XmlAttribute attr = node.Attributes[i];
                if(!IsOwlRdfXmlProperty(attr) || (attr.Name=="rdf:type") || (attr.Name == "rdf:value"))
                {
                    if(IsUnqualifiedRdfProperty(node.Attributes[i]))
                        OnWarning("Unqualified use of rdf:"+node.Attributes[i].LocalName);

                    //create a new edge
                    string edgeUri = PrependXmlBase(attr.NamespaceURI+attr.LocalName, GetXmlBase(node));
                    OwlEdge rEdge = new OwlEdge(edgeUri);
                    OwlNode childNode = null;
                    if(attr.Name == "rdf:type")
                    {
                        childNode = (OwlNode)_owlGraph.AddNode(PrependXmlBase(attr.Value,GetXmlBase(node)));
                    }
                    else
                    {
                        childNode = (OwlLiteral)_owlGraph.AddLiteral(attr.Value,rNode.LangID,GetDatatypeUri(node));
                    }

                    rNode.AttachChildEdge(rEdge);
                    rEdge.AttachChildNode(childNode);
                    //add the new edge to the graph
                    _owlGraph.AddEdge(rEdge);
                }
            }
        }
        /// <summary>
        /// Process the rdf:value and any attributes not in the rdf or xml namespace
        /// </summary>
        /// <param name="node">The XmlNode that attributes appear on</param>
        /// <param name="rEdge">The Owl edge that the attributes must be applied to.</param>
        private void ParseEdgeAttributes(XmlNode node, OwlEdge rEdge)
        {
            //go through all the attributes
            int count = node.Attributes.Count;
            for(int i=0;i<count;i++)
            {
                XmlAttribute attr = node.Attributes[i];

                if((!IsSyntacticElement(attr))  || (attr.Prefix == "rdf" && attr.LocalName == "value"))
                {
                    if((attr.NamespaceURI == null) || (attr.NamespaceURI.Length == 0))
                    {
                        OnError("Unqualified attribute: "+attr.LocalName);
                        continue;
                    }
                    if((attr.Name == "rdf:value")&& (rEdge.ChildNode is OwlLiteral))
                    {
                        OnError("Cannot use rdf:value ("+attr.Value+") as property for a literal ("+((OwlLiteral)rEdge.ChildNode).Value+").");
                        continue;
                    }
                    //if the childnode of the edge is a literal then it cant have any arcs going out from it
                    if((rEdge.ChildNode != null) && (rEdge.ChildNode is OwlLiteral) && (attr.Name != "rdf:datatype"))
                    {
                        OnError("Cannot have property "+attr.Name+" for an Owl Literal "+rEdge.ChildNode.ID);
                        continue;
                    }

                    string edgeID = attr.NamespaceURI+attr.LocalName;
                    string literalValue = attr.Value;
                    string langID = rEdge.LangID;
                    string datatypeUri = GetDatatypeUri(node);

                    //if this edge does not have a child node then create a blank node and add it
                    if(rEdge.ChildNode == null)
                        rEdge.AttachChildNode((OwlNode)_owlGraph.AddNode(GetBlankNodeUri(null)));

                    //make an edge from the child of rEdge
                    OwlEdge childEdge = new OwlEdge();
                    childEdge.ID = edgeID;
                    OwlLiteral childLiteral = (OwlLiteral)_owlGraph.AddLiteral(literalValue,langID,datatypeUri);

                    rEdge.ChildNode.AttachChildEdge(childEdge);
                    //attach it to the edge
                    childEdge.AttachChildNode(childLiteral);
                    //add the edge to the graph
                    _owlGraph.AddEdge(childEdge);
                }
            }
        }
Exemple #11
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");
        }
        /// <summary>
        /// Merges the srcGraph into this graph object
        /// </summary>
        /// <param name="srcGraph">An object that implements the IOwlGraph interace</param>
        /// <param name="skipDuplicateEdges">A flag that indicates whether duplicate edges present in both graphs should be skipped during the merge process.</param>
        public void Merge(IOwlGraph srcGraph, bool skipDuplicateEdges)
        {
            if(srcGraph == null)
                return;
            Hashtable literalsAdded = new Hashtable();
            //go through all the nodes in the source graph
            IDictionaryEnumerator enumerator = (IDictionaryEnumerator)srcGraph.Nodes.GetEnumerator();
            while(enumerator.MoveNext())
            {
                //Console.WriteLine(((IOwlNode)(enumerator.Value)).ID);
                //add this node to the graph
                IOwlNode srcParentNode = (IOwlNode)enumerator.Value;
                IOwlNode destParentNode = AddNode(srcParentNode.ID);
                //go through all of the src node's child edges
                foreach(IOwlEdge srcChildEdge in srcParentNode.ChildEdges)
                {
                    //for each of the src node's child edges do...
                    IOwlNode destChildNode;
                    if(srcChildEdge.ChildNode is IOwlLiteral)
                    {
                        IOwlLiteral srcChildLiteral = srcChildEdge.ChildNode as IOwlLiteral;
                        literalsAdded[srcChildLiteral] = srcChildLiteral;
                        destChildNode = AddLiteral(srcChildLiteral.Value, srcChildLiteral.LangID, srcChildLiteral.Datatype);
                    }
                    else
                    {
                        destChildNode = AddNode(srcChildEdge.ChildNode.ID);
                    }

                    //Now we have the parent and the child nodes added to the graph..

                    bool edgeExists = false;
                    if(skipDuplicateEdges)
                    {
                        //does the new parent node and the new child node have an edge with the same ID as srcChildEdge?
                        //go through all the child edges of destParentNode
                        foreach(OwlEdge tempEdge in destParentNode.ChildEdges)
                        {
                            if((tempEdge.ChildNode == destChildNode) && (tempEdge.ID == srcChildEdge.ID))
                            {
                                edgeExists = true;
                                break;
                            }
                        }
                    }
                    if(!skipDuplicateEdges || (skipDuplicateEdges && !edgeExists))
                    {
                        OwlEdge destChildEdge = new OwlEdge(srcChildEdge.ID);
                        destParentNode.AttachChildEdge(destChildEdge);
                        destChildEdge.AttachChildNode(destChildNode);
                        //add the edge to the graph
                        AddEdge(destChildEdge);
                    }
                }
            }
        }