@emaarco/bpmn-js-data-simulation 0.6.0 → 0.7.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/dist/index.js CHANGED
@@ -11,11 +11,13 @@ class GatewayRouter {
11
11
  }
12
12
  const failed = evaluations.find((evaluation) => evaluation.outcome === "error");
13
13
  if (failed) {
14
- return this.stuck(
14
+ const decision = this.stuck(
15
15
  evaluations,
16
16
  defaultFlow,
17
17
  `Condition of "${failed.flow.name ?? failed.flow.id}" failed: ${failed.reason}`
18
18
  );
19
+ const missingVariables = [...new Set(evaluations.flatMap((evaluation) => evaluation.missingVariables ?? []))];
20
+ return missingVariables.length ? { ...decision, missingVariables } : decision;
19
21
  }
20
22
  const taken = evaluations.filter((evaluation) => evaluation.outcome === "taken");
21
23
  if (taken.length) {
@@ -45,7 +47,8 @@ class GatewayRouter {
45
47
  }
46
48
  const evaluation = evaluateCondition(flow.expression);
47
49
  if (!evaluation.ok) {
48
- return { flow, outcome: "error", reason: evaluation.reason };
50
+ const { reason, missingVariables } = evaluation;
51
+ return missingVariables ? { flow, outcome: "error", reason, missingVariables } : { flow, outcome: "error", reason };
49
52
  }
50
53
  if (typeof evaluation.value !== "boolean") {
51
54
  return {
@@ -2465,6 +2468,10 @@ class PathResolver {
2465
2468
  const NICE_STEPS = [1, 2, 5, 10];
2466
2469
  const RANGE_STEPS = 100;
2467
2470
  const NUMBER_LITERAL = /(?<![\w.])\d+(?:\.\d+)?/g;
2471
+ const COMPARISON = String.raw`(?:<=|>=|==|!=|<|>|=|\b(?:lt|gt|le|ge|eq|ne)\b)`;
2472
+ const EQUALITY = String.raw`(?:==|!=|=|\b(?:eq|ne)\b)`;
2473
+ const BOOLEAN_LEFT = String.raw`(?:^|(?<![\w$.])\(|\bnot\s*\(|!|&&|\|\||\b(?:and|or|not)\b)`;
2474
+ const BOOLEAN_RIGHT = String.raw`(?=$|\)|&&|\|\||\b(?:and|or)\b)`;
2468
2475
  class PathVariableDeriver {
2469
2476
  derive(conditions, values, rangeOverrides = {}) {
2470
2477
  const names = [...new Set(conditions.flatMap((condition) => condition.variableNames))];
@@ -2474,14 +2481,36 @@ class PathVariableDeriver {
2474
2481
  });
2475
2482
  }
2476
2483
  variableFor(name, value, conditionsReadingName, rangeOverride) {
2477
- if (typeof value === "boolean") return { name, type: "boolean" };
2478
- if (typeof value === "number") {
2484
+ const type = value === void 0 ? this.typeFromConditions(name, conditionsReadingName) : typeof value;
2485
+ if (type === "boolean") return { name, type: "boolean" };
2486
+ if (type === "number") {
2479
2487
  const comparedNumbers = conditionsReadingName.flatMap((condition) => this.numberLiterals(condition.expression));
2480
- return { name, type: "number", range: this.rangeAround(value, comparedNumbers, rangeOverride) };
2488
+ return { name, type: "number", range: this.rangeAround(Number(value ?? 0), comparedNumbers, rangeOverride) };
2481
2489
  }
2482
2490
  if (value === void 0 || typeof value === "string") return { name, type: "text" };
2483
2491
  return { name, type: "json" };
2484
2492
  }
2493
+ // Without a value the conditions tell the type: compared with a number, or used as a condition of its own.
2494
+ typeFromConditions(name, conditions) {
2495
+ const variable = String.raw`(?<![\w.$])${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![\w.(])`;
2496
+ const comparedWithNumber = new RegExp(
2497
+ String.raw`${variable}\s*${COMPARISON}\s*-?\d|\d\s*${COMPARISON}\s*${variable}`
2498
+ );
2499
+ const usedAsBoolean = new RegExp(
2500
+ String.raw`${BOOLEAN_LEFT}\s*${variable}\s*${BOOLEAN_RIGHT}|${variable}\s*${EQUALITY}\s*(?:true|false)\b|\b(?:true|false)\s*${EQUALITY}\s*${variable}`
2501
+ );
2502
+ const bodies = conditions.map((condition) => this.body(condition.expression));
2503
+ if (bodies.some((body) => comparedWithNumber.test(body))) return "number";
2504
+ if (bodies.some((body) => usedAsBoolean.test(body))) return "boolean";
2505
+ return void 0;
2506
+ }
2507
+ // The expression without its FEEL `=` or its JUEL `${…}`.
2508
+ body(expression) {
2509
+ const trimmed = expression.trim();
2510
+ const juel = /^\$\{([\s\S]*)\}$/.exec(trimmed);
2511
+ if (juel) return juel[1].trim();
2512
+ return trimmed.replace(/^=/, "").trim();
2513
+ }
2485
2514
  // The value itself is not doubled: a slider dragged to the end then keeps its range when derived again.
2486
2515
  rangeAround(value, comparedNumbers, override) {
2487
2516
  const min = override.min ?? 0;
@@ -2565,7 +2594,7 @@ class PathMarkers {
2565
2594
  for (const id of path.taken) this.mark(id, TAKEN_MARKER);
2566
2595
  for (const id of path.skipped) this.mark(id, SKIPPED_MARKER);
2567
2596
  for (const [gatewayId, decision] of Object.entries(path.decisions)) {
2568
- if (decision.stuckReason) this.mark(gatewayId, STUCK_MARKER);
2597
+ if (decision.stuckReason && !decision.missingVariables) this.mark(gatewayId, STUCK_MARKER);
2569
2598
  }
2570
2599
  }
2571
2600
  clear() {
@@ -2617,7 +2646,7 @@ class PathSimulation {
2617
2646
  }
2618
2647
  simulate(variables, options = {}) {
2619
2648
  const graph = this.pathGraphReader.read(options.measures ?? []);
2620
- const path = this.pathResolver.resolve(graph, (expression) => this.engine.evaluate(expression, variables));
2649
+ const path = this.pathResolver.resolve(graph, (expression) => this.evaluate(expression, variables));
2621
2650
  this.pathMarkers.show(path);
2622
2651
  return path;
2623
2652
  }
@@ -2630,6 +2659,13 @@ class PathSimulation {
2630
2659
  clear() {
2631
2660
  this.pathMarkers.clear();
2632
2661
  }
2662
+ // A failed condition names the variables it reads that are not set: the gateway then waits for data.
2663
+ evaluate(expression, variables) {
2664
+ const evaluation = this.engine.evaluate(expression, variables);
2665
+ if (evaluation.ok) return evaluation;
2666
+ const missingVariables = this.engine.readVariableNames(expression).filter((name) => variables[name] === void 0);
2667
+ return missingVariables.length ? { ...evaluation, missingVariables } : evaluation;
2668
+ }
2633
2669
  evaluableConditions() {
2634
2670
  return this.pathGraphReader.read([]).nodes.flatMap((node) => node.outgoing.map((flow) => flow.expression)).filter((expression) => expression !== void 0 && this.engine.canEvaluate(expression)).map((expression) => ({ expression, variableNames: this.engine.readVariableNames(expression) }));
2635
2671
  }