@sccharts-lab/engine 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +277 -0
- package/README.md +125 -2
- package/dist/codegen/expr.d.ts +36 -0
- package/dist/codegen/expr.js +252 -0
- package/dist/codegen/expr.js.map +1 -0
- package/dist/codegen/index.d.ts +4 -0
- package/dist/codegen/index.js +238 -0
- package/dist/codegen/index.js.map +1 -0
- package/dist/codegen/layout.d.ts +57 -0
- package/dist/codegen/layout.js +306 -0
- package/dist/codegen/layout.js.map +1 -0
- package/dist/codegen/logic.d.ts +31 -0
- package/dist/codegen/logic.js +258 -0
- package/dist/codegen/logic.js.map +1 -0
- package/dist/codegen/methods.d.ts +17 -0
- package/dist/codegen/methods.js +242 -0
- package/dist/codegen/methods.js.map +1 -0
- package/dist/codegen/reset.d.ts +6 -0
- package/dist/codegen/reset.js +110 -0
- package/dist/codegen/reset.js.map +1 -0
- package/dist/codegen/types.d.ts +81 -0
- package/dist/codegen/types.js +3 -0
- package/dist/codegen/types.js.map +1 -0
- package/dist/format.d.ts +136 -0
- package/dist/format.js +872 -0
- package/dist/format.js.map +1 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +507 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +1 -0
- package/dist/internal.js +4 -0
- package/dist/internal.js.map +1 -0
- package/dist/parser/ids.d.ts +4 -0
- package/dist/parser/ids.js +172 -0
- package/dist/parser/ids.js.map +1 -0
- package/dist/parser/imports.d.ts +2 -0
- package/dist/parser/imports.js +28 -0
- package/dist/parser/imports.js.map +1 -0
- package/dist/parser/interface.d.ts +10 -0
- package/dist/parser/interface.js +93 -0
- package/dist/parser/interface.js.map +1 -0
- package/dist/parser/kielerWording.d.ts +2 -0
- package/dist/parser/kielerWording.js +196 -0
- package/dist/parser/kielerWording.js.map +1 -0
- package/dist/parser/lexer.d.ts +25 -0
- package/dist/parser/lexer.js +334 -0
- package/dist/parser/lexer.js.map +1 -0
- package/dist/parser/parser.d.ts +29 -0
- package/dist/parser/parser.js +3249 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/parser/resolve.d.ts +46 -0
- package/dist/parser/resolve.js +919 -0
- package/dist/parser/resolve.js.map +1 -0
- package/dist/parser/source.d.ts +15 -0
- package/dist/parser/source.js +42 -0
- package/dist/parser/source.js.map +1 -0
- package/dist/parser/validate.d.ts +5 -0
- package/dist/parser/validate.js +1430 -0
- package/dist/parser/validate.js.map +1 -0
- package/dist/sim/abortRegions.d.ts +9 -0
- package/dist/sim/abortRegions.js +192 -0
- package/dist/sim/abortRegions.js.map +1 -0
- package/dist/sim/analysis.d.ts +113 -0
- package/dist/sim/analysis.js +1104 -0
- package/dist/sim/analysis.js.map +1 -0
- package/dist/sim/codegen.d.ts +30 -0
- package/dist/sim/codegen.js +730 -0
- package/dist/sim/codegen.js.map +1 -0
- package/dist/sim/compile.d.ts +43 -0
- package/dist/sim/compile.js +1062 -0
- package/dist/sim/compile.js.map +1 -0
- package/dist/sim/deps.d.ts +73 -0
- package/dist/sim/deps.js +481 -0
- package/dist/sim/deps.js.map +1 -0
- package/dist/sim/describe.d.ts +22 -0
- package/dist/sim/describe.js +272 -0
- package/dist/sim/describe.js.map +1 -0
- package/dist/sim/describePreemption.d.ts +22 -0
- package/dist/sim/describePreemption.js +90 -0
- package/dist/sim/describePreemption.js.map +1 -0
- package/dist/sim/describeStructure.d.ts +75 -0
- package/dist/sim/describeStructure.js +293 -0
- package/dist/sim/describeStructure.js.map +1 -0
- package/dist/sim/eval.d.ts +99 -0
- package/dist/sim/eval.js +644 -0
- package/dist/sim/eval.js.map +1 -0
- package/dist/sim/exec.d.ts +212 -0
- package/dist/sim/exec.js +874 -0
- package/dist/sim/exec.js.map +1 -0
- package/dist/sim/fold.d.ts +2 -0
- package/dist/sim/fold.js +40 -0
- package/dist/sim/fold.js.map +1 -0
- package/dist/sim/goblock.d.ts +16 -0
- package/dist/sim/goblock.js +171 -0
- package/dist/sim/goblock.js.map +1 -0
- package/dist/sim/guardIndices.d.ts +13 -0
- package/dist/sim/guardIndices.js +137 -0
- package/dist/sim/guardIndices.js.map +1 -0
- package/dist/sim/index.d.ts +43 -0
- package/dist/sim/index.js +138 -0
- package/dist/sim/index.js.map +1 -0
- package/dist/sim/lower.d.ts +33 -0
- package/dist/sim/lower.js +716 -0
- package/dist/sim/lower.js.map +1 -0
- package/dist/sim/memory.d.ts +47 -0
- package/dist/sim/memory.js +328 -0
- package/dist/sim/memory.js.map +1 -0
- package/dist/sim/model.d.ts +487 -0
- package/dist/sim/model.js +772 -0
- package/dist/sim/model.js.map +1 -0
- package/dist/sim/passes/abort.d.ts +2 -0
- package/dist/sim/passes/abort.js +269 -0
- package/dist/sim/passes/abort.js.map +1 -0
- package/dist/sim/passes/abortRegion.d.ts +10 -0
- package/dist/sim/passes/abortRegion.js +62 -0
- package/dist/sim/passes/abortRegion.js.map +1 -0
- package/dist/sim/passes/arrayAssignment.d.ts +2 -0
- package/dist/sim/passes/arrayAssignment.js +147 -0
- package/dist/sim/passes/arrayAssignment.js.map +1 -0
- package/dist/sim/passes/codeEffects.d.ts +2 -0
- package/dist/sim/passes/codeEffects.js +14 -0
- package/dist/sim/passes/codeEffects.js.map +1 -0
- package/dist/sim/passes/complexFinalState.d.ts +4 -0
- package/dist/sim/passes/complexFinalState.js +77 -0
- package/dist/sim/passes/complexFinalState.js.map +1 -0
- package/dist/sim/passes/connector.d.ts +2 -0
- package/dist/sim/passes/connector.js +18 -0
- package/dist/sim/passes/connector.js.map +1 -0
- package/dist/sim/passes/contained.d.ts +37 -0
- package/dist/sim/passes/contained.js +198 -0
- package/dist/sim/passes/contained.js.map +1 -0
- package/dist/sim/passes/context.d.ts +39 -0
- package/dist/sim/passes/context.js +3 -0
- package/dist/sim/passes/context.js.map +1 -0
- package/dist/sim/passes/countDelay.d.ts +2 -0
- package/dist/sim/passes/countDelay.js +51 -0
- package/dist/sim/passes/countDelay.js.map +1 -0
- package/dist/sim/passes/dataflow.d.ts +11 -0
- package/dist/sim/passes/dataflow.js +319 -0
- package/dist/sim/passes/dataflow.js.map +1 -0
- package/dist/sim/passes/deferred.d.ts +2 -0
- package/dist/sim/passes/deferred.js +345 -0
- package/dist/sim/passes/deferred.js.map +1 -0
- package/dist/sim/passes/detect.d.ts +10 -0
- package/dist/sim/passes/detect.js +33 -0
- package/dist/sim/passes/detect.js.map +1 -0
- package/dist/sim/passes/during.d.ts +2 -0
- package/dist/sim/passes/during.js +67 -0
- package/dist/sim/passes/during.js.map +1 -0
- package/dist/sim/passes/entry.d.ts +5 -0
- package/dist/sim/passes/entry.js +93 -0
- package/dist/sim/passes/entry.js.map +1 -0
- package/dist/sim/passes/enum.d.ts +2 -0
- package/dist/sim/passes/enum.js +22 -0
- package/dist/sim/passes/enum.js.map +1 -0
- package/dist/sim/passes/exit.d.ts +4 -0
- package/dist/sim/passes/exit.js +120 -0
- package/dist/sim/passes/exit.js.map +1 -0
- package/dist/sim/passes/finalRegion.d.ts +2 -0
- package/dist/sim/passes/finalRegion.js +21 -0
- package/dist/sim/passes/finalRegion.js.map +1 -0
- package/dist/sim/passes/followedBy.d.ts +2 -0
- package/dist/sim/passes/followedBy.js +78 -0
- package/dist/sim/passes/followedBy.js.map +1 -0
- package/dist/sim/passes/forRegion.d.ts +9 -0
- package/dist/sim/passes/forRegion.js +230 -0
- package/dist/sim/passes/forRegion.js.map +1 -0
- package/dist/sim/passes/history.d.ts +12 -0
- package/dist/sim/passes/history.js +115 -0
- package/dist/sim/passes/history.js.map +1 -0
- package/dist/sim/passes/hostCode.d.ts +2 -0
- package/dist/sim/passes/hostCode.js +152 -0
- package/dist/sim/passes/hostCode.js.map +1 -0
- package/dist/sim/passes/index.d.ts +4 -0
- package/dist/sim/passes/index.js +84 -0
- package/dist/sim/passes/index.js.map +1 -0
- package/dist/sim/passes/inheritance.d.ts +8 -0
- package/dist/sim/passes/inheritance.js +233 -0
- package/dist/sim/passes/inheritance.js.map +1 -0
- package/dist/sim/passes/initOperator.d.ts +2 -0
- package/dist/sim/passes/initOperator.js +68 -0
- package/dist/sim/passes/initOperator.js.map +1 -0
- package/dist/sim/passes/initialization.d.ts +2 -0
- package/dist/sim/passes/initialization.js +69 -0
- package/dist/sim/passes/initialization.js.map +1 -0
- package/dist/sim/passes/ode.d.ts +2 -0
- package/dist/sim/passes/ode.js +14 -0
- package/dist/sim/passes/ode.js.map +1 -0
- package/dist/sim/passes/period.d.ts +2 -0
- package/dist/sim/passes/period.js +106 -0
- package/dist/sim/passes/period.js.map +1 -0
- package/dist/sim/passes/prTransitions.d.ts +2 -0
- package/dist/sim/passes/prTransitions.js +12 -0
- package/dist/sim/passes/prTransitions.js.map +1 -0
- package/dist/sim/passes/pre.d.ts +4 -0
- package/dist/sim/passes/pre.js +326 -0
- package/dist/sim/passes/pre.js.map +1 -0
- package/dist/sim/passes/quirks.d.ts +2 -0
- package/dist/sim/passes/quirks.js +169 -0
- package/dist/sim/passes/quirks.js.map +1 -0
- package/dist/sim/passes/reference.d.ts +60 -0
- package/dist/sim/passes/reference.js +386 -0
- package/dist/sim/passes/reference.js.map +1 -0
- package/dist/sim/passes/regionActions.d.ts +2 -0
- package/dist/sim/passes/regionActions.js +56 -0
- package/dist/sim/passes/regionActions.js.map +1 -0
- package/dist/sim/passes/sequentialFollowedBy.d.ts +2 -0
- package/dist/sim/passes/sequentialFollowedBy.js +72 -0
- package/dist/sim/passes/sequentialFollowedBy.js.map +1 -0
- package/dist/sim/passes/signal.d.ts +2 -0
- package/dist/sim/passes/signal.js +230 -0
- package/dist/sim/passes/signal.js.map +1 -0
- package/dist/sim/passes/static.d.ts +5 -0
- package/dist/sim/passes/static.js +67 -0
- package/dist/sim/passes/static.js.map +1 -0
- package/dist/sim/passes/suspend.d.ts +10 -0
- package/dist/sim/passes/suspend.js +71 -0
- package/dist/sim/passes/suspend.js.map +1 -0
- package/dist/sim/passes/termination.d.ts +9 -0
- package/dist/sim/passes/termination.js +69 -0
- package/dist/sim/passes/termination.js.map +1 -0
- package/dist/sim/passes/timedAutomata.d.ts +10 -0
- package/dist/sim/passes/timedAutomata.js +437 -0
- package/dist/sim/passes/timedAutomata.js.map +1 -0
- package/dist/sim/passes/unpackClasses.d.ts +2 -0
- package/dist/sim/passes/unpackClasses.js +54 -0
- package/dist/sim/passes/unpackClasses.js.map +1 -0
- package/dist/sim/passes/userSchedule.d.ts +2 -0
- package/dist/sim/passes/userSchedule.js +43 -0
- package/dist/sim/passes/userSchedule.js.map +1 -0
- package/dist/sim/passes/weakSuspend.d.ts +2 -0
- package/dist/sim/passes/weakSuspend.js +145 -0
- package/dist/sim/passes/weakSuspend.js.map +1 -0
- package/dist/sim/preemptionLog.d.ts +41 -0
- package/dist/sim/preemptionLog.js +133 -0
- package/dist/sim/preemptionLog.js.map +1 -0
- package/dist/sim/random.d.ts +12 -0
- package/dist/sim/random.js +40 -0
- package/dist/sim/random.js.map +1 -0
- package/dist/sim/reasons.d.ts +29 -0
- package/dist/sim/reasons.js +168 -0
- package/dist/sim/reasons.js.map +1 -0
- package/dist/sim/scg.d.ts +124 -0
- package/dist/sim/scg.js +723 -0
- package/dist/sim/scg.js.map +1 -0
- package/dist/sim/sfr.d.ts +4 -0
- package/dist/sim/sfr.js +264 -0
- package/dist/sim/sfr.js.map +1 -0
- package/dist/sim/stringConditions.d.ts +3 -0
- package/dist/sim/stringConditions.js +42 -0
- package/dist/sim/stringConditions.js.map +1 -0
- package/dist/sim/structure/copy.d.ts +4 -0
- package/dist/sim/structure/copy.js +140 -0
- package/dist/sim/structure/copy.js.map +1 -0
- package/dist/sim/structure/delays.d.ts +30 -0
- package/dist/sim/structure/delays.js +127 -0
- package/dist/sim/structure/delays.js.map +1 -0
- package/dist/sim/structure/instance.d.ts +15 -0
- package/dist/sim/structure/instance.js +69 -0
- package/dist/sim/structure/instance.js.map +1 -0
- package/dist/sim/structure/methods.d.ts +19 -0
- package/dist/sim/structure/methods.js +550 -0
- package/dist/sim/structure/methods.js.map +1 -0
- package/dist/sim/structure/rewrite.d.ts +20 -0
- package/dist/sim/structure/rewrite.js +113 -0
- package/dist/sim/structure/rewrite.js.map +1 -0
- package/dist/sim/structure/track.d.ts +22 -0
- package/dist/sim/structure/track.js +123 -0
- package/dist/sim/structure/track.js.map +1 -0
- package/dist/sim/tracker.d.ts +77 -0
- package/dist/sim/tracker.js +265 -0
- package/dist/sim/tracker.js.map +1 -0
- package/dist/sim/values.d.ts +31 -0
- package/dist/sim/values.js +103 -0
- package/dist/sim/values.js.map +1 -0
- package/dist/types.d.ts +670 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +58 -3
package/dist/sim/scg.js
ADDED
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
// The sequentially constructive graph (SCG) of the core chart: what KIELER's core chain (Trigger/Effect,
|
|
2
|
+
// Surface/Depth: sccharts/processors/TriggerEffect.xtend, SurfaceDepth.xtend) and its SCG transformation
|
|
3
|
+
// (sccharts/processors/scg/SCGTransformation.xtend) produce, built in one step.
|
|
4
|
+
//
|
|
5
|
+
// superstate fork -> one thread per control-flow region (entry ... exit) -> join -> termination target
|
|
6
|
+
// final state the thread's exit (a final state without outgoing transitions)
|
|
7
|
+
// simple state surface: the immediate transitions in priority order (conditionals), then a pause;
|
|
8
|
+
// depth (next tick): all transitions in priority order, then back to the pause
|
|
9
|
+
// transition one assignment node per effect, then the target state's first node
|
|
10
|
+
//
|
|
11
|
+
// Only nodes reachable from the root entry exist (KIELER's dead code removal). Every node knows the core state
|
|
12
|
+
// and transition or action it implements, so everything maps back to the source.
|
|
13
|
+
//
|
|
14
|
+
// The graph also has KIELER's shape where later analyses depend on it:
|
|
15
|
+
// - the effects Trigger/Effect moves into states of their own are one chain, shared by an immediate
|
|
16
|
+
// transition's surface copy and its depth original;
|
|
17
|
+
// - Surface/Depth's depth transition optimisation (DTO): when the last transitions are immediate, the depth
|
|
18
|
+
// continues in the surface's conditionals for them instead of testing them again;
|
|
19
|
+
// - every control flow has KIELER's creation rank (`seq`): the order of a node's incoming control flows in
|
|
20
|
+
// KIELER's EMF model, which decides where its depth-first searches look first (the schizophrenia cure's
|
|
21
|
+
// hasConcurrentDelay, propagateDelay). Flows are created state by state in region order (generated states
|
|
22
|
+
// come after the written ones: first Trigger/Effect's, then Surface/Depth's), flows into final states move to
|
|
23
|
+
// the region's exit afterwards (trimExitNodes), and later rewiring appends (scg.seq).
|
|
24
|
+
import { allStates, controlflowRegions, controlflowRegionsContainStates, isImplicitlyImmediate, regionsMayTerminate } from "./model.js";
|
|
25
|
+
/** After the root's flows ([0, ...]): flows moved into region exits by trimExitNodes. */
|
|
26
|
+
const TRIMMED = 1;
|
|
27
|
+
/** A state's flows after those of its regions (the join's). */
|
|
28
|
+
const LAST = 1e9;
|
|
29
|
+
function compareKeys(a, b) {
|
|
30
|
+
const n = Math.min(a.length, b.length);
|
|
31
|
+
for (let i = 0; i < n; i++)
|
|
32
|
+
if (a[i] !== b[i])
|
|
33
|
+
return a[i] - b[i];
|
|
34
|
+
return a.length - b.length;
|
|
35
|
+
}
|
|
36
|
+
/** Number of KIELER effects an effect stands for: the TTS reset is one per transition, action marks none. */
|
|
37
|
+
function weightOf(e, ttsCount) {
|
|
38
|
+
if (e.k !== 'mark')
|
|
39
|
+
return 1;
|
|
40
|
+
// Action marks and the preemption passes' tracker hooks are not KIELER effects.
|
|
41
|
+
if (e.m.kind === 'action' || e.m.kind === 'enterRegions' || e.m.kind === 'resume')
|
|
42
|
+
return 0;
|
|
43
|
+
// Transitions of a referenced chart's copy are not counted by Taken Transition Signaling (structure.md S-15).
|
|
44
|
+
if (e.m.kind === 'take' && e.m.index < 0)
|
|
45
|
+
return 0;
|
|
46
|
+
if (e.m.kind === 'ttsReset')
|
|
47
|
+
return Math.max(1, ttsCount);
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
/** A mark node that stands for no KIELER effect (KIELER's graph has no node for it, weightOf). */
|
|
51
|
+
export function noKielerNode(n) {
|
|
52
|
+
return n?.kind === 'assign' && n.effect?.k === 'mark' && weightOf(n.effect, 1) === 0;
|
|
53
|
+
}
|
|
54
|
+
/** KIELER's equals2 (KExpressionsCompareExtensions): structural for operators, references and literals only. */
|
|
55
|
+
function equals2(a, b) {
|
|
56
|
+
if (!a || !b)
|
|
57
|
+
return a === b;
|
|
58
|
+
if (a.k !== b.k)
|
|
59
|
+
return false;
|
|
60
|
+
switch (a.k) {
|
|
61
|
+
case 'lit': return a.t === b.t && a.v === b.v;
|
|
62
|
+
case 'host': return a.text === b.text;
|
|
63
|
+
case 'ref': {
|
|
64
|
+
const o = b;
|
|
65
|
+
return a.v === o.v && a.idx.length === o.idx.length && a.idx.every((x, i) => equals2(x, o.idx[i]));
|
|
66
|
+
}
|
|
67
|
+
case 'op': {
|
|
68
|
+
const o = b;
|
|
69
|
+
return a.op === o.op && a.args.length === o.args.length && a.args.every((x, i) => equals2(x, o.args[i]));
|
|
70
|
+
}
|
|
71
|
+
case 'vector': {
|
|
72
|
+
const o = b;
|
|
73
|
+
return a.values.length === o.values.length && a.values.every((x, i) => equals2(x, o.values[i]));
|
|
74
|
+
}
|
|
75
|
+
default: return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
class Builder {
|
|
79
|
+
ttsCount;
|
|
80
|
+
nodes = [];
|
|
81
|
+
threads = [];
|
|
82
|
+
stateNodes = new Map();
|
|
83
|
+
problems = [];
|
|
84
|
+
resolving = new Set();
|
|
85
|
+
/** States being merged into the target of their silent transition (simpleState), and the silent cycles found. */
|
|
86
|
+
silentMerging = new Set();
|
|
87
|
+
silentCycle = new Set();
|
|
88
|
+
/** Creation keys of the flows (converted to ranks at the end). */
|
|
89
|
+
keys = new Map();
|
|
90
|
+
/** Position of every state and region in KIELER's SCG connection order. */
|
|
91
|
+
pos = new Map();
|
|
92
|
+
rpos = new Map();
|
|
93
|
+
/** Pre-order index (Surface/Depth handles states in this order). */
|
|
94
|
+
pre = new Map();
|
|
95
|
+
/** Trigger/Effect: per transition it splits, the key of the state each effect ends in (undefined: no state). */
|
|
96
|
+
te = new Map();
|
|
97
|
+
/** Shared effect chains of the transitions Trigger/Effect splits. */
|
|
98
|
+
teChains = new Map();
|
|
99
|
+
/** The final state a state's node is (final states, and states that only pass on to one). */
|
|
100
|
+
finalOf = new Map();
|
|
101
|
+
/** Node creation keys in KIELER's node list. */
|
|
102
|
+
genKeys = new Map();
|
|
103
|
+
/** Transitions of referenced charts that KIELER's optimisations remove (the tracker follows them on entry). */
|
|
104
|
+
silentHops = [];
|
|
105
|
+
/** States a depth was redirected into by DTO across states (one more incoming transition in KIELER's model). */
|
|
106
|
+
dtoIncoming = new Map();
|
|
107
|
+
constructor(root, ttsCount) {
|
|
108
|
+
this.ttsCount = ttsCount;
|
|
109
|
+
const visit = (s, key) => {
|
|
110
|
+
this.pos.set(s, key);
|
|
111
|
+
controlflowRegions(s).forEach((r, ri) => {
|
|
112
|
+
const rk = [...key, ri];
|
|
113
|
+
this.rpos.set(r, rk);
|
|
114
|
+
r.states.forEach((x, i) => visit(x, [...rk, 0, i]));
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
visit(root, [0]);
|
|
118
|
+
allStates(root).forEach((s, i) => this.pre.set(s, i));
|
|
119
|
+
// Trigger/Effect handles the transitions in containment order (a state's inner transitions before its own)
|
|
120
|
+
// and appends each effect state to the target's region.
|
|
121
|
+
let te = 0;
|
|
122
|
+
const visitTE = (s) => {
|
|
123
|
+
for (const r of controlflowRegions(s))
|
|
124
|
+
for (const x of r.states)
|
|
125
|
+
visitTE(x);
|
|
126
|
+
for (const t of s.out) {
|
|
127
|
+
if (!this.splits(t))
|
|
128
|
+
continue;
|
|
129
|
+
const region = this.rpos.get(t.target.parent) ?? [0];
|
|
130
|
+
this.te.set(t, t.effects.map(e => {
|
|
131
|
+
const w = weightOf(e, this.ttsCount);
|
|
132
|
+
if (!w)
|
|
133
|
+
return undefined;
|
|
134
|
+
te += w;
|
|
135
|
+
return [...region, 1, te - 1];
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
visitTE(root);
|
|
140
|
+
}
|
|
141
|
+
weight(t) {
|
|
142
|
+
return t.effects.reduce((n, e) => n + weightOf(e, this.ttsCount), 0);
|
|
143
|
+
}
|
|
144
|
+
/** TriggerEffect: does it move the transition's effects into states of their own? */
|
|
145
|
+
splits(t) {
|
|
146
|
+
const w = this.weight(t);
|
|
147
|
+
return ((t.trigger !== undefined || t.delay !== 'immediate' || t.preemption === 'termination') && w > 0) || w > 1;
|
|
148
|
+
}
|
|
149
|
+
/** After Trigger/Effect, does the transition still carry effects? */
|
|
150
|
+
keepsEffects(t) {
|
|
151
|
+
return !this.splits(t) && this.weight(t) > 0;
|
|
152
|
+
}
|
|
153
|
+
node(kind, thread, extra, gen) {
|
|
154
|
+
const n = { id: this.nodes.length, kind, thread, ...extra };
|
|
155
|
+
this.nodes.push(n);
|
|
156
|
+
this.genKeys.set(n, gen);
|
|
157
|
+
return n;
|
|
158
|
+
}
|
|
159
|
+
thread(parent, region, fork) {
|
|
160
|
+
const t = {
|
|
161
|
+
id: this.threads.length, region, fork, parent, depth: parent ? parent.depth + 1 : 0, final: !!region?.final, rank: 0,
|
|
162
|
+
};
|
|
163
|
+
this.threads.push(t);
|
|
164
|
+
const R = region ? this.rpos.get(region) ?? [] : [];
|
|
165
|
+
t.entry = this.node('entry', t, {}, [...R, -1, 0]);
|
|
166
|
+
t.exit = this.node('exit', t, {}, [...R, -1, 1]);
|
|
167
|
+
return t;
|
|
168
|
+
}
|
|
169
|
+
/** Sets a control flow. `target`: the state it leads to (a flow into a final state moves to the region's exit). */
|
|
170
|
+
link(src, slot, to, key, target) {
|
|
171
|
+
src[slot] = to;
|
|
172
|
+
if (!to)
|
|
173
|
+
return;
|
|
174
|
+
let k = this.keys.get(src);
|
|
175
|
+
if (!k)
|
|
176
|
+
this.keys.set(src, (k = {}));
|
|
177
|
+
const final = target && to.kind === 'exit' && to === to.thread.exit ? this.finalOf.get(target) : undefined;
|
|
178
|
+
k[slot] = final ? [TRIMMED, ...(this.pos.get(final) ?? []), ...key] : key;
|
|
179
|
+
}
|
|
180
|
+
threadKey(fork, key) {
|
|
181
|
+
let k = this.keys.get(fork);
|
|
182
|
+
if (!k)
|
|
183
|
+
this.keys.set(fork, (k = {}));
|
|
184
|
+
(k.threads ??= []).push(key);
|
|
185
|
+
}
|
|
186
|
+
/** The first node of state `s` in thread `t` (built on first use). */
|
|
187
|
+
stateNode(s, t) {
|
|
188
|
+
const known = this.stateNodes.get(s);
|
|
189
|
+
if (known)
|
|
190
|
+
return known;
|
|
191
|
+
if (this.resolving.has(s)) {
|
|
192
|
+
// A cycle of states merged into the targets of their silent transitions (a referenced chart's immediate
|
|
193
|
+
// transitions without trigger have no KIELER effects): Surface/Depth merges them into one state with an
|
|
194
|
+
// untriggered self-loop, which it then drops (simpleState). The states have no node, as for that loop.
|
|
195
|
+
const path = [...this.resolving].slice([...this.resolving].indexOf(s));
|
|
196
|
+
if (path.every(x => this.silentMerging.has(x))) {
|
|
197
|
+
for (const x of path)
|
|
198
|
+
this.silentCycle.add(x);
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
// A cycle of immediate transitions without trigger and effects: no node in between.
|
|
202
|
+
this.problems.push({ message: 'instantaneous loop of transitions without trigger and effects', states: [...this.resolving] });
|
|
203
|
+
const loop = this.node('assign', t, { state: s }, [...(this.pos.get(s) ?? [0]), 0]);
|
|
204
|
+
this.stateNodes.set(s, loop);
|
|
205
|
+
this.link(loop, 'next', loop, this.pos.get(s) ?? [0]);
|
|
206
|
+
return loop;
|
|
207
|
+
}
|
|
208
|
+
if (s.root || controlflowRegionsContainStates(s))
|
|
209
|
+
return this.superstate(s, t);
|
|
210
|
+
if (s.final && s.out.length === 0) {
|
|
211
|
+
this.stateNodes.set(s, t.exit);
|
|
212
|
+
this.finalOf.set(s, s);
|
|
213
|
+
return t.exit;
|
|
214
|
+
}
|
|
215
|
+
return this.simpleState(s, t);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* The state a region starts in: SCGTransformation takes the first initial state of the region after
|
|
219
|
+
* Surface/Depth. A region has several only after Deferred copied an initial state (passes/deferred.ts: like
|
|
220
|
+
* EcoreUtil.copy, the copy keeps the flag); Surface/Depth's superfluous immediate transition optimisation then
|
|
221
|
+
* replaces a simple state whose first transition is delayed by its `_Pause` state, at the end of the region.
|
|
222
|
+
*/
|
|
223
|
+
initialOf(region) {
|
|
224
|
+
const initials = region.states.filter(x => x.initial);
|
|
225
|
+
if (initials.length < 2)
|
|
226
|
+
return initials[0];
|
|
227
|
+
const movedBySurfaceDepth = (s) => {
|
|
228
|
+
if (controlflowRegionsContainStates(s))
|
|
229
|
+
return false;
|
|
230
|
+
if (s.out.length === 0)
|
|
231
|
+
return !s.final;
|
|
232
|
+
const t0 = s.out[0];
|
|
233
|
+
if (s.out.length === 1 && t0.preemption === 'termination')
|
|
234
|
+
return false;
|
|
235
|
+
if (s.out.length === 1 && t0.trigger === undefined && !isImplicitlyImmediate(t0) && !this.keepsEffects(t0))
|
|
236
|
+
return false;
|
|
237
|
+
if (s.out.length > 1 && conditionalPattern(this, t0, s.out[1]))
|
|
238
|
+
return false;
|
|
239
|
+
// Immediate transitions are duplicated to the front and keep the state; it is merged only without them.
|
|
240
|
+
return !s.out.some(x => x.delay === 'immediate');
|
|
241
|
+
};
|
|
242
|
+
return initials.find(x => !movedBySurfaceDepth(x)) ?? initials[0];
|
|
243
|
+
}
|
|
244
|
+
superstate(s, t) {
|
|
245
|
+
const P = this.pos.get(s) ?? [0];
|
|
246
|
+
const fork = this.node('fork', t, { state: s }, [...P, -1, 0]);
|
|
247
|
+
const join = this.node('join', t, { state: s }, [...P, -1, 1]);
|
|
248
|
+
fork.join = join;
|
|
249
|
+
join.fork = fork;
|
|
250
|
+
this.stateNodes.set(s, fork);
|
|
251
|
+
fork.threads = [];
|
|
252
|
+
controlflowRegions(s).forEach((region, ri) => {
|
|
253
|
+
const R = this.rpos.get(region) ?? [...P, ri];
|
|
254
|
+
const child = this.thread(t, region, fork);
|
|
255
|
+
fork.threads.push(child);
|
|
256
|
+
this.threadKey(fork, [...R, -3]);
|
|
257
|
+
this.link(child.exit, 'next', join, [...R, -2]);
|
|
258
|
+
const initial = this.initialOf(region);
|
|
259
|
+
this.link(child.entry, 'next', initial ? this.stateNode(initial, child) : undefined, [...R, -1], initial);
|
|
260
|
+
});
|
|
261
|
+
if (s.out.length > 0) {
|
|
262
|
+
this.chainFrom(join, 'next', s.out[0], t, [...P, LAST], P);
|
|
263
|
+
}
|
|
264
|
+
else if (s.root) {
|
|
265
|
+
this.link(join, 'next', t.exit, [...P, LAST]);
|
|
266
|
+
}
|
|
267
|
+
else if (!s.final && regionsMayTerminate(s)) {
|
|
268
|
+
// Surface/Depth: a halt superstate that may terminate continues in __sd_HaltState (a pause loop).
|
|
269
|
+
const halt = [...(this.rpos.get(s.parent) ?? P), 2, this.pre.get(s) ?? 0, 0];
|
|
270
|
+
const surface = this.node('surface', t, { state: s }, [...halt, 0]);
|
|
271
|
+
const depth = this.node('depth', t, { state: s }, [...halt, 1]);
|
|
272
|
+
surface.depth = depth;
|
|
273
|
+
depth.surface = surface;
|
|
274
|
+
this.link(depth, 'next', surface, [...halt, 0]);
|
|
275
|
+
this.link(join, 'next', surface, [...P, LAST]);
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
this.link(join, 'next', t.exit, [...P, LAST]);
|
|
279
|
+
}
|
|
280
|
+
return fork;
|
|
281
|
+
}
|
|
282
|
+
simpleState(s, t) {
|
|
283
|
+
const P = this.pos.get(s) ?? [0];
|
|
284
|
+
const R = this.rpos.get(s.parent) ?? P;
|
|
285
|
+
const preIndex = this.pre.get(s) ?? 0;
|
|
286
|
+
let generated = 0;
|
|
287
|
+
/** The next state Surface/Depth creates for this state. */
|
|
288
|
+
const sd = () => [...R, 2, preIndex, generated++];
|
|
289
|
+
// The first immediate transition is an untriggered self-loop with no KIELER effects (in a referenced chart):
|
|
290
|
+
// Surface/Depth's clean-ups (superfluous conditional states, then superfluous immediate transitions) drop it and
|
|
291
|
+
// the transitions after it. The state is left without transitions, so KIELER's SCG has no node for it: control
|
|
292
|
+
// that reaches it ends there (the thread neither pauses nor exits, and its join treats it as done).
|
|
293
|
+
const firstImmediate = s.out.find(x => x.delay === 'immediate');
|
|
294
|
+
if (firstImmediate && firstImmediate.target === s && firstImmediate.trigger === undefined && silentTake(firstImmediate))
|
|
295
|
+
return undefined;
|
|
296
|
+
if (s.out.length === 0) {
|
|
297
|
+
// A halt state: KIELER adds a delayed self-loop; after Surface/Depth's clean-ups its pause is a
|
|
298
|
+
// generated state.
|
|
299
|
+
const key = sd();
|
|
300
|
+
const surface = this.node('surface', t, { state: s }, [...key, 0]);
|
|
301
|
+
const depth = this.node('depth', t, { state: s }, [...key, 1]);
|
|
302
|
+
surface.depth = depth;
|
|
303
|
+
depth.surface = surface;
|
|
304
|
+
this.link(depth, 'next', surface, [...key, 0]);
|
|
305
|
+
this.stateNodes.set(s, surface);
|
|
306
|
+
return surface;
|
|
307
|
+
}
|
|
308
|
+
const out = s.out;
|
|
309
|
+
const t0 = out[0];
|
|
310
|
+
if (out.length === 1 && t0.trigger === undefined && !isImplicitlyImmediate(t0) && !this.keepsEffects(t0)) {
|
|
311
|
+
// Surface/Depth leaves a plain pause alone: the state is the pause.
|
|
312
|
+
const surface = this.node('surface', t, { state: s }, [...P, 0]);
|
|
313
|
+
const depth = this.node('depth', t, { state: s }, [...P, 1]);
|
|
314
|
+
surface.depth = depth;
|
|
315
|
+
depth.surface = surface;
|
|
316
|
+
this.stateNodes.set(s, surface);
|
|
317
|
+
this.resolving.add(s);
|
|
318
|
+
this.chainFrom(depth, 'next', t0, t, [...P, 0], P);
|
|
319
|
+
this.resolving.delete(s);
|
|
320
|
+
return surface;
|
|
321
|
+
}
|
|
322
|
+
// Surface/Depth: the immediate transitions are copied to the front (they are the surface); the originals
|
|
323
|
+
// count as delayed, so the pause comes before them and the depth checks every transition in order.
|
|
324
|
+
const surfaceItems = out.filter(x => x.delay === 'immediate');
|
|
325
|
+
const order = [
|
|
326
|
+
...surfaceItems.map(x => ({ t: x, surface: true })),
|
|
327
|
+
...out.map(x => ({ t: x, surface: false })),
|
|
328
|
+
];
|
|
329
|
+
// DTO: the depth's last transitions continue in the surface's conditionals for them.
|
|
330
|
+
// Pairs are compared from the back (the surface's last conditional with the depth's last one) while they
|
|
331
|
+
// have the same target and equal triggers; the comparison stops at the state itself, whose incoming
|
|
332
|
+
// transitions come from elsewhere.
|
|
333
|
+
let merged = 0;
|
|
334
|
+
while (merged < surfaceItems.length && merged < out.length) {
|
|
335
|
+
const a = surfaceItems[surfaceItems.length - 1 - merged];
|
|
336
|
+
const b = out[out.length - 1 - merged];
|
|
337
|
+
const sameTarget = a === b || (!this.splits(a) && !this.splits(b) && a.target === b.target);
|
|
338
|
+
const sameTrigger = (a.trigger === undefined && b.trigger === undefined) || equals2(a.trigger, b.trigger);
|
|
339
|
+
if (!sameTarget || !sameTrigger)
|
|
340
|
+
break;
|
|
341
|
+
merged++;
|
|
342
|
+
}
|
|
343
|
+
// KIELER's DTO does not stop at the state: when the state has exactly one incoming transition, an immediate one,
|
|
344
|
+
// it goes on comparing the depth's next transition (from the back) with the first other transition of that
|
|
345
|
+
// transition's source, and merges them too: the depth then continues in the predecessor (SurfaceDepth.xtend,
|
|
346
|
+
// the loop over stateAfterDepth). This happens with the connector the Abort transformation puts before the
|
|
347
|
+
// target of a superstate's join: the depth re-runs the connector, and its transition counts again.
|
|
348
|
+
let continueAt;
|
|
349
|
+
if (surfaceItems.length > 0 && merged === surfaceItems.length) {
|
|
350
|
+
let at = s;
|
|
351
|
+
while (merged < out.length) {
|
|
352
|
+
if (at.incoming.length !== 1 || (this.dtoIncoming.get(at) ?? 0) > 0 || at.initial)
|
|
353
|
+
break;
|
|
354
|
+
const t1 = at.incoming[0];
|
|
355
|
+
if (t1.delay !== 'immediate')
|
|
356
|
+
break;
|
|
357
|
+
const k1 = t1.source;
|
|
358
|
+
const k1Out = this.untransformedOut(k1, s);
|
|
359
|
+
const tk1 = k1Out?.find(x => x !== t1);
|
|
360
|
+
const tk2 = out[out.length - 1 - merged];
|
|
361
|
+
if (!tk1 || k1 === s)
|
|
362
|
+
break;
|
|
363
|
+
const sameTarget = !this.splits(tk1) && !this.splits(tk2) && tk1.target === tk2.target;
|
|
364
|
+
const sameTrigger = (tk1.trigger === undefined && tk2.trigger === undefined) || equals2(tk1.trigger, tk2.trigger);
|
|
365
|
+
if (!sameTarget || !sameTrigger)
|
|
366
|
+
break;
|
|
367
|
+
merged++;
|
|
368
|
+
continueAt = k1;
|
|
369
|
+
at = k1;
|
|
370
|
+
}
|
|
371
|
+
if (continueAt)
|
|
372
|
+
this.dtoIncoming.set(continueAt, (this.dtoIncoming.get(continueAt) ?? 0) + 1);
|
|
373
|
+
}
|
|
374
|
+
const depthCount = out.length - merged;
|
|
375
|
+
const surfaceFirst = [];
|
|
376
|
+
let first;
|
|
377
|
+
let pause;
|
|
378
|
+
/** Where the next node of the chain is attached (its flow's source, slot and key). */
|
|
379
|
+
let attach;
|
|
380
|
+
const register = (n) => {
|
|
381
|
+
if (!first && n) {
|
|
382
|
+
first = n;
|
|
383
|
+
this.stateNodes.set(s, n);
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
this.resolving.add(s);
|
|
387
|
+
let ended = false;
|
|
388
|
+
let depthIndex = 0;
|
|
389
|
+
for (let i = 0; i < order.length; i++) {
|
|
390
|
+
const item = order[i];
|
|
391
|
+
if (!item.surface && !pause) {
|
|
392
|
+
const key = sd();
|
|
393
|
+
sd(); // _Depth (merged into the next state by the optimisations)
|
|
394
|
+
pause = this.node('surface', t, { state: s }, [...key, 0]);
|
|
395
|
+
const depth = this.node('depth', t, { state: s }, [...key, 1]);
|
|
396
|
+
pause.depth = depth;
|
|
397
|
+
depth.surface = pause;
|
|
398
|
+
if (attach)
|
|
399
|
+
this.link(attach.src, attach.slot, pause, attach.key);
|
|
400
|
+
register(pause);
|
|
401
|
+
attach = { src: depth, slot: 'next', key: [...key, 0] };
|
|
402
|
+
}
|
|
403
|
+
if (!item.surface) {
|
|
404
|
+
if (depthIndex === depthCount) {
|
|
405
|
+
// Merged by DTO: continue in the surface's conditional for this transition (or in the predecessor).
|
|
406
|
+
const target = continueAt ? this.stateNode(continueAt, t) : surfaceFirst[surfaceItems.length - merged];
|
|
407
|
+
if (attach && target)
|
|
408
|
+
this.link(attach.src, attach.slot, target, attach.key);
|
|
409
|
+
ended = true;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
depthIndex++;
|
|
413
|
+
}
|
|
414
|
+
const holder = i === 0 && item.surface ? P : sd();
|
|
415
|
+
const tr = item.t;
|
|
416
|
+
if (tr.trigger !== undefined) {
|
|
417
|
+
const cond = this.node('cond', t, { state: s, transition: tr, expr: tr.trigger }, [...holder, 0]);
|
|
418
|
+
if (attach)
|
|
419
|
+
this.link(attach.src, attach.slot, cond, attach.key);
|
|
420
|
+
register(cond);
|
|
421
|
+
if (item.surface)
|
|
422
|
+
surfaceFirst.push(cond);
|
|
423
|
+
this.chainFrom(cond, 'then', tr, t, [...holder, 0], holder);
|
|
424
|
+
attach = { src: cond, slot: 'else', key: [...holder, 1] };
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
// Untriggered: always taken; the rest of the chain is unreachable.
|
|
428
|
+
// A referenced chart's transitions are effectless in KIELER (their take marks are not KIELER effects):
|
|
429
|
+
// Surface/Depth's superfluous immediate transition optimisation merges such a state into its target.
|
|
430
|
+
const silent = silentTake(tr);
|
|
431
|
+
if (!first && (!tr.effects.length || silent)) {
|
|
432
|
+
const hops = this.silentHops.length;
|
|
433
|
+
if (silent)
|
|
434
|
+
for (const e of tr.effects)
|
|
435
|
+
if (e.k === 'mark' && e.m.kind === 'take')
|
|
436
|
+
this.silentHops.push(e.m.key ?? e.m.transition);
|
|
437
|
+
// The state has no node of its own: it is its target's node.
|
|
438
|
+
this.silentMerging.add(s);
|
|
439
|
+
const target = this.stateNode(tr.target, t);
|
|
440
|
+
this.silentMerging.delete(s);
|
|
441
|
+
// In a silent cycle the region stays in the state it entered (the tracker follows no hop).
|
|
442
|
+
if (!target && this.silentCycle.has(s))
|
|
443
|
+
this.silentHops.length = hops;
|
|
444
|
+
this.resolving.delete(s);
|
|
445
|
+
if (!this.stateNodes.has(s) && target)
|
|
446
|
+
this.stateNodes.set(s, target);
|
|
447
|
+
const final = this.finalOf.get(tr.target);
|
|
448
|
+
if (final && target?.kind === 'exit')
|
|
449
|
+
this.finalOf.set(s, final);
|
|
450
|
+
return target;
|
|
451
|
+
}
|
|
452
|
+
const head = this.chainHead(tr, t, holder, register);
|
|
453
|
+
if (attach)
|
|
454
|
+
this.link(attach.src, attach.slot, head.node, attach.key, head.target);
|
|
455
|
+
else
|
|
456
|
+
register(head.node);
|
|
457
|
+
if (item.surface)
|
|
458
|
+
surfaceFirst.push(head.node);
|
|
459
|
+
ended = true;
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
if (!ended && attach && pause)
|
|
464
|
+
this.link(attach.src, attach.slot, pause, attach.key);
|
|
465
|
+
this.resolving.delete(s);
|
|
466
|
+
return first;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* The outgoing transitions KIELER's DTO sees on state `k` while it transforms `s`: those of the chart if Surface/Depth
|
|
470
|
+
* has not transformed `k` yet (it comes later in pre-order) or leaves it alone; undefined if it rewrote `k` (its
|
|
471
|
+
* other transitions then lead into Surface/Depth's own states, which no transition of `s` targets).
|
|
472
|
+
*/
|
|
473
|
+
untransformedOut(k, s) {
|
|
474
|
+
if ((this.pre.get(k) ?? 0) > (this.pre.get(s) ?? 0))
|
|
475
|
+
return k.out;
|
|
476
|
+
if (k.root || (k.out.length === 0 && k.final))
|
|
477
|
+
return k.out;
|
|
478
|
+
if (k.out.length === 0)
|
|
479
|
+
return controlflowRegionsContainStates(k) && !regionsMayTerminate(k) ? k.out : undefined;
|
|
480
|
+
const t0 = k.out[0];
|
|
481
|
+
if (k.out.length === 1 && t0.preemption === 'termination')
|
|
482
|
+
return k.out;
|
|
483
|
+
const imm0 = isImplicitlyImmediate(t0);
|
|
484
|
+
if (k.out.length === 1 && t0.trigger === undefined && imm0 === this.keepsEffects(t0))
|
|
485
|
+
return k.out;
|
|
486
|
+
if (k.out.length > 1 && conditionalPattern(this, t0, k.out[1]))
|
|
487
|
+
return k.out.slice(0, 2);
|
|
488
|
+
return undefined;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* The nodes of a transition, linked from `src`: one assignment per effect (Trigger/Effect), then the target
|
|
492
|
+
* state's first node. `holder` is the state that keeps the transition's effects when Trigger/Effect does not
|
|
493
|
+
* move them.
|
|
494
|
+
*/
|
|
495
|
+
chainFrom(src, slot, tr, t, key, holder) {
|
|
496
|
+
const head = this.chainHead(tr, t, holder, undefined, (n) => this.link(src, slot, n, key));
|
|
497
|
+
if (head.attached)
|
|
498
|
+
return;
|
|
499
|
+
this.link(src, slot, head.node, key, head.target);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Builds the transition's chain and returns its first node (`target`: the state it is, when the transition has
|
|
503
|
+
* no effects). `onFirst` hears about the first effect node before the target is built, so recursion through
|
|
504
|
+
* the target finds the state resolved; `attachFirst` links it (then `attached` is true).
|
|
505
|
+
*/
|
|
506
|
+
chainHead(tr, t, holder, onFirst, attachFirst) {
|
|
507
|
+
if (!tr.effects.length)
|
|
508
|
+
return { node: this.stateNode(tr.target, t), target: tr.target };
|
|
509
|
+
const split = this.te.get(tr);
|
|
510
|
+
if (split) {
|
|
511
|
+
const shared = this.teChains.get(tr);
|
|
512
|
+
if (shared) {
|
|
513
|
+
onFirst?.(shared);
|
|
514
|
+
attachFirst?.(shared);
|
|
515
|
+
return { node: shared, attached: !!attachFirst };
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
let first;
|
|
519
|
+
let last;
|
|
520
|
+
let lastKey = holder;
|
|
521
|
+
tr.effects.forEach((effect, i) => {
|
|
522
|
+
const own = split ? split.slice(i).find(k => k !== undefined) ?? holder : holder;
|
|
523
|
+
const n = this.node('assign', t, { state: tr.source, transition: tr, effect }, [...own, 0]);
|
|
524
|
+
if (last)
|
|
525
|
+
this.link(last, 'next', n, lastKey);
|
|
526
|
+
if (!first) {
|
|
527
|
+
first = n;
|
|
528
|
+
if (split)
|
|
529
|
+
this.teChains.set(tr, n);
|
|
530
|
+
onFirst?.(n);
|
|
531
|
+
attachFirst?.(n);
|
|
532
|
+
}
|
|
533
|
+
last = n;
|
|
534
|
+
if (weightOf(effect, this.ttsCount) > 0)
|
|
535
|
+
lastKey = split ? [...(split[i] ?? holder), 0] : [...holder, 0];
|
|
536
|
+
});
|
|
537
|
+
this.link(last, 'next', this.stateNode(tr.target, t), lastKey, tr.target);
|
|
538
|
+
return { node: first, attached: !!attachFirst };
|
|
539
|
+
}
|
|
540
|
+
/** Converts the creation keys to ranks (the order of every node's incoming control flows in KIELER). */
|
|
541
|
+
finish() {
|
|
542
|
+
const all = [];
|
|
543
|
+
for (const [n, k] of this.keys) {
|
|
544
|
+
const seq = (n.seq = {});
|
|
545
|
+
for (const slot of ['next', 'then', 'else']) {
|
|
546
|
+
const key = k[slot];
|
|
547
|
+
if (key)
|
|
548
|
+
all.push({ key, set: v => { seq[slot] = v; } });
|
|
549
|
+
}
|
|
550
|
+
if (k.threads) {
|
|
551
|
+
seq.threads = [];
|
|
552
|
+
k.threads.forEach((key, i) => all.push({ key, set: v => { seq.threads[i] = v; } }));
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
all.sort((a, b) => compareKeys(a.key, b.key));
|
|
556
|
+
all.forEach((x, i) => x.set(i));
|
|
557
|
+
const order = [...this.nodes].sort((a, b) => compareKeys(this.genKeys.get(a) ?? [], this.genKeys.get(b) ?? []));
|
|
558
|
+
order.forEach((n, i) => { n.gen = i; });
|
|
559
|
+
// Threads in KIELER's node order (entries are created region by region).
|
|
560
|
+
const ranked = [...this.threads].sort((a, b) => compareKeys(a.region ? this.rpos.get(a.region) ?? [] : [], b.region ? this.rpos.get(b.region) ?? [] : []));
|
|
561
|
+
ranked.forEach((th, i) => { th.rank = i; });
|
|
562
|
+
return all.length;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
/** A referenced chart's transition with no KIELER effects: only its take mark (index -1, not counted, scg.ts weightOf). */
|
|
566
|
+
function silentTake(t) {
|
|
567
|
+
return t.effects.length > 0 && t.effects.every(e => e.k === 'mark' && e.m.kind === 'take' && e.m.index < 0);
|
|
568
|
+
}
|
|
569
|
+
/** After Trigger/Effect: does the transition keep its effects (immediate, untriggered, not a termination, one effect)? */
|
|
570
|
+
function effectless(b, t) {
|
|
571
|
+
return !b.keepsEffects(t);
|
|
572
|
+
}
|
|
573
|
+
/** SurfaceDepth's "conditional" shape: an immediate triggered transition, then an immediate default, both without effects. */
|
|
574
|
+
function conditionalPattern(b, t0, t1) {
|
|
575
|
+
return isImplicitlyImmediate(t0) && t0.trigger !== undefined && effectless(b, t0) &&
|
|
576
|
+
isImplicitlyImmediate(t1) && t1.trigger === undefined && effectless(b, t1);
|
|
577
|
+
}
|
|
578
|
+
/** `ttsCount`: the number of transitions Taken Transition Signaling counts (its reset is one effect each). */
|
|
579
|
+
export function buildSCG(root, ttsCount = 1) {
|
|
580
|
+
const b = new Builder(root, ttsCount);
|
|
581
|
+
// Surface/Depth runs on every state (reachable or not) and recognises an already normalised conditional
|
|
582
|
+
// (an immediate triggered transition, then an immediate default): it deletes the transitions after the second
|
|
583
|
+
// by index while the list shrinks, so with four or more transitions KIELER throws (SurfaceDepth.xtend).
|
|
584
|
+
for (const s of allStates(root)) {
|
|
585
|
+
if (s.root || controlflowRegionsContainStates(s) || s.out.length < 4)
|
|
586
|
+
continue;
|
|
587
|
+
if (conditionalPattern(b, s.out[0], s.out[1])) {
|
|
588
|
+
b.problems.push({
|
|
589
|
+
message: 'conditional-pattern', states: [s],
|
|
590
|
+
kieler: 'org.eclipse.emf.common.util.BasicEList$BasicIndexOutOfBoundsException: index=3, size=3 (SurfaceDepth)',
|
|
591
|
+
});
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
const rootThread = b.thread(undefined, undefined, undefined);
|
|
596
|
+
const first = b.stateNode(root, rootThread);
|
|
597
|
+
rootThread.entry.next = first;
|
|
598
|
+
// KIELER's netlist gives a conditional whose two branches lead to the same node the condition on both paths
|
|
599
|
+
// (the guard becomes `(g && c) || (g && c)`): the else branch never passes control. This arises when a
|
|
600
|
+
// generated state has a triggered and an untriggered transition to the same target without effects (the
|
|
601
|
+
// watcher region of a superstate with several delayed strong aborts, the last without trigger).
|
|
602
|
+
// Marks that are no KIELER effects (a referenced chart's transitions carry such take marks) are no nodes there.
|
|
603
|
+
const silent = (x) => x?.kind === 'assign' && x.effect?.k === 'mark' && weightOf(x.effect, ttsCount) === 0 && x.next !== undefined;
|
|
604
|
+
const through = (x) => {
|
|
605
|
+
const seen = new Set();
|
|
606
|
+
while (silent(x) && !seen.has(x)) {
|
|
607
|
+
seen.add(x);
|
|
608
|
+
x = x.next;
|
|
609
|
+
}
|
|
610
|
+
return x;
|
|
611
|
+
};
|
|
612
|
+
for (const n of b.nodes) {
|
|
613
|
+
if (n.kind !== 'cond' || n.then === undefined || n.else === undefined)
|
|
614
|
+
continue;
|
|
615
|
+
const target = through(n.then);
|
|
616
|
+
if (target !== through(n.else))
|
|
617
|
+
continue;
|
|
618
|
+
n.else = undefined;
|
|
619
|
+
n.sameElse = true;
|
|
620
|
+
if (target !== n.then)
|
|
621
|
+
n.sameTarget = target;
|
|
622
|
+
b.problems.push({ message: 'same-branches', states: n.state ? [n.state] : [] });
|
|
623
|
+
}
|
|
624
|
+
const count = b.finish();
|
|
625
|
+
// The root entry's flow is created last.
|
|
626
|
+
rootThread.entry.seq = { next: count };
|
|
627
|
+
return { nodes: b.nodes, threads: b.threads, root: rootThread, stateNodes: b.stateNodes, problems: b.problems, seq: count + 1, silentHops: b.silentHops };
|
|
628
|
+
}
|
|
629
|
+
/** Successors of a node within a tick (a surface ends the tick; its depth is reached in the next one). */
|
|
630
|
+
export function successors(n) {
|
|
631
|
+
switch (n.kind) {
|
|
632
|
+
case 'cond': return [n.then, n.else].filter((x) => x !== undefined);
|
|
633
|
+
case 'fork': return n.threads.map(t => t.entry);
|
|
634
|
+
case 'surface': return [];
|
|
635
|
+
default: return n.next ? [n.next] : [];
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
/** Successors including the surface -> depth step (for reachability). */
|
|
639
|
+
export function allSuccessors(n) {
|
|
640
|
+
return n.kind === 'surface' ? [n.depth] : successors(n);
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Control-flow predecessors in the order of KIELER's incoming control flows (a depth has none from its surface).
|
|
644
|
+
* Flows without a rank (should not happen) come last.
|
|
645
|
+
*/
|
|
646
|
+
export function predecessors(scg) {
|
|
647
|
+
const lists = new Map();
|
|
648
|
+
const add = (from, to, seq) => {
|
|
649
|
+
let l = lists.get(to);
|
|
650
|
+
if (!l)
|
|
651
|
+
lists.set(to, (l = []));
|
|
652
|
+
l.push({ from, seq: seq ?? Infinity });
|
|
653
|
+
};
|
|
654
|
+
for (const n of scg.nodes) {
|
|
655
|
+
const seq = n.seq;
|
|
656
|
+
switch (n.kind) {
|
|
657
|
+
case 'cond':
|
|
658
|
+
if (n.then)
|
|
659
|
+
add(n, n.then, seq?.then);
|
|
660
|
+
if (n.else)
|
|
661
|
+
add(n, n.else, seq?.else);
|
|
662
|
+
break;
|
|
663
|
+
case 'fork':
|
|
664
|
+
n.threads.forEach((t, i) => add(n, t.entry, seq?.threads?.[i]));
|
|
665
|
+
break;
|
|
666
|
+
case 'surface':
|
|
667
|
+
break;
|
|
668
|
+
default:
|
|
669
|
+
if (n.next)
|
|
670
|
+
add(n, n.next, seq?.next);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
const m = new Map();
|
|
674
|
+
for (const [to, l] of lists) {
|
|
675
|
+
if (l.length > 1)
|
|
676
|
+
l.sort((a, b) => a.seq - b.seq);
|
|
677
|
+
m.set(to, l.map(x => x.from));
|
|
678
|
+
}
|
|
679
|
+
return m;
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* A copy of the graph (nodes, threads, state mapping) that analyses can change independently; `map` takes an
|
|
683
|
+
* original node to its copy.
|
|
684
|
+
*/
|
|
685
|
+
export function cloneSCG(scg) {
|
|
686
|
+
const map = new Map();
|
|
687
|
+
const tmap = new Map();
|
|
688
|
+
for (const n of scg.nodes)
|
|
689
|
+
map.set(n, { ...n, seq: n.seq ? { ...n.seq, threads: n.seq.threads ? [...n.seq.threads] : undefined } : undefined });
|
|
690
|
+
for (const t of scg.threads)
|
|
691
|
+
tmap.set(t, { ...t });
|
|
692
|
+
const m = (n) => (n ? map.get(n) ?? n : n);
|
|
693
|
+
const mt = (t) => (t ? tmap.get(t) ?? t : t);
|
|
694
|
+
for (const c of map.values()) {
|
|
695
|
+
c.thread = mt(c.thread);
|
|
696
|
+
c.next = m(c.next);
|
|
697
|
+
c.then = m(c.then);
|
|
698
|
+
c.else = m(c.else);
|
|
699
|
+
c.join = m(c.join);
|
|
700
|
+
c.fork = m(c.fork);
|
|
701
|
+
c.depth = m(c.depth);
|
|
702
|
+
c.surface = m(c.surface);
|
|
703
|
+
c.schizo = m(c.schizo);
|
|
704
|
+
c.copyOf = m(c.copyOf);
|
|
705
|
+
c.sameTarget = m(c.sameTarget);
|
|
706
|
+
if (c.threads)
|
|
707
|
+
c.threads = c.threads.map(t => mt(t));
|
|
708
|
+
}
|
|
709
|
+
for (const c of tmap.values()) {
|
|
710
|
+
c.entry = m(c.entry);
|
|
711
|
+
c.exit = m(c.exit);
|
|
712
|
+
c.fork = m(c.fork);
|
|
713
|
+
c.parent = mt(c.parent);
|
|
714
|
+
}
|
|
715
|
+
const stateNodes = new Map();
|
|
716
|
+
for (const [st, n] of scg.stateNodes)
|
|
717
|
+
stateNodes.set(st, m(n));
|
|
718
|
+
return {
|
|
719
|
+
scg: { nodes: scg.nodes.map(n => map.get(n)), threads: scg.threads.map(t => tmap.get(t)), root: tmap.get(scg.root), stateNodes, problems: scg.problems, seq: scg.seq, silentHops: scg.silentHops },
|
|
720
|
+
map,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
//# sourceMappingURL=scg.js.map
|