-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProvenanceGraphTraverser.ts
213 lines (193 loc) · 6.54 KB
/
ProvenanceGraphTraverser.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
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
import {
IProvenanceGraphTraverser,
ProvenanceNode,
StateNode,
IActionFunctionRegistry,
IProvenanceGraph,
NodeIdentifier,
ActionFunctionWithThis,
IProvenanceTracker,
Handler
} from './api';
import { isReversibleAction, isStateNode } from './utils';
import mitt from './mitt';
function isNextNodeInTrackUp(currentNode: ProvenanceNode, nextNode: ProvenanceNode): boolean {
if (isStateNode(currentNode) && currentNode.parent === nextNode) {
return true;
} else if (isStateNode(nextNode) && nextNode.parent !== currentNode) {
// This is a guard against the illegitimate use of this function for unconnected nodes
/* istanbul ignore next */
throw new Error('Unconnected nodes, you probably should not be using this function');
} else {
return false;
}
}
function findPathToTargetNode(
currentNode: ProvenanceNode,
targetNode: ProvenanceNode,
track: ProvenanceNode[],
comingFromNode: ProvenanceNode = currentNode
): boolean {
if (currentNode && currentNode === targetNode) {
track.unshift(currentNode);
return true;
} else if (currentNode) {
// Map the StateNodes in the children StateEdges
const nodesToCheck: ProvenanceNode[] = [...currentNode.children];
// Add the parent node to that same list
/* istanbul ignore else */
if (isStateNode(currentNode)) {
nodesToCheck.push(currentNode.parent);
}
for (const node of nodesToCheck) {
// If the node to check is in the track already, skip it.
if (node === comingFromNode) {
continue;
}
/* istanbul ignore else */
if (findPathToTargetNode(node, targetNode, track, currentNode)) {
track.unshift(currentNode);
return true;
}
}
}
/* istanbul ignore next */
return false;
}
class IrreversibleError extends Error {
invalidTraversal = true;
}
export class ProvenanceGraphTraverser implements IProvenanceGraphTraverser {
public graph: IProvenanceGraph;
public tracker: IProvenanceTracker | null;
/**
* trackingWhenTraversing === false disables tracking when traversing to prevent feedback.
* When applying an action, the object we're tracking might trigger its event listeners. This
* means that more Nodes are added to the ProvenanceGraph when traversing, which is most likely
* unwanted behaviour.
*
* It will enable/disable immediately before/after calling the action. So if the event is emitted
* asynchronously after the action, it will not work.
*/
public trackingWhenTraversing = false;
private registry: IActionFunctionRegistry;
private _mitt: any;
constructor(
registry: IActionFunctionRegistry,
graph: IProvenanceGraph,
tracker: IProvenanceTracker | null = null
) {
this.registry = registry;
this.graph = graph;
this.tracker = tracker;
this._mitt = mitt();
}
async executeFunctions(
functionsToDo: ActionFunctionWithThis[],
argumentsToDo: any[],
transitionTimes: number[]
): Promise<StateNode> {
let result;
for (let i = 0; i < functionsToDo.length; i++) {
const funcWithThis = functionsToDo[i];
let promise: any;
if (this.tracker && this.tracker.acceptActions && !this.trackingWhenTraversing) {
this.tracker.acceptActions = false;
promise = funcWithThis.func.apply(funcWithThis.thisArg, argumentsToDo[i]);
this.tracker.acceptActions = true;
} else {
promise = funcWithThis.func.apply(funcWithThis.thisArg, argumentsToDo[i]);
}
result = await promise;
}
return result;
}
/**
* Finds shortest path between current node and node with request identifer.
* Calls the do/undo functions of actions on the path.
*
* @param id Node identifier
*/
async toStateNode(
id: NodeIdentifier,
transtionTime?: number
): Promise<ProvenanceNode | undefined> {
const currentNode = this.graph.current;
const targetNode = this.graph.getNode(id);
if (currentNode === targetNode) {
return Promise.resolve(currentNode);
}
const trackToTarget: ProvenanceNode[] = [];
const success = findPathToTargetNode(currentNode, targetNode, trackToTarget);
/* istanbul ignore if */
if (!success) {
throw new Error('No path to target node found in graph');
}
let functionsToDo: ActionFunctionWithThis[], argumentsToDo: any[];
const transitionTimes: number[] = [];
try {
const arg = this.getFunctionsAndArgsFromTrack(trackToTarget);
functionsToDo = arg.functionsToDo;
argumentsToDo = arg.argumentsToDo;
functionsToDo.forEach((func: any) => {
transitionTimes.push(transtionTime || 0);
});
} catch (error) {
if (error.invalidTraversal) {
this._mitt.emit('invalidTraversal', targetNode);
return undefined;
} else {
/* istanbul ignore next */
throw error; // should never happen
}
}
const result = await this.executeFunctions(functionsToDo, argumentsToDo, transitionTimes);
this.graph.current = targetNode;
return result;
}
private getFunctionsAndArgsFromTrack(
track: ProvenanceNode[]
): {
functionsToDo: ActionFunctionWithThis[];
argumentsToDo: any[];
} {
const functionsToDo: ActionFunctionWithThis[] = [];
const argumentsToDo: any[] = [];
for (let i = 0; i < track.length - 1; i++) {
const thisNode = track[i];
const nextNode = track[i + 1];
const up = isNextNodeInTrackUp(thisNode, nextNode);
if (up) {
/* istanbul ignore else */
if (isStateNode(thisNode)) {
if (!isReversibleAction(thisNode.action)) {
throw new IrreversibleError('trying to undo an Irreversible action');
}
const undoFunc = this.registry.getFunctionByName(thisNode.action.undo);
functionsToDo.push(undoFunc);
argumentsToDo.push(thisNode.action.undoArguments);
} else {
/* istanbul ignore next */
throw new Error('Going up from root? unreachable error ... i hope');
}
} else {
/* istanbul ignore else */
if (isStateNode(nextNode)) {
const doFunc = this.registry.getFunctionByName(nextNode.action.do);
functionsToDo.push(doFunc);
argumentsToDo.push(nextNode.action.doArguments);
} else {
/* istanbul ignore next */
throw new Error('Going down to the root? unreachable error ... i hope');
}
}
}
return { functionsToDo, argumentsToDo };
}
on(type: string, handler: Handler) {
this._mitt.on(type, handler);
}
off(type: string, handler: Handler) {
this._mitt.off(type, handler);
}
}