/
Graph.cs
1673 lines (1571 loc) · 59.6 KB
/
Graph.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VelocityDb;
using VelocityDb.Collection.BTree;
using VelocityDb.Session;
using ElementId = System.Int32;
using VertexId = System.Int32;
using EdgeId = System.Int32;
using PropertyTypeId = System.Int32;
using PropertyId = System.Int32;
using VertexTypeId = System.Int32;
using TypeId = System.Int32;
using EdgeTypeId = System.Int32;
using EdgeIdVertexId = System.UInt64;
using VelocityGraph.Frontenac.Blueprints;
using VelocityGraph.Frontenac.Blueprints.Util;
using VelocityGraph.Frontenac.Blueprints.Util.IO.GraphSON;
using System.Globalization;
using VelocityDb.Collection;
using System.IO;
using System.Diagnostics.Contracts;
//using VelocityGraph.Frontenac.Blueprints.Util.IO.GraphJson; Until VelocityGraph.Frontenac.Blueprints is updated with extensions added by VelocityGraph
using Newtonsoft.Json; // temp Until VelocityGraph.Frontenac.Blueprints is updated with extensions added by VelocityGraph
using Newtonsoft.Json.Linq;// temp Until VelocityGraph.Frontenac.Blueprints is updated with extensions added by VelocityGraph
using Newtonsoft.Json.Serialization;// temp Until VelocityGraph.Frontenac.Blueprints is updated with extensions added by VelocityGraph
using VelocityDb.Exceptions;
using VelocityGraph.Exceptions;
namespace VelocityGraph
{
/// <summary>
/// Choose <see cref="VelocityDb.IOptimizedPersistable"/> instead of <see cref="object"/> for property types where you only will store objects of type IOptimizedPersistable as property value.
/// </summary>
public enum DataType
{
/// <summary>
/// Correspond to <see cref="bool"/>
/// </summary>
Boolean,
/// <summary>
/// Correspond to <see cref="int"/>
/// </summary>
Integer,
/// <summary>
/// Correspond to <see cref="long"/>
/// </summary>
Long,
/// <summary>
/// Correspond to <see cref="double"/>
/// </summary>
Double,
/// <summary>
/// Correspond to <see cref="Single"/>
/// </summary>
Single,
/// <summary>
/// Correspond to <see cref="System.DateTime"/>
/// </summary>
DateTime,
/// <summary>
/// Correspond to <see cref="string"/>
/// </summary>
String,
/// <summary>
/// Correspond to <see cref="object"/>
/// </summary>
Object,
/// <summary>
/// Correspond to <see cref="VelocityDb.IOptimizedPersistable"/>
/// </summary>
IOptimizedPersistable
}
/// <summary>
/// A property can be index or not or index can require each entry to be unique
/// </summary>
public enum PropertyKind
{
/// <summary>
/// Add to index
/// </summary>
Indexed,
/// <summary>
/// Add to index and require value to be unique
/// </summary>
Unique,
/// <summary>
/// Don't index
/// </summary>
NotIndexed
}
/// <summary>
/// Graph is the root object of a graph. Most graph api is on this class but useful api also exist on <see cref="VertexType"/> and <see cref="EdgeType"/>.
/// </summary>
[Serializable]
public partial class Graph : OptimizedPersistable, IGraph
{
enum GraphFlags { VertexIdSetPerType = 1 };
UInt32 m_flags;
BTreeMap<string, VertexType> m_stringToVertexType;
WeakIOptimizedPersistableReference<WeakReferenceList<VertexType>> m_vertexTypes;
BTreeMap<string, EdgeType> m_stringToEdgeType;
WeakIOptimizedPersistableReference<WeakReferenceList<EdgeType>> m_edgeTypes;
WeakIOptimizedPersistableReference<WeakReferenceList<PropertyType>> m_propertyTypes;
WeakIOptimizedPersistableReference<VelocityDbList<Range<VertexId>>> m_vertices;
internal BTreeMap<VertexId, VertexTypeId> m_vertexIdToVertexType;
[NonSerialized]
SessionBase m_session;
static readonly Features s_features = new Features();
static Graph()
{
s_features.SupportsDuplicateEdges = true;
s_features.SupportsSelfLoops = true;
s_features.SupportsSerializableObjectProperty = true;
s_features.SupportsBooleanProperty = true;
s_features.SupportsDoubleProperty = true;
s_features.SupportsFloatProperty = true;
s_features.SupportsIntegerProperty = true;
s_features.SupportsPrimitiveArrayProperty = true;
s_features.SupportsUniformListProperty = true;
s_features.SupportsMixedListProperty = true;
s_features.SupportsLongProperty = true;
s_features.SupportsMapProperty = true;
s_features.SupportsStringProperty = true;
s_features.IgnoresSuppliedIds = false;
s_features.IsPersistent = true;
s_features.IsRdfModel = false;
s_features.IsWrapper = false;
s_features.SupportsIndices = false;
s_features.SupportsKeyIndices = false;
s_features.SupportsVertexKeyIndex = false;
s_features.SupportsEdgeKeyIndex = false;
s_features.SupportsVertexIndex = false;
s_features.SupportsEdgeIndex = false;
s_features.SupportsTransactions = true;
s_features.SupportsVertexIteration = true;
s_features.SupportsEdgeIteration = true;
s_features.SupportsEdgeRetrieval = true;
s_features.SupportsVertexProperties = true;
s_features.SupportsEdgeProperties = true;
s_features.SupportsThreadedTransactions = true;
}
/// <summary>
/// Creates a new <see cref="Graph"/>
/// </summary>
/// <param name="session">The active session</param>
/// <param name="vertexIdSetPerVertexType">Set to <see langword="false"/> if you want graph wide unique <see cref="Vertex"/> ids, by default <see langword="true"/> each <see cref="VertexType"/> maintains its own set of <see cref="Vertex"/> ids</param>
public Graph(SessionBase session, bool vertexIdSetPerVertexType = true)
{
session.Persist(this);
m_session = session;
m_flags = 0;
m_stringToVertexType = new BTreeMap<string, VertexType>(null, session);
var vertexTypes = new WeakReferenceList<VertexType>();
session.Persist(vertexTypes);
m_vertexTypes = new WeakIOptimizedPersistableReference<WeakReferenceList<VertexType>>(vertexTypes);
m_stringToEdgeType = new BTreeMap<string, EdgeType>(null, session);
if (vertexIdSetPerVertexType)
m_flags += (UInt32) GraphFlags.VertexIdSetPerType;
else
{
m_vertexIdToVertexType = new BTreeMap<EdgeTypeId, EdgeTypeId>(null, session);
var vertices = new VelocityDbList<Range<VertexId>>();
session.Persist(vertices);
m_vertices = new WeakIOptimizedPersistableReference<VelocityDbList<Range<VertexId>>>(vertices);
}
var edgeTypes = new WeakReferenceList<EdgeType>();
session.Persist(edgeTypes);
m_edgeTypes = new WeakIOptimizedPersistableReference<WeakReferenceList<EdgeType>>(edgeTypes);
var propertyTypes = new WeakReferenceList<PropertyType>();
session.Persist(propertyTypes);
m_propertyTypes = new WeakIOptimizedPersistableReference<WeakReferenceList<PropertyType>>(propertyTypes);
NewVertexType("default");
NewEdgeType("default", true); // not sure if we need "directed" or not as edge type parameter ???
}
internal VertexId AllocateVertexId(VertexId vId = 0, VelocityDbList<Range<VertexId>> vertecis = null)
{
if (vertecis == null)
{
if (this.m_vertices == null)
return vId;
vertecis = Vertices;
}
Range<VertexId> range;
if (vId != 0)
{
range = new Range<VertexId>(vId, vId);
if (vertecis.Count == 0)
vertecis.Add(range);
else
{
bool isEqual;
int pos = vertecis.BinarySearch(range, out isEqual);
int originalPos = pos;
if (isEqual)
throw new VertexAllreadyExistException($"Vertex with id {vId} already exist");
Range<VertexId> existingRange = vertecis[pos == vertecis.Count ? pos - 1 : pos];
if (existingRange.Min == 0 && existingRange.Max == 0 || (pos > 0 && existingRange.Min > vId + 1))
existingRange = vertecis[--pos];
if (existingRange.Min - 1 == vId)
{
range = new Range<VertexId>(existingRange.Min - 1, existingRange.Max);
if (pos > 0)
{
Range<VertexId> priorRange = vertecis[pos - 1];
if (priorRange.Max + 1 == range.Min)
{
range = new Range<VertexId>(priorRange.Min, range.Max);
vertecis[pos - 1] = range;
vertecis.RemoveAt(pos);
}
else
vertecis[pos] = range;
}
else
vertecis[pos] = range;
}
else if (existingRange.Max + 1 == vId)
{
range = new Range<VertexId>(existingRange.Min, existingRange.Max + 1);
if (vertecis.Count > pos)
{
Range<VertexId> nextRange = vertecis[pos + 1];
if (nextRange.Min == range.Max + 1)
{
range = new Range<VertexId>(range.Min, nextRange.Max);
vertecis.RemoveAt(pos);
vertecis[pos] = range;
}
else
vertecis[pos] = range;
}
else
vertecis[pos == vertecis.Count ? pos - 1 : pos] = range;
}
else if (vId >= existingRange.Min && vId <= existingRange.Max)
{
throw new VertexAllreadyExistException($"Vertex with id {vId} already exist");
}
else
vertecis.Insert(originalPos, range);
#if VERIFY
int i = 0;
Range<VertexId> p = default(Range<VertexId>);
foreach (Range<VertexId> r in vertecis)
{
if (i++ > 0)
{
if (p.Min >= r.Min)
throw new UnexpectedException("Wrong order");
if (p.Max == r.Min + 1)
throw new UnexpectedException("Failed to merge");
}
p = r;
}
#endif
}
}
else
{
vId = 1;
switch (vertecis.Count)
{
case 0:
range = new Range<VertexId>(1, 1);
vertecis.Add(range);
break;
case 1:
range = vertecis.First();
if (range.Min == 1)
{
vId = range.Max + 1;
range = new Range<VertexId>(1, vId);
}
else
{
vId = range.Min - 1;
range = new Range<VertexId>(vId, range.Max);
}
vertecis[0] = range;
break;
default:
{
range = vertecis.First();
switch (range.Min)
{
case 1:
Range<VertexId> nextRange = vertecis[1];
if (range.Max + 2 == nextRange.Min)
{
vertecis.RemoveAt(1);
vId = range.Max + 1;
range = new Range<VertexId>(range.Min, nextRange.Max);
vertecis[0] = range;
}
else
{
vId = range.Max + 1;
range = new Range<VertexId>(range.Min, vId);
vertecis[0] = range;
}
break;
default:
vId = range.Min - 1;
range = new Range<VertexId>(vId, range.Max);
vertecis[0] = range;
break;
}
}
break;
}
}
return vId;
}
/// <summary>
/// Opens a graph
/// </summary>
/// <param name="session">session to be associated with the opened graph</param>
/// <param name="graphInstance">if multiple graphs exist, choose which one to open</param>
/// <returns>A graph</returns>
public static Graph Open(SessionBase session, int graphInstance = 0)
{
UInt32 dbNum = session.DatabaseNumberOf(typeof(Graph));
Database db = session.OpenDatabase(dbNum, false, false);
if (db != null)
{
int ct = 0;
foreach (Graph g in db.AllObjects<Graph>())
{
if (ct++ == graphInstance)
return g;
}
}
return null;
}
/// <summary>
/// Add an edge to the graph. The added edges requires a recommended identifier, a tail vertex, an head vertex, and a label.
/// Like adding a vertex, the provided object identifier may be ignored by the implementation.
/// </summary>
/// <param name="id">the recommended object identifier</param>
/// <param name="outVertex">the vertex on the tail of the edge</param>
/// <param name="inVertex">the vertex on the head of the edge</param>
/// <param name="label">the label associated with the edge</param>
/// <returns>the newly created edge</returns>
public virtual IEdge AddEdge(object id, IVertex outVertex, IVertex inVertex, string label)
{
EdgeType et;
if (label != null && label.Length > 0)
{
et = FindEdgeType(label);
if (et == null)
et = NewEdgeType(label, true);
}
else if (id is UInt64)
{
UInt64 fullId = (UInt64)id;
EdgeTypeId edgeTypeId = (EdgeTypeId)(fullId >> 32);
et = EdgeTypes[edgeTypeId];
}
else
et = EdgeTypes[0];
Vertex tail = outVertex as Vertex;
Vertex head = inVertex as Vertex;
return et.NewEdge(tail, head);
}
/// <summary>
/// Create a new vertex (of VertexType "default"), add it to the graph, and return the newly created vertex.
/// </summary>
/// <returns>the newly created vertex</returns>
public virtual Vertex AddVertex()
{
VertexType vt = VertexTypes[0];
return vt.NewVertex(0);
}
/// <summary>
/// Create a new vertex (of VertexType "default"), add it to the graph, and return the newly created vertex.
/// </summary>
/// <param name="id">the recommended object identifier</param>
/// <returns>the newly created vertex</returns>
public virtual IVertex AddVertex(object id)
{
VertexType vt;
VertexId vId = 0;
if (id != null && id is UInt64)
{
UInt64 fullId = (UInt64)id;
vId = (VertexId)id;
VertexTypeId vertexTypeId = (VertexTypeId)(fullId >> 32);
vt = VertexTypes[vertexTypeId];
}
else
vt = VertexTypes[0];
return vt.NewVertex(vId);
}
/// <summary>
/// Return the edge referenced by the provided object identifier.
/// If no edge is referenced by that identifier, then return null.
/// </summary>
/// <param name="id">the identifier of the edge to retrieved from the graph</param>
/// <returns>the edge referenced by the provided identifier or null when no such edge exists</returns>
public IEdge GetEdge(object id)
{
if (id == null)
throw new ArgumentException("id may not be null, it should be a UInt64");
if (id is UInt64)
{
UInt64 fullId = (UInt64)id;
EdgeTypeId edgeTypeId = (EdgeTypeId)(fullId >> 32);
EdgeType et = EdgeTypes[edgeTypeId];
EdgeId edgeId = (EdgeId)fullId;
Edge edge = et.GetEdge(edgeId);
return edge;
}
return null;
}
/// <summary>
/// Get a <see cref="EdgeType"/> associated with an <see cref="Edge"/> id
/// </summary>
/// <param name="id">Id to look for</param>
/// <returns>the associated edge type</returns>
public EdgeType GetEdgeType(EdgeId id)
{
if (id < EdgeTypes.Count)
{
EdgeType anEdgeType = EdgeTypes[id];
return anEdgeType;
}
throw new EdgeTypeDoesNotExistException();
}
/// <summary>
/// Return an iterable to all the edges in the graph.
/// </summary>
/// <returns>an iterable reference to all edges in the graph</returns>
public IEnumerable<IEdge> GetEdges()
{
foreach (EdgeType et in EdgeTypes)
foreach (IEdge edge in et.GetEdges())
yield return edge;
}
/// <summary>
/// Return an iterable to all the edges in the graph that have a particular key/value property.
/// </summary>
/// <param name="key">the key of the edge</param>
/// <param name="value">the value of the edge</param>
/// <returns>an iterable of edges with provided key and value</returns>
public virtual IEnumerable<IEdge> GetEdges(string key, object value)
{
switch (Type.GetTypeCode(value.GetType()))
{
case TypeCode.Boolean:
return GetEdges<bool>(key, (bool)value);
case TypeCode.Single:
return GetEdges<float>(key, (float)value);
case TypeCode.Double:
return GetEdges<double>(key, (double)value);
default:
return GetEdges<IComparable>(key, (IComparable)value);
};
}
/// <summary>
/// Return an iterable to all the edges in the graph that have a particular key/value property.
/// </summary>
/// <param name="key">the key of the edge</param>
/// <param name="value">the value of the edge</param>
/// <returns>an iterable of edges with provided key and value</returns>
public virtual IEnumerable<IEdge> GetEdges<T>(string key, T value) where T : IComparable
{
foreach (EdgeType et in EdgeTypes)
{
PropertyType pt = et.FindProperty(key);
if (pt != null)
foreach (IEdge edge in pt.GetPropertyEdges(value))
yield return edge;
}
}
internal WeakReferenceList<EdgeType> EdgeTypes
{
get
{
return m_edgeTypes.GetTarget(false, Session);
}
}
/// <summary>
/// Get all <see cref="PropertyType"/>s defined for this <see cref="Graph"/>
/// </summary>
internal WeakReferenceList<PropertyType> PropertyTypes
{
get
{
return m_propertyTypes.GetTarget(false, Session);
}
}
/// <summary>
/// Returns the features of the underlying graph.
/// </summary>
public virtual Features Features
{
get
{
return s_features;
}
}
/// <inheritdoc />
protected override SessionBase Session
{
get
{
if (m_session != null)
return m_session;
return base.Session;
}
}
/// <summary>
/// Removes all edges and vertices from the graph.
/// </summary>
public void Clear()
{
foreach (Edge edge in GetEdges())
{
edge.Remove();
}
foreach (Vertex vertex in GetVertices())
{
vertex.Remove();
}
}
/* public static Dictionary<Vertex, HashSet<Edge>> CombineIntersection(Dictionary<Vertex, HashSet<Edge>> objs1, Dictionary<Vertex, HashSet<Edge>> objs2)
{
Dictionary<Vertex, HashSet<Edge>> clone = new Dictionary<Vertex, HashSet<Edge>>();
foreach (KeyValuePair<Vertex, HashSet<Edge>> p in objs1)
{
HashSet<Edge> edges;
if (objs2.TryGetValue(p.Key, out edges))
{
HashSet<Edge> edgesAll = new HashSet<Edge>(edges);
edgesAll.UnionWith(p.Value);
clone.Add(p.Key, edgesAll);
}
}
return clone;
}*/
/// <summary>
/// Combines to sets of edges by intersection
/// </summary>
/// <param name="objs1">first set of edges</param>
/// <param name="objs2">second set of edges</param>
/// <returns>Intersection of sets</returns>
public static HashSet<Edge> CombineIntersection(HashSet<Edge> objs1, HashSet<Edge> objs2)
{
HashSet<Edge> clone = new HashSet<Edge>(objs1);
clone.IntersectWith(objs2);
return clone;
}
/// <summary>
/// Creates a new node type.
/// </summary>
/// <param name="name">Unique name for the new vertex type.</param>
/// <param name="baseType">Base VertexType for the new VertexType.</param>
/// <returns>Unique graph type identifier.</returns>
public VertexType NewVertexType(string name, VertexType baseType = null)
{
VertexType aType;
if (m_stringToVertexType.TryGetValue(name, out aType) == false)
{
var vertexTypes = VertexTypes;
int pos = -1;
for (int i = 0; i < vertexTypes.Count; i++)
{
if (vertexTypes[i] == null)
{
pos = i;
break;
}
}
Update();
if (pos < 0)
pos = vertexTypes.Count;
aType = new VertexType(pos, name, baseType, this);
vertexTypes[pos] = aType;
m_stringToVertexType.AddFast(name, aType);
}
return aType;
}
/// <summary>
/// Creates a new edge type.
/// </summary>
/// <param name="name">Unique name for the new edge type.</param>
/// <param name="biderectional">If true, this creates a biderectional edge type, otherwise this creates a unidirectional edge type.</param>
/// <param name="baseType">Base EdgeType for the new EdgeType.</param>
/// <returns>Unique edge type.</returns>
/// <returns>a new edge type</returns>
public EdgeType NewEdgeType(string name, bool biderectional, EdgeType baseType = null)
{
EdgeType aType;
if (m_stringToEdgeType.TryGetValue(name, out aType) == false)
{
var edgeTypes = EdgeTypes;
int pos = -1;
for (int i = 0; i < edgeTypes.Count; i++)
{
if (edgeTypes[i] == null)
{
pos = i;
break;
}
}
Update();
if (pos < 0)
pos = edgeTypes.Count;
aType = new EdgeType(pos, name, null, null, biderectional, baseType, this);
edgeTypes[pos] = aType;
m_stringToEdgeType.AddFast(name, aType);
}
return aType;
}
internal void RemoveEdgeTypeRef(EdgeType et)
{
Update();
m_stringToEdgeType.Remove(et.TypeName);
EdgeTypes[et.TypeId] = null;
}
internal void RemoveVertexTypeRef(VertexType vt)
{
Update();
m_stringToVertexType.Remove(vt.TypeName);
VertexTypes[vt.TypeId] = null;
}
/// <summary>
/// Creates a new edge type.
/// </summary>
/// <param name="name">Unique name for the new edge type.</param>
/// <param name="biderectional">If true, this creates a biderectional edge type, otherwise this creates a unidirectional edge type.</param>
/// <returns>Unique edge type.</returns>
/// <param name="tailType">a fixed tail VertexType</param>
/// <param name="headType">a fixed head VertexType</param>
/// <param name="baseType">if specified make the new <see cref="EdgeType"/> a subclass of this <see cref="EdgeType"/></param>
/// <returns>a new edge type</returns>
public EdgeType NewEdgeType(string name, bool biderectional, VertexType tailType, VertexType headType, EdgeType baseType = null)
{
EdgeType aType;
var edgeTypes = EdgeTypes;
if (m_stringToEdgeType.TryGetValue(name, out aType) == false)
{
int pos = -1;
for (int i = 0; i < edgeTypes.Count; i++)
{
if (edgeTypes[i] == null)
{
pos = i;
break;
}
}
Update();
if (pos < 0)
pos = edgeTypes.Count;
aType = new EdgeType(pos, name, tailType, headType, biderectional, baseType, this);
edgeTypes[pos] = aType;
m_stringToEdgeType.AddFast(name, aType);
}
return aType;
}
/// <summary>
/// Creates a new node instance.
/// </summary>
/// <param name="vertexType">Node type identifier.</param>
/// <returns>Unique OID of the new node instance.</returns>
public Vertex NewVertex(VertexType vertexType, VertexId vId = 0)
{
vId = AllocateVertexId(vId);
vId = AllocateVertexId(vId, vertexType.Vertices);
if (m_vertexIdToVertexType != null)
m_vertexIdToVertexType.AddFast(vId, vertexType.TypeId);
return new Vertex(this, vertexType, vId);
}
/// <summary>
/// Creates a new Property.
/// </summary>
/// <param name="vertexType">Node or edge type identifier.</param>
/// <param name="name">Unique name for the new Property.</param>
/// <param name="dt">Data type for the new Property.</param>
/// <param name="kind">Property kind.</param>
/// <returns>Unique Property identifier.</returns>
public PropertyType NewVertexProperty(VertexType vertexType, string name, DataType dt, PropertyKind kind)
{
return vertexType.NewProperty(name, dt, kind);
}
/// <summary>
/// Creates a new Property.
/// </summary>
/// <param name="edgeType">Node or edge type identifier.</param>
/// <param name="name">Unique name for the new Property.</param>
/// <param name="dt">Data type for the new Property.</param>
/// <param name="kind">Property kind.</param>
/// <returns>Unique Property identifier.</returns>
public PropertyType NewEdgeProperty(EdgeType edgeType, string name, DataType dt, PropertyKind kind)
{
return edgeType.NewProperty(name, dt, kind);
}
/// <summary>
/// Creates a new edge instance.
/// </summary>
/// <param name="edgeType">Edge type identifier.</param>
/// <param name="tail">Source OID.</param>
/// <param name="head">Target OID. </param>
/// <returns>Unique OID of the new edge instance.</returns>
public Edge NewEdge(EdgeType edgeType, Vertex tail, Vertex head)
{
return edgeType.NewEdge(tail, head);
}
/// <summary>
/// Creates a new edge instance.
/// The tail of the edge will be any node having the given tailV Value for the given tailAttr Property identifier,
/// and the head of the edge will be any node having the given headV Value for the given headAttr Property identifier.
/// </summary>
/// <param name="edgeType">Node or edge type identifier.</param>
/// <param name="tailAttr">Property identifier.</param>
/// <param name="tailV">Tail value</param>
/// <param name="headAttr">Property identifier.</param>
/// <param name="headV">Head value</param>
/// <returns>Unique edge instance.</returns>
public IEdge NewEdge(EdgeType edgeType, PropertyType tailAttr, object tailV, PropertyType headAttr, object headV)
{
return edgeType.NewEdgeX(PropertyTypes, tailAttr, tailV, headAttr, headV, Session);
}
/// <summary>
/// Selects all neighbor Vertices from or to each of the node OID in the given collection and for the given edge type.
/// </summary>
/// <param name="vertices">Vertex collection.</param>
/// <param name="etype">Edge type identifier.</param>
/// <param name="dir">Direction.</param>
/// <returns>Dictionary of vertex keys with edges path to vertex</returns>
public Dictionary<Vertex, HashSet<Edge>> Traverse(Vertex[] vertices, EdgeType etype, Direction dir)
{
Dictionary<Vertex, HashSet<Edge>> result = new Dictionary<Vertex, HashSet<Edge>>();
foreach (Vertex vertex in vertices)
{
Dictionary<Vertex, HashSet<Edge>> t = vertex.Traverse(etype, dir);
foreach (KeyValuePair<Vertex, HashSet<Edge>> p2 in t)
{
HashSet<Edge> edges;
if (result.TryGetValue(p2.Key, out edges))
foreach (Edge edge in p2.Value)
edges.Add(edge);
else
{
edges = new HashSet<Edge>();
foreach (Edge edge in p2.Value)
edges.Add(edge);
result[p2.Key] = edges;
}
}
}
return result;
}
/// <summary>
/// Finds vertex having the given value for the given property.
/// </summary>
/// <param name="property"></param>
/// <param name="v">a value to look for</param>
/// <param name="errorIfNotFound">if <c>true</c>, throw an exception if an error is found</param>
/// <returns>the vertex matching</returns>
public Vertex FindVertex(PropertyType property, IComparable v, bool errorIfNotFound = true)
{
return property.GetPropertyVertex(v, errorIfNotFound);
}
/// <summary>
/// Get a count of all vertices in this graph
/// </summary>
/// <returns>count of vertices</returns>
public long CountVertices()
{
long ct = 0;
Session.LoadFields(this);
foreach (VertexType vt in VertexTypes)
ct += vt.CountVertices();
return ct;
}
/// <summary>
/// Returns a value indicating whether a Vertex exists for the specified vertex id.
/// </summary>
/// <param name="vertexId">A Vertex id</param>
/// <returns>Returns <see langword="true"/> if a Vertex exist with specified <paramref name="vertexId"/> is found in this <see cref="Graph"/>; otherwise, <see langword="false"/>.</returns>
public bool ContainsVertex(VertexId vertexId)
{
if (VertexIdSetPerType)
throw new NotSupportedException("ContainsVertex by VertexId on graph level is not supported when using VertexIdSetPerType");
var vertices = Vertices;
if (vertices.Count > 0)
{
Range<VertexId> range = new Range<VertexId>(vertexId, vertexId);
bool isEqual;
int pos = vertices.BinarySearch(range, out isEqual);
if (pos >= 0)
{
if (pos == vertices.Count)
--pos;
range = vertices[pos];
if (range.Contains(vertexId))
return true;
if (pos > 0)
{
range = vertices[--pos];
if (range.Contains(vertexId))
return true;
}
}
}
return false;
}
/// <summary>
/// Returns a count of all edges in this graph
/// </summary>
/// <returns>the total count of edges in this graph</returns>
public long CountEdges()
{
long ct = 0;
Session.LoadFields(this);
foreach (EdgeType et in EdgeTypes)
ct += et.CountEdges();
return ct;
}
/// <summary>
/// A shutdown function is required to properly close the graph.
/// This is important for implementations that utilize disk-based serializations.
/// </summary>
public void Shutdown()
{
if (Session.InTransaction)
Session.Commit();
}
/// <summary>
/// Finds the type id associated with a particular edge type. Lookup by name.
/// </summary>
/// <param name="name">The name of the edge/node type being looked up</param>
/// <returns>A node/edge type id or -1 if not found.</returns>
public EdgeType FindEdgeType(string name)
{
EdgeType eType;
//if (stringToRestrictedEdgeType.TryGetValue(name, out eType))
// return eType.TypeId;
if (m_stringToEdgeType.TryGetValue(name, out eType))
return eType;
return null;
}
/// <summary>
/// Finds the type id associated with a particular vertexe type. Lookup by name.
/// </summary>
/// <param name="name">The name of the edge/node type being looked up</param>
/// <returns>A node/edge type id or -1 if not found.</returns>
public VertexType FindVertexType(string name)
{
VertexType nType;
if (m_stringToVertexType.TryGetValue(name, out nType))
return nType;
return null;
}
/// <summary>
/// Place this type of of object on its own page
/// </summary>
/// <returns>
/// The default maximum number of objects per page
/// </returns>
public override UInt16 ObjectsPerPage
{
get
{
return 1;
}
}
/// <summary>
/// Finds a <see cref="PropertyType"/> of <see cref="VertexType"/>.
/// </summary>
/// <param name="vertexType">vertex type with property type</param>
/// <param name="name">a name of a property</param>
/// <returns></returns>
public PropertyType FindVertexProperty(VertexType vertexType, string name)
{
return vertexType.FindProperty(name);
}
/// <summary>
/// Generate a query object that can be used to fine tune which edges/vertices are retrieved from the graph.
/// </summary>
/// <returns>a graph query object with methods for constraining which data is pulled from the underlying graph</returns>
public IQuery Query()
{
return new DefaultGraphQuery(this);
}
/// <summary>
/// Remove the provided edge from the graph.
/// </summary>
/// <param name="edge">the edge to remove from the graph</param>
public void RemoveEdge(IEdge edge)
{
Edge e = edge as Edge;
e.EdgeType.RemoveEdge(e);
}
/// <summary>
/// Removes a property type except if any vertex or edge is using it
/// </summary>
/// <param name="pt">property type to remove</param>
public void RemovePropertyType(PropertyType pt)
{
if (pt.IsVertexProperty)
{
VertexType vt = VertexTypes[pt.TypeId];
foreach (Vertex vertex in vt.GetVertices())
{
IComparable v = pt.GetPropertyValue(vertex.VertexId);
if (v != null)
throw new PropertyTypeInUseException();
}
}
else
{
EdgeType et = EdgeTypes[pt.TypeId];
foreach (Edge edge in et.GetEdges())
{
IComparable v = pt.GetPropertyValue(edge.EdgeId);
if (v != null)
throw new PropertyTypeInUseException();
}
}
if (pt.IsVertexProperty)
{
VertexType vt = VertexTypes[pt.TypeId];
vt.m_stringToPropertyType.Remove(pt.Name);
}
else
{
EdgeType et = EdgeTypes[pt.TypeId];
et.m_stringToPropertyType.Remove(pt.Name);
}
Update();
PropertyTypes.Remove(pt);
pt.Unpersist(Session);
}
/// <summary>
/// Remove the provided vertex from the graph.
/// Upon removing the vertex, all the edges by which the vertex is connected must be removed as well.
/// </summary>
/// <param name="vertex">the vertex to remove from the graph</param>
public void RemoveVertex(IVertex vertex)
{
Vertex v = vertex as Vertex;
v.VertexType.RemoveVertex(v);
}
/// <summary>
/// Removes a <see cref="VertexType"/> from this graph. An exception is thrown if the <see cref="VertexType"/> is in use.
/// </summary>
/// <param name="type">a <see cref="VertexType"/> instance</param>