/
BaseGraph.cs
1001 lines (852 loc) · 37.5 KB
/
BaseGraph.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
// <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.Collections.Generic;
using System.Linq;
namespace VDS.RDF
{
/// <summary>
/// Abstract Base Implementation of the <see cref="IGraph">IGraph</see> interface.
/// </summary>
public abstract class BaseGraph : IGraph
{
#region Variables
/// <summary>
/// Collection of Triples in the Graph.
/// </summary>
protected BaseTripleCollection _triples;
/// <summary>
/// The factory to use when creating nodes in this graph.
/// </summary>
protected readonly INodeFactory NodeFactory;
/// <summary>
/// The factory to use when creating URIs in this graph.
/// </summary>
/// <remarks>This property delegates to the <see cref="INodeFactory.UriFactory"/> property of <see cref="NodeFactory"/>.</remarks>
public IUriFactory UriFactory
{
get => NodeFactory.UriFactory;
set => NodeFactory.UriFactory = value;
}
/// <summary>
/// The name of the graph.
/// </summary>
protected readonly IRefNode _name;
/// <summary>
/// Blank Node ID Mapper.
/// </summary>
protected BlankNodeMapper _bnodemapper;
private readonly TripleEventHandler TripleAddedHandler;
private readonly TripleEventHandler TripleRemovedHandler;
#endregion
#region Constructor
/// <summary>
/// Creates a new Base Graph using the given Triple Collection.
/// </summary>
/// <param name="tripleCollection">Triple Collection to use. If null, a new <see cref="TreeIndexedTripleCollection"/> will be used.</param>
/// <param name="graphName">The name to assign to the graph.</param>
/// <param name="nodeFactory">The factory to use when creating nodes in this graph.</param>
/// <param name="uriFactory">The factory to use when creating URIs in this graph. If not specified or null, defaults to the <see cref="INodeFactory.UriFactory"/> property of <paramref name="nodeFactory"/> or else <see cref="VDS.RDF.UriFactory.Root">root UriFactory</see> if <paramref name="nodeFactory"/> is null.</param>
protected BaseGraph(BaseTripleCollection tripleCollection, IRefNode graphName = null, INodeFactory nodeFactory = null, IUriFactory uriFactory = null)
{
_triples = tripleCollection ?? new TreeIndexedTripleCollection();
_bnodemapper = new BlankNodeMapper();
NodeFactory = nodeFactory ?? new NodeFactory(new NodeFactoryOptions(), uriFactory:uriFactory);
UriFactory = uriFactory ?? NodeFactory.UriFactory;
_name = graphName;
// Create Event Handlers and attach to the Triple Collection
TripleAddedHandler = OnTripleAsserted;
TripleRemovedHandler = OnTripleRetracted;
AttachEventHandlers(_triples);
}
/// <summary>
/// Creates a new Base Graph which uses the default <see cref="TreeIndexedTripleCollection" /> as the Triple Collection.
/// </summary>
/// <param name="graphName">The name to assign to the new graph.</param>
protected BaseGraph(IRefNode graphName = null)
: this(new TreeIndexedTripleCollection(), graphName)
{
}
#endregion
#region Properties
/// <summary>
/// Gets the set of Triples described in this Graph.
/// </summary>
public virtual BaseTripleCollection Triples => _triples;
/// <summary>
/// Gets the quoted triples in the graph.
/// </summary>
public virtual IEnumerable<Triple> QuotedTriples => _triples.SubjectNodes.OfType<ITripleNode>()
.Union(_triples.ObjectNodes.OfType<ITripleNode>()).Select(tn => tn.Triple);
/// <inheritdoc />
public virtual IEnumerable<INode> Nodes => _triples.SubjectNodes.Union(_triples.ObjectNodes);
/// <inheritdoc />
public virtual IEnumerable<INode> AllNodes
{
get { return _triples.Asserted.SelectMany(t => t.Nodes).Distinct(); }
}
/// <inheritdoc />
public virtual IEnumerable<INode> QuotedNodes => _triples.QuotedSubjectNodes.Union(_triples.QuotedObjectNodes);
/// <inheritdoc />
public virtual IEnumerable<INode> AllQuotedNodes => _triples.QuotedSubjectNodes
.Union(_triples.QuotedPredicateNodes).Union(_triples.QuotedObjectNodes);
/// <summary>
/// Gets the Namespace Mapper for this Graph which contains all in use Namespace Prefixes and their URIs.
/// </summary>
/// <returns></returns>
public INamespaceMapper NamespaceMap => NodeFactory.NamespaceMap;
/// <summary>
/// Gets the current Base Uri for the Graph.
/// </summary>
/// <remarks>
/// This value may be changed during Graph population depending on whether the Concrete syntax allows the Base Uri to be changed and how the Parser handles this.
/// </remarks>
public Uri BaseUri
{
get => NodeFactory.BaseUri;
set => NodeFactory.BaseUri = value;
}
/// <summary>
/// Get or set the name of the graph.
/// </summary>
public virtual IRefNode Name
{
get => _name;
}
/// <summary>
/// Gets whether a Graph is Empty ie. Contains No Triples or Nodes.
/// </summary>
public virtual bool IsEmpty => _triples.Count == 0;
/// <summary>
/// Get or set whether to normalize the value strings of literal nodes on creation.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
public virtual bool NormalizeLiteralValues { get; set; } = Options.LiteralValueNormalization;
#pragma warning restore CS0618 // Type or member is obsolete
/// <inheritdoc />
public LanguageTagValidationMode LanguageTagValidation
{
get => NodeFactory.LanguageTagValidation;
set => NodeFactory.LanguageTagValidation = value;
}
#endregion
#region Triple Assertion & Retraction
/// <summary>
/// Asserts a Triple in the Graph.
/// </summary>
/// <param name="t">The Triple to add to the Graph.</param>
public abstract bool Assert(Triple t);
/// <summary>
/// Asserts a List of Triples in the graph.
/// </summary>
/// <param name="ts">List of Triples in the form of an IEnumerable.</param>
public abstract bool Assert(IEnumerable<Triple> ts);
/// <summary>
/// Retracts a Triple from the Graph.
/// </summary>
/// <param name="t">Triple to Retract.</param>
/// <remarks>Current implementation may have some defunct Nodes left in the Graph as only the Triple is retracted.</remarks>
public abstract bool Retract(Triple t);
/// <summary>
/// Retracts a enumeration of Triples from the graph.
/// </summary>
/// <param name="ts">Enumeration of Triples to retract.</param>
public abstract bool Retract(IEnumerable<Triple> ts);
/// <summary>
/// Clears all Triples from the Graph.
/// </summary>
/// <remarks>
/// <para>
/// The Graph will raise the <see cref="ClearRequested">ClearRequested</see> event at the start of the Clear operation which allows for aborting the operation if the operation is cancelled by an event handler. On completing the Clear the <see cref="Cleared">Cleared</see> event will be raised.
/// </para>
/// </remarks>
public virtual void Clear()
{
if (!RaiseClearRequested()) return;
Retract(Triples.ToList());
RaiseCleared();
}
#endregion
#region NodeFactory methods
/// <summary>
/// Creates a Blank Node with a new automatically generated ID.
/// </summary>
/// <returns></returns>
public virtual IBlankNode CreateBlankNode()
{
return NodeFactory.CreateBlankNode();
}
/// <summary>
/// Creates a Blank Node with the given Node ID.
/// </summary>
/// <param name="nodeId">Node ID.</param>
/// <returns></returns>
public virtual IBlankNode CreateBlankNode(string nodeId)
{
return NodeFactory.CreateBlankNode(nodeId);
}
/// <summary>
/// Creates a Graph Literal Node which represents the empty Subgraph.
/// </summary>
/// <returns></returns>
public virtual IGraphLiteralNode CreateGraphLiteralNode()
{
return NodeFactory.CreateGraphLiteralNode();
}
/// <summary>
/// Creates a Graph Literal Node which represents the given Subgraph.
/// </summary>
/// <param name="subgraph">Subgraph.</param>
/// <returns></returns>
public virtual IGraphLiteralNode CreateGraphLiteralNode(IGraph subgraph)
{
return NodeFactory.CreateGraphLiteralNode(subgraph);
}
/// <summary>
/// Creates a Literal Node with the given Value and Data Type.
/// </summary>
/// <param name="literal">Value of the Literal.</param>
/// <param name="datatype">Data Type URI of the Literal.</param>
/// <returns></returns>
public virtual ILiteralNode CreateLiteralNode(string literal, Uri datatype)
{
return NodeFactory.CreateLiteralNode(literal, datatype);
}
/// <summary>
/// Creates a Literal Node with the given Value.
/// </summary>
/// <param name="literal">Value of the Literal.</param>
/// <returns></returns>
public virtual ILiteralNode CreateLiteralNode(string literal)
{
return NodeFactory.CreateLiteralNode(literal);
}
/// <summary>
/// Creates a Literal Node with the given Value and Language.
/// </summary>
/// <param name="literal">Value of the Literal.</param>
/// <param name="langSpec">Language Specifier for the Literal.</param>
/// <returns></returns>
public virtual ILiteralNode CreateLiteralNode(string literal, string langSpec)
{
return NodeFactory.CreateLiteralNode(literal, langSpec);
}
/// <summary>
/// Creates a URI Node for the given URI.
/// </summary>
/// <param name="uri">URI.</param>
/// <returns></returns>
public virtual IUriNode CreateUriNode(Uri uri)
{
return NodeFactory.CreateUriNode(uri);
}
/// <summary>
/// Creates a URI Node for the given QName using the Graphs NamespaceMap to resolve the QName.
/// </summary>
/// <param name="qName">QName.</param>
/// <returns></returns>
public virtual IUriNode CreateUriNode(string qName)
{
return NodeFactory.CreateUriNode(qName);
}
/// <summary>
/// Creates a URI Node that corresponds to the current Base URI of the node factory.
/// </summary>
/// <returns></returns>
public virtual IUriNode CreateUriNode()
{
return NodeFactory.CreateUriNode();
}
/// <summary>
/// Creates a Variable Node for the given Variable Name.
/// </summary>
/// <param name="varName"></param>
/// <returns></returns>
public virtual IVariableNode CreateVariableNode(string varName)
{
return NodeFactory.CreateVariableNode(varName);
}
/// <summary>
/// Creates a node that quotes the given triple.
/// </summary>
/// <param name="triple">The triple to be the quoted value of the created node.</param>
/// <returns></returns>
public virtual ITripleNode CreateTripleNode(Triple triple)
{
return NodeFactory.CreateTripleNode(triple);
}
#endregion
#region Node Selection
/// <summary>
/// Returns the Blank Node with the given Identifier.
/// </summary>
/// <param name="nodeId">The Identifier of the Blank Node to select.</param>
/// <returns>Either the Blank Node or null if no Node with the given Identifier exists.</returns>
public abstract IBlankNode GetBlankNode(string nodeId);
/// <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 abstract ILiteralNode GetLiteralNode(string literal, string langspec);
/// <summary>
/// Returns the LiteralNode with the given Value if it exists.
/// </summary>
/// <param name="literal">The literal value of the Node to select.</param>
/// <returns>Either the LiteralNode Or null if no Node with the given Value exists.</returns>
/// <remarks>The LiteralNode in the Graph must have no Language or DataType set.</remarks>
public abstract ILiteralNode GetLiteralNode(string literal);
/// <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 abstract ILiteralNode GetLiteralNode(string literal, Uri datatype);
/// <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 abstract IUriNode GetUriNode(string qname);
/// <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 abstract IUriNode GetUriNode(Uri uri);
/// <summary>
/// Selects the Triple Node with the given Triple value if it exists in the graph.
/// </summary>
/// <param name="triple">Triple.</param>
/// <returns>The triple node if it exists in the graph or else null.</returns>
public abstract ITripleNode GetTripleNode(Triple triple);
#endregion
#region Triple Selection
/// <summary>
/// Gets all the Triples involving the given Uri.
/// </summary>
/// <param name="uri">The Uri to find Triples involving.</param>
/// <returns>Zero/More Triples.</returns>
public abstract IEnumerable<Triple> GetTriples(Uri uri);
/// <summary>
/// Gets all the Triples involving the given Node.
/// </summary>
/// <param name="n">The Node to find Triples involving.</param>
/// <returns>Zero/More Triples.</returns>
public abstract IEnumerable<Triple> GetTriples(INode n);
/// <summary>
/// Gets all the Triples with the given Uri as the Object.
/// </summary>
/// <param name="u">The Uri to find Triples with it as the Object.</param>
/// <returns>Zero/More Triples.</returns>
public abstract IEnumerable<Triple> GetTriplesWithObject(Uri u);
/// <summary>
/// Gets all the Triples with the given Node as the Object.
/// </summary>
/// <param name="n">The Node to find Triples with it as the Object.</param>
/// <returns></returns>
public abstract IEnumerable<Triple> GetTriplesWithObject(INode n);
/// <summary>
/// Gets all the Triples with the given Node as the Predicate.
/// </summary>
/// <param name="n">The Node to find Triples with it as the Predicate.</param>
/// <returns></returns>
public abstract IEnumerable<Triple> GetTriplesWithPredicate(INode n);
/// <summary>
/// Gets all the Triples with the given Uri as the Predicate.
/// </summary>
/// <param name="u">The Uri to find Triples with it as the Predicate.</param>
/// <returns>Zero/More Triples.</returns>
public abstract IEnumerable<Triple> GetTriplesWithPredicate(Uri u);
/// <summary>
/// Gets all the Triples with the given Node as the Subject.
/// </summary>
/// <param name="n">The Node to find Triples with it as the Subject.</param>
/// <returns>Zero/More Triples.</returns>
public abstract IEnumerable<Triple> GetTriplesWithSubject(INode n);
/// <summary>
/// Gets all the Triples with the given Uri as the Subject.
/// </summary>
/// <param name="u">The Uri to find Triples with it as the Subject.</param>
/// <returns>Zero/More Triples.</returns>
public abstract IEnumerable<Triple> GetTriplesWithSubject(Uri u);
/// <summary>
/// Selects all Triples with the given Subject and Predicate.
/// </summary>
/// <param name="subj">Subject.</param>
/// <param name="pred">Predicate.</param>
/// <returns></returns>
public abstract IEnumerable<Triple> GetTriplesWithSubjectPredicate(INode subj, INode pred);
/// <summary>
/// Selects all Triples with the given Subject and Object.
/// </summary>
/// <param name="subj">Subject.</param>
/// <param name="obj">Object.</param>
/// <returns></returns>
public abstract IEnumerable<Triple> GetTriplesWithSubjectObject(INode subj, INode obj);
/// <summary>
/// Selects all Triples with the given Predicate and Object.
/// </summary>
/// <param name="pred">Predicate.</param>
/// <param name="obj">Object.</param>
/// <returns></returns>
public abstract IEnumerable<Triple> GetTriplesWithPredicateObject(INode pred, INode obj);
/// <inheritdoc/>
public virtual bool ContainsTriple(Triple t)
{
return _triples.Contains(t);
}
/// <summary>
/// Gets whether a given triple is quoted in this graph.
/// </summary>
/// <param name="t">Triple to test.</param>
/// <returns>True if the triple is quoted in this graph, false otherwise.</returns>
public virtual bool ContainsQuotedTriple(Triple t)
{
return _triples.ContainsQuoted(t);
}
/// <inheritdoc/>
public virtual IEnumerable<Triple> GetQuoted(Uri uri) => GetQuoted(new UriNode(uri));
/// <inheritdoc/>
public abstract IEnumerable<Triple> GetQuoted(INode n);
/// <inheritdoc/>
public virtual IEnumerable<Triple> GetQuotedWithObject(Uri u) => GetQuotedWithObject(new UriNode(u));
/// <inheritdoc/>
public abstract IEnumerable<Triple> GetQuotedWithObject(INode n);
/// <inheritdoc/>
public abstract IEnumerable<Triple> GetQuotedWithPredicate(INode n);
/// <inheritdoc/>
public virtual IEnumerable<Triple> GetQuotedWithPredicate(Uri u) => GetQuotedWithPredicate(new UriNode(u));
/// <inheritdoc/>
public abstract IEnumerable<Triple> GetQuotedWithSubject(INode n);
/// <inheritdoc/>
public virtual IEnumerable<Triple> GetQuotedWithSubject(Uri u) => GetQuotedWithSubject(new UriNode(u));
/// <inheritdoc/>
public abstract IEnumerable<Triple> GetQuotedWithSubjectPredicate(INode subj, INode pred);
/// <inheritdoc/>
public abstract IEnumerable<Triple> GetQuotedWithSubjectObject(INode subj, INode obj);
/// <inheritdoc/>
public abstract IEnumerable<Triple> GetQuotedWithPredicateObject(INode pred, INode obj);
#endregion
#region Graph Merging
/// <summary>
/// Merges another Graph into the current Graph.
/// </summary>
/// <param name="g">Graph to Merge into this Graph.</param>
/// <remarks>The Graph on which you invoke this method will preserve its Blank Node IDs while the Blank Nodes from the Graph being merged in will be given new IDs as required in the scope of this Graph.</remarks>
public virtual void Merge(IGraph g)
{
Merge(g, false);
}
/// <summary>
/// Merges another Graph into the current Graph.
/// </summary>
/// <param name="g">Graph to Merge into this Graph.</param>
/// <param name="keepOriginalGraphUri">Indicates that the Merge should preserve the Graph URIs of Nodes so they refer to the Graph they originated in.</param>
/// <remarks>
/// <para>
/// The Graph on which you invoke this method will preserve its Blank Node IDs while the Blank Nodes from the Graph being merged in will be given new IDs as required in the scope of this Graph.
/// </para>
/// <para>
/// The Graph will raise the <see cref="MergeRequested">MergeRequested</see> event before the Merge operation which gives any event handlers the oppurtunity to cancel this event. When the Merge operation is completed the <see cref="Merged">Merged</see> event is raised.
/// </para>
/// </remarks>
public virtual void Merge(IGraph g, bool keepOriginalGraphUri)
{
if (ReferenceEquals(this, g)) throw new RdfException("You cannot Merge an RDF Graph with itself");
// Check that the merge can go ahead
if (!RaiseMergeRequested()) return;
// First copy and Prefixes across which aren't defined in this Graph
NodeFactory.NamespaceMap.Import(g.NamespaceMap);
if (IsEmpty)
{
// Empty Graph so do a quick copy
Assert(g.Triples);
}
else
{
// Prepare a mapping of Blank Nodes to Blank Nodes
var mapping = new Dictionary<INode, IBlankNode>();
foreach (Triple t in g.Triples)
{
INode s = MapBlankNode(t.Subject, mapping);
INode p = MapBlankNode(t.Predicate, mapping);
INode o = MapBlankNode(t.Object, mapping);
Assert(new Triple(s, p, o, t.Context));
}
}
RaiseMerged();
}
private INode MapBlankNode(INode node, IDictionary<INode, IBlankNode> mapping)
{
switch (node.NodeType)
{
case NodeType.Triple:
var tn = node as ITripleNode;
return tn == null || tn.Triple.IsGroundTriple
? tn
: new TripleNode(new Triple(
MapBlankNode(tn.Triple.Subject, mapping),
MapBlankNode(tn.Triple.Predicate, mapping),
MapBlankNode(tn.Triple.Object, mapping)));
case NodeType.Blank:
if (mapping.TryGetValue(node, out IBlankNode mapped)) return mapped;
IBlankNode tmp = CreateBlankNode();
mapping.Add(node, tmp);
return tmp;
default:
return node;
}
}
/// <inheritdoc />
public void Unstar()
{
RdfStarHelper.Unstar(this);
}
#endregion
#region Graph Equality
/// <summary>
/// Determines whether a Graph is equal to another Object.
/// </summary>
/// <param name="other">Other graph to compare to.</param>
/// <returns></returns>
/// <remarks>
/// <para>
/// Graph Equality is determined by a somewhat complex algorithm which is explained in the remarks of the other overload for Equals.
/// </para>
/// </remarks>
public bool Equals(IGraph other)
{
return Equals(other, out _);
}
/// <summary>
/// Determines whether this Graph is equal to the given Graph.
/// </summary>
/// <param name="g">Graph to test for equality.</param>
/// <param name="mapping">Mapping of Blank Nodes iff the Graphs are equal and contain some Blank Nodes.</param>
/// <returns></returns>
/// <remarks>
/// See <see cref="GraphMatcher"/> for documentation of the equality algorithm used.
/// </remarks>
public virtual bool Equals(IGraph g, out Dictionary<INode, INode> mapping)
{
// Set the mapping to be null
mapping = null;
var matcher = new GraphMatcher();
if (matcher.Equals(this, g))
{
mapping = matcher.Mapping;
return true;
}
return false;
}
#endregion
#region Sub-Graph Matching
/// <summary>
/// Checks whether this Graph is a sub-graph of the given Graph.
/// </summary>
/// <param name="g">Graph.</param>
/// <returns></returns>
public bool IsSubGraphOf(IGraph g)
{
return IsSubGraphOf(g, out Dictionary<INode, INode> _);
}
/// <summary>
/// Checks whether this Graph is a sub-graph of the given Graph.
/// </summary>
/// <param name="g">Graph.</param>
/// <param name="mapping">Mapping of Blank Nodes.</param>
/// <returns></returns>
public bool IsSubGraphOf(IGraph g, out Dictionary<INode, INode> mapping)
{
// Set the mapping to be null
mapping = null;
var matcher = new SubGraphMatcher();
if (matcher.IsSubGraph(this, g))
{
mapping = matcher.Mapping;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Checks whether this Graph has the given Graph as a sub-graph.
/// </summary>
/// <param name="g">Graph.</param>
/// <returns></returns>
public bool HasSubGraph(IGraph g)
{
return g.IsSubGraphOf(this);
}
/// <summary>
/// Checks whether this Graph has the given Graph as a sub-graph.
/// </summary>
/// <param name="g">Graph.</param>
/// <param name="mapping">Mapping of Blank Nodes.</param>
/// <returns></returns>
public bool HasSubGraph(IGraph g, out Dictionary<INode, INode> mapping)
{
return g.IsSubGraphOf(this, out mapping);
}
#endregion
#region Graph Difference
/// <summary>
/// Computes the Difference between this Graph the given Graph.
/// </summary>
/// <param name="g">Graph.</param>
/// <returns></returns>
/// <remarks>
/// <para>
/// Produces a report which shows the changes that must be made to this Graph to produce the given Graph.
/// </para>
/// </remarks>
public GraphDiffReport Difference(IGraph g)
{
var differ = new GraphDiff();
return differ.Difference(this, g);
}
#endregion
#region Helper Functions
/// <summary>
/// Helper function for Resolving QNames to URIs.
/// </summary>
/// <param name="qname">QName to resolve to a Uri.</param>
/// <returns></returns>
public virtual Uri ResolveQName(string qname)
{
return NodeFactory.ResolveQName(qname);
}
/// <summary>
/// Creates a new unused Blank Node ID and returns it.
/// </summary>
/// <returns></returns>
public virtual string GetNextBlankNodeID()
{
return _bnodemapper.GetNextID();
}
#endregion
#region Events
/// <summary>
/// Event which is raised when a Triple is asserted in the Graph
/// </summary>
public event TripleEventHandler TripleAsserted;
/// <summary>
/// Event which is raised when a Triple is retracted from the Graph
/// </summary>
public event TripleEventHandler TripleRetracted;
/// <summary>
/// Event which is raised when the Graph contents change
/// </summary>
public event GraphEventHandler Changed;
/// <summary>
/// Event which is raised just before the Graph is cleared of its contents
/// </summary>
public event CancellableGraphEventHandler ClearRequested;
/// <summary>
/// Event which is raised after the Graph is cleared of its contents
/// </summary>
public event GraphEventHandler Cleared;
/// <summary>
/// Event which is raised when a Merge operation is requested on the Graph
/// </summary>
public event CancellableGraphEventHandler MergeRequested;
/// <summary>
/// Event which is raised when a Merge operation is completed on the Graph
/// </summary>
public event GraphEventHandler Merged;
/// <summary>
/// Event Handler which handles the <see cref="BaseTripleCollection.TripleAdded">Triple Added</see> event from the underlying Triple Collection by raising the Graph's <see cref="TripleAsserted">TripleAsserted</see> event.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="args">Triple Event Arguments.</param>
protected virtual void OnTripleAsserted(object sender, TripleEventArgs args)
{
RaiseTripleAsserted(args);
}
/// <summary>
/// Helper method for raising the <see cref="TripleAsserted">Triple Asserted</see> event manually.
/// </summary>
/// <param name="args">Triple Event Arguments.</param>
protected void RaiseTripleAsserted(TripleEventArgs args)
{
args.Graph = this;
TripleAsserted?.Invoke(this, args);
RaiseGraphChanged(args);
}
/// <summary>
/// Helper method for raising the <see cref="TripleAsserted">Triple Asserted</see> event manually.
/// </summary>
/// <param name="t">Triple.</param>
protected void RaiseTripleAsserted(Triple t)
{
TripleEventHandler d = TripleAsserted;
GraphEventHandler e = Changed;
if (d != null || e != null)
{
var args = new TripleEventArgs(t, this);
d?.Invoke(this, args);
e?.Invoke(this, new GraphEventArgs(this, args));
}
}
/// <summary>
/// Event Handler which handles the <see cref="BaseTripleCollection.TripleRemoved">Triple Removed</see> event from the underlying Triple Collection by raising the Graph's <see cref="TripleRetracted">Triple Retracted</see> event.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="args">Triple Event Arguments.</param>
protected virtual void OnTripleRetracted(object sender, TripleEventArgs args)
{
RaiseTripleRetracted(args);
}
/// <summary>
/// Helper method for raising the <see cref="TripleRetracted">Triple Retracted</see> event manually.
/// </summary>
/// <param name="args"></param>
protected void RaiseTripleRetracted(TripleEventArgs args)
{
TripleEventHandler d = TripleRetracted;
args.Graph = this;
d?.Invoke(this, args);
RaiseGraphChanged(args);
}
/// <summary>
/// Helper method for raising the <see cref="TripleRetracted">Triple Retracted</see> event manually.
/// </summary>
/// <param name="t">Triple.</param>
protected void RaiseTripleRetracted(Triple t)
{
TripleEventHandler d = TripleRetracted;
GraphEventHandler e = Changed;
if (d != null || e != null)
{
var args = new TripleEventArgs(t, this, false);
d?.Invoke(this, args);
e?.Invoke(this, new GraphEventArgs(this, args));
}
}
/// <summary>
/// Helper method for raising the <see cref="Changed">Changed</see> event.
/// </summary>
/// <param name="args">Triple Event Arguments.</param>
protected void RaiseGraphChanged(TripleEventArgs args)
{
Changed?.Invoke(this, new GraphEventArgs(this, args));
}
/// <summary>
/// Helper method for raising the <see cref="Changed">Changed</see> event.
/// </summary>
protected void RaiseGraphChanged()
{
Changed?.Invoke(this, new GraphEventArgs(this));
}
/// <summary>
/// Helper method for raising the <see cref="ClearRequested">Clear Requested</see> event and returning whether any of the Event Handlers cancelled the operation.
/// </summary>
/// <returns>True if the operation can continue, false if it should be aborted.</returns>
protected bool RaiseClearRequested()
{
CancellableGraphEventHandler d = ClearRequested;
if (d != null)
{
var args = new CancellableGraphEventArgs(this);
d(this, args);
return !args.Cancel;
}
else
{
return true;
}
}
/// <summary>
/// Helper method for raising the <see cref="Cleared">Cleared</see> event.
/// </summary>
protected void RaiseCleared()
{
Cleared?.Invoke(this, new GraphEventArgs(this));
}
/// <summary>
/// Helper method for raising the <see cref="MergeRequested">Merge Requested</see> event and returning whether any of the Event Handlers cancelled the operation.
/// </summary>
/// <returns>True if the operation can continue, false if it should be aborted.</returns>
protected bool RaiseMergeRequested()
{
CancellableGraphEventHandler d = MergeRequested;
if (d != null)
{
var args = new CancellableGraphEventArgs(this);
d(this, args);
return !args.Cancel;
}
else
{
return true;
}
}
/// <summary>
/// Helper method for raising the <see cref="Merged">Merged</see> event.
/// </summary>
protected void RaiseMerged()
{
Merged?.Invoke(this, new GraphEventArgs(this));
}
/// <summary>
/// Helper method for attaching the necessary event Handlers to a Triple Collection.
/// </summary>
/// <param name="tripleCollection">Triple Collection.</param>
/// <remarks>
/// May be useful if you replace the Triple Collection after instantiation e.g. as done in <see cref="Query.SparqlView">SparqlView</see>'s.
/// </remarks>
protected void AttachEventHandlers(BaseTripleCollection tripleCollection)
{
tripleCollection.TripleAdded += TripleAddedHandler;
tripleCollection.TripleRemoved += TripleRemovedHandler;
}
/// <summary>
/// Helper method for detaching the necessary event Handlers from a Triple Collection.
/// </summary>
/// <param name="tripleCollection">Triple Collection.</param>
/// <remarks>
/// May be useful if you replace the Triple Collection after instantiation e.g. as done in <see cref="Query.SparqlView">SparqlView</see>'s.
/// </remarks>
protected void DetachEventHandlers(BaseTripleCollection tripleCollection)
{
tripleCollection.TripleAdded -= TripleAddedHandler;
tripleCollection.TripleRemoved -= TripleRemovedHandler;
}
#endregion
/// <summary>
/// Disposes of a Graph.
/// </summary>
public virtual void Dispose()
{
DetachEventHandlers(_triples);
}
}