state-machine-cat 12.0.19 → 12.0.20

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.
@@ -0,0 +1,9 @@
1
+ export class Counter {
2
+ _lHWM = 0;
3
+ constructor(pStart = 0) {
4
+ this._lHWM = pStart;
5
+ }
6
+ next() {
7
+ return ++this._lHWM;
8
+ }
9
+ }
@@ -1,6 +1,6 @@
1
1
  import Ajv from "ajv";
2
2
  import options from "../options.mjs";
3
- import { parse as parseSmCat } from "./smcat/smcat-parser.mjs";
3
+ import { parse as parseSmCat } from "./smcat/parse.mjs";
4
4
  import { parse as parseSCXML } from "./scxml/index.mjs";
5
5
  import $schema from "./smcat-ast.schema.mjs";
6
6
  const ajv = new Ajv();
@@ -2,7 +2,6 @@ import StateMachineModel from "../state-machine-model.mjs";
2
2
  const TRIGGER_RE_AS_A_STRING =
3
3
  "^(entry|activity|exit)\\s*/\\s*([^\\n$]*)(\\n|$)";
4
4
  const TRIGGER_RE = new RegExp(TRIGGER_RE_AS_A_STRING);
5
- let gTransitionIdHwm = 0;
6
5
  function stateExists(pKnownStateNames, pName) {
7
6
  return pKnownStateNames.includes(pName);
8
7
  }
@@ -181,12 +180,6 @@ function extractActions(pString) {
181
180
  .map((pActivityCandidate) => pActivityCandidate.trim())
182
181
  .map(extractAction);
183
182
  }
184
- function nextTransitionId() {
185
- return ++gTransitionIdHwm;
186
- }
187
- function resetTransitionId() {
188
- gTransitionIdHwm = 0;
189
- }
190
183
  export default {
191
184
  initState,
192
185
  extractUndeclaredStates,
@@ -198,6 +191,4 @@ export default {
198
191
  extractActions,
199
192
  setIf,
200
193
  setIfNotEmpty,
201
- nextTransitionId,
202
- resetTransitionId,
203
194
  };
@@ -1,8 +1,9 @@
1
1
  import fastxml from "fast-xml-parser";
2
2
  import he from "he";
3
3
  import traverse from "neotraverse";
4
- import utl from "../../transform/utl.mjs";
4
+ import { Counter } from "../../counter.mjs";
5
5
  import parserHelpers from "../parser-helpers.mjs";
6
+ import utl from "../../transform/utl.mjs";
6
7
  import { castArray } from "./utl.mjs";
7
8
  import { normalizeMachine } from "./normalize-machine.mjs";
8
9
  const formatLabel = utl.formatLabel;
@@ -97,12 +98,13 @@ function extractTransitionAttributes(pTransition) {
97
98
  }
98
99
  return lReturnValue;
99
100
  }
100
- function reduceTransition(pState) {
101
+ function reduceTransition(pState, pCounter) {
101
102
  return (pAllTransitions, pTransition) => {
102
103
  const lTargets = (pTransition?.target ?? pState.id).split(/\s+/);
103
104
  const lTransitionAttributes = extractTransitionAttributes(pTransition);
104
105
  return pAllTransitions.concat(
105
106
  lTargets.map((pTarget) => ({
107
+ id: pCounter.next(),
106
108
  from: pState.id,
107
109
  to: pTarget,
108
110
  ...lTransitionAttributes,
@@ -110,13 +112,13 @@ function reduceTransition(pState) {
110
112
  );
111
113
  };
112
114
  }
113
- function extractTransitions(pStates) {
115
+ function extractTransitions(pStates, pCounter) {
114
116
  return pStates
115
117
  .filter((pState) => Object.hasOwn(pState, "transition"))
116
118
  .reduce((pAllTransitions, pThisState) => {
117
119
  const lTransitionAsArray = castArray(pThisState.transition);
118
120
  return pAllTransitions.concat(
119
- lTransitionAsArray.reduce(reduceTransition(pThisState), []),
121
+ lTransitionAsArray.reduce(reduceTransition(pThisState, pCounter), []),
120
122
  );
121
123
  }, []);
122
124
  }
@@ -130,9 +132,10 @@ function mapMachine(pSCXMLStateMachine) {
130
132
  .concat(lNormalizedMachine.history.map(mapState("history")))
131
133
  .concat(lNormalizedMachine.final.map(mapState("final"))),
132
134
  };
133
- const lTransitions = extractTransitions(lNormalizedMachine.initial)
134
- .concat(extractTransitions(lNormalizedMachine.state))
135
- .concat(extractTransitions(lNormalizedMachine.parallel));
135
+ const lCounter = new Counter();
136
+ const lTransitions = extractTransitions(lNormalizedMachine.initial, lCounter)
137
+ .concat(extractTransitions(lNormalizedMachine.state, lCounter))
138
+ .concat(extractTransitions(lNormalizedMachine.parallel, lCounter));
136
139
  if (lTransitions.length > 0) {
137
140
  lReturnValue.transitions = lTransitions;
138
141
  }
@@ -0,0 +1,5 @@
1
+ import { Counter } from "../../counter.mjs";
2
+ import { parse as pegParse } from "./smcat-parser.mjs";
3
+ export function parse(pScript) {
4
+ return pegParse(pScript, { counter: new Counter() });
5
+ }
@@ -321,7 +321,6 @@ function peg$parse(input, options) {
321
321
  var peg$e78 = peg$classExpectation(["\r", "\n"], true, false);
322
322
  var peg$e79 = peg$otherExpectation("comment");
323
323
  var peg$f0 = function (statemachine) {
324
- parserHelpers.resetTransitionId();
325
324
  statemachine.states = parserHelpers.extractUndeclaredStates(statemachine);
326
325
  return parserHelpers.classifyForkJoins(statemachine);
327
326
  };
@@ -431,7 +430,7 @@ function peg$parse(input, options) {
431
430
  ),
432
431
  );
433
432
  parserHelpers.setIfNotEmpty(trans, "note", notes);
434
- trans.id = parserHelpers.nextTransitionId();
433
+ trans.id = options.counter.next();
435
434
  return trans;
436
435
  };
437
436
  var peg$f20 = function (from_, to) {
@@ -122,7 +122,7 @@ export default {
122
122
  type: "array",
123
123
  items: {
124
124
  type: "object",
125
- required: ["from", "to"],
125
+ required: ["id", "from", "to"],
126
126
  additionalProperties: false,
127
127
  properties: {
128
128
  id: {
@@ -213,25 +213,32 @@ function transition(pTransition, pIndent, pOptions, pModel) {
213
213
  ? ` lhead="cluster_${pTransition.to}"`
214
214
  : "";
215
215
  const lTransitionName = `tr_${pTransition.from}_${pTransition.to}_${pTransition.id}`;
216
- if (pTransition.note) {
217
- const lNoteName = `note_${lTransitionName}`;
218
- const lNoteNodeName = `i_${lNoteName}`;
219
- const lNoteNode = `\n${pIndent} "${lNoteNodeName}" [shape=point style=invis margin=0 width=0 height=0 fixedsize=true]`;
220
- const lTransitionFrom = `\n${pIndent} "${pTransition.from}" -> "${lNoteNodeName}" [arrowhead=none${lTail}${lColorAttribute}]`;
221
- const lTransitionTo = `\n${pIndent} "${lNoteNodeName}" -> "${pTransition.to}" [label="${lLabel}"${lHead}${lColorAttribute}${lFontColorAttribute}]`;
222
- const lLineToNote = `\n${pIndent} "${lNoteNodeName}" -> "${lNoteName}" [style=dashed arrowtail=none arrowhead=none weight=0]`;
223
- const lNote = `\n${pIndent} "${lNoteName}" [label="${noteToLabel(pTransition.note)}" shape=note fontsize=10 color=black fontcolor=black fillcolor="#ffffcc" penwidth=1.0]`;
224
- return lNoteNode + lTransitionFrom + lTransitionTo + lLineToNote + lNote;
225
- }
226
216
  if (isCompositeSelf(pModel, pTransition)) {
227
217
  const { lTailPorts, lHeadPorts } = getTransitionPorts(
228
218
  pOptions,
229
219
  pModel,
230
220
  pTransition,
231
221
  );
232
- const lTransitionFrom = `\n${pIndent} "${pTransition.from}" -> "self_tr_${pTransition.from}_${pTransition.to}_${pTransition.id}" [label="${lLabel}" arrowhead=none class="${lClass}"${lTailPorts}${lTail}${lColorAttribute}${lFontColorAttribute}]`;
222
+ let lNoteAndLine = "";
223
+ if (pTransition.note) {
224
+ const lNoteName = `note_${lTransitionName}`;
225
+ const lLineToNote = `\n${pIndent} "${lNoteName}" -> "self_tr_${pTransition.from}_${pTransition.to}_${pTransition.id}" [style=dashed arrowtail=none arrowhead=none weight=0]`;
226
+ const lNote = `\n${pIndent} "${lNoteName}" [label="${noteToLabel(pTransition.note)}" shape=note fontsize=10 color=black fontcolor=black fillcolor="#ffffcc" penwidth=1.0]`;
227
+ lNoteAndLine = lLineToNote + lNote;
228
+ }
229
+ const lTransitionFrom = `\n${pIndent} "${pTransition.from}" -> "self_tr_${pTransition.from}_${pTransition.to}_${pTransition.id}" [label="${lLabel}" arrowhead=none class="${lClass}"${lTailPorts}${lTail}${lColorAttribute}${lFontColorAttribute}${lPenWidth}]`;
233
230
  const lTransitionTo = `\n${pIndent} "self_tr_${pTransition.from}_${pTransition.to}_${pTransition.id}" -> "${pTransition.to}" [class="${lClass}"${lHead}${lHeadPorts}${lColorAttribute}${lPenWidth}]`;
234
- return lTransitionFrom + lTransitionTo;
231
+ return lTransitionFrom + lTransitionTo + lNoteAndLine;
232
+ }
233
+ if (pTransition.note) {
234
+ const lNoteName = `note_${lTransitionName}`;
235
+ const lNoteNodeName = `i_${lNoteName}`;
236
+ const lNoteNode = `\n${pIndent} "${lNoteNodeName}" [shape=point style=invis margin=0 width=0 height=0 fixedsize=true]`;
237
+ const lTransitionFrom = `\n${pIndent} "${pTransition.from}" -> "${lNoteNodeName}" [arrowhead=none${lTail}${lColorAttribute}${lPenWidth}]`;
238
+ const lTransitionTo = `\n${pIndent} "${lNoteNodeName}" -> "${pTransition.to}" [label="${lLabel}"${lHead}${lColorAttribute}${lFontColorAttribute}${lPenWidth}]`;
239
+ const lLineToNote = `\n${pIndent} "${lNoteNodeName}" -> "${lNoteName}" [style=dashed arrowtail=none arrowhead=none weight=0]`;
240
+ const lNote = `\n${pIndent} "${lNoteName}" [label="${noteToLabel(pTransition.note)}" shape=note fontsize=10 color=black fontcolor=black fillcolor="#ffffcc" penwidth=1.0]`;
241
+ return lNoteNode + lTransitionFrom + lTransitionTo + lLineToNote + lNote;
235
242
  }
236
243
  return `\n${pIndent} "${pTransition.from}" -> "${pTransition.to}" [label="${lLabel}" class="${lClass}"${lTail}${lHead}${lColorAttribute}${lFontColorAttribute}${lPenWidth}]`;
237
244
  }
@@ -68,6 +68,9 @@ export default class StateMachineModel {
68
68
  (pTransition) => pTransition.to === pToStateName,
69
69
  );
70
70
  }
71
+ getMaximumTransitionId() {
72
+ return Math.max(...this._flattenedTransitions.map(({ id }) => id));
73
+ }
71
74
  findTransitionsToSiblings(pStateName, pExcludeIds) {
72
75
  return this._flattenedTransitions.filter(
73
76
  (pTransition) =>
@@ -1,16 +1,22 @@
1
1
  import StateMachineModel from "../state-machine-model.mjs";
2
+ import { Counter } from "../counter.mjs";
2
3
  import utl from "./utl.mjs";
3
4
  function fuseTransitionAttribute(pIncomingThing, pOutgoingThing, pJoinChar) {
4
5
  return pIncomingThing
5
6
  ? `${pIncomingThing}${pJoinChar}${pOutgoingThing}`
6
7
  : pOutgoingThing;
7
8
  }
8
- function fuseIncomingToOutgoing(pIncomingTransition, pOutgoingTransition) {
9
+ function fuseIncomingToOutgoing(
10
+ pIncomingTransition,
11
+ pOutgoingTransition,
12
+ pCounter,
13
+ ) {
9
14
  const lReturnValue = {
10
15
  ...pIncomingTransition,
11
16
  ...pOutgoingTransition,
12
17
  from: pIncomingTransition.from,
13
18
  to: pOutgoingTransition.to,
19
+ id: pCounter.next(),
14
20
  };
15
21
  if (pOutgoingTransition.action) {
16
22
  lReturnValue.action = fuseTransitionAttribute(
@@ -32,13 +38,14 @@ function fuseTransitions(
32
38
  pTransitions,
33
39
  pPseudoStateNames,
34
40
  pOutgoingTransitionMap,
41
+ pCounter,
35
42
  ) {
36
43
  return pTransitions.reduce((pAll, pTransition) => {
37
44
  pPseudoStateNames.forEach((pStateName, pIndex) => {
38
45
  if (pStateName === pTransition.to && pOutgoingTransitionMap[pStateName]) {
39
46
  pAll = pAll.concat(
40
47
  pOutgoingTransitionMap[pStateName].map((pOutgoingTransition) =>
41
- fuseIncomingToOutgoing(pTransition, pOutgoingTransition),
48
+ fuseIncomingToOutgoing(pTransition, pOutgoingTransition, pCounter),
42
49
  ),
43
50
  );
44
51
  } else {
@@ -52,6 +59,7 @@ function deSugarPseudoStates(
52
59
  pMachine,
53
60
  pPseudoStateNames,
54
61
  pOutgoingTransitionMap,
62
+ pCounter,
55
63
  ) {
56
64
  const lMachine = structuredClone(pMachine);
57
65
  if (lMachine.transitions && pPseudoStateNames.length > 0) {
@@ -59,6 +67,7 @@ function deSugarPseudoStates(
59
67
  lMachine.transitions,
60
68
  pPseudoStateNames,
61
69
  pOutgoingTransitionMap,
70
+ pCounter,
62
71
  );
63
72
  }
64
73
  lMachine.states = lMachine.states.map((pState) =>
@@ -69,6 +78,7 @@ function deSugarPseudoStates(
69
78
  pState.statemachine,
70
79
  pPseudoStateNames,
71
80
  pOutgoingTransitionMap,
81
+ pCounter,
72
82
  ),
73
83
  }
74
84
  : pState,
@@ -117,10 +127,12 @@ export default (
117
127
  },
118
128
  {},
119
129
  );
130
+ const lMaximumTransitionId = lModel.getMaximumTransitionId();
120
131
  const lMachine = deSugarPseudoStates(
121
132
  pMachine,
122
133
  lPseudoStateNames,
123
134
  lOutgoingTransitionMap,
135
+ new Counter(lMaximumTransitionId),
124
136
  );
125
137
  return removeStatesCascading(lMachine, lPseudoStateNames);
126
138
  };
package/dist/version.mjs CHANGED
@@ -1 +1 @@
1
- export const version = "12.0.19";
1
+ export const version = "12.0.20";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-machine-cat",
3
- "version": "12.0.19",
3
+ "version": "12.0.20",
4
4
  "description": "write beautiful state charts",
5
5
  "main": "./dist/index.mjs",
6
6
  "module": "./dist/index.mjs",
@@ -83,6 +83,10 @@ export interface IState {
83
83
  }
84
84
 
85
85
  export interface ITransition {
86
+ /**
87
+ * The id of the transition. Unique within the root state machine.
88
+ */
89
+ id: number;
86
90
  /**
87
91
  * The name of the IState the transition is from
88
92
  */
@@ -91,10 +95,6 @@ export interface ITransition {
91
95
  * The name of the IState the transition is to
92
96
  */
93
97
  to: string;
94
- /**
95
- * The id of the transition. Unique within the root state machine.
96
- */
97
- id: number;
98
98
  /**
99
99
  * A display label to represent this transition. Parsers can parse this
100
100
  * label into events conditions and actions.