1
|
|
|
const _ = require('deepdash')(require('lodash')); |
2
|
|
|
const check = require('check-types'); |
3
|
|
|
const { JgfGraph } = require('./jgfGraph'); |
4
|
|
|
const { JgfMultiGraph } = require('./jgfMultiGraph'); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Transforms graphs or multigraphs to json or vice versa. |
8
|
|
|
* |
9
|
|
|
* Note that this is just called decorator for semantic reasons and does not follow the GoF decorator design pattern. |
10
|
|
|
*/ |
11
|
|
|
class JgfJsonDecorator { |
12
|
|
|
|
13
|
|
|
// static fromJson(json) { |
14
|
|
|
// // todo: return graph or multigraph depending on data |
15
|
|
|
// } |
16
|
|
|
|
17
|
|
|
static _guardAgainstInvalidGraphObject(graph) { |
18
|
|
|
if (!check.instance(graph, JgfGraph) && !check.instance(graph, JgfMultiGraph)) { |
19
|
|
|
throw new Error('JgfJsonDecorator can only decorate graphs or multigraphs.'); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// todo: add doc block |
24
|
|
|
// todo: check if just retunrning a multigraph for a single one makes sense according to spec |
25
|
|
|
static toJson(graph) { |
26
|
|
|
this._guardAgainstInvalidGraphObject(graph); |
27
|
|
|
|
28
|
|
|
let normalizedGraph = this._normalizeToMultiGraph(graph); |
29
|
|
|
let allGraphsJson = []; |
30
|
|
|
|
31
|
|
|
_.each(normalizedGraph.graphs, (singleGraph) => { |
32
|
|
|
|
33
|
|
|
let singleGraphJson = { |
34
|
|
|
type: singleGraph.type, |
35
|
|
|
label: singleGraph.label, |
36
|
|
|
directed: singleGraph.directed, |
37
|
|
|
metadata: singleGraph.metadata, |
38
|
|
|
nodes: [], |
39
|
|
|
edges: [], |
40
|
|
|
}; |
41
|
|
|
|
42
|
|
|
this._nodesToJson(singleGraph, singleGraphJson); |
43
|
|
|
this._edgesToJson(singleGraph, singleGraphJson); |
44
|
|
|
|
45
|
|
|
allGraphsJson.push(singleGraphJson); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
return this._removeNullValues(allGraphsJson); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
static _removeNullValues(json) { |
52
|
|
|
return _.filterDeep(json, (value) => value !== null); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param {JgfGraph} graph |
57
|
|
|
* @param {object} json |
58
|
|
|
*/ |
59
|
|
|
static _edgesToJson(graph, json) { |
60
|
|
|
_.each(graph.edges, (edge) => { |
61
|
|
|
json.edges.push({ |
62
|
|
|
source: edge.source, |
63
|
|
|
target: edge.target, |
64
|
|
|
relation: edge.relation, |
65
|
|
|
label: edge.label, |
66
|
|
|
metadata: edge.metadata, |
67
|
|
|
directed: edge.directed, |
68
|
|
|
}); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param {JgfGraph} graph |
74
|
|
|
* @param {object} json |
75
|
|
|
*/ |
76
|
|
|
static _nodesToJson(graph, json) { |
77
|
|
|
_.each(graph.nodes, (node) => { |
78
|
|
|
json.nodes.push({ |
79
|
|
|
id: node.id, |
80
|
|
|
label: node.label, |
81
|
|
|
metadata: node.metadata, |
82
|
|
|
}); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
static _normalizeToMultiGraph(graph) { |
87
|
|
|
let normalizedGraph = graph; |
88
|
|
|
if (check.instance(graph, JgfGraph)) { |
89
|
|
|
normalizedGraph = new JgfMultiGraph(); |
90
|
|
|
normalizedGraph.addGraph(graph); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return normalizedGraph; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
module.exports = { |
98
|
|
|
JgfJsonDecorator, |
99
|
|
|
}; |