Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Fix for rules with 2 LHS conditions not properly being able to trigger an action when only one condition becomes false and true again (#1546)

Dominique Kleeven642a061d44f9
diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
index 362cee635..344f92c47 100644
--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
@@ -480,6 +480,25 @@ public class JsonRulesBuilder extends RulesBuilder {
groupMatches = ruleConditionGroup.getItems().stream()
.map(ruleCondition -> conditionStateMap.get(ruleCondition.tag))
.allMatch(ruleConditionState -> ruleConditionState.lastEvaluationResult != null && ruleConditionState.lastEvaluationResult.matches);
+
+
+ // Prevent stale matches from blocking rule re-triggering for multi LHS condition groups
+ if (!groupMatches && ruleConditionGroup.getItems().size() > 1) {
+
+ boolean anyConditionHasNoPreviousMatches = ruleConditionGroup.getItems().stream()
+ .map(ruleCondition -> conditionStateMap.get(ruleCondition.tag))
+ .anyMatch(ruleConditionState -> ruleConditionState.previouslyMatchedAssetStates.isEmpty());
+
+ // Clear previously matched asset states, so they can be re-matched and trigger the rule when the group matches again
+ if (anyConditionHasNoPreviousMatches) {
+ ruleConditionGroup.getItems().forEach(ruleCondition -> {
+ RuleConditionState conditionState = conditionStateMap.get(ruleCondition.tag);
+ if (conditionState != null && !conditionState.previouslyMatchedAssetStates.isEmpty()) {
+ conditionState.previouslyMatchedAssetStates.clear();
+ }
+ });
+ }
+ }
} else {
groupMatches = ruleConditionGroup.getItems().stream()
.map(ruleCondition -> conditionStateMap.get(ruleCondition.tag))
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
index 05d5fa3a2..98751c3e0 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
@@ -1281,6 +1281,147 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
}
}
+ def "Trigger actions based on multiple LHS conditions"() {
+ given: "the container environment is started"
+ def conditions = new PollingConditions(timeout: 15, delay: 0.2)
+ def container = startContainer(defaultConfig(), defaultServices())
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def rulesService = container.getService(RulesService.class)
+ def rulesetStorageService = container.getService(RulesetStorageService.class)
+ def timerService = container.getService(TimerService.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ RulesEngine realmBuildingEngine
+
+ and: "the pseudo clock is stopped"
+ stopPseudoClock()
+
+ when: "a light asset is added to the building realm"
+ def lightId = UniqueIdentifierGenerator.generateId("Light")
+ def lightAsset = new LightAsset("Light")
+ .setId(lightId)
+ .setRealm(keycloakTestSetup.realmBuilding.name)
+ .setLocation(new GeoJSONPoint(0, 0))
+
+
+ def ruleState = new MetaItem<>(MetaItemType.RULE_STATE)
+ lightAsset.getAttributes().get("brightness").get().addMeta(ruleState)
+ lightAsset.getAttributes().get("onOff").get().addMeta(ruleState)
+ assetStorageService.merge(lightAsset)
+
+ then: "the light asset should be present"
+ conditions.eventually {
+ def light = assetStorageService.find(lightId)
+ assert light != null
+ }
+
+ // RuleSet: WHEN:
+ // Any light asset has brightness greater than 50
+ // AND
+ // Any light asset has onOff set to true
+ // THEN:
+ // Set the notes of the light asset to "Matched"
+ when: "a ruleset with multiple LHS conditions has been added"
+ def rulesStr = getClass().getResource("/org/openremote/test/rules/JsonMultipleLHSConditionsRule.json").text
+ def rule = parse(rulesStr, JsonRulesetDefinition.class).orElseThrow()
+ Ruleset ruleset = new RealmRuleset(
+ keycloakTestSetup.realmBuilding.name,
+ "Multi LHS Rule",
+ Ruleset.Lang.JSON,
+ rulesStr)
+ ruleset = rulesetStorageService.merge(ruleset)
+ def lastFireTimestamp = 0
+
+ then: "the rule engines to become available and be running with asset states inserted"
+ conditions.eventually {
+ realmBuildingEngine = rulesService.realmEngines.get(keycloakTestSetup.realmBuilding.name)
+ assert realmBuildingEngine != null
+ assert realmBuildingEngine.isRunning()
+ assert realmBuildingEngine.facts.assetStates.size() == DEMO_RULE_STATES_SMART_BUILDING + 2 // 2 additional rule states
+ assert realmBuildingEngine.lastFireTimestamp == timerService.getNow().toEpochMilli()
+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
+ }
+
+ and: "the light asset should have its notes attribute be empty"
+ conditions.eventually {
+ def light = assetStorageService.find(lightId)
+ assert light.getAttribute("notes").get().getValue().orElse("") == ""
+ }
+
+ when: "the light asset brightness is updated to 100 and onOff is updated to true and time advances for 3 seconds"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.BRIGHTNESS, 100))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.ON_OFF, true))
+
+ // advance time to 3 seconds to ensure the rule engine has fired
+ advancePseudoClock(3, TimeUnit.SECONDS, container)
+
+ then: "the light asset should have its notes attribute be Matched because the LHS conditions were satisfied"
+ conditions.eventually {
+ def light = assetStorageService.find(lightId)
+ assert light.getAttribute("notes").get().getValue().orElse("") == "Matched"
+ }
+
+ and: "the rule engine should have fired"
+ conditions.eventually {
+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
+ }
+
+ when: "notes is updated to empty and time advances for 3 seconds"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.NOTES, ""))
+
+ // advance time to 3 seconds to ensure the rule engine has fired
+ advancePseudoClock(3, TimeUnit.SECONDS, container)
+
+ then: "the light asset should have its notes attribute be empty"
+ conditions.eventually {
+ def light = assetStorageService.find(lightId)
+ assert light.getAttribute("notes").get().getValue().orElse("") == ""
+ }
+
+ and: "the rule engine should have fired"
+ conditions.eventually {
+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
+ }
+
+ when: "brightness is updated to 10, thus not satisfying the LHS conditions and time advances for 3 seconds"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.BRIGHTNESS, 10))
+
+ // advance time to 3 seconds to ensure the rule engine has fired
+ advancePseudoClock(3, TimeUnit.SECONDS, container)
+
+ then: "the light asset should have its notes attribute be empty because the LHS conditions were not satisfied"
+ conditions.eventually {
+ def light = assetStorageService.find(lightId)
+ assert light.getAttribute("notes").get().getValue().orElse("") == ""
+ }
+
+ and: "the rule engine should have fired"
+ conditions.eventually {
+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
+ }
+
+ when: "brightness is updated to 100, thus satisfying the LHS conditions and time advances for 3 seconds"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(lightId, LightAsset.BRIGHTNESS, 100))
+
+ // advance time to 3 seconds to ensure the rule engine has fired
+ advancePseudoClock(3, TimeUnit.SECONDS, container)
+
+ then: "the light asset should have its notes attribute be set to Matched because the LHS conditions were satisfied"
+ conditions.eventually {
+ def light = assetStorageService.find(lightId)
+ assert light.getAttribute("notes").get().getValue().orElse("") == "Matched"
+ }
+
+ and: "the rule engine should have fired"
+ conditions.eventually {
+ assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
+ }
+ }
+
def "Trigger actions based on the position of the sun"() {
given: "the container environment is started"
diff --git a/test/src/test/resources/org/openremote/test/rules/JsonMultipleLHSConditionsRule.json b/test/src/test/resources/org/openremote/test/rules/JsonMultipleLHSConditionsRule.json
new file mode 100644
index 000000000..c12022482
--- /dev/null
+++ b/test/src/test/resources/org/openremote/test/rules/JsonMultipleLHSConditionsRule.json
@@ -0,0 +1,79 @@
+{
+ "rules": [
+ {
+ "recurrence": {
+ "mins": 0
+ },
+ "when": {
+ "operator": "OR",
+ "groups": [
+ {
+ "operator": "AND",
+ "items": [
+ {
+ "assets": {
+ "types": [
+ "LightAsset"
+ ],
+ "attributes": {
+ "items": [
+ {
+ "name": {
+ "predicateType": "string",
+ "match": "EXACT",
+ "value": "onOff"
+ },
+ "value": {
+ "predicateType": "boolean",
+ "value": true
+ }
+ }
+ ]
+ }
+ }
+ },
+ {
+ "assets": {
+ "types": [
+ "LightAsset"
+ ],
+ "attributes": {
+ "items": [
+ {
+ "name": {
+ "predicateType": "string",
+ "match": "EXACT",
+ "value": "brightness"
+ },
+ "value": {
+ "predicateType": "number",
+ "operator": "GREATER_THAN",
+ "value": 50
+ }
+ }
+ ]
+ }
+ }
+ }
+ ]
+ }
+ ]
+ },
+ "then": [
+ {
+ "action": "write-attribute",
+ "target": {
+ "matchedAssets": {
+ "types": [
+ "LightAsset"
+ ]
+ }
+ },
+ "value": "Matched",
+ "attributeName": "notes"
+ }
+ ],
+ "name": "Multi LHS Rule"
+ }
+ ]
+}
\ No newline at end of file