Exemplo n.º 1
0
 /// <summary>
 /// Determines whether this Node is equal to a Graph Literal Node
 /// </summary>
 /// <param name="other">Graph Literal Node</param>
 /// <returns></returns>
 public override bool Equals(IGraphLiteralNode other)
 {
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(EqualityHelper.AreGraphLiteralsEqual(this, other));
 }
Exemplo n.º 2
0
 /// <summary>
 /// Determines whether this Node is equal to a Graph Literal Node (should always be false).
 /// </summary>
 /// <param name="other">Graph Literal Node.</param>
 /// <returns></returns>
 public override bool Equals(IGraphLiteralNode other)
 {
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(false);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a Node for the Context
        /// </summary>
        /// <param name="n">Node</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// In effect all this does is ensure that all Nodes end up in the same Graph which may occassionally not happen otherwise when Graph wrappers are involved
        /// </para>
        /// </remarks>
        public INode GetNode(INode n)
        {
            if (this._nodeMap == null)
            {
                this._nodeMap = new MultiDictionary <INode, INode>(new FastVirtualNodeComparer());
            }

            if (this._nodeMap.ContainsKey(n))
            {
                return(this._nodeMap[n]);
            }

            INode temp;

            switch (n.NodeType)
            {
            case NodeType.Blank:
                temp = this.GetBlankNode(((IBlankNode)n).InternalID);
                break;

            case NodeType.Variable:
                IVariableNode v = (IVariableNode)n;
                temp = this._factory.CreateVariableNode(v.VariableName);
                break;

            case NodeType.GraphLiteral:
                IGraphLiteralNode g = (IGraphLiteralNode)n;
                temp = this._factory.CreateGraphLiteralNode(g.SubGraph);
                break;

            case NodeType.Uri:
                IUriNode u = (IUriNode)n;
                temp = this._factory.CreateUriNode(u.Uri);
                break;

            case NodeType.Literal:
                ILiteralNode l = (ILiteralNode)n;
                if (l.DataType != null)
                {
                    temp = this._factory.CreateLiteralNode(l.Value, l.DataType);
                }
                else if (!l.Language.Equals(String.Empty))
                {
                    temp = this._factory.CreateLiteralNode(l.Value, l.Language);
                }
                else
                {
                    temp = this._factory.CreateLiteralNode(l.Value);
                }
                break;

            default:
                throw new RdfQueryException("Cannot construct unknown Node Types");
            }
            this._nodeMap.Add(n, temp);
            return(temp);
        }
        /// <summary>
        /// Formats a Graph Literal Node for Notation 3
        /// </summary>
        /// <param name="glit">Graph Literal</param>
        /// <param name="segment">Triple Segment</param>
        /// <returns></returns>
        protected override string FormatGraphLiteralNode(IGraphLiteralNode glit, TripleSegment? segment)
        {
            if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.GraphLiteralPredicatesUnserializable(this.FormatName));

            StringBuilder output = new StringBuilder();
            output.Append("{");
            foreach (Triple t in glit.SubGraph.Triples)
            {
                output.Append(this.Format(t));
            }
            output.Append("}");
            return output.ToString();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Compares two Graph Literals
        /// </summary>
        /// <param name="a">First Graph Literal</param>
        /// <param name="b">Second Graph Literal</param>
        /// <returns></returns>
        public static int CompareGraphLiterals(IGraphLiteralNode a, IGraphLiteralNode b)
        {
            if (ReferenceEquals(a, b)) return 0;
            if (a == null)
            {
                if (b == null) return 0;
                return -1;
            }
            else if (b == null)
            {
                return 1;
            }

            return a.SubGraph.Triples.Count.CompareTo(b.SubGraph.Triples.Count);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Returns an Integer indicating the Ordering of this Node compared to another Node.
 /// </summary>
 /// <param name="other">Node to test against.</param>
 /// <returns></returns>
 public override int CompareTo(IGraphLiteralNode other)
 {
     if (ReferenceEquals(this, other))
     {
         return(0);
     }
     if (other == null)
     {
         // We are always greater than nulls
         return(1);
     }
     else
     {
         // We are less than Graph Literal Nodes
         return(-1);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Formats a Graph Literal Node for Notation 3.
        /// </summary>
        /// <param name="glit">Graph Literal.</param>
        /// <param name="segment">Triple Segment.</param>
        /// <returns></returns>
        protected override string FormatGraphLiteralNode(IGraphLiteralNode glit, TripleSegment?segment)
        {
            if (segment == TripleSegment.Predicate)
            {
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralPredicatesUnserializable(FormatName));
            }

            StringBuilder output = new StringBuilder();

            output.Append("{");
            foreach (Triple t in glit.SubGraph.Triples)
            {
                output.Append(Format(t));
            }
            output.Append("}");
            return(output.ToString());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Returns an Integer indicating the Ordering of this Node compared to another Node
        /// </summary>
        /// <param name="other">Node to test against</param>
        /// <returns></returns>
        public override int CompareTo(IGraphLiteralNode other)
        {
            if (ReferenceEquals(this, other))
            {
                return(0);
            }

            if (other == null)
            {
                // Variables are considered greater than null
                return(1);
            }
            else
            {
                // Variable Nodes are less than everything else
                return(-1);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns an Integer indicating the Ordering of this Node compared to another Node.
        /// </summary>
        /// <param name="other">Node to test against.</param>
        /// <returns></returns>
        public override int CompareTo(IGraphLiteralNode other)
        {
            if (ReferenceEquals(this, other))
            {
                return(0);
            }

            if (other == null)
            {
                // Everything is greater than a null
                // Return a 1 to indicate this
                return(1);
            }
            else
            {
                // URI Nodes are less than Graph Literal Nodes
                return(-1);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Compares two Graph Literals.
        /// </summary>
        /// <param name="a">First Graph Literal.</param>
        /// <param name="b">Second Graph Literal.</param>
        /// <returns></returns>
        public static int CompareGraphLiterals(IGraphLiteralNode a, IGraphLiteralNode b)
        {
            if (ReferenceEquals(a, b))
            {
                return(0);
            }
            if (a == null)
            {
                if (b == null)
                {
                    return(0);
                }
                return(-1);
            }
            else if (b == null)
            {
                return(1);
            }

            return(a.SubGraph.Triples.Count.CompareTo(b.SubGraph.Triples.Count));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Determines whether two Graph Literals are equal.
        /// </summary>
        /// <param name="a">First Blank Node.</param>
        /// <param name="b">Second Blank Node.</param>
        /// <returns></returns>
        public static bool AreGraphLiteralsEqual(IGraphLiteralNode a, IGraphLiteralNode b)
        {
            if (ReferenceEquals(a, b))
            {
                return(true);
            }
            if (a == null)
            {
                if (b == null)
                {
                    return(true);
                }
                return(false);
            }
            else if (b == null)
            {
                return(false);
            }

            return(a.SubGraph.Equals(b.SubGraph));
        }
Exemplo n.º 12
0
 /// <summary>
 /// Nodes must implement an Equals method so we can do type specific equality.
 /// </summary>
 /// <param name="other">Node to check for equality.</param>
 /// <returns></returns>
 /// <remarks>
 /// Nodes implementations are also required to implement an override of the non-generic Equals method.  Standard implementations of some equality comparisons can be found in <see cref="EqualityHelper">EqualityHelper</see>.
 /// </remarks>
 public abstract bool Equals(IGraphLiteralNode other);
Exemplo n.º 13
0
 /// <summary>
 /// Nodes must implement a CompareTo method to allow them to be Sorted.
 /// </summary>
 /// <param name="other">Node to compare self to.</param>
 /// <returns></returns>
 /// <remarks>
 /// Implementations should use the SPARQL Term Sort Order for ordering nodes (as opposed to value sort order).  Standard implementations of Node type specific comparisons can be found in <see cref="ComparisonHelper">ComparisonHelper</see>.
 /// </remarks>
 public abstract int CompareTo(IGraphLiteralNode other);
Exemplo n.º 14
0
        /// <summary>
        /// Determines whether two Graph Literals are equal
        /// </summary>
        /// <param name="a">First Blank Node</param>
        /// <param name="b">Second Blank Node</param>
        /// <returns></returns>
        public static bool AreGraphLiteralsEqual(IGraphLiteralNode a, IGraphLiteralNode b)
        {
            if (ReferenceEquals(a, b)) return true;
            if (a == null)
            {
                if (b == null) return true;
                return false;
            }
            else if (b == null)
            {
                return false;
            }

            return a.SubGraph.Equals(b.SubGraph);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Formats a Graph Literal Node as a String for the given Format
 /// </summary>
 /// <param name="glit">Graph Literal</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected virtual String FormatGraphLiteralNode(IGraphLiteralNode glit, TripleSegment? segment)
 {
     throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable(this._format));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Formats a Graph Literal Node as a String for the given Format.
 /// </summary>
 /// <param name="glit">Graph Literal.</param>
 /// <param name="segment">Triple Segment.</param>
 /// <returns></returns>
 protected virtual String FormatGraphLiteralNode(IGraphLiteralNode glit, TripleSegment?segment)
 {
     throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable(_format));
 }
Exemplo n.º 17
0
 /// <summary>
 /// Nodes must implement an Equals method so we can do type specific equality
 /// </summary>
 /// <param name="other">Node to check for equality</param>
 /// <returns></returns>
 /// <remarks>
 /// Nodes implementations are also required to implement an override of the non-generic Equals method.  Standard implementations of some equality comparisons can be found in <see cref="EqualityHelper">EqualityHelper</see>
 /// </remarks>
 public abstract bool Equals(IGraphLiteralNode other);
Exemplo n.º 18
0
 public bool Equals(IGraphLiteralNode other)
 {
     return(_source.Equals(other));
 }
Exemplo n.º 19
0
 public int CompareTo(IGraphLiteralNode other)
 {
     return(_source.CompareTo(other));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Returns an Integer indicating the Ordering of this Node compared to another Node
 /// </summary>
 /// <param name="other">Node to test against</param>
 /// <returns></returns>
 public override int CompareTo(IGraphLiteralNode other)
 {
     return(ComparisonHelper.CompareGraphLiterals(this, (IGraphLiteralNode)other));
 }
Exemplo n.º 21
0
 /// <summary>
 /// Compares this Node to another Graph LiteralNode
 /// </summary>
 /// <param name="other">Other Graph Literal Node</param>
 /// <returns></returns>
 /// <remarks>
 /// Unless Virtual Equality (equality based on the Virtual RDF Provider and Virtual ID) can be determined or the Nodes are of different types then the Nodes value will have to be materialised in order to perform comparison.
 /// </remarks>
 public virtual int CompareTo(IGraphLiteralNode other)
 {
     return(this.CompareTo((INode)other));
 }
Exemplo n.º 22
0
 /// <summary>
 /// Nodes must implement a CompareTo method to allow them to be Sorted
 /// </summary>
 /// <param name="other">Node to compare self to</param>
 /// <returns></returns>
 /// <remarks>
 /// Implementations should use the SPARQL Term Sort Order for ordering nodes (as opposed to value sort order).  Standard implementations of Node type specific comparisons can be found in <see cref="ComparisonHelper">ComparisonHelper</see>
 /// </remarks>
 public abstract int CompareTo(IGraphLiteralNode other);
Exemplo n.º 23
0
 /// <summary>
 /// Checks this Node for equality against another Graph Literal Node
 /// </summary>
 /// <param name="other">Other Graph Literal Node</param>
 /// <returns></returns>
 /// <remarks>
 /// Unless Virtual Equality (equality based on the Virtual RDF Provider and Virtual ID) can be determined or the Nodes are of different types then the Nodes value will have to be materialised in order to perform the equality check.
 /// </remarks>
 public virtual bool Equals(IGraphLiteralNode other)
 {
     return(this.TypedEquality(other));
 }
        /// <summary>
        /// Takes a <see cref="INode">INode</see> and converts it to a <see cref="IValuedNode">IValuedNode</see> if it is not already an instance that implements the interface.
        /// </summary>
        /// <param name="n">Node.</param>
        /// <returns>Valued Node.</returns>
        public static IValuedNode AsValuedNode(this INode n)
        {
            if (n == null)
            {
                return(null);
            }
            if (n is IValuedNode)
            {
                return((IValuedNode)n);
            }

            switch (n.NodeType)
            {
            case NodeType.Blank:
                IBlankNode b = (IBlankNode)n;
                return(new BlankNode(n.Graph, b.InternalID));

            case NodeType.GraphLiteral:
                IGraphLiteralNode glit = (IGraphLiteralNode)n;
                return(new GraphLiteralNode(n.Graph, glit.SubGraph));

            case NodeType.Literal:
                ILiteralNode lit = (ILiteralNode)n;
                // Decide what kind of valued node to produce based on node datatype
                if (lit.DataType != null)
                {
                    String dt = lit.DataType.AbsoluteUri;
                    switch (dt)
                    {
                    case XmlSpecsHelper.XmlSchemaDataTypeBoolean:
                        bool bVal;
                        if (Boolean.TryParse(lit.Value, out bVal))
                        {
                            return(new BooleanNode(n.Graph, bVal));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeByte:
                        // xsd:byte actually maps to SignedByte in .Net
                        sbyte sbVal;
                        if (sbyte.TryParse(lit.Value, out sbVal))
                        {
                            return(new SignedByteNode(n.Graph, sbVal, lit.Value));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeDate:
                        DateTime date;
                        if (DateTime.TryParse(lit.Value, null, DateTimeStyles.AdjustToUniversal, out date))
                        {
                            return(new DateNode(n.Graph, date, lit.Value));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeDateTime:
                        DateTime dateTime;
                        if (DateTime.TryParse(lit.Value, null, DateTimeStyles.AdjustToUniversal, out dateTime))
                        {
                            return(new DateTimeNode(n.Graph, dateTime, lit.Value));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeDayTimeDuration:
                    case XmlSpecsHelper.XmlSchemaDataTypeDuration:
                        TimeSpan timeSpan;
                        try
                        {
                            timeSpan = XmlConvert.ToTimeSpan(lit.Value);
                            return(new TimeSpanNode(n.Graph, timeSpan, lit.Value, lit.DataType));
                        }
                        catch
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeDecimal:
                        Decimal dec;
                        if (Decimal.TryParse(lit.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out dec))
                        {
                            return(new DecimalNode(n.Graph, dec, lit.Value));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeDouble:
                        Double dbl;
                        if (Double.TryParse(lit.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out dbl))
                        {
                            return(new DoubleNode(n.Graph, dbl, lit.Value));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeFloat:
                        Single flt;
                        if (Single.TryParse(lit.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out flt))
                        {
                            return(new FloatNode(n.Graph, flt, lit.Value));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeInt:
                    case XmlSpecsHelper.XmlSchemaDataTypeInteger:
                    case XmlSpecsHelper.XmlSchemaDataTypeLong:
                    case XmlSpecsHelper.XmlSchemaDataTypeShort:
                        long lng;
                        if (Int64.TryParse(lit.Value, out lng))
                        {
                            return(new LongNode(n.Graph, lng, lit.Value, lit.DataType));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeNegativeInteger:
                    case XmlSpecsHelper.XmlSchemaDataTypeNonPositiveInteger:
                        // Must be below zero
                        long neglng;
                        if (Int64.TryParse(lit.Value, out neglng) && neglng < 0)
                        {
                            return(new LongNode(n.Graph, neglng, lit.Value, lit.DataType));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeNonNegativeInteger:
                    case XmlSpecsHelper.XmlSchemaDataTypePositiveInteger:
                        // Must be above zero
                        long poslng;
                        if (Int64.TryParse(lit.Value, out poslng) && poslng >= 0)
                        {
                            return(new LongNode(n.Graph, poslng, lit.Value, lit.DataType));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeUnsignedByte:
                        // xsd:unsignedByte actually maps to Byte in .Net
                        byte byVal;
                        if (byte.TryParse(lit.Value, out byVal))
                        {
                            return(new ByteNode(n.Graph, byVal, lit.Value));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    case XmlSpecsHelper.XmlSchemaDataTypeUnsignedInt:
                    case XmlSpecsHelper.XmlSchemaDataTypeUnsignedLong:
                    case XmlSpecsHelper.XmlSchemaDataTypeUnsignedShort:
                        // Must be unsigned
                        ulong ulng;
                        if (UInt64.TryParse(lit.Value, out ulng))
                        {
                            return(new UnsignedLongNode(n.Graph, ulng, lit.Value, lit.DataType));
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }

                    default:
                        if (SparqlSpecsHelper.IntegerDataTypes.Contains(dt))
                        {
                            long l;
                            if (Int64.TryParse(lit.Value, out l))
                            {
                                return(new LongNode(n.Graph, l, lit.Value, lit.DataType));
                            }
                            else
                            {
                                return(new StringNode(n.Graph, lit.Value, lit.DataType));
                            }
                        }
                        else
                        {
                            return(new StringNode(n.Graph, lit.Value, lit.DataType));
                        }
                    }
                }
                else if (!lit.Language.Equals(String.Empty))
                {
                    return(new StringNode(n.Graph, lit.Value, lit.Language));
                }
                else
                {
                    return(new StringNode(n.Graph, lit.Value));
                }

            case NodeType.Uri:
                IUriNode u = (IUriNode)n;
                return(new UriNode(n.Graph, u.Uri));

            case NodeType.Variable:
                IVariableNode v = (IVariableNode)n;
                return(new VariableNode(n.Graph, v.VariableName));

            default:
                throw new RdfQueryException("Cannot create a valued node for an unknown node type");
            }
        }
Exemplo n.º 25
0
 /// <summary>
 /// Creates a new Virtual Graph Literal Node
 /// </summary>
 /// <param name="g">Graph the Node belongs to</param>
 /// <param name="id">Virtual ID</param>
 /// <param name="provider">Virtual RDF Provider</param>
 /// <param name="value">Materialised Values</param>
 public SimpleVirtualGraphLiteralNode(IGraph g, int id, IVirtualRdfProvider <int, int> provider, IGraphLiteralNode value)
     : base(g, id, provider, value)
 {
 }
Exemplo n.º 26
0
        /// <summary>
        /// Generates Output for Nodes in Notation 3 syntax.
        /// </summary>
        /// <param name="context">Writer Context.</param>
        /// <param name="n">Node to generate output for.</param>
        /// <param name="segment">Segment of the Triple being output.</param>
        /// <param name="indent">Indent to use for pretty printing.</param>
        /// <returns></returns>
        private String GenerateNodeOutput(CompressingTurtleWriterContext context, INode n, TripleSegment segment, int indent)
        {
            StringBuilder output = new StringBuilder();

            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (context.Collections.ContainsKey(n))
                {
                    output.Append(GenerateCollectionOutput(context, context.Collections[n], indent));
                }
                else
                {
                    return(context.NodeFormatter.Format(n, segment));
                }
                break;

            case NodeType.GraphLiteral:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralPredicatesUnserializable("Notation 3"));
                }

                output.Append("{");
                IGraphLiteralNode glit = (IGraphLiteralNode)n;

                StringBuilder temp = new StringBuilder();
                CompressingTurtleWriterContext subcontext = new CompressingTurtleWriterContext(glit.SubGraph, new System.IO.StringWriter(temp));
                subcontext.NodeFormatter = context.NodeFormatter;
                bool contextWritten = false;

                // Write Triples 1 at a Time on a single line
                foreach (Triple t in subcontext.Graph.Triples)
                {
                    if (!contextWritten && t.Context != null && t.Context is VariableContext)
                    {
                        contextWritten = GenerateVariableQuantificationOutput(subcontext, (VariableContext)t.Context);
                        if (contextWritten)
                        {
                            output.Append(temp.ToString());
                        }
                    }

                    output.Append(GenerateNodeOutput(subcontext, t.Subject, TripleSegment.Subject, 0));
                    output.Append(" ");
                    output.Append(GenerateNodeOutput(subcontext, t.Predicate, TripleSegment.Predicate, 0));
                    output.Append(" ");
                    output.Append(GenerateNodeOutput(subcontext, t.Object, TripleSegment.Object, 0));
                    output.Append(". ");
                }

                output.Append("}");
                break;

            case NodeType.Literal:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Notation 3"));
                }
                return(context.NodeFormatter.Format(n, segment));

            case NodeType.Uri:
                return(context.NodeFormatter.Format(n, segment));

            case NodeType.Variable:
                return(context.NodeFormatter.Format(n, segment));

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Notation 3"));
            }

            return(output.ToString());
        }