@emaarco/bpmn-js-data-simulation 0.6.0 → 0.7.1

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,23 +2468,54 @@ 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 RELATIONAL = String.raw`(?:<=|>=|<|>|\b(?:lt|gt|le|ge)\b)`;
2473
+ const EQUALITY = String.raw`(?:==|!=|=|\b(?:eq|ne)\b)`;
2474
+ const BOOLEAN_LEFT = String.raw`(?:^|(?<![\w$.])\(|\bnot\s*\(|!|&&|\|\||\b(?:and|or|not)\b)`;
2475
+ const BOOLEAN_RIGHT = String.raw`(?=$|\)|&&|\|\||\b(?:and|or)\b)`;
2468
2476
  class PathVariableDeriver {
2469
2477
  derive(conditions, values, rangeOverrides = {}) {
2470
2478
  const names = [...new Set(conditions.flatMap((condition) => condition.variableNames))];
2471
2479
  return names.map((name) => {
2472
2480
  const conditionsReadingName = conditions.filter((condition) => condition.variableNames.includes(name));
2473
- return this.variableFor(name, values[name], conditionsReadingName, rangeOverrides[name] ?? {});
2481
+ return this.variableFor(name, values, conditionsReadingName, rangeOverrides[name] ?? {});
2474
2482
  });
2475
2483
  }
2476
- variableFor(name, value, conditionsReadingName, rangeOverride) {
2477
- if (typeof value === "boolean") return { name, type: "boolean" };
2478
- if (typeof value === "number") {
2479
- const comparedNumbers = conditionsReadingName.flatMap((condition) => this.numberLiterals(condition.expression));
2480
- return { name, type: "number", range: this.rangeAround(value, comparedNumbers, rangeOverride) };
2484
+ variableFor(name, values, conditionsReadingName, rangeOverride) {
2485
+ const value = values[name];
2486
+ const type = value === void 0 ? this.typeFromConditions(name, conditionsReadingName) : typeof value;
2487
+ if (type === "boolean") return { name, type: "boolean" };
2488
+ if (type === "number") {
2489
+ const comparedNumbers = conditionsReadingName.flatMap((condition) => [
2490
+ ...this.numberLiterals(condition.expression),
2491
+ ...this.numbersOfOtherVariables(name, condition, values)
2492
+ ]);
2493
+ return { name, type: "number", range: this.rangeAround(Number(value ?? 0), comparedNumbers, rangeOverride) };
2481
2494
  }
2482
2495
  if (value === void 0 || typeof value === "string") return { name, type: "text" };
2483
2496
  return { name, type: "json" };
2484
2497
  }
2498
+ // Without a value the conditions tell the type: compared with a number or by <, <=, > or >=, or used as a condition of its own.
2499
+ typeFromConditions(name, conditions) {
2500
+ const variable = String.raw`(?<![\w.$])${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![\w.(])`;
2501
+ const comparedWithNumber = new RegExp(
2502
+ String.raw`${variable}\s*${COMPARISON}\s*-?\d|\d\s*${COMPARISON}\s*${variable}|${variable}\s*${RELATIONAL}|${RELATIONAL}\s*${variable}`
2503
+ );
2504
+ const usedAsBoolean = new RegExp(
2505
+ String.raw`${BOOLEAN_LEFT}\s*${variable}\s*${BOOLEAN_RIGHT}|${variable}\s*${EQUALITY}\s*(?:true|false)\b|\b(?:true|false)\s*${EQUALITY}\s*${variable}`
2506
+ );
2507
+ const bodies = conditions.map((condition) => this.body(condition.expression));
2508
+ if (bodies.some((body) => comparedWithNumber.test(body))) return "number";
2509
+ if (bodies.some((body) => usedAsBoolean.test(body))) return "boolean";
2510
+ return void 0;
2511
+ }
2512
+ // The expression without its FEEL `=` or its JUEL `${…}`.
2513
+ body(expression) {
2514
+ const trimmed = expression.trim();
2515
+ const juel = /^\$\{([\s\S]*)\}$/.exec(trimmed);
2516
+ if (juel) return juel[1].trim();
2517
+ return trimmed.replace(/^=/, "").trim();
2518
+ }
2485
2519
  // The value itself is not doubled: a slider dragged to the end then keeps its range when derived again.
2486
2520
  rangeAround(value, comparedNumbers, override) {
2487
2521
  const min = override.min ?? 0;
@@ -2493,6 +2527,10 @@ class PathVariableDeriver {
2493
2527
  const magnitude = 10 ** Math.floor(Math.log10(value));
2494
2528
  return NICE_STEPS.find((step) => step * magnitude >= value) * magnitude;
2495
2529
  }
2530
+ // The numbers the other variables of the condition hold, e.g. a minimum score the variable is compared with.
2531
+ numbersOfOtherVariables(name, condition, values) {
2532
+ return condition.variableNames.filter((otherName) => otherName !== name).map((otherName) => values[otherName]).filter((otherValue) => typeof otherValue === "number");
2533
+ }
2496
2534
  numberLiterals(expression) {
2497
2535
  return (expression.match(NUMBER_LITERAL) ?? []).map(Number);
2498
2536
  }
@@ -2565,7 +2603,7 @@ class PathMarkers {
2565
2603
  for (const id of path.taken) this.mark(id, TAKEN_MARKER);
2566
2604
  for (const id of path.skipped) this.mark(id, SKIPPED_MARKER);
2567
2605
  for (const [gatewayId, decision] of Object.entries(path.decisions)) {
2568
- if (decision.stuckReason) this.mark(gatewayId, STUCK_MARKER);
2606
+ if (decision.stuckReason && !decision.missingVariables) this.mark(gatewayId, STUCK_MARKER);
2569
2607
  }
2570
2608
  }
2571
2609
  clear() {
@@ -2617,7 +2655,7 @@ class PathSimulation {
2617
2655
  }
2618
2656
  simulate(variables, options = {}) {
2619
2657
  const graph = this.pathGraphReader.read(options.measures ?? []);
2620
- const path = this.pathResolver.resolve(graph, (expression) => this.engine.evaluate(expression, variables));
2658
+ const path = this.pathResolver.resolve(graph, (expression) => this.evaluate(expression, variables));
2621
2659
  this.pathMarkers.show(path);
2622
2660
  return path;
2623
2661
  }
@@ -2630,6 +2668,13 @@ class PathSimulation {
2630
2668
  clear() {
2631
2669
  this.pathMarkers.clear();
2632
2670
  }
2671
+ // A failed condition names the variables it reads that are not set: the gateway then waits for data.
2672
+ evaluate(expression, variables) {
2673
+ const evaluation = this.engine.evaluate(expression, variables);
2674
+ if (evaluation.ok) return evaluation;
2675
+ const missingVariables = this.engine.readVariableNames(expression).filter((name) => variables[name] === void 0);
2676
+ return missingVariables.length ? { ...evaluation, missingVariables } : evaluation;
2677
+ }
2633
2678
  evaluableConditions() {
2634
2679
  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
2680
  }