Exemple #1
0
        /// <summary>
        /// Returns the UriNode with the given QName if it exists
        /// </summary>
        /// <param name="qname">The QName of the Node to select</param>
        /// <returns></returns>
        public override IUriNode GetUriNode(String qname)
        {
            IUriNode test             = new UriNode(this, qname);
            IEnumerable <IUriNode> us = from u in this.Nodes.UriNodes()
                                        where u.Equals(test)
                                        select u;

            return(us.FirstOrDefault());
        }
Exemple #2
0
        /// <summary>
        /// Selects all Triples which have a Uri Node with the given Uri from all the Query Triples
        /// </summary>
        /// <param name="uri">Uri</param>
        /// <returns></returns>
        public IEnumerable <Triple> GetTriples(Uri uri)
        {
            IUriNode u = new UriNode(null, uri);

            return(from g in this._graphs
                   from t in g.Triples
                   where t.Involves(u)
                   select t);
        }
Exemple #3
0
        /// <summary>
        /// Returns the UriNode with the given Uri if it exists
        /// </summary>
        /// <param name="uri">The Uri of the Node to select</param>
        /// <returns>Either the UriNode Or null if no Node with the given Uri exists</returns>
        public override IUriNode GetUriNode(Uri uri)
        {
            IUriNode test             = new UriNode(this, uri);
            IEnumerable <IUriNode> us = from u in Nodes.UriNodes()
                                        where u.Equals(test)
                                        select u;

            return(us.FirstOrDefault());
        }
Exemple #4
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");
            }
        }
Exemple #5
0
        /// <summary>
        /// Selects all Triples where the Subject is a Uri Node with the given Uri from a Subset of Graphs in the Triple Store
        /// </summary>
        /// <param name="graphUris">List of the Graph URIs of Graphs you want to select over</param>
        /// <param name="u">Uri</param>
        /// <returns></returns>
        public IEnumerable <Triple> GetTriplesWithSubject(List <Uri> graphUris, Uri u)
        {
            IUriNode uri = new UriNode(null, u);

            IEnumerable <Triple> ts = from g in this._graphs
                                      where graphUris.Contains(g.BaseUri)
                                      from t in g.Triples
                                      where t.HasSubject(uri)
                                      select t;

            return(ts);
        }
Exemple #6
0
        /// <summary>
        /// Selects all Triples which have a Uri Node with the given Uri from a Subset of Graphs in the Triple Store
        /// </summary>
        /// <param name="graphUris">List of the Graph URIs of Graphs you want to select over</param>
        /// <param name="uri">Uri</param>
        /// <returns></returns>
        public IEnumerable <Triple> GetTriples(List <Uri> graphUris, Uri uri)
        {
            IUriNode u = new UriNode(null, uri);

            IEnumerable <Triple> ts = from g in this._graphs
                                      where graphUris.Contains(g.BaseUri)
                                      from t in g.Triples
                                      where t.Involves(u)
                                      select t;

            return(ts);
        }
        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");
        }
Exemple #8
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");
      }
    }
Exemple #9
0
        /// <summary>
        /// Checks whether the Triple involves a given Uri.
        /// </summary>
        /// <param name="uri">The Uri to test upon.</param>
        /// <returns>True if the Triple has a UriNode with the given Uri.</returns>
        public bool Involves(Uri uri)
        {
            IUriNode temp = new UriNode(null, uri);

            // Does the Subject involve this Uri?
            if (_subject.Equals(temp))
            {
                return(true);
            }
            // Does the Predicate involve this Uri?
            if (_predicate.Equals(temp))
            {
                return(true);
            }
            // Does the Object involve this Uri?
            if (_object.Equals(temp))
            {
                return(true);
            }
            // Not Involved!
            return(false);
        }