1
|
|
|
const check = require('check-types'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* An edge object represents a edge between two nodes in a graph. |
5
|
|
|
*/ |
6
|
|
|
class JGFEdge { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Constructor |
10
|
|
|
* @param {string} source Source node id |
11
|
|
|
* @param {string} target Target node id |
12
|
|
|
* @param {string,null} relation Edge relation (AKA 'relationship type') |
13
|
|
|
* @param {string,null} label Edge label (the display name of the edge) |
14
|
|
|
* @param {object,null} metadata Custom edge meta data |
15
|
|
|
* @param {boolean,null} directed true for a directed edge, false for undirected |
16
|
|
|
*/ |
17
|
|
|
constructor(source, target, relation = null, label = null, metadata = null, directed = true) { |
18
|
|
|
this.constructor._guardAgainstEmptyStringParameter('source', source); |
19
|
|
|
this.constructor._guardAgainstEmptyStringParameter('target', target); |
20
|
|
|
|
21
|
|
|
this._source = source; |
22
|
|
|
this._target = target; |
23
|
|
|
this._relation = relation; |
24
|
|
|
this._label = label; |
25
|
|
|
this.metadata = metadata; |
26
|
|
|
this.directed = directed; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// todo: all three of these guards could probably go into some global guarding class |
30
|
|
|
static _guardAgainstEmptyStringParameter(name, value) { |
31
|
|
|
if (!check.nonEmptyString(value)) { |
32
|
|
|
throw new Error('Parameter "' + name + '" has to be an non-empty string.'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
static _guardAgainstInvalidMetaData(metadata) { |
37
|
|
|
if (!check.nonEmptyObject(metadata)) { |
38
|
|
|
throw new Error('Metadata on an node has to be an object.'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
static _guardAgainstInvalidDirected(directed) { |
43
|
|
|
if (!check.boolean(directed)) { |
44
|
|
|
throw new Error('Directed flag on an edge has to be boolean.'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
set source(source) { |
49
|
|
|
this._source = source; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
get source() { |
53
|
|
|
return this._source; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
set target(target) { |
57
|
|
|
this._target = target; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
get target() { |
61
|
|
|
return this._target; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
set relation(relation) { |
65
|
|
|
this._relation = relation; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
get relation() { |
69
|
|
|
return this._relation; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
set label(label) { |
73
|
|
|
this._label = label; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
get label() { |
77
|
|
|
return this._label; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
set metadata(metadata) { |
81
|
|
|
if (check.assigned(metadata)) { |
82
|
|
|
this.constructor._guardAgainstInvalidMetaData(metadata); |
83
|
|
|
} |
84
|
|
|
this._metadata = metadata; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
get metadata() { |
88
|
|
|
return this._metadata; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
set directed(directed) { |
92
|
|
|
if (check.assigned(directed)) { |
93
|
|
|
this.constructor._guardAgainstInvalidDirected(directed); |
94
|
|
|
} |
95
|
|
|
this._directed = directed; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
get directed() { |
99
|
|
|
return this._directed; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
module.exports = { |
104
|
|
|
JGFEdge, |
105
|
|
|
}; |