diff --git a/src/plugins/condition/Condition.js b/src/plugins/condition/Condition.js index c35650bd84..ab3bc0858d 100644 --- a/src/plugins/condition/Condition.js +++ b/src/plugins/condition/Condition.js @@ -24,6 +24,7 @@ import * as EventEmitter from 'eventemitter3'; import uuid from 'uuid'; import TelemetryCriterion from "@/plugins/condition/criterion/TelemetryCriterion"; import { TRIGGER } from "@/plugins/condition/utils/constants"; +import {computeCondition} from "@/plugins/condition/utils/evaluator"; /* * conditionDefinition = { @@ -56,6 +57,7 @@ export default class ConditionClass extends EventEmitter { this.openmct = openmct; this.id = this.openmct.objects.makeKeyString(conditionDefinition.identifier); this.criteria = []; + this.criteriaResults = {}; if (conditionDefinition.definition.criteria) { this.createCriteria(conditionDefinition.definition.criteria); } @@ -71,12 +73,11 @@ export default class ConditionClass extends EventEmitter { update(newDomainObject) { this.updateTrigger(newDomainObject.definition.trigger); this.updateCriteria(newDomainObject.definition.criteria); - this.handleConditionUpdated(); } - updateTrigger(conditionDefinition) { - if (this.trigger !== conditionDefinition.trigger) { - this.trigger = conditionDefinition.trigger; + updateTrigger(trigger) { + if (this.trigger !== trigger) { + this.trigger = trigger; this.handleConditionUpdated(); } } @@ -109,6 +110,7 @@ export default class ConditionClass extends EventEmitter { let criterionDefinitionWithId = this.generateCriterion(criterionDefinition || null); let criterion = new TelemetryCriterion(criterionDefinitionWithId, this.openmct); criterion.on('criterionUpdated', (obj) => this.handleCriterionUpdated(obj)); + criterion.on('criterionResultUpdated', (obj) => this.handleCriterionResult(obj)); if (!this.criteria) { this.criteria = []; } @@ -138,12 +140,17 @@ export default class ConditionClass extends EventEmitter { if (found) { const newCriterionDefinition = this.generateCriterion(criterionDefinition); let newCriterion = new TelemetryCriterion(newCriterionDefinition, this.openmct); + newCriterion.on('criterionUpdated', (obj) => this.handleCriterionUpdated(obj)); + newCriterion.on('criterionResultUpdated', (obj) => this.handleCriterionResult(obj)); + let criterion = found.item; criterion.unsubscribe(); - criterion.off('criterionUpdated', (result) => { - this.handleCriterionUpdated(id, result); - }); + criterion.off('criterionUpdated', (obj) => this.handleCriterionUpdated(obj)); + criterion.off('criterionResultUpdated', (obj) => this.handleCriterionResult(obj)); this.criteria.splice(found.index, 1, newCriterion); + if (this.criteriaResults[criterion.id] !== undefined) { + delete this.criteriaResults[criterion.id]; + } this.handleConditionUpdated(); } } @@ -163,6 +170,9 @@ export default class ConditionClass extends EventEmitter { this.handleCriterionUpdated(id, result); }); this.criteria.splice(found.index, 1); + if (this.criteriaResults[criterion.id] !== undefined) { + delete this.criteriaResults[criterion.id]; + } return true; } return false; @@ -173,14 +183,30 @@ export default class ConditionClass extends EventEmitter { if (found) { this.criteria[found.index] = criterion.data; //Most likely don't need this. + this.subscribe(); this.emitEvent('conditionUpdated', { trigger: this.trigger, criteria: this.criteria }); } + } + + handleCriterionResult(eventData) { + let id = eventData.id; + let result = eventData.data.result; + let found = this.findCriterion(id); + if (found) { + this.criteriaResults[id] = result; + } this.handleConditionUpdated(); } + subscribe() { + this.criteria.forEach((criterion) => { + criterion.subscribe(); + }) + } + handleConditionUpdated() { // trigger an updated event so that consumers can react accordingly this.evaluate(); @@ -202,11 +228,7 @@ export default class ConditionClass extends EventEmitter { //TODO: implement as part of the evaluator class task. evaluate() { - if (this.trigger === TRIGGER.ANY) { - this.result = true; - } else if (this.trigger === TRIGGER.ALL) { - this.result = false; - } + this.result = computeCondition(this.criteriaResults, this.trigger === TRIGGER.ALL); } emitEvent(eventName, data) { diff --git a/src/plugins/condition/ConditionSetViewProvider.js b/src/plugins/condition/ConditionSetViewProvider.js index 38a3d65726..267f10718c 100644 --- a/src/plugins/condition/ConditionSetViewProvider.js +++ b/src/plugins/condition/ConditionSetViewProvider.js @@ -58,7 +58,7 @@ export default class ConditionSetViewProvider { isEditing } }, - template: '' + template: '' }); }, onEditModeChange: (isEditing) => { diff --git a/src/plugins/condition/ConditionSpec.js b/src/plugins/condition/ConditionSpec.js index cc32914871..18334bcada 100644 --- a/src/plugins/condition/ConditionSpec.js +++ b/src/plugins/condition/ConditionSpec.js @@ -64,15 +64,17 @@ describe("The condition", function () { openmct.telemetry.getMetadata.and.returnValue(testTelemetryObject.telemetry.values); testConditionDefinition = { - trigger: TRIGGER.ANY, - criteria: [ - { - operation: 'equalTo', - input: false, - metaDataKey: 'value', - key: testTelemetryObject.identifier - } - ] + definition: { + trigger: TRIGGER.ANY, + criteria: [ + { + operation: 'equalTo', + input: false, + metaDataKey: 'value', + key: testTelemetryObject.identifier + } + ] + } }; conditionObj = new Condition( @@ -85,7 +87,7 @@ describe("The condition", function () { }); it("generates criteria with an id", function () { - const testCriterion = testConditionDefinition.criteria[0]; + const testCriterion = testConditionDefinition.definition.criteria[0]; let criterion = conditionObj.generateCriterion(testCriterion); expect(criterion.id).toBeDefined(); expect(criterion.operation).toEqual(testCriterion.operation); @@ -102,14 +104,13 @@ describe("The condition", function () { expect(conditionObj.criteria.length).toEqual(1); let criterion = conditionObj.criteria[0]; expect(criterion instanceof TelemetryCriterion).toBeTrue(); - expect(criterion.operator).toEqual(testConditionDefinition.operator); - expect(criterion.input).toEqual(testConditionDefinition.input); - expect(criterion.metaDataKey).toEqual(testConditionDefinition.metaDataKey); - expect(criterion.key).toEqual(testConditionDefinition.key); + expect(criterion.operator).toEqual(testConditionDefinition.definition.criteria[0].operator); + expect(criterion.input).toEqual(testConditionDefinition.definition.criteria[0].input); + expect(criterion.metaDataKey).toEqual(testConditionDefinition.definition.criteria[0].metaDataKey); }); it("initializes with the trigger from the condition definition", function () { - expect(conditionObj.trigger).toEqual(testConditionDefinition.trigger); + expect(conditionObj.trigger).toEqual(testConditionDefinition.definition.trigger); }); it("destroys all criteria for a condition", function () { diff --git a/src/plugins/condition/components/Condition.vue b/src/plugins/condition/components/Condition.vue index ae041c6ed2..1e37bc6479 100644 --- a/src/plugins/condition/components/Condition.vue +++ b/src/plugins/condition/components/Condition.vue @@ -1,4 +1,5 @@