Ejemplo n.º 1
0
        public void NodesHashCodes()
        {
            Console.WriteLine("Tests that Literal and URI Nodes produce different Hashes");
            Console.WriteLine();

            //Create the Nodes
            Graph        g = new Graph();
            IUriNode     u = g.CreateUriNode(new Uri("http://www.google.com"));
            ILiteralNode l = g.CreateLiteralNode("http://www.google.com/");

            Console.WriteLine("Created a URI and Literal Node both referring to 'http://www.google.com'");
            Console.WriteLine("String form of URI Node is:");
            Console.WriteLine(u.ToString());
            Console.WriteLine("String form of Literal Node is:");
            Console.WriteLine(l.ToString());
            Console.WriteLine("Hash Code of URI Node is " + u.GetHashCode());
            Console.WriteLine("Hash Code of Literal Node is " + l.GetHashCode());
            Console.WriteLine("Hash Codes are Equal? " + u.GetHashCode().Equals(l.GetHashCode()));
            Console.WriteLine("Nodes are equal? " + u.Equals(l));

            Assert.NotEqual(u.GetHashCode(), l.GetHashCode());
            Assert.NotSame(u, l);
            //Assert.NotEqual(u, l);

            //Create some plain and typed literals which may have colliding Hash Codes
            ILiteralNode plain = g.CreateLiteralNode("test^^http://example.org/type");
            ILiteralNode typed = g.CreateLiteralNode("test", new Uri("http://example.org/type"));

            Console.WriteLine();
            Console.WriteLine("Created a Plain and Typed Literal where the String representations are identical");
            Console.WriteLine("Plain Literal String form is:");
            Console.WriteLine(plain.ToString());
            Console.WriteLine("Typed Literal String from is:");
            Console.WriteLine(typed.ToString());
            Console.WriteLine("Hash Code of Plain Literal is " + plain.GetHashCode());
            Console.WriteLine("Hash Code of Typed Literal is " + typed.GetHashCode());
            Console.WriteLine("Hash Codes are Equal? " + plain.GetHashCode().Equals(typed.GetHashCode()));
            Console.WriteLine("Nodes are equal? " + plain.Equals(typed));

            Assert.NotEqual(plain.GetHashCode(), typed.GetHashCode());
            Assert.NotEqual(plain, typed);

            //Create Triples
            IBlankNode b = g.CreateBlankNode();
            IUriNode   type = g.CreateUriNode("rdf:type");
            Triple     t1, t2;

            t1 = new Triple(b, type, u);
            t2 = new Triple(b, type, l);

            Console.WriteLine();
            Console.WriteLine("Created two Triples stating a Blank Node has rdf:type of the URI Nodes created earlier");
            Console.WriteLine("String form of Triple 1 (using URI Node) is:");
            Console.WriteLine(t1.ToString());
            Console.WriteLine("String form of Triple 2 (using Literal Node) is:");
            Console.WriteLine(t2.ToString());
            Console.WriteLine("Hash Code of Triple 1 is " + t1.GetHashCode());
            Console.WriteLine("Hash Code of Triple 2 is " + t2.GetHashCode());
            Console.WriteLine("Hash Codes are Equal? " + t1.GetHashCode().Equals(t2.GetHashCode()));
            Console.WriteLine("Triples are Equal? " + t1.Equals(t2));

            Assert.NotEqual(t1.GetHashCode(), t2.GetHashCode());
            Assert.NotEqual(t1, t2);

            //Create Triples from the earlier Literal Nodes
            t1 = new Triple(b, type, plain);
            t2 = new Triple(b, type, typed);

            Console.WriteLine();
            Console.WriteLine("Created two Triples stating a Blank Node has rdf:type of the Literal Nodes created earlier");
            Console.WriteLine("String form of Triple 1 (using Plain Literal) is:");
            Console.WriteLine(t1.ToString());
            Console.WriteLine("String form of Triple 2 (using Typed Literal) is:");
            Console.WriteLine(t2.ToString());
            Console.WriteLine("Hash Code of Triple 1 is " + t1.GetHashCode());
            Console.WriteLine("Hash Code of Triple 2 is " + t2.GetHashCode());
            Console.WriteLine("Hash Codes are Equal? " + t1.GetHashCode().Equals(t2.GetHashCode()));
            Console.WriteLine("Triples are Equal? " + t1.Equals(t2));

            Assert.NotEqual(t1.GetHashCode(), t2.GetHashCode());
            Assert.NotEqual(t1, t2);
        }
Ejemplo n.º 2
0
        public void CoreDataModel()
        {
            // 1. A Graph--------------------------------------------------------------------
            // We start off by needing a container for our triples so we create a new Graph.
            IGraph g = new Graph {
                BaseUri = new Uri("http://www.example.org/")
            };
            // ------------------------------------------------------------------------------

            // 2. URI Nodes -----------------------------------------------------------------
            // Triples are composed of INode things so we create some of these aswell.
            // Create a URI Node that refers to some specific URI
            IUriNode dotNetRDF = g.CreateUriNode(UriFactory.Create("http://www.dotnetrdf.org"));

            //Create a URI Node using a QName but we need to define a Namespace first
            g.NamespaceMap.AddNamespace("ex", UriFactory.Create("http://example.org/namespace/"));
            IUriNode demo = g.CreateUriNode("ex:demo");

            // we use the Create() method of the UriFactory class to create URIs since this takes advantage of dotNetRDF's
            // URI interning feature to reduce memory usage and speed up equality comparisons on URIs.
            // ------------------------------------------------------------------------------

            // 3. Blank Nodes ---------------------------------------------------------------
            // As well as URI nodes triples can also contain Blank nodes. A Blank node represents
            // an anonymous resource.

            // Create an anonymous Blank Node
            // Each call to this constructor generates a Blank Node with a new unique identifier within the Graph
            IBlankNode anon = g.CreateBlankNode();

            // Create a named Blank Node
            // Reusing the same ID results in the same Blank Node within the Graph
            // Note that if the ID refers to an automatically assigned ID that is already in use the returned
            // Blank Node will be given an alternative ID
            IBlankNode named = g.CreateBlankNode("ID");

            // name clash with auto assigned ID
            IBlankNode namedAgain = g.CreateBlankNode(anon.InternalID);

            Assert.AreEqual("ID", named.InternalID);
            Assert.AreNotEqual(anon.InternalID, namedAgain.InternalID);

            // ------------------------------------------------------------------------------

            // 4. Literal Nodes -------------------------------------------------------------
            // Literal nodes consist of a value, a datatype and a language code. The second two
            // are optional.

            // Create a Plain Literal
            ILiteralNode plain = g.CreateLiteralNode("some value");

            Assert.IsNull(plain.DataType);
            Assert.IsTrue(string.IsNullOrEmpty(plain.Language));
            Assert.AreEqual("some value", plain.Value);

            // Create some Language Specified Literal
            ILiteralNode hello   = g.CreateLiteralNode("hello", "en");
            ILiteralNode bonjour = g.CreateLiteralNode("bonjour", "fr");

            // Create some typed Literals
            ILiteralNode number = g.CreateLiteralNode("1", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeInteger));
            ILiteralNode tr     = g.CreateLiteralNode("true", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeBoolean));

            // Literal Node equality is somewhat more complex but basically follows the following rules:
            //    If a Language Specifier is present both Nodes must have an identical Language Specifier
            //    If a Data Type URI is present both Nodes must have an identical Data Type URI
            //    The String value of the Literal must match on a character by character basis (using Ordinal comparison)

            ILiteralNode one1 = g.CreateLiteralNode("1", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeInteger));
            ILiteralNode one2 = g.CreateLiteralNode("0001", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeInteger));

            // Use Options.LiteralEqualityMode = LiteralEqualityMode.Loose; to perform equality based on typed values
            // rather than the string representation. This is a global flag and has overhead so should be used sparingly.

            Assert.IsFalse(one1.Equals(one2));
            Options.LiteralEqualityMode = LiteralEqualityMode.Loose;
            Assert.IsTrue(one1.Equals(one2));
            // ------------------------------------------------------------------------------

            // 5. Triple --------------------------------------------------------------------
            g = new Graph();

            // Create some Nodes
            dotNetRDF = g.CreateUriNode(UriFactory.Create("http://www.dotnetrdf.org"));
            IUriNode     createdBy = g.CreateUriNode(UriFactory.Create("http://example.org/createdBy"));
            ILiteralNode robVesse  = g.CreateLiteralNode("Rob Vesse");

            // Assert this Triple
            Triple t = new Triple(dotNetRDF, createdBy, robVesse);

            g.Assert(t);
            Assert.AreEqual(1, g.Triples.Count);
            // ------------------------------------------------------------------------------
        }