-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProvenanceGraph.ts
146 lines (128 loc) · 3.7 KB
/
ProvenanceGraph.ts
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
import {
Application,
Handler,
IProvenanceGraph,
NodeIdentifier,
ProvenanceNode,
RootNode,
SerializedProvenanceGraph,
SerializedProvenanceNode,
SerializedStateNode,
IScreenShotProvider,
IScreenShot
} from './api';
import { generateTimestamp, generateUUID, isStateNode } from './utils';
import mitt from './mitt';
/**
* Provenance Graph implementation
*
* @param version The version of the software to track the provenance of
*
*/
export class ProvenanceGraph implements IProvenanceGraph {
public application: Application;
public readonly root: RootNode;
private _current: ProvenanceNode;
private _mitt: any;
private _nodes: { [key: string]: ProvenanceNode } = {};
constructor(application: Application, userid: string = 'Unknown', rootNode?: RootNode) {
this._mitt = mitt();
this.application = application;
if (rootNode) {
this.root = rootNode;
} else {
this.root = {
id: generateUUID(),
label: 'Root',
metadata: {
createdBy: userid,
createdOn: generateTimestamp()
},
children: [],
artifacts: {}
} as RootNode;
}
this.addNode(this.root);
this._current = this.root;
}
addNode(node: ProvenanceNode): void {
if (this._nodes[node.id]) {
throw new Error('Node already added');
}
this._nodes[node.id] = node;
this._mitt.emit('nodeAdded', node);
}
getNode(id: NodeIdentifier): ProvenanceNode {
const result = this._nodes[id];
if (!result) {
throw new Error('Node id not found');
}
return this._nodes[id];
}
get current(): ProvenanceNode {
return this._current;
}
set current(node: ProvenanceNode) {
if (!this._nodes[node.id]) {
throw new Error('Node id not found');
}
this._current = node;
this._mitt.emit('currentChanged', node);
}
get nodes() {
return this._nodes;
}
emitNodeChangedEvent(node: ProvenanceNode) {
/* istanbul ignore if */
if (!this._nodes[node.id]) {
throw new Error('Node id not found');
}
this._mitt.emit('nodeChanged', node);
}
on(type: string, handler: Handler) {
this._mitt.on(type, handler);
}
off(type: string, handler: Handler) {
this._mitt.off(type, handler);
}
}
/* Beware that deeply nested properties in serializedProvenanceGraph is mutated in the process */
export function restoreProvenanceGraph(
serializedProvenanceGraph: SerializedProvenanceGraph
): ProvenanceGraph {
const nodes: { [key: string]: any } = {};
// restore nodes as key value
for (const node of serializedProvenanceGraph.nodes) {
nodes[node.id] = { ...node };
}
// restore parent/children relations
for (const nodeId of Object.keys(nodes)) {
const node = nodes[nodeId];
node.children = node.children.map((id: string) => nodes[id]);
if ('parent' in node) {
node.parent = nodes[node.parent];
}
}
const graph = new ProvenanceGraph(serializedProvenanceGraph.application);
(graph as any)._nodes = nodes;
(graph as any)._current = nodes[serializedProvenanceGraph.current];
(graph as any).root = nodes[serializedProvenanceGraph.root];
return graph;
}
export function serializeProvenanceGraph(graph: ProvenanceGraph): SerializedProvenanceGraph {
const nodes = Object.keys(graph.nodes).map(nodeId => {
const node = graph.getNode(nodeId);
const serializedNode: SerializedProvenanceNode = { ...node } as any;
if (isStateNode(node)) {
(serializedNode as SerializedStateNode).parent = node.parent.id;
}
serializedNode.children = node.children.map(child => child.id);
return serializedNode;
});
return {
nodes,
root: graph.root.id,
application: graph.application,
current: graph.current.id
};
}