Esempio n. 1
0
        /// <summary>
        /// Merges another Graph into the current Graph.
        /// </summary>
        /// <param name="g">Graph to Merge into this Graph.</param>
        /// <param name="keepOriginalGraphUri">Indicates that the Merge should preserve the Graph URIs of Nodes so they refer to the Graph they originated in.</param>
        /// <remarks>
        /// <para>
        /// The Graph on which you invoke this method will preserve its Blank Node IDs while the Blank Nodes from the Graph being merged in will be given new IDs as required in the scope of this Graph.
        /// </para>
        /// <para>
        /// The Graph will raise the <see cref="MergeRequested">MergeRequested</see> event before the Merge operation which gives any event handlers the oppurtunity to cancel this event.  When the Merge operation is completed the <see cref="Merged">Merged</see> event is raised.
        /// </para>
        /// </remarks>
        public virtual void Merge(IGraph g, bool keepOriginalGraphUri)
        {
            if (ReferenceEquals(this, g))
            {
                throw new RdfException("You cannot Merge an RDF Graph with itself");
            }

            // Check that the merge can go ahead
            if (!RaiseMergeRequested())
            {
                return;
            }

            // First copy and Prefixes across which aren't defined in this Graph
            _nsmapper.Import(g.NamespaceMap);

            if (IsEmpty)
            {
                // Empty Graph so do a quick copy
                foreach (Triple t in g.Triples)
                {
                    Assert(new Triple(Tools.CopyNode(t.Subject, this, keepOriginalGraphUri), Tools.CopyNode(t.Predicate, this, keepOriginalGraphUri), Tools.CopyNode(t.Object, this, keepOriginalGraphUri), t.Context));
                }
            }
            else
            {
                // Prepare a mapping of Blank Nodes to Blank Nodes
                Dictionary <INode, IBlankNode> mapping = new Dictionary <INode, IBlankNode>();

                foreach (Triple t in g.Triples)
                {
                    INode s, p, o;
                    if (t.Subject.NodeType == NodeType.Blank)
                    {
                        if (!mapping.ContainsKey(t.Subject))
                        {
                            IBlankNode temp = CreateBlankNode();
                            if (keepOriginalGraphUri)
                            {
                                temp.GraphUri = t.Subject.GraphUri;
                            }
                            mapping.Add(t.Subject, temp);
                        }
                        s = mapping[t.Subject];
                    }
                    else
                    {
                        s = Tools.CopyNode(t.Subject, this, keepOriginalGraphUri);
                    }

                    if (t.Predicate.NodeType == NodeType.Blank)
                    {
                        if (!mapping.ContainsKey(t.Predicate))
                        {
                            IBlankNode temp = CreateBlankNode();
                            if (keepOriginalGraphUri)
                            {
                                temp.GraphUri = t.Predicate.GraphUri;
                            }
                            mapping.Add(t.Predicate, temp);
                        }
                        p = mapping[t.Predicate];
                    }
                    else
                    {
                        p = Tools.CopyNode(t.Predicate, this, keepOriginalGraphUri);
                    }

                    if (t.Object.NodeType == NodeType.Blank)
                    {
                        if (!mapping.ContainsKey(t.Object))
                        {
                            IBlankNode temp = CreateBlankNode();
                            if (keepOriginalGraphUri)
                            {
                                temp.GraphUri = t.Object.GraphUri;
                            }
                            mapping.Add(t.Object, temp);
                        }
                        o = mapping[t.Object];
                    }
                    else
                    {
                        o = Tools.CopyNode(t.Object, this, keepOriginalGraphUri);
                    }

                    Assert(new Triple(s, p, o, t.Context));
                }
            }

            RaiseMerged();
        }
        public void NamespaceMapperEvent()
        {
            bool eventRaised = false;

            NamespaceChanged added   = delegate(String prefix, Uri u) { eventRaised = true; };
            NamespaceChanged changed = delegate(String prefix, Uri u) { eventRaised = true; };
            NamespaceChanged removed = delegate(String prefix, Uri u) { eventRaised = true; };

            NamespaceMapper nsmap = new NamespaceMapper();

            nsmap.NamespaceAdded    += added;
            nsmap.NamespaceModified += changed;
            nsmap.NamespaceRemoved  += removed;

            Console.WriteLine("Trying to add the RDF Namespace, this should already be defined");
            nsmap.AddNamespace("rdf", new Uri(NamespaceMapper.RDF));
            Assert.False(eventRaised);
            eventRaised = false;
            Console.WriteLine();

            Console.WriteLine("Trying to add an example Namespace which isn't defined");
            nsmap.AddNamespace("ex", new Uri("http://example.org/"));
            Assert.True(eventRaised);
            eventRaised = false;
            Console.WriteLine(nsmap.GetNamespaceUri("ex").AbsoluteUri);
            Console.WriteLine();

            Console.WriteLine("Trying to modify the example Namespace");
            nsmap.AddNamespace("ex", new Uri("http://example.org/test/"));
            Assert.True(eventRaised);
            eventRaised = false;
            Console.WriteLine(nsmap.GetNamespaceUri("ex").AbsoluteUri);
            Console.WriteLine();

            Console.WriteLine("Trying to remove the example Namespace");
            nsmap.RemoveNamespace("ex");
            Assert.True(eventRaised);
            eventRaised = false;
            Console.WriteLine();

            Console.WriteLine("Trying to remove a non-existent Namespace");
            nsmap.RemoveNamespace("ex");
            Assert.False(eventRaised);
            eventRaised = false;
            Console.WriteLine();

            Console.WriteLine("Adding some example Namespace back in again for an import test");
            nsmap.AddNamespace("ex", new Uri("http://example.org/"));
            nsmap.AddNamespace("ns0", new Uri("http://example.org/clashes/"));

            Console.WriteLine("Creating another Namespace Mapper with the ex prefix mapped to a different URI");
            NamespaceMapper nsmap2 = new NamespaceMapper();

            nsmap2.AddNamespace("ex", new Uri("http://example.org/test/"));

            Console.WriteLine("Importing the new NamespaceMapper into the original");
            nsmap.Import(nsmap2);
            Console.WriteLine("NamespaceMapper now contains the following Namespaces:");
            foreach (String prefix in nsmap.Prefixes)
            {
                Console.WriteLine("\t" + prefix + " <" + nsmap.GetNamespaceUri(prefix).AbsoluteUri + ">");
            }
            Assert.Equal(nsmap.GetNamespaceUri("ex"), new Uri("http://example.org/"));
            Assert.Equal(nsmap.GetNamespaceUri("ns1"), new Uri("http://example.org/test/"));
        }