@machinemetrics/io-adapter-lib 2.32.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/.circleci/config.yml +141 -0
- package/.eslintrc.json +36 -0
- package/.gitattributes +12 -0
- package/CHANGELOG.md +544 -0
- package/README.md +2 -0
- package/index.js +17 -0
- package/lib/config/adapterConfig.js +535 -0
- package/lib/config/common/allowDenyList.js +58 -0
- package/lib/config/common/jsonPath.js +44 -0
- package/lib/config/common/labjackU3T4Common.js +227 -0
- package/lib/config/common/validations.js +142 -0
- package/lib/config/configError.js +32 -0
- package/lib/config/device/adam-6052Config.js +103 -0
- package/lib/config/device/brotherHTTPConfig.js +215 -0
- package/lib/config/device/ethernetIPConfig.js +191 -0
- package/lib/config/device/ifmIotConfig.js +245 -0
- package/lib/config/device/jsonHttpConfig.js +76 -0
- package/lib/config/device/labjackT4Config.js +39 -0
- package/lib/config/device/labjackT7Config.js +192 -0
- package/lib/config/device/labjackU3Config.js +32 -0
- package/lib/config/device/modbusTcpConfig.js +336 -0
- package/lib/config/device/mqttBaseConfig.js +70 -0
- package/lib/config/device/mqttConfig.js +113 -0
- package/lib/config/device/mqttLincolnConfig.js +62 -0
- package/lib/config/device/mtconnectAdapterConfig.js +42 -0
- package/lib/config/device/mtconnectBaseConfig.js +136 -0
- package/lib/config/device/mtconnectConfig.js +52 -0
- package/lib/config/device/mtconnectHaasConfig.js +15 -0
- package/lib/config/device/nullConfig.js +81 -0
- package/lib/config/device/opcuaConfig.js +205 -0
- package/lib/config/device/pcccConfig.js +88 -0
- package/lib/config/engineConfigV1.js +382 -0
- package/lib/config/engineConfigV2.js +106 -0
- package/lib/config/generator/counterGenConfig.js +15 -0
- package/lib/config/generator/cronGenConfig.js +33 -0
- package/lib/config/generator/dateTimeGenConfig.js +33 -0
- package/lib/config/index.js +339 -0
- package/lib/config/transformConfigUtil.js +296 -0
- package/lib/engine/dataOutput.js +357 -0
- package/lib/engine/deviceOutput.js +186 -0
- package/lib/engine/engineV1.js +480 -0
- package/lib/engine/engineV2.js +719 -0
- package/lib/engine/index.js +34 -0
- package/lib/engine/transformBuilderV1.js +111 -0
- package/lib/engine/transformBuilderV2.js +74 -0
- package/lib/expressionService.js +330 -0
- package/lib/math.js +142 -0
- package/lib/transform/accumulate.js +98 -0
- package/lib/transform/average.js +56 -0
- package/lib/transform/debounce.js +152 -0
- package/lib/transform/downsample.js +69 -0
- package/lib/transform/edge.js +34 -0
- package/lib/transform/expression.js +91 -0
- package/lib/transform/fallingEdge.js +70 -0
- package/lib/transform/fromBuffer.js +89 -0
- package/lib/transform/hash.js +41 -0
- package/lib/transform/ignoreValue.js +118 -0
- package/lib/transform/index.js +93 -0
- package/lib/transform/invert.js +25 -0
- package/lib/transform/latch.js +99 -0
- package/lib/transform/latchValue.js +115 -0
- package/lib/transform/logicAnd.js +67 -0
- package/lib/transform/logicOr.js +67 -0
- package/lib/transform/map.js +115 -0
- package/lib/transform/max.js +57 -0
- package/lib/transform/maxLength.js +39 -0
- package/lib/transform/min.js +57 -0
- package/lib/transform/minDelta.js +40 -0
- package/lib/transform/offDelay.js +89 -0
- package/lib/transform/onDelay.js +89 -0
- package/lib/transform/patternEscape.js +24 -0
- package/lib/transform/patternMatch.js +194 -0
- package/lib/transform/patternReplace.js +170 -0
- package/lib/transform/patternTest.js +85 -0
- package/lib/transform/rateOfChange.js +56 -0
- package/lib/transform/reject.js +115 -0
- package/lib/transform/resample.js +96 -0
- package/lib/transform/risingEdge.js +90 -0
- package/lib/transform/risingEdgeCounter.js +179 -0
- package/lib/transform/sampleInterval.js +12 -0
- package/lib/transform/source.js +116 -0
- package/lib/transform/state.js +201 -0
- package/lib/transform/threshold.js +52 -0
- package/lib/transform/toBuffer.js +159 -0
- package/lib/transform/toggle.js +118 -0
- package/lib/transform/transformState.js +193 -0
- package/lib/transform/trim.js +27 -0
- package/lib/transform/util/chainSource.js +96 -0
- package/lib/transform/util/ringBuffer.js +34 -0
- package/lib/transform/valueChange.js +34 -0
- package/lib/transform/valueDecrease.js +38 -0
- package/lib/transform/valueIncrease.js +38 -0
- package/lib/transform/valueIncreaseDiff.js +66 -0
- package/lib/transform/whenUnavailable.js +73 -0
- package/lib/transform/windowCount.js +86 -0
- package/lib/util/fileUtil.js +44 -0
- package/package.json +38 -0
- package/test/.eslintrc.json +15 -0
- package/test/chainedTransform.test.js +88 -0
- package/test/conditions.test.js +118 -0
- package/test/config/ab-pccc.test.js +41 -0
- package/test/config/adam-6052.test.js +16 -0
- package/test/config/adapter.test.js +109 -0
- package/test/config/brother-http.test.js +19 -0
- package/test/config/ethernet-ip.test.js +19 -0
- package/test/config/ifm-iot.test.js +20 -0
- package/test/config/json-http.test.js +47 -0
- package/test/config/labjack-t4.test.js +78 -0
- package/test/config/labjack-t7.test.js +18 -0
- package/test/config/labjack-u3.test.js +17 -0
- package/test/config/modbusTcp.test.js +87 -0
- package/test/config/mqtt.test.js +19 -0
- package/test/config/mqttLincoln.test.js +29 -0
- package/test/config/mtconnect.test.js +63 -0
- package/test/config/mtconnectAdapter.test.js +124 -0
- package/test/config/mtconnectHaas.test.js +15 -0
- package/test/config/null.test.js +16 -0
- package/test/config/opcua.test.js +97 -0
- package/test/config-tests.js +102 -0
- package/test/configFiles/conditions.yml +37 -0
- package/test/configFiles/data-items-legacy.yml +24 -0
- package/test/configFiles/data-items-shorthand.yml +14 -0
- package/test/configFiles/data-items.yml +12 -0
- package/test/configFiles/device/ab-pccc-default.yml +3 -0
- package/test/configFiles/device/ab-pccc-full.yml +13 -0
- package/test/configFiles/device/adam-6052-default.yml +2 -0
- package/test/configFiles/device/brother-http-default.yml +3 -0
- package/test/configFiles/device/ethernet-ip-default.yml +2 -0
- package/test/configFiles/device/ifm-iot-default.yml +2 -0
- package/test/configFiles/device/json-http-bad-prop.yml +13 -0
- package/test/configFiles/device/json-http-bad-prop2.yml +13 -0
- package/test/configFiles/device/json-http-default.yml +3 -0
- package/test/configFiles/device/json-http-std.yml +13 -0
- package/test/configFiles/device/labjack-t4-condition-exprs.yaml +46 -0
- package/test/configFiles/device/labjack-t4-default.yml +3 -0
- package/test/configFiles/device/labjack-t4-pins-alt.yaml +41 -0
- package/test/configFiles/device/labjack-t4-pins.yaml +40 -0
- package/test/configFiles/device/labjack-t4-v1.yaml +29 -0
- package/test/configFiles/device/labjack-t7-default.yml +2 -0
- package/test/configFiles/device/labjack-u3-default.yml +2 -0
- package/test/configFiles/device/modbus-partial.yml +4 -0
- package/test/configFiles/device/modbus-std-extended.yaml +23 -0
- package/test/configFiles/device/modbus-std.yaml +34 -0
- package/test/configFiles/device/modbus-tcp-default.yml +3 -0
- package/test/configFiles/device/mqtt-default.yml +2 -0
- package/test/configFiles/device/mqtt-lincoln-default.yml +3 -0
- package/test/configFiles/device/mqtt-lincoln-full.yml +7 -0
- package/test/configFiles/device/mtconnect-adapter-default.yml +3 -0
- package/test/configFiles/device/mtconnect-adapter-keys.yaml +18 -0
- package/test/configFiles/device/mtconnect-complex-keys.yaml +17 -0
- package/test/configFiles/device/mtconnect-default.yml +3 -0
- package/test/configFiles/device/mtconnect-duplicate-allow-keys.yaml +10 -0
- package/test/configFiles/device/mtconnect-duplicate-declare-keys.yaml +8 -0
- package/test/configFiles/device/mtconnect-duplicate-deny-keys.yaml +10 -0
- package/test/configFiles/device/mtconnect-haas-default.yml +3 -0
- package/test/configFiles/device/mtconnect-std.yaml +18 -0
- package/test/configFiles/device/null-default.yml +1 -0
- package/test/configFiles/device/opcua-bad-tag.yml +18 -0
- package/test/configFiles/device/opcua-bad-tag2.yml +18 -0
- package/test/configFiles/device/opcua-default.yml +3 -0
- package/test/configFiles/device/opcua-std.yml +18 -0
- package/test/configFiles/dump-test.yml +11 -0
- package/test/configFiles/expressionCond.yml +46 -0
- package/test/configFiles/min-config-t4.yaml +4 -0
- package/test/configFiles/min-config-u3.yaml +3 -0
- package/test/configFiles/missing-device.yaml +2 -0
- package/test/configFiles/parse-error1.yml +9 -0
- package/test/configFiles/parse-error2.yml +9 -0
- package/test/configFiles/repro/buffer-convert-repro.yml +15 -0
- package/test/configFiles/repro/chained-delay-timing-repro.yml +13 -0
- package/test/configFiles/repro/count-init-repro.yml +45 -0
- package/test/configFiles/repro/cycle-break-repro.yml +44 -0
- package/test/configFiles/repro/debounce-repro.yml +46 -0
- package/test/configFiles/repro/diff-count-repro.yml +34 -0
- package/test/configFiles/repro/engine-hang-repro.yml +9 -0
- package/test/configFiles/repro/latch-apm-repro.yml +26 -0
- package/test/configFiles/repro/lockout-count-repro.yml +33 -0
- package/test/configFiles/repro/program-extract-repro.yml +38 -0
- package/test/configFiles/repro/state-latch-repro.yml +47 -0
- package/test/configFiles/repro/ternary-repro.yml +26 -0
- package/test/configFiles/transform/debounce.yml +12 -0
- package/test/configFiles/transform/expression.yml +34 -0
- package/test/configFiles/transform/ignoreValue.yml +31 -0
- package/test/configFiles/transform/latch.yml +11 -0
- package/test/configFiles/transform/latchValue.yml +31 -0
- package/test/configFiles/transform/logicAnd.yml +14 -0
- package/test/configFiles/transform/logicOr.yml +14 -0
- package/test/configFiles/transform/map.yml +19 -0
- package/test/configFiles/transform/maxLength.yml +13 -0
- package/test/configFiles/transform/offDelay.yml +12 -0
- package/test/configFiles/transform/pattern-escape.yml +10 -0
- package/test/configFiles/transform/pattern-match.yml +57 -0
- package/test/configFiles/transform/pattern-replace.yml +34 -0
- package/test/configFiles/transform/pattern-test.yml +25 -0
- package/test/configFiles/transform/reject.yml +24 -0
- package/test/configFiles/transform/risingEdgeCounter.yml +36 -0
- package/test/configFiles/transform/source.yml +20 -0
- package/test/configFiles/transform/state.yml +56 -0
- package/test/configFiles/transform/toggle.yml +19 -0
- package/test/configFiles/transform/whenUnavailable.yml +19 -0
- package/test/dataFiles/noisy-pulse.txt +11330 -0
- package/test/dataItems.test.js +140 -0
- package/test/engine-v1-tests.js +418 -0
- package/test/engine-v2-tests.js +284 -0
- package/test/expression-tests.js +171 -0
- package/test/expressionService.test.js +154 -0
- package/test/expressionServiceCondition.test.js +130 -0
- package/test/repro/buffer-convert-repro.test.js +38 -0
- package/test/repro/chained-delay-timing-repro.test.js +34 -0
- package/test/repro/count-init-repro.test.js +46 -0
- package/test/repro/cylce-break-repro.test.js +57 -0
- package/test/repro/debounce-repro.test.js +65 -0
- package/test/repro/diff-count-repro.test.js +79 -0
- package/test/repro/engine-hang-repro.test.js +38 -0
- package/test/repro/latch-apm-repro.test.js +119 -0
- package/test/repro/lockout-count-repro.test.js +84 -0
- package/test/repro/program-extract-repro.test.js +40 -0
- package/test/repro/state-latch-repro.test.js +63 -0
- package/test/repro/ternary-repro.test.js +43 -0
- package/test/transform/accumulte.test.js +18 -0
- package/test/transform/average.test.js +22 -0
- package/test/transform/debounce.test.js +70 -0
- package/test/transform/downsample.test.js +30 -0
- package/test/transform/edge.test.js +27 -0
- package/test/transform/expression.test.js +189 -0
- package/test/transform/fallingEdge.test.js +59 -0
- package/test/transform/fromBuffer.test.js +60 -0
- package/test/transform/hash.test.js +34 -0
- package/test/transform/ignoreValue.test.js +123 -0
- package/test/transform/invert.test.js +26 -0
- package/test/transform/latch.test.js +33 -0
- package/test/transform/latchValue.test.js +126 -0
- package/test/transform/logicAnd.test.js +80 -0
- package/test/transform/logicOr.test.js +80 -0
- package/test/transform/map.test.js +42 -0
- package/test/transform/max.test.js +30 -0
- package/test/transform/maxLength.test.js +32 -0
- package/test/transform/min.test.js +30 -0
- package/test/transform/minDelta.test.js +14 -0
- package/test/transform/offDelay.test.js +123 -0
- package/test/transform/onDelay.test.js +105 -0
- package/test/transform/patternEscape.test.js +18 -0
- package/test/transform/patternMatch.test.js +177 -0
- package/test/transform/patternReplace.test.js +95 -0
- package/test/transform/patternTest.test.js +105 -0
- package/test/transform/rateOfChange.test.js +34 -0
- package/test/transform/reject.test.js +56 -0
- package/test/transform/resample.test.js +193 -0
- package/test/transform/risingEdge.test.js +60 -0
- package/test/transform/risingEdgeCounter.test.js +227 -0
- package/test/transform/sampleInterval.test.js +22 -0
- package/test/transform/source.test.js +137 -0
- package/test/transform/state.test.js +248 -0
- package/test/transform/threshold.test.js +78 -0
- package/test/transform/toBuffer.test.js +60 -0
- package/test/transform/toggle.test.js +92 -0
- package/test/transform/trim.test.js +30 -0
- package/test/transform/valueChange.test.js +14 -0
- package/test/transform/valueDecrease.test.js +32 -0
- package/test/transform/valueIncrease.test.js +32 -0
- package/test/transform/valueIncreaseDiff.test.js +32 -0
- package/test/transform/whenUnavailable.test.js +93 -0
- package/test/transform/windowCount.test.js +26 -0
- package/test/util/testUtils.js +405 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const EngineV1 = require('./engineV1');
|
|
4
|
+
const EngineV2 = require('./engineV2');
|
|
5
|
+
const TransformBuilderV1 = require('./transformBuilderV1');
|
|
6
|
+
const TransformBuilderV2 = require('./transformBuilderV2');
|
|
7
|
+
|
|
8
|
+
class Engine {
|
|
9
|
+
static load(config, options = {}) {
|
|
10
|
+
if (config.engineVersion === 1) {
|
|
11
|
+
return new EngineV1(config, options);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (config.engineVersion >= 2) {
|
|
15
|
+
const pool = new EngineV2(config, options);
|
|
16
|
+
|
|
17
|
+
if (config.configVersion === 1) {
|
|
18
|
+
const builder = new TransformBuilderV1(config);
|
|
19
|
+
builder.build(pool);
|
|
20
|
+
} else if (config.configVersion === 2) {
|
|
21
|
+
const builder = new TransformBuilderV2(config);
|
|
22
|
+
builder.build(pool);
|
|
23
|
+
} else {
|
|
24
|
+
throw new Error('Unsupported config version');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return pool;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
throw new Error('Unable to determine correct engine to load');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
module.exports = Engine;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const Transform = require('../transform');
|
|
5
|
+
|
|
6
|
+
class TransformBuilderV1 {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
build(dataPool) {
|
|
12
|
+
_.each(this.config.vpins, (vpin) => {
|
|
13
|
+
let chain = null;
|
|
14
|
+
if (vpin.type === 'analog') {
|
|
15
|
+
chain = this.buildAnalogTransformChain(vpin);
|
|
16
|
+
} else if (vpin.type === 'digital') {
|
|
17
|
+
chain = this.buildDigitalTransformChain(vpin);
|
|
18
|
+
} else if (vpin.type === 'counter') {
|
|
19
|
+
chain = this.buildCounterTransformChain(vpin, dataPool);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (chain) {
|
|
23
|
+
dataPool.addVariable(vpin.name, chain);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
buildAnalogTransformChain(defn) {
|
|
29
|
+
if (!defn || defn.type !== 'analog') {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const transform = new Transform.sampleInterval(defn.sampleInterval);
|
|
34
|
+
if (defn.sampleFunc === 'avg') {
|
|
35
|
+
transform.chain(new Transform.average(defn.sampleWindow));
|
|
36
|
+
} else if (defn.sampleFunc === 'max') {
|
|
37
|
+
transform.chain(new Transform.max(defn.sampleWindow));
|
|
38
|
+
} else if (defn.sampleFunc === 'min') {
|
|
39
|
+
transform.chain(new Transform.min(defn.sampleWindow));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (defn.minDelta) {
|
|
43
|
+
transform.chain(new Transform.minDelta(defn.minDelta));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return transform;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
buildDigitalTransformChain(defn) {
|
|
50
|
+
if (!defn || (defn.type !== 'digital' && defn.type !== 'counter')) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const transform = new Transform.threshold(defn.vThresh);
|
|
55
|
+
if (defn.invert) {
|
|
56
|
+
transform.chain(new Transform.invert());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (defn.filter) {
|
|
60
|
+
transform.chain(new Transform.debounce(defn.filter));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (defn.onDelay) {
|
|
64
|
+
transform.chain(new Transform.onDelay(defn.onDelay));
|
|
65
|
+
}
|
|
66
|
+
if (defn.offDelay) {
|
|
67
|
+
transform.chain(new Transform.offDelay(defn.offDelay));
|
|
68
|
+
}
|
|
69
|
+
if (defn.onLatch) {
|
|
70
|
+
transform.chain(new Transform.risingEdge());
|
|
71
|
+
transform.chain(new Transform.offDelay(defn.onLatch));
|
|
72
|
+
}
|
|
73
|
+
if (defn.offLatch) {
|
|
74
|
+
transform.chain(new Transform.fallingEdge());
|
|
75
|
+
transform.chain(new Transform.onDelay(defn.offLatch));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return transform;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
buildCounterTransformChain(defn, dataPool) {
|
|
82
|
+
if (!defn || defn.type !== 'counter') {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const transform = this.buildDigitalTransformChain(defn);
|
|
87
|
+
|
|
88
|
+
if (defn.edge === 'rising') {
|
|
89
|
+
transform.chain(new Transform.risingEdge({
|
|
90
|
+
minDwell: defn.minPulseWidth,
|
|
91
|
+
maxDwell: defn.maxPulseWidth,
|
|
92
|
+
}));
|
|
93
|
+
} else if (defn.edge === 'falling') {
|
|
94
|
+
transform.chain(new Transform.fallingEdge({
|
|
95
|
+
minDwell: defn.minPulseWidth,
|
|
96
|
+
maxDwell: defn.maxPulseWidth,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (defn.suspend) {
|
|
101
|
+
transform.chain(new Transform.invert());
|
|
102
|
+
transform.chain(new Transform.logicAnd(dataPool, defn.suspend));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
transform.chain(new Transform.risingEdgeCounter());
|
|
106
|
+
|
|
107
|
+
return transform;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = TransformBuilderV1;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const Transform = require('../transform');
|
|
5
|
+
|
|
6
|
+
class TransformBuilderV2 {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
|
|
10
|
+
this.transformMap = Object.values(Transform).reduce((trmap, transform) => {
|
|
11
|
+
trmap[transform.op] = transform;
|
|
12
|
+
transform.opAliases.forEach((alias) => {
|
|
13
|
+
trmap[alias] = transform;
|
|
14
|
+
});
|
|
15
|
+
return trmap;
|
|
16
|
+
}, {});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
build(engine) {
|
|
20
|
+
// NB: This is required if using native node events
|
|
21
|
+
// If there's a lot of variables, increase the max listener count for engine
|
|
22
|
+
// const varCount = _.size(this.config.engine.variables);
|
|
23
|
+
// if (varCount > engine.getMaxListeners()) {
|
|
24
|
+
// engine.setMaxListeners(varCount);
|
|
25
|
+
// }
|
|
26
|
+
|
|
27
|
+
_.each(this.config.engine.variables, (v, varName) => {
|
|
28
|
+
if (!v.transform) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const transform = this.createTransformChain(engine, v.transform, varName, true);
|
|
33
|
+
if (transform) {
|
|
34
|
+
engine.addVariable(varName, transform);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
createTransformChain(engine, transformList, varName, base = false) {
|
|
40
|
+
return _.reduce(transformList, (transform, transDefn) => {
|
|
41
|
+
const step = this.createTransformStep(transDefn, varName, engine, base && transform === null);
|
|
42
|
+
if (step !== null) {
|
|
43
|
+
if (transform === null) {
|
|
44
|
+
transform = step;
|
|
45
|
+
} else {
|
|
46
|
+
transform.chain(step);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return transform;
|
|
50
|
+
}, null);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
createTransformStep(transDefn, varName, engine, base = false) {
|
|
54
|
+
const makeTransformArgs = (defn) => {
|
|
55
|
+
return {
|
|
56
|
+
engine,
|
|
57
|
+
config: this.config,
|
|
58
|
+
path: defn.path,
|
|
59
|
+
rootPath: `variables.${varName}`,
|
|
60
|
+
baseTransform: base,
|
|
61
|
+
args: defn.args,
|
|
62
|
+
builder: this,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const args = makeTransformArgs(transDefn);
|
|
67
|
+
if (!this.transformMap[transDefn.func]) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return this.transformMap[transDefn.func].create(args);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
module.exports = TransformBuilderV2;
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const math = require('./math');
|
|
5
|
+
|
|
6
|
+
class ExpressionService {
|
|
7
|
+
constructor(config = {}) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
this.availableNamesByChannel = {};
|
|
10
|
+
this.referencedNamesByChannel = {};
|
|
11
|
+
this.referencedNamesByPath = {};
|
|
12
|
+
this.pathsByReferenceName = {};
|
|
13
|
+
this.defaultValues = {};
|
|
14
|
+
this.channelsLookup = {};
|
|
15
|
+
this.stringExpressionsByPath = {};
|
|
16
|
+
|
|
17
|
+
this.resetStatistics();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
resetStatistics() {
|
|
21
|
+
this.stats = {
|
|
22
|
+
parseStringExprCount: 0,
|
|
23
|
+
parseStringExprCounts: {},
|
|
24
|
+
computeStringExprCount: 0,
|
|
25
|
+
computeStringExprCounts: {},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
isNameValid(name) {
|
|
30
|
+
if (_.isEmpty(name)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return !!name.match(/^\w($|[\w-]*\w)$/);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
isNameDefined(name, channel) {
|
|
37
|
+
return this.channelSetValueExists(this.availableNamesByChannel, name, channel);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
definedNames(channel) {
|
|
41
|
+
return this.channelSetValues(this.availableNamesByChannel, channel);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
isNameReferenced(name, channel) {
|
|
45
|
+
return this.channelSetValueExists(this.referencedNamesByChannel, name, channel);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
referencedNames(channel) {
|
|
49
|
+
return this.channelSetValues(this.referencedNamesByChannel, channel);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
channelSetValueExists(channelSet, value, channel) {
|
|
53
|
+
if (channel) {
|
|
54
|
+
return _.includes(channelSet[channel], value);
|
|
55
|
+
}
|
|
56
|
+
return _.reduce(channelSet, (res, set) => {
|
|
57
|
+
return res || _.includes(set, value);
|
|
58
|
+
}, false);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
channelSetValues(channelSet, channel) {
|
|
62
|
+
if (channel) {
|
|
63
|
+
return channelSet[channel] || [];
|
|
64
|
+
}
|
|
65
|
+
return _(channelSet).values().flatten().uniq().value();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
addName({ name, channel, defaultValue } = {}) {
|
|
69
|
+
if (!name || !channel) {
|
|
70
|
+
throw new Error('Incomplete identifier');
|
|
71
|
+
}
|
|
72
|
+
if (this.isNameDefined(name)) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this.defaultValues[name] = defaultValue;
|
|
77
|
+
this.channelsLookup[name] = channel;
|
|
78
|
+
|
|
79
|
+
if (!this.availableNamesByChannel[channel]) {
|
|
80
|
+
this.availableNamesByChannel[channel] = [];
|
|
81
|
+
}
|
|
82
|
+
this.availableNamesByChannel[channel].push(name);
|
|
83
|
+
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
addExpression({ expression, path } = {}) {
|
|
88
|
+
if (!expression || !path) {
|
|
89
|
+
throw new Error('Incomplete expression');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const names = this.definedNames();
|
|
93
|
+
_.each(names, (name) => {
|
|
94
|
+
if (this.findName(expression, name)) {
|
|
95
|
+
const channel = this.channelsLookup[name];
|
|
96
|
+
if (channel) {
|
|
97
|
+
if (!this.referencedNamesByChannel[channel]) {
|
|
98
|
+
this.referencedNamesByChannel[channel] = [];
|
|
99
|
+
}
|
|
100
|
+
if (!this.isNameReferenced(name, channel)) {
|
|
101
|
+
this.referencedNamesByChannel[channel].push(name);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!this.referencedNamesByPath[path]) {
|
|
106
|
+
this.referencedNamesByPath[path] = [];
|
|
107
|
+
}
|
|
108
|
+
if (!_.includes(this.referencedNamesByPath[path], name)) {
|
|
109
|
+
this.referencedNamesByPath[path].push(name);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!this.pathsByReferenceName[name]) {
|
|
113
|
+
this.pathsByReferenceName[name] = [];
|
|
114
|
+
}
|
|
115
|
+
if (!_.includes(this.pathsByReferenceName[name], path)) {
|
|
116
|
+
this.pathsByReferenceName[name].push(path);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
addStringExpression({ string, path, withThis } = {}) {
|
|
123
|
+
if (!string || !path) {
|
|
124
|
+
throw new Error('Incomplete expression');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const parts = this.parseStringExpr(string);
|
|
128
|
+
_.each(parts, (part) => {
|
|
129
|
+
if (part.expression) {
|
|
130
|
+
this.addExpression({
|
|
131
|
+
expression: part.expression,
|
|
132
|
+
path,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const state = { };
|
|
138
|
+
if (withThis) {
|
|
139
|
+
state.this = 0;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.stringExpressionsByPath[path] = _.map(parts, (part) => {
|
|
143
|
+
if (part.expression) {
|
|
144
|
+
part.compiledExpression = this.compileExpression(part.expression, state);
|
|
145
|
+
}
|
|
146
|
+
return part;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
parseStringExpr(str, exprSet = []) {
|
|
151
|
+
if (_.isNil(str)) {
|
|
152
|
+
return exprSet;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
str = str.toString();
|
|
156
|
+
this.stats.parseStringExprCount += 1;
|
|
157
|
+
this.stats.parseStringExprCounts[str] = (this.stats.parseStringExprCounts[str] ?? 0) + 1;
|
|
158
|
+
|
|
159
|
+
const match = str.match(/(?<!\\)\${((\\}|[^}])*)}/);
|
|
160
|
+
if (!match) {
|
|
161
|
+
return [...exprSet, { text: this.unescapeText(str) }];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const part1 = {
|
|
165
|
+
text: this.unescapeText(str.slice(0, match.index)),
|
|
166
|
+
};
|
|
167
|
+
const part2 = {
|
|
168
|
+
text: str.slice(match.index, match.index + match[0].length),
|
|
169
|
+
expression: this.unescapeExpression(match[1]),
|
|
170
|
+
};
|
|
171
|
+
const part3 = str.slice(match.index + match[0].length);
|
|
172
|
+
|
|
173
|
+
let parts12 = [part1, part2];
|
|
174
|
+
if (_.isEmpty(part1.text)) {
|
|
175
|
+
parts12 = [part2];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (_.isEmpty(part3)) {
|
|
179
|
+
return [...exprSet, ...parts12];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return this.parseStringExpr(part3, [...exprSet, ...parts12]);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
unescapeText(str) {
|
|
186
|
+
return str.replace(/\\\${/g, '${');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
unescapeExpression(str) {
|
|
190
|
+
return str.replace(/\\}/g, '}');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
findName(exprStr, name) {
|
|
194
|
+
const pattern = new RegExp(`(?:^|(?<=[\\s\\[(,:?]))${name}(?:(?=[\\[\\]\\s),.:?])|$)`, 'g');
|
|
195
|
+
return exprStr.match(pattern) !== null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
findCompoundIdentifiers(str) {
|
|
199
|
+
const matches = str.match(/[A-Za-z_][A-Za-z0-9_-]*(?:(?:\.[A-Za-z][A-Za-z0-9_-]*)|(?:\['[^'"]+'\]))+/g);
|
|
200
|
+
return matches ?? [];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
tokenizeCompoundIdentifier(compound) {
|
|
204
|
+
const first = (compound.match(/^[A-Za-z_][A-Za-z0-9_-]*/) ?? [])[0];
|
|
205
|
+
const rest = [];
|
|
206
|
+
|
|
207
|
+
compound = compound.substr(first.length);
|
|
208
|
+
while (compound.length > 0) {
|
|
209
|
+
let match = compound.match(/^\.([A-Za-z_][A-Za-z0-9_-]*)/);
|
|
210
|
+
if (match) {
|
|
211
|
+
rest.push(match[1]);
|
|
212
|
+
compound = compound.substr(match[0].length);
|
|
213
|
+
} else {
|
|
214
|
+
match = compound.match(/^\['([^'"]+)'\]/);
|
|
215
|
+
if (match) {
|
|
216
|
+
rest.push(match[1]);
|
|
217
|
+
compound = compound.substr(match[0].length);
|
|
218
|
+
} else {
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return compound.length === 0 ? [first, ...rest] : null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
compileExpression(expression, scope = {}) {
|
|
228
|
+
if (_.isEmpty(expression)) {
|
|
229
|
+
throw new Error('expression must have an expression');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
let errMessage = null;
|
|
233
|
+
let compiledExpression = null;
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
compiledExpression = this.compileInnerExpression(expression, scope);
|
|
237
|
+
} catch (err) {
|
|
238
|
+
errMessage = err.message;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Do a second compile test with this referencing an object
|
|
242
|
+
// Allows expressions of type: this.subkey, this.subkey.subsubkey, etc.
|
|
243
|
+
if (errMessage) {
|
|
244
|
+
const objThisMatch = this.findCompoundIdentifiers(expression);
|
|
245
|
+
if (objThisMatch) {
|
|
246
|
+
const addedScope = objThisMatch.reduce((accum, s) => {
|
|
247
|
+
const tokens = this.tokenizeCompoundIdentifier(s);
|
|
248
|
+
if (tokens) {
|
|
249
|
+
const first = tokens[0];
|
|
250
|
+
if (first === 'this' || this.channelsLookup[first]) {
|
|
251
|
+
accum[first] = _.reduceRight(_.tail(tokens), (sub, item) => {
|
|
252
|
+
return { [item]: sub };
|
|
253
|
+
}, 0);
|
|
254
|
+
} else {
|
|
255
|
+
accum[first] = 0;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return accum;
|
|
259
|
+
}, {});
|
|
260
|
+
|
|
261
|
+
try {
|
|
262
|
+
return this.compileInnerExpression(expression, { ...scope, ...addedScope });
|
|
263
|
+
} catch (err) {
|
|
264
|
+
throw new Error(`Problem evaluating expression: ${errMessage}`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Not an exception we know how to handle
|
|
269
|
+
throw new Error(`Problem evaluating expression: ${errMessage}`);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return compiledExpression;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
compileInnerExpression(exprStr, extraState = {}) {
|
|
276
|
+
const compiledExpr = math.compile(exprStr);
|
|
277
|
+
compiledExpr.evaluate({ ...this.defaultValues, ...extraState });
|
|
278
|
+
return compiledExpr;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
expressionTriggers(path) {
|
|
282
|
+
return this.referencedNamesByPath[path] || [];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
expressionsTriggeredBy(name) {
|
|
286
|
+
return this.pathsByReferenceName[name] || [];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
hasStringExpression(path) {
|
|
290
|
+
return !!this.stringExpressionsByPath[path];
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
isStaticExpression(path) {
|
|
294
|
+
if (!this.hasStringExpression(path)) {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const parts = this.stringExpressionsByPath[path];
|
|
299
|
+
if (parts.length > 1) {
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return !parts[0].expression;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
computeStringExpression(path, state) {
|
|
307
|
+
this.stats.computeStringExprCount += 1;
|
|
308
|
+
this.stats.computeStringExprCounts[path] = (this.stats.computeStringExprCounts[path] ?? 0) + 1;
|
|
309
|
+
|
|
310
|
+
const expr = this.stringExpressionsByPath[path];
|
|
311
|
+
if (!expr || expr.length === 0) {
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (expr.length === 1 && !expr[0].compiledExpression) {
|
|
316
|
+
return expr[0].text;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const parts = _.map(expr, (part) => {
|
|
320
|
+
if (part.compiledExpression) {
|
|
321
|
+
return part.compiledExpression.evaluate(state);
|
|
322
|
+
}
|
|
323
|
+
return part.text;
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
return parts.join('');
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
module.exports = ExpressionService;
|
package/lib/math.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const mathjs = require('mathjs');
|
|
5
|
+
const md5 = require('md5');
|
|
6
|
+
|
|
7
|
+
const math = mathjs.create(mathjs.all, {
|
|
8
|
+
matrix: 'Array',
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const isAlpha = math.parse.isAlpha;
|
|
12
|
+
math.parse.isAlpha = (c, prev, next) => {
|
|
13
|
+
return isAlpha(c, prev, next) || (c === '-' && prev && next);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const equalText = math.equalText;
|
|
17
|
+
const concat = math.concat;
|
|
18
|
+
const size = math.size;
|
|
19
|
+
|
|
20
|
+
const equal = (a, b) => {
|
|
21
|
+
const aStr = _.isString(a);
|
|
22
|
+
const bStr = _.isString(b);
|
|
23
|
+
if (aStr && !bStr && !_.isNil(b)) {
|
|
24
|
+
b = b.toString();
|
|
25
|
+
} else if (bStr && !aStr && !_.isNil(a)) {
|
|
26
|
+
a = a.toString();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return a === b;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
math.import({
|
|
33
|
+
import: () => { throw new Error('Function import is disabled'); },
|
|
34
|
+
createUnit: () => { throw new Error('Function createUnit is disabled'); },
|
|
35
|
+
evaluate: () => { throw new Error('Function evaluate is disabled'); },
|
|
36
|
+
// parse: () => { throw new Error('Function parse is disabled'); },
|
|
37
|
+
simplify: () => { throw new Error('Function simplify is disabled'); },
|
|
38
|
+
derivative: () => { throw new Error('Function derivative is disabled'); },
|
|
39
|
+
equal,
|
|
40
|
+
unequal: (a, b) => {
|
|
41
|
+
return !equal(a, b);
|
|
42
|
+
},
|
|
43
|
+
equalText: (a, b) => { return equalText(a.toString(), b.toString()); },
|
|
44
|
+
concat: (...args) => { return concat(...args.map(arg => arg.toString())); },
|
|
45
|
+
size: (v) => {
|
|
46
|
+
if (_.isString(v)) {
|
|
47
|
+
return v.length;
|
|
48
|
+
}
|
|
49
|
+
return size(v);
|
|
50
|
+
},
|
|
51
|
+
flag: (value, index) => {
|
|
52
|
+
if (_.isArray(value)) {
|
|
53
|
+
return (index < value.length) ? !!value[index] : false;
|
|
54
|
+
}
|
|
55
|
+
return (value & (1 << index)) !== 0;
|
|
56
|
+
},
|
|
57
|
+
startsWith: (s, v) => {
|
|
58
|
+
if (!_.isString(s)) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return s.startsWith(v);
|
|
62
|
+
},
|
|
63
|
+
endsWith: (s, v) => {
|
|
64
|
+
if (!_.isString(s)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return s.endsWith(v);
|
|
68
|
+
},
|
|
69
|
+
contains: (s, v) => {
|
|
70
|
+
if (_.isString(s)) {
|
|
71
|
+
return s.includes(v);
|
|
72
|
+
} else if (_.isArray(s)) {
|
|
73
|
+
v = v?.toString();
|
|
74
|
+
return s.find(p => p?.toString() === v) !== undefined;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
},
|
|
78
|
+
match: (s, pattern, group = 0) => {
|
|
79
|
+
if (!_.isString(s)) {
|
|
80
|
+
return '';
|
|
81
|
+
}
|
|
82
|
+
const regex = new RegExp(pattern);
|
|
83
|
+
const match = s.match(regex);
|
|
84
|
+
if (!match || _.isNil(match[group])) {
|
|
85
|
+
return '';
|
|
86
|
+
}
|
|
87
|
+
return match[group];
|
|
88
|
+
},
|
|
89
|
+
test: (s, pattern) => {
|
|
90
|
+
if (!_.isString(s)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return new RegExp(pattern).test(s);
|
|
94
|
+
},
|
|
95
|
+
replace: (s, pattern, replacement) => {
|
|
96
|
+
if (!_.isString(s)) {
|
|
97
|
+
return s;
|
|
98
|
+
}
|
|
99
|
+
const regex = new RegExp(pattern, 'g');
|
|
100
|
+
return s.replace(regex, replacement);
|
|
101
|
+
},
|
|
102
|
+
md5: (s) => {
|
|
103
|
+
return md5(s);
|
|
104
|
+
},
|
|
105
|
+
lpad: (s, length, v) => {
|
|
106
|
+
if (!_.isString(s)) {
|
|
107
|
+
s = s.toString();
|
|
108
|
+
}
|
|
109
|
+
return s.padStart(length, v);
|
|
110
|
+
},
|
|
111
|
+
rpad: (s, length, v) => {
|
|
112
|
+
if (!_.isString(s)) {
|
|
113
|
+
s = s.toString();
|
|
114
|
+
}
|
|
115
|
+
return s.padEnd(length, v);
|
|
116
|
+
},
|
|
117
|
+
asc: (n) => {
|
|
118
|
+
const inner = (n) => {
|
|
119
|
+
if (_.isString(n)) {
|
|
120
|
+
n = Number(n);
|
|
121
|
+
}
|
|
122
|
+
if (n <= 0) {
|
|
123
|
+
return '';
|
|
124
|
+
}
|
|
125
|
+
return String.fromCharCode(n);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
if (_.isArray(n)) {
|
|
129
|
+
return _.map(n, inner).join('');
|
|
130
|
+
}
|
|
131
|
+
return inner(n);
|
|
132
|
+
},
|
|
133
|
+
ord: (s, index = 0) => {
|
|
134
|
+
if (!_.isString(s)) {
|
|
135
|
+
return NaN;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return s.charCodeAt(index);
|
|
139
|
+
}
|
|
140
|
+
}, { override: true });
|
|
141
|
+
|
|
142
|
+
module.exports = math;
|