Ejemplo n.º 1
0
        /// <summary>
        /// Returns the LiteralNode with the given Value and given Data Type if it exists
        /// </summary>
        /// <param name="literal">The literal value of the Node to select</param>
        /// <param name="datatype">The Uri for the Data Type of the Literal to select</param>
        /// <returns>Either the LiteralNode Or null if no Node with the given Value and Data Type exists</returns>
        public override ILiteralNode GetLiteralNode(String literal, Uri datatype)
        {
            ILiteralNode test             = new LiteralNode(this, literal, datatype);
            IEnumerable <ILiteralNode> ls = from l in this.Nodes.LiteralNodes()
                                            where l.Equals(test)
                                            select l;

            return(ls.FirstOrDefault());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the LiteralNode with the given Value in the given Language if it exists
        /// </summary>
        /// <param name="literal">The literal value of the Node to select</param>
        /// <param name="langspec">The Language Specifier for the Node to select</param>
        /// <returns>Either the LiteralNode Or null if no Node with the given Value and Language Specifier exists</returns>
        public override ILiteralNode GetLiteralNode(String literal, String langspec)
        {
            ILiteralNode test             = new LiteralNode(this, literal, langspec);
            IEnumerable <ILiteralNode> ls = from l in Nodes.LiteralNodes()
                                            where l.Equals(test)
                                            select l;

            return(ls.FirstOrDefault());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Copies a Node so it can be used in another Graph since by default Triples cannot contain Nodes from more than one Graph
        /// </summary>
        /// <param name="original">Node to Copy</param>
        /// <param name="target">Graph to Copy into</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// <strong>Warning:</strong> Copying Blank Nodes may lead to unforseen circumstances since no remapping of IDs between Graphs is done
        /// </para>
        /// </remarks>
        public static INode CopyNode(INode original, IGraph target)
        {
            //No need to copy if it's already in the relevant Graph
            if (ReferenceEquals(original.Graph, target))
            {
                return(original);
            }

            if (original.NodeType == NodeType.Uri)
            {
                IUriNode u  = (IUriNode)original;
                IUriNode u2 = new UriNode(target, u.Uri);

                return(u2);
            }
            else if (original.NodeType == NodeType.Literal)
            {
                ILiteralNode l = (ILiteralNode)original;
                ILiteralNode l2;
                if (l.Language.Equals(String.Empty))
                {
                    if (!(l.DataType == null))
                    {
                        l2 = new LiteralNode(target, l.Value, l.DataType);
                    }
                    else
                    {
                        l2 = new LiteralNode(target, l.Value);
                    }
                }
                else
                {
                    l2 = new LiteralNode(target, l.Value, l.Language);
                }

                return(l2);
            }
            else if (original.NodeType == NodeType.Blank)
            {
                IBlankNode b = (IBlankNode)original;
                IBlankNode b2;

                b2 = new BlankNode(target, b.InternalID);
                return(b2);
            }
            else if (original.NodeType == NodeType.Variable)
            {
                IVariableNode v = (IVariableNode)original;
                return(new VariableNode(target, v.VariableName));
            }
            else
            {
                throw new RdfException("Unable to Copy '" + original.GetType().ToString() + "' Nodes between Graphs");
            }
        }
Ejemplo n.º 4
0
        public void NodesNullNodeEquality()
        {
            UriNode     nullUri     = null;
            LiteralNode nullLiteral = null;
            BlankNode   nullBNode   = null;

            Graph        g           = new Graph();
            IUriNode     someUri     = g.CreateUriNode(new Uri("http://example.org"));
            ILiteralNode someLiteral = g.CreateLiteralNode("A Literal");
            IBlankNode   someBNode   = g.CreateBlankNode();

            Assert.Equal(nullUri, nullUri);
            Assert.Equal(nullUri, null);
            Assert.Equal(null, nullUri);
            Assert.True(nullUri == nullUri, "Null URI Node should be equal to self");
            Assert.True(nullUri == null, "Null URI Node should be equal to a null");
            Assert.True(null == nullUri, "Null should be equal to a Null URI Node");
            Assert.False(nullUri != nullUri, "Null URI Node should be equal to self");
            Assert.False(nullUri != null, "Null URI Node should be equal to a null");
            Assert.False(null != nullUri, "Null should be equal to a Null URI Node");
            Assert.NotEqual(nullUri, someUri);
            Assert.NotEqual(someUri, nullUri);
            Assert.False(nullUri == someUri, "Null URI Node should not be equal to an actual URI Node");
            Assert.False(someUri == nullUri, "Null URI Node should not be equal to an actual URI Node");

            Assert.Equal(nullLiteral, nullLiteral);
            Assert.Equal(nullLiteral, null);
            Assert.Equal(null, nullLiteral);
            Assert.True(nullLiteral == nullLiteral, "Null Literal Node should be equal to self");
            Assert.True(nullLiteral == null, "Null Literal Node should be equal to a null");
            Assert.True(null == nullLiteral, "Null should be equal to a Null Literal Node");
            Assert.False(nullLiteral != nullLiteral, "Null Literal Node should be equal to self");
            Assert.False(nullLiteral != null, "Null Literal Node should be equal to a null");
            Assert.False(null != nullLiteral, "Null should be equal to a Null Literal Node");
            Assert.NotEqual(nullLiteral, someLiteral);
            Assert.NotEqual(someLiteral, nullLiteral);
            Assert.False(nullLiteral == someLiteral, "Null Literal Node should not be equal to an actual Literal Node");
            Assert.False(someLiteral == nullLiteral, "Null Literal Node should not be equal to an actual Literal Node");

            Assert.Equal(nullBNode, nullBNode);
            Assert.Equal(nullBNode, null);
            Assert.Equal(null, nullBNode);
            Assert.True(nullBNode == nullBNode, "Null BNode Node should be equal to self");
            Assert.True(nullBNode == null, "Null BNode Node should be equal to a null");
            Assert.True(null == nullBNode, "Null should be equal to a Null BNode Node");
            Assert.False(nullBNode != nullBNode, "Null BNode Node should be equal to self");
            Assert.False(nullBNode != null, "Null BNode Node should be equal to a null");
            Assert.False(null != nullBNode, "Null should be equal to a Null BNode Node");
            Assert.NotEqual(nullBNode, someBNode);
            Assert.NotEqual(someBNode, nullBNode);
            Assert.False(nullBNode == someBNode, "Null BNode Node should not be equal to an actual BNode Node");
            Assert.False(someBNode == nullBNode, "Null BNode Node should not be equal to an actual BNode Node");
        }
Ejemplo n.º 5
0
    /// <summary>
    /// Copies a Node so it can be used in another Graph since by default Triples cannot contain Nodes from more than one Graph
    /// </summary>
    /// <param name="original">Node to Copy</param>
    /// <param name="target">Graph to Copy into</param>
    /// <returns></returns>
    /// <remarks>
    /// <para>
    /// <strong>Warning:</strong> Copying Blank Nodes may lead to unforseen circumstances since no remapping of IDs between Graphs is done
    /// </para>
    /// </remarks>
    public static INode CopyNode(INode original, IGraph target) {
      // No need to copy if it's already in the relevant Graph
      if (ReferenceEquals(original.Graph, target)) return original;

      // if a node can copy itself then let it do it
      var selfcopyable_original = original as Storage.Virtualisation.ICanCopy;
      if (selfcopyable_original != null) return selfcopyable_original.CopyNode(target);

      // if it doesn't, copy it's values:

      if (original.NodeType == NodeType.Uri) {
        IUriNode u = (IUriNode)original;
        IUriNode u2 = new UriNode(target, u.Uri);

        return u2;
      } else if (original.NodeType == NodeType.Literal) {
        ILiteralNode l = (ILiteralNode)original;
        ILiteralNode l2;
        if (l.Language.Equals(String.Empty)) {
          if (!(l.DataType == null)) {
            l2 = new LiteralNode(target, l.Value, l.DataType);
          } else {
            l2 = new LiteralNode(target, l.Value);
          }
        } else {
          l2 = new LiteralNode(target, l.Value, l.Language);
        }

        return l2;
      } else if (original.NodeType == NodeType.Blank) {
        IBlankNode b = (IBlankNode)original;
        IBlankNode b2;

        b2 = new BlankNode(target, b.InternalID);
        return b2;
      } else if (original.NodeType == NodeType.Variable) {
        IVariableNode v = (IVariableNode)original;
        return new VariableNode(target, v.VariableName);
      } else {
        throw new RdfException("Unable to Copy '" + original.GetType().ToString() + "' Nodes between Graphs");
      }
    }