-
Notifications
You must be signed in to change notification settings - Fork 0
/
jgfGraph.js
194 lines (165 loc) · 4.9 KB
/
jgfGraph.js
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
const check = require('check-types');
const _ = require('lodash');
const { JgfEdge } = require('./jgfEdge');
const { Guard } = require('./guard');
/**
* An graph object represents the full graph and contains all nodes and edges that the graph consists of.
*/
class JgfGraph {
/**
* Constructor
* @param {string} type Graph classification.
* @param {string} label A text display for the graph.
* @param {boolean} directed Pass true for a directed graph, false for an undirected graph.
* @param {object|null} metadata Custom graph metadata.
*/
constructor(type = '', label = '', directed = true, metadata = null) {
this._nodes = [];
this._edges = [];
this.type = type;
this.label = label;
this.directed = directed;
this._metadata = metadata;
}
/**
* @param {string} nodeId Node to be found.
* @private
*/
_findNodeById(nodeId) {
let foundNode = _.find(this._nodes, (existingNode) => existingNode.id === nodeId);
if (!foundNode) {
throw new Error(`A node does not exist with id = ${nodeId}`);
}
return foundNode;
}
/**
* @param {JgfNode} node Node to be found.
* @private
*/
_nodeExists(node) {
return this._nodeExistsById(node.id);
}
/**
* @param {string} nodeId Node to be found.
* @private
*/
_nodeExistsById(nodeId) {
let foundNode = _.find(this._nodes, (existingNode) => existingNode.id === nodeId);
return Boolean(foundNode);
}
/**
* Sets the graph meta data.
*/
set metadata(value) {
Guard.assertValidMetadataOrNull(value);
this._metadata = value;
}
/**
* Returns the graph meta data.
*/
get metadata() {
return this._metadata;
}
/**
* Returns all nodes.
*/
get nodes() {
return this._nodes;
}
/**
* Returns all edges.
*/
get edges() {
return this._edges;
}
/**
* Adds a node to the graph.
* @param {JgfNode} node Node to be added.
* @throws Error if the node already exists.
*/
addNode(node) {
if (this._nodeExists(node)) {
throw new Error(`A node already exists with id = ${node.id}`);
}
this._nodes.push(node);
}
/**
* Adds multiple nodes to the graph.
* @param {JgfNode[]} nodes A collection of Jgf node objects to be added.
* @throws Error if one of nodes already exists.
*/
addNodes(nodes) {
for (let node of nodes) {
this.addNode(node);
}
}
/**
* Removes an existing node from the graph.
* @param {JgfNode} node Node to be removed.
*/
removeNode(node) {
if (!this._nodeExists(node)) {
throw new Error(`A node does not exist with id = ${node.id}`);
}
_.remove(this._nodes, (existingNode) => existingNode.id === node.id);
}
/**
* Get a node by a node ID.
* @param {string} nodeId Unique node ID.
*/
getNodeById(nodeId) {
return this._findNodeById(nodeId);
}
/**
* Adds an edge to the graph.
* @param {JgfEdge} edge The edge to be added.
*/
addEdge(edge) {
this._guardAgainstNonExistentNodes(edge.source, edge.target);
this._edges.push(edge);
}
_guardAgainstNonExistentNodes(source, target) {
if (!this._nodeExistsById(source)) {
throw new Error(`addEdge failed: source node isn't found in nodes. source = ${source}`);
}
if (!this._nodeExistsById(target)) {
throw new Error(`addEdge failed: target node isn't found in nodes. target = ${target}`);
}
}
/**
* Adds multiple edges to the graph.
* @param {JgfEdge[]} edges A collection of Jgf edge objects to be added.
*/
addEdges(edges) {
for (let edge of edges) {
this.addEdge(edge);
}
}
/**
* Removes existing edge from the graph.
* @param {JgfEdge} edge Edge to be removed.
*/
removeEdge(edge) {
_.remove(this._edges, (existingEdge) => existingEdge.isEqualTo(edge, true));
}
/**
* Get edges between source node and target node, with an optional edge relation.
* @param {string} source Source node ID.
* @param {string} target Target node ID.
* @param {string|null} relation If passed, only edges having this relation will be returned.
*/
getEdgesByNodes(source, target, relation = null) {
this._guardAgainstNonExistentNodes(source, target);
let edge = new JgfEdge(source, target, relation);
return _.filter(this._edges, (existingEdge) => existingEdge.isEqualTo(edge, check.assigned(relation)));
}
get graphDimensions() {
return {
nodes: this._nodes.length,
edges: this._edges.length,
};
}
}
module.exports = {
JgfGraph,
};