@emaarco/bpmn-js-data-simulation 0.4.0 → 0.5.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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { Evaluation, FlowEvaluation, FlowOutcome, GatewayKind, Mapping, OutgoingFlow, RoutingDecision, Scenario, SimulatedPath, StartField, StartFieldType, Variables, } from './domain/model.js';
1
+ export type { Evaluation, FlowEvaluation, FlowOutcome, GatewayKind, Mapping, NumberRange, OutgoingFlow, PathVariable, RoutingDecision, Scenario, SimulatedPath, StartField, StartFieldType, Variables, } from './domain/model.js';
2
2
  export { GatewayRouter } from './domain/GatewayRouter.js';
3
3
  export { ScenarioError } from './domain/ScenarioError.js';
4
4
  export { ScenarioParser } from './domain/ScenarioParser.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,UAAU,EACV,cAAc,EACd,WAAW,EACX,WAAW,EACX,OAAO,EACP,YAAY,EACZ,eAAe,EACf,QAAQ,EACR,aAAa,EACb,UAAU,EACV,cAAc,EACd,SAAS,GACV,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAA;AAE7D,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAA;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAClE,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAC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AE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAClE,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AACvG,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAA;AAE5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAA;AACzE,YAAY,EACV,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,QAAQ,EACR,oBAAoB,EACpB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,qCAAqC,CAAA;AAE5C,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AACzF,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAA;AAEzE,YAAY,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAA;AAC3F,OAAO,EACL,oBAAoB,EACpB,oBAAoB,IAAI,OAAO,EAC/B,KAAK,+BAA+B,EACpC,oBAAoB,GACrB,MAAM,wBAAwB,CAAA;AAC/B,YAAY,EAAE,cAAc,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAA"}
package/dist/index.js CHANGED
@@ -1037,6 +1037,9 @@ function is(element, type) {
1037
1037
  const businessObject = element.businessObject;
1038
1038
  return typeof businessObject?.$instanceOf === "function" && businessObject.$instanceOf(type);
1039
1039
  }
1040
+ function flowNodesOf(elementRegistry) {
1041
+ return elementRegistry.filter((element) => element.id === element.businessObject.id && is(element, "bpmn:FlowNode"));
1042
+ }
1040
1043
  function isGateway(element) {
1041
1044
  return element.type === "bpmn:ExclusiveGateway" || element.type === "bpmn:InclusiveGateway";
1042
1045
  }
@@ -2103,6 +2106,40 @@ class PathResolver {
2103
2106
  }
2104
2107
  }
2105
2108
 
2109
+ const NICE_STEPS = [1, 2, 5, 10];
2110
+ const RANGE_STEPS = 100;
2111
+ const NUMBER_LITERAL = /(?<![\w.])\d+(?:\.\d+)?/g;
2112
+ class PathVariableDeriver {
2113
+ derive(conditions, values) {
2114
+ const names = [...new Set(conditions.flatMap((condition) => condition.variableNames))];
2115
+ return names.map((name) => {
2116
+ const conditionsReadingName = conditions.filter((condition) => condition.variableNames.includes(name));
2117
+ return this.variableFor(name, values[name], conditionsReadingName);
2118
+ });
2119
+ }
2120
+ variableFor(name, value, conditionsReadingName) {
2121
+ if (typeof value === "boolean") return { name, type: "boolean" };
2122
+ if (typeof value === "number") {
2123
+ const comparedNumbers = conditionsReadingName.flatMap((condition) => this.numberLiterals(condition.expression));
2124
+ return { name, type: "number", range: this.rangeAround(value, comparedNumbers) };
2125
+ }
2126
+ if (value === void 0 || typeof value === "string") return { name, type: "text" };
2127
+ return { name, type: "json" };
2128
+ }
2129
+ rangeAround(value, comparedNumbers) {
2130
+ const max = this.niceCeiling(2 * Math.max(Math.abs(value), ...comparedNumbers));
2131
+ return { min: 0, max, step: max / RANGE_STEPS };
2132
+ }
2133
+ niceCeiling(value) {
2134
+ if (value <= 0) return 1;
2135
+ const magnitude = 10 ** Math.floor(Math.log10(value));
2136
+ return NICE_STEPS.find((step) => step * magnitude >= value) * magnitude;
2137
+ }
2138
+ numberLiterals(expression) {
2139
+ return (expression.match(NUMBER_LITERAL) ?? []).map(Number);
2140
+ }
2141
+ }
2142
+
2106
2143
  const GATEWAY_KINDS = {
2107
2144
  "bpmn:ExclusiveGateway": "exclusive",
2108
2145
  "bpmn:InclusiveGateway": "inclusive"
@@ -2113,9 +2150,7 @@ class PathGraphReader {
2113
2150
  this.engine = engine;
2114
2151
  }
2115
2152
  read(measureNames) {
2116
- const flowNodes = this.elementRegistry.filter(
2117
- (element) => element.id === element.businessObject.id && is(element, "bpmn:FlowNode")
2118
- );
2153
+ const flowNodes = flowNodesOf(this.elementRegistry);
2119
2154
  const startEventsIn = (container) => flowNodes.filter((node) => is(node, "bpmn:StartEvent") && node.businessObject.$parent === container);
2120
2155
  const processStartIds = flowNodes.filter((node) => is(node, "bpmn:StartEvent") && this.isProcess(node.businessObject.$parent)).map((node) => node.id);
2121
2156
  return {
@@ -2177,12 +2212,35 @@ class PathMarkers {
2177
2212
  }
2178
2213
  }
2179
2214
 
2215
+ class ScenarioVariables {
2216
+ constructor(elementRegistry, engine) {
2217
+ this.elementRegistry = elementRegistry;
2218
+ this.engine = engine;
2219
+ }
2220
+ of(scenario, fallbacks) {
2221
+ const fallbackVariables = fallbacks.map((fallback) => this.afterOutputMappings(fallback));
2222
+ return Object.assign({}, ...fallbackVariables, this.afterOutputMappings(scenario));
2223
+ }
2224
+ afterOutputMappings(scenario) {
2225
+ let variables = Object.assign({}, scenario?.variables, ...Object.values(scenario?.taskResults ?? {}));
2226
+ for (const flowNode of flowNodesOf(this.elementRegistry)) {
2227
+ for (const { source, target } of this.engine.readIoMappings(flowNode.businessObject).outputs) {
2228
+ const evaluation = this.engine.evaluate(source, variables);
2229
+ if (evaluation.ok) variables = this.engine.writeVariable(variables, target, evaluation.value);
2230
+ }
2231
+ }
2232
+ return variables;
2233
+ }
2234
+ }
2235
+
2180
2236
  class PathSimulation {
2181
2237
  constructor(config, elementRegistry, canvas) {
2182
2238
  this.pathResolver = new PathResolver();
2239
+ this.pathVariableDeriver = new PathVariableDeriver();
2183
2240
  this.engine = new EngineRegistry().resolve(config?.engine ?? "zeebe");
2184
2241
  this.pathGraphReader = new PathGraphReader(elementRegistry, this.engine);
2185
2242
  this.pathMarkers = new PathMarkers(canvas, elementRegistry);
2243
+ this.scenarioVariables = new ScenarioVariables(elementRegistry, this.engine);
2186
2244
  }
2187
2245
  getEngine() {
2188
2246
  return this.engine;
@@ -2193,9 +2251,18 @@ class PathSimulation {
2193
2251
  this.pathMarkers.show(path);
2194
2252
  return path;
2195
2253
  }
2254
+ deriveVariables(values) {
2255
+ return this.pathVariableDeriver.derive(this.evaluableConditions(), values);
2256
+ }
2257
+ variablesOf(scenario, fallbacks = []) {
2258
+ return this.scenarioVariables.of(scenario, fallbacks);
2259
+ }
2196
2260
  clear() {
2197
2261
  this.pathMarkers.clear();
2198
2262
  }
2263
+ evaluableConditions() {
2264
+ 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) }));
2265
+ }
2199
2266
  }
2200
2267
  PathSimulation.$inject = ["config.dataSimulation", "elementRegistry", "canvas"];
2201
2268