/
BaseGraphLiteralNode.cs
432 lines (383 loc) · 14.8 KB
/
BaseGraphLiteralNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
// <copyright>
// dotNetRDF is free and open source software licensed under the MIT License
// -------------------------------------------------------------------------
//
// Copyright (c) 2009-2023 dotNetRDF Project (http://dotnetrdf.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
*/
using System;
using System.Text;
using VDS.RDF.Nodes;
using VDS.RDF.Query;
using VDS.RDF.Query.Expressions;
namespace VDS.RDF
{
/// <summary>
/// Abstract Base Class for Graph Literal Nodes.
/// </summary>
public abstract class BaseGraphLiteralNode
: BaseNode, IGraphLiteralNode, IEquatable<BaseGraphLiteralNode>, IComparable<BaseGraphLiteralNode>, IValuedNode
{
private readonly int _hashCode;
/// <summary>
/// Creates a new graph literal node which represents a new empty sub-graph.
/// </summary>
protected internal BaseGraphLiteralNode() : this(new Graph())
{
}
/// <summary>
/// Creates a new Graph Literal Node which represents the given sub-graph.
/// </summary>
/// <param name="subGraph">Sub-graph this node represents.</param>
protected internal BaseGraphLiteralNode(IGraph subGraph)
: base(NodeType.GraphLiteral)
{
SubGraph = subGraph;
// Compute Hash Code
// _hashCode = (_nodeType + ToString()).GetHashCode(); -- Not a good hashcode as can be different for isomorphic graphs
_hashCode = 0; // TODO: Create a hash function for graphs?
}
/// <summary>
/// Gets the sub-graph that this Node represents.
/// </summary>
public IGraph SubGraph { get; }
/// <summary>Serves as the default hash function.</summary>
/// <returns>A hash code for the current object.</returns>
public override int GetHashCode()
{
return _hashCode;
}
/// <summary>
/// Implementation of the Equals method for Graph Literal Nodes. Graph Literals are considered Equal if their respective sub-graphs are equal.
/// </summary>
/// <param name="obj">Object to compare the Node with.</param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj is INode node)
{
return Equals(node);
}
// Can only be equal to other Nodes
return false;
}
/// <summary>
/// Implementation of the Equals method for Graph Literal Nodes. Graph Literals are considered Equal if their respective sub-graphs are equal.
/// </summary>
/// <param name="other">Object to compare the Node with.</param>
/// <returns></returns>
public override bool Equals(INode other)
{
if (other == null) return false;
if (ReferenceEquals(this, other)) return true;
if (other.NodeType == NodeType.GraphLiteral)
{
return EqualityHelper.AreGraphLiteralsEqual(this, (IGraphLiteralNode)other);
}
// Can only be equal to a Graph Literal Node
return false;
}
/// <summary>
/// Determines whether this Node is equal to a RefNode (should always be false).
/// </summary>
/// <param name="other">Ref Node (a blank node or URI node).</param>
/// <returns></returns>
public override bool Equals(IRefNode other)
{
return false;
}
/// <summary>
/// Determines whether this Node is equal to a Blank Node (should always be false).
/// </summary>
/// <param name="other">Blank Node.</param>
/// <returns></returns>
public override bool Equals(IBlankNode other)
{
return false;
}
/// <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)
{
return ReferenceEquals(this, other) || EqualityHelper.AreGraphLiteralsEqual(this, other);
}
/// <summary>
/// Determines whether this Node is equal to a Literal Node (should always be false).
/// </summary>
/// <param name="other">Literal Node.</param>
/// <returns></returns>
public override bool Equals(ILiteralNode other)
{
return false;
}
/// <summary>
/// Determines whether this Node is equal to a URI Node (should always be false).
/// </summary>
/// <param name="other">URI Node.</param>
/// <returns></returns>
public override bool Equals(IUriNode other)
{
return false;
}
/// <summary>
/// Determines whether this Node is equal to a Variable Node (should always be false).
/// </summary>
/// <param name="other">Variable Node.</param>
/// <returns></returns>
public override bool Equals(IVariableNode other)
{
return false;
}
/// <summary>
/// Determines whether this Node is equal to a Triple Node (should always be false).
/// </summary>
/// <param name="other">Triple Node.</param>
/// <returns></returns>
public override bool Equals(ITripleNode other)
{
return false;
}
/// <summary>
/// Determines whether this Node is equal to a Graph Literal Node.
/// </summary>
/// <param name="other">Graph Literal Node.</param>
/// <returns></returns>
public bool Equals(BaseGraphLiteralNode other)
{
return Equals((IGraphLiteralNode)other);
}
/// <summary>
/// Implementation of ToString for Graph Literals which produces a String representation of the sub-graph in N3 style syntax.
/// </summary>
/// <returns></returns>
public override string ToString()
{
var output = new StringBuilder();
// Use N3 Style notation for Graph Literal string representation
output.Append("{");
// Add all the Triples in the sub-graph
foreach (Triple t in SubGraph.Triples)
{
output.Append(t);
}
output.Append("}");
return output.ToString();
}
/// <summary>
/// Implementation of CompareTo for Graph Literals.
/// </summary>
/// <param name="other">Node to compare to.</param>
/// <returns></returns>
/// <remarks>
/// Graph Literal Nodes are greater than Blank Nodes, Uri Nodes, Literal Nodes and Nulls.
/// </remarks>
public override int CompareTo(INode other)
{
if (ReferenceEquals(this, other)) return 0;
return other?.NodeType switch
{
// Graph literal nodes are less than triple nodes
NodeType.Triple => -1,
NodeType.GraphLiteral => ComparisonHelper.CompareGraphLiterals(this, other as IGraphLiteralNode),
// Graph literal nodes are greater than every other type of node (and nulls)
_ => 1
};
}
/// <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(IRefNode other)
{
// We are always greater than everything but Triple Nodes.
if (other == null) return 1;
return other.NodeType == NodeType.Triple ? -1 : 1;
}
/// <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(IBlankNode other)
{
// We are always greater than everything but Triple Nodes.
return 1;
}
/// <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, other);
}
/// <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(ILiteralNode other)
{
// We are always greater than everything but Triple Nodes.
return 1;
}
/// <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(IUriNode other)
{
// We are always greater than everything but Triple Nodes.
return 1;
}
/// <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(IVariableNode other)
{
// We are always greater than everything but Triple Nodes.
return 1;
}
/// <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(ITripleNode other)
{
// We are greater than a Null but less than a Triple Node.
return other == null ? 1 : -1;
}
/// <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 int CompareTo(BaseGraphLiteralNode other)
{
return CompareTo((IGraphLiteralNode)other);
}
#region IValuedNode Members
/// <summary>
/// Throws an error as Graph Literals cannot be cast to a string.
/// </summary>
/// <returns></returns>
public string AsString()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Throws an error as Graph Literals cannot be cast to an integer.
/// </summary>
/// <returns></returns>
public long AsInteger()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Throws an error as Graph Literals cannot be cast to a decimal.
/// </summary>
/// <returns></returns>
public decimal AsDecimal()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Throws an error as Graph Literals cannot be cast to a float.
/// </summary>
/// <returns></returns>
public float AsFloat()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Throws an error as Graph Literals cannot be cast to a double.
/// </summary>
/// <returns></returns>
public double AsDouble()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Throws an error as Graph Literals cannot be cast to a boolean.
/// </summary>
/// <returns></returns>
public bool AsBoolean()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Throws an error as Graph Literals cannot be cast to a date time.
/// </summary>
/// <returns></returns>
public DateTime AsDateTime()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Throws an error as Graph Literals cannot be cast to a date time.
/// </summary>
/// <returns></returns>
public DateTimeOffset AsDateTimeOffset()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Throws an error as Graph Literals cannot be cast to a time span.
/// </summary>
/// <returns></returns>
public TimeSpan AsTimeSpan()
{
throw new RdfQueryException("Unable to cast a Graph Literal Node to a type");
}
/// <summary>
/// Gets the URI of the datatype this valued node represents as a String.
/// </summary>
public string EffectiveType
{
get
{
return string.Empty;
}
}
/// <summary>
/// Gets the numeric type of the node.
/// </summary>
public SparqlNumericType NumericType
{
get
{
return SparqlNumericType.NaN;
}
}
#endregion
}
}