100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Breaking change to JSON Rules: Overhaul of JSON rules added otherwise, onStart, onStop and removed and (now combined with when) to rule object model
Rich Turner1e01ec121f66
diff --git a/agent/src/main/java/org/openremote/agent/protocol/timer/CronExpressionParser.java b/agent/src/main/java/org/openremote/agent/protocol/timer/CronExpressionParser.javaindex cf1e1a370..1a5b19728 100644--- a/agent/src/main/java/org/openremote/agent/protocol/timer/CronExpressionParser.java+++ b/agent/src/main/java/org/openremote/agent/protocol/timer/CronExpressionParser.java@@ -20,6 +20,7 @@package org.openremote.agent.protocol.timer;import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.TextUtil;import org.quartz.CronExpression;import java.util.EnumSet;@@ -130,7 +131,7 @@ public class CronExpressionParser {protected boolean compatible = true;protected boolean valid;- protected CronExpressionParser(String cronExpression) {+ public CronExpressionParser(String cronExpression) {valid = CronExpression.isValidExpression(cronExpression);originalCronExpression = cronExpression;String[] fields = cronExpression.split("\\s+");@@ -140,7 +141,7 @@ public class CronExpressionParser {compatible = false;} else {for (int i=0; i<3; i++) {- Integer value = parseNumberExpression(fields[i]);+ Integer value = TextUtil.asInteger(fields[i]).orElse(null);if (value == null) {compatible = false;} else {@@ -155,7 +156,7 @@ public class CronExpressionParser {}}- protected boolean isCompatible() {+ public boolean isCompatible() {return compatible;}@@ -163,7 +164,7 @@ public class CronExpressionParser {return valid;}- protected String buildCronExpression() {+ public String buildCronExpression() {if (!isCompatible()) {return originalCronExpression;} else {@@ -171,7 +172,7 @@ public class CronExpressionParser {}}- protected void setTime(int hours, int minutes, int seconds) {+ public void setTime(int hours, int minutes, int seconds) {hours = Math.max(0, hours % 24);minutes = Math.max(0, minutes % 60);seconds = Math.max(0, seconds % 60);@@ -182,17 +183,7 @@ public class CronExpressionParser {timeValues[SECONDS] = seconds;}- protected String getFormattedTime() {+ public String getFormattedTime() {return String.format("%02d:%02d:%02d", timeValues[HOURS], timeValues[MINUTES], timeValues[SECONDS]);}-- protected static Integer parseNumberExpression(String field) {- try {- return Integer.parseInt(field);- } catch(NumberFormatException e) {- LOG.info("Field is not a simple integer expression: " + field);- return null;- }- }-}diff --git a/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.javaindex a097c43b7..d245ef44f 100644--- a/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java@@ -256,9 +256,9 @@ public class TimerProtocol extends AbstractProtocol {Integer seconds;if (writeTimeValues.length != 3- || (hours = CronExpressionParser.parseNumberExpression(writeTimeValues[0])) == null- || (minutes = CronExpressionParser.parseNumberExpression(writeTimeValues[1])) == null- || (seconds = CronExpressionParser.parseNumberExpression(writeTimeValues[2])) == null) {+ || (hours = TextUtil.asInteger(writeTimeValues[0]).orElse(null)) == null+ || (minutes = TextUtil.asInteger(writeTimeValues[1]).orElse(null)) == null+ || (seconds = TextUtil.asInteger(writeTimeValues[2]).orElse(null)) == null) {LOG.info("Expected value to be in format HH:MM:SS, actual: " + writeValue);return;}diff --git a/container/src/main/java/org/openremote/container/util/Util.java b/container/src/main/java/org/openremote/container/util/Util.javaindex 0495770d1..c4c0eaa9a 100644--- a/container/src/main/java/org/openremote/container/util/Util.java+++ b/container/src/main/java/org/openremote/container/util/Util.java@@ -4,6 +4,10 @@ import java.lang.reflect.Array;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;+import java.util.Set;+import java.util.concurrent.ConcurrentHashMap;+import java.util.function.Function;+import java.util.function.Predicate;/*** TODO Old functions for old rules, can be deleted?@@ -139,4 +143,8 @@ public class Util {return (String.format("%.1f", d) + suffix);}+ public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {+ Set<Object> seen = ConcurrentHashMap.newKeySet();+ return t -> seen.add(keyExtractor.apply(t));+ }}diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex bcccc893d..b2ae5f621 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -34,6 +34,7 @@ import org.openremote.model.query.filter.*;import org.openremote.model.rules.AssetState;import org.openremote.model.rules.json.RuleCondition;import org.openremote.model.rules.json.RuleOperator;+import org.openremote.model.rules.json.RuleTrigger;import org.openremote.model.util.Pair;import org.openremote.model.util.TimeUtil;import org.openremote.model.value.Value;@@ -427,40 +428,8 @@ public class AssetQueryPredicate implements Predicate<AssetState> {return meta -> meta.stream().anyMatch(metaItemPredicate);}- public static Predicate<RulesFacts> asPredicate(TimerService timerService, AssetStorageService assetStorageService, RuleCondition<NewAssetQuery> condition) {-- if ((condition.predicates == null || condition.predicates.length == 0)- && (condition.conditions == null || condition.conditions.length == 0)) {- return facts -> true;- }-- RuleOperator operator = condition.operator == null ? RuleOperator.AND : condition.operator;-- List<Predicate<RulesFacts>> assetPredicates = new ArrayList<>();-- if (condition.predicates != null && condition.predicates.length > 0) {- assetPredicates.addAll(- Arrays.stream(condition.predicates)- .map(p ->- (Predicate<RulesFacts>) facts ->- facts.matchAssetState(p).findFirst().isPresent())- .collect(Collectors.toList())- );- }-- if (condition.conditions != null && condition.conditions.length > 0) {- assetPredicates.addAll(- Arrays.stream(condition.conditions)- .map(c -> asPredicate(timerService, assetStorageService, c)).collect(Collectors.toList())- );- }-- return asPredicate(assetPredicates, operator);- }-public static Predicate<AssetState> asPredicate(Supplier<Long> currentMillisProducer, RuleCondition<AttributePredicate> condition) {- if ((condition.predicates == null || condition.predicates.length == 0)- && (condition.conditions == null || condition.conditions.length == 0)) {+ if (conditionIsEmpty(condition)) {return as -> true;}@@ -490,7 +459,12 @@ public class AssetQueryPredicate implements Predicate<AssetState> {return asPredicate(assetStatePredicates, operator);}- protected static <T> Predicate<T> asPredicate(List<Predicate<T>> predicates, RuleOperator operator) {+ protected static boolean conditionIsEmpty(RuleCondition condition) {+ return (condition.predicates == null || condition.predicates.length == 0)+ && (condition.conditions == null || condition.conditions.length == 0);+ }++ protected static <T> Predicate<T> asPredicate(Collection<Predicate<T>> predicates, RuleOperator operator) {return in -> {boolean matched = false;diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.javaindex 57cc0af93..eefb67698 100644--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java@@ -19,43 +19,44 @@*/package org.openremote.manager.rules;+import com.fasterxml.jackson.core.JsonProcessingException;+import org.openremote.container.Container;import org.openremote.container.timer.TimerService;import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.concurrent.ManagerExecutorService;+import org.openremote.manager.rules.facade.AssetsFacade;import org.openremote.manager.rules.facade.NotificationsFacade;-import org.openremote.model.AbstractValueHolder;import org.openremote.model.asset.Asset;import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeValueDescriptor;import org.openremote.model.notification.Notification;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.BaseAssetQuery;+import org.openremote.model.query.NewAssetQuery;import org.openremote.model.query.UserQuery;+import org.openremote.model.query.filter.AttributePredicate;import org.openremote.model.rules.AssetState;import org.openremote.model.rules.Assets;-import org.openremote.model.rules.Ruleset;import org.openremote.model.rules.Users;import org.openremote.model.rules.json.*;+import org.openremote.model.util.Pair;import org.openremote.model.util.TextUtil;+import org.openremote.model.util.TimeUtil;import org.openremote.model.value.*;+import org.quartz.CronExpression;-import java.util.List;-import java.util.Objects;-import java.util.Optional;+import java.util.*;import java.util.function.BiConsumer;-import java.util.function.BiPredicate;import java.util.function.Predicate;+import java.util.logging.Level;import java.util.stream.Collectors;import java.util.stream.Stream;-public class JsonRulesBuilder extends RulesBuilder {-- static class RuleFiredInfo {- AssetState firedAssetState;+import static org.openremote.container.util.Util.distinctByKey;+import static org.openremote.manager.rules.AssetQueryPredicate.conditionIsEmpty;+import static org.openremote.model.query.filter.LocationAttributePredicate.getLocationPredicates;- RuleFiredInfo(AssetState firedAssetState) {- this.firedAssetState = firedAssetState;- }- }+public class JsonRulesBuilder extends RulesBuilder {static class Targets {public TargetType type;@@ -67,6 +68,224 @@ public class JsonRulesBuilder extends RulesBuilder {}}+ static class RuleActionExecution {+ Runnable runnable;+ long delay;++ public RuleActionExecution(Runnable runnable, long delay) {+ this.runnable = runnable;+ this.delay = delay;+ }+ }++ static class RuleTriggerState {++ final TimerService timerService;+ boolean notifiedLocationPredicates;+ BaseAssetQuery.OrderBy orderBy;+ int limit;+ RuleCondition<AttributePredicate> attributePredicates = null;+ RuleTrigger ruleTrigger;+ Set<AssetState> unfilteredAssetStates = new HashSet<>();+ Set<AssetState> previouslyMatchedAssetStates = new HashSet<>();+ Set<AssetState> previouslyUnmatchedAssetStates;+ Map<AssetState, Long> previouslyMatchedExpiryTimes;+ long nextExecuteMillis;+ long resetDurationMillis;+ Runnable updateNextExecuteTime;+ CronExpression timerExpression;+ RuleTriggerResult lastTriggerResult;++ public RuleTriggerState(RuleTrigger ruleTrigger, boolean trackUnmatched, TimerService timerService) {+ this.timerService = timerService;+ this.ruleTrigger = ruleTrigger;++ if (trackUnmatched) {+ previouslyUnmatchedAssetStates = new HashSet<>();+ }++ if (ruleTriggerResetHasTimer(ruleTrigger.reset)) {+ previouslyMatchedExpiryTimes = new HashMap<>();+ if (!TextUtil.isNullOrEmpty(ruleTrigger.reset.timer) && TimeUtil.isTimeDuration(ruleTrigger.reset.timer)) {+ resetDurationMillis = TimeUtil.parseTimeDuration(ruleTrigger.reset.timer);+ }+ }++ if (!TextUtil.isNullOrEmpty(ruleTrigger.timer)) {+ try {+ if (TimeUtil.isTimeDuration(ruleTrigger.timer)) {+ nextExecuteMillis = timerService.getCurrentTimeMillis();+ long duration = TimeUtil.parseTimeDuration(ruleTrigger.timer);+ updateNextExecuteTime = () -> nextExecuteMillis += duration;+ }+ if (CronExpression.isValidExpression(ruleTrigger.timer)) {+ timerExpression = new CronExpression(ruleTrigger.timer);+ nextExecuteMillis = timerExpression.getNextValidTimeAfter(new Date(timerService.getCurrentTimeMillis())).getTime();+ updateNextExecuteTime = () ->+ nextExecuteMillis = timerExpression.getNextInvalidTimeAfter(timerExpression.getNextInvalidTimeAfter(new Date(nextExecuteMillis))).getTime();+ }+ } catch (Exception e) {+ RulesEngine.RULES_LOG.log(Level.SEVERE, "Failed to parse rule trigger timer expression: " + ruleTrigger.timer, e);+ }+ }++ // Pull out order, limit and attribute predicates so they can be applied at required times+ if (ruleTrigger.assets != null) {+ orderBy = ruleTrigger.assets.orderBy;+ limit = ruleTrigger.assets.limit;+ attributePredicates = ruleTrigger.assets.attributes;+ ruleTrigger.assets.orderBy = null;+ ruleTrigger.assets.limit = 0;+ ruleTrigger.assets.attributes = null;+ }+ }++ void updateUnfilteredAssetStates(RulesFacts facts) {+ if (ruleTrigger.timer != null) {+ unfilteredAssetStates = new HashSet<>(facts.getAssetStates());+ } else if (ruleTrigger.assets != null) {+ NewAssetQuery query = ruleTrigger.assets;+ unfilteredAssetStates = facts.matchAssetState(query).collect(Collectors.toSet());++ // Use this opportunity to notify RulesFacts about any location predicates+ if (!notifiedLocationPredicates) {+ List<AttributePredicate> flattenedAttributePredicates = RuleCondition.flatten(Collections.singletonList(attributePredicates));+ facts.storeLocationPredicates(getLocationPredicates(flattenedAttributePredicates));+ notifiedLocationPredicates = true;+ }+ }+ }+++ void update() {+ if (!TextUtil.isNullOrEmpty(ruleTrigger.timer)) {+ lastTriggerResult = null;++ if (updateNextExecuteTime != null && nextExecuteMillis < timerService.getCurrentTimeMillis()) {+ updateNextExecuteTime.run();+ lastTriggerResult = new RuleTriggerResult(true, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList());+ }++ return;+ }++ if (unfilteredAssetStates.isEmpty()) {+ // Maybe assets have been deleted so remove any previous match data+ previouslyMatchedAssetStates.clear();+ if (previouslyUnmatchedAssetStates != null) {+ previouslyUnmatchedAssetStates.clear();+ }+ lastTriggerResult = new RuleTriggerResult(false, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList());+ return;+ }++ List<AssetState> matchedAssetStates;+ List<AssetState> unmatchedAssetStates = Collections.emptyList();++ if (attributePredicates == null) {+ matchedAssetStates = new ArrayList<>(unfilteredAssetStates);+ } else {+ Predicate<AssetState> predicate = AssetQueryPredicate.asPredicate(timerService::getCurrentTimeMillis, attributePredicates);+ Map<Boolean,List<AssetState>> results = unfilteredAssetStates.stream().collect(Collectors.groupingBy(predicate::test));+ matchedAssetStates = results.get(true);+ unmatchedAssetStates = results.get(false);++ if (previouslyUnmatchedAssetStates != null) {+ // Clear out previous unmatched that now match+ previouslyUnmatchedAssetStates.removeIf(matchedAssetStates::contains);++ // Filter out previous un-matches to avoid re-triggering+ unmatchedAssetStates.removeIf(previouslyUnmatchedAssetStates::contains);+ }+ }++ // Apply reset logic to make previously matched asset states eligible for matching again+ if (ruleTrigger.reset == null || ruleTrigger.reset.noLongerMatches) {+ previouslyMatchedAssetStates.removeIf(assetState -> !matchedAssetStates.contains(assetState));+ }++ if (ruleTriggerResetHasTimer(ruleTrigger.reset)) {+ previouslyMatchedAssetStates.removeIf(assetState -> {+ Long timestamp = previouslyMatchedExpiryTimes.get(assetState);+ boolean expired = timestamp != null && timerService.getCurrentTimeMillis() > timestamp;+ if (expired) {+ previouslyMatchedExpiryTimes.remove(assetState);+ }+ return expired;+ });+ }++ if (ruleTrigger.reset != null && ruleTrigger.reset.valueChanges) {+ previouslyMatchedAssetStates.removeIf(previousAssetState -> {+ int index = matchedAssetStates.indexOf(previousAssetState);+ if (index < 0) {+ return false;+ }++ return !Objects.equals(previousAssetState.getValue().orElse(null), matchedAssetStates.get(index).getValue().orElse(null));+ });+ }++ // Filter out previous matches to avoid re-triggering+ matchedAssetStates.removeIf(assetState -> previouslyMatchedAssetStates.contains(assetState));++ // Select unique assets from the states+ Stream<AssetState> matchedAssetStateStream = matchedAssetStates.stream().filter(distinctByKey(AssetState::getId));+ Stream<AssetState> unmatchedAssetStateStream = unmatchedAssetStates.stream().filter(distinctByKey(AssetState::getId));++ // Order and limit+ if (orderBy != null) {+ matchedAssetStateStream = matchedAssetStateStream.sorted(RulesFacts.asComparator(orderBy));+ }++ if (limit > 0) {+ matchedAssetStateStream = matchedAssetStateStream.limit(limit);+ }++ Collection<String> matchedAssetIds = matchedAssetStateStream.map(AssetState::getId).collect(Collectors.toList());++ // Filter out unmatched asset ids that are in the matched list+ Collection<String> unmatchedAssetIds = unmatchedAssetStateStream+ .filter(assetState -> !matchedAssetIds.contains(assetState.getId()))+ .map(AssetState::getId)+ .collect(Collectors.toList());++ lastTriggerResult = new RuleTriggerResult((!matchedAssetIds.isEmpty() || !unmatchedAssetIds.isEmpty()), matchedAssetStates, matchedAssetIds, unmatchedAssetStates, unmatchedAssetIds);+ }++ Collection<String> getMatchedAssetIds() {++ if (lastTriggerResult == null) {+ return Collections.emptyList();+ }+ return lastTriggerResult.matchedAssetIds;+ }++ Collection<String> getUnmatchedAssetIds() {+ if (lastTriggerResult == null) {+ return Collections.emptyList();+ }++ return lastTriggerResult.unmatchedAssetIds;+ }+ }++ static class RuleTriggerResult {+ boolean matches;+ Collection<AssetState> matchedAssetStates;+ Collection<AssetState> unmatchedAssetStates;+ Collection<String> matchedAssetIds;+ Collection<String> unmatchedAssetIds;++ public RuleTriggerResult(boolean matches, Collection<AssetState> matchedAssetStates, Collection<String> matchedAssetIds, Collection<AssetState> unmatchedAssetStates, Collection<String> unmatchedAssetIds) {+ this.matches = matches;+ this.matchedAssetStates = matchedAssetStates;+ this.matchedAssetIds = matchedAssetIds;+ this.unmatchedAssetStates = unmatchedAssetStates;+ this.unmatchedAssetIds = unmatchedAssetIds;+ }+ }+enum TargetType {ASSET,USER@@ -79,6 +298,7 @@ public class JsonRulesBuilder extends RulesBuilder {final protected NotificationsFacade notificationFacade;final protected ManagerExecutorService executorService;final protected BiConsumer<Runnable, Long> scheduledActionConsumer;+ final protected Map<String, Map<String, RuleTriggerState>> ruleTriggerStateMap = new HashMap<>();public JsonRulesBuilder(TimerService timerService, AssetStorageService assetStorageService, ManagerExecutorService executorService, Assets assetsFacade, Users usersFacade, NotificationsFacade notificationFacade, BiConsumer<Runnable, Long> scheduledActionConsumer) {this.timerService = timerService;@@ -90,36 +310,22 @@ public class JsonRulesBuilder extends RulesBuilder {this.scheduledActionConsumer = scheduledActionConsumer;}- protected static String buildResetFactName(Ruleset ruleset, Rule rule, AssetState assetState) {- return ruleset.getId() + "_" + ruleset.getVersion() + "_" + rule.name + "_" + assetState.getId() + "_" + assetState.getAttributeName();+ public void onAssetStatesChanged(RulesFacts facts) {+ ruleTriggerStateMap.values().forEach(triggerStateMap -> triggerStateMap.values().forEach(ruleTriggerState -> ruleTriggerState.updateUnfilteredAssetStates(facts)));}- protected static List<String> getUserIds(Users users, UserQuery userQuery) {- return users.query()- .tenant(userQuery.tenantPredicate)- .assetPath(userQuery.pathPredicate)- .asset(userQuery.assetPredicate)- .limit(userQuery.limit)- .getResults();- }+ public JsonRulesBuilder add(Rule rule) {- protected static List<String> getAssetIds(Assets assets, org.openremote.model.query.AssetQuery assetQuery) {- return assets.query()- .ids(assetQuery.ids)- .name(assetQuery.name)- .parent(assetQuery.parent)- .path(assetQuery.path)- .type(assetQuery.type)- .attributes(assetQuery.attribute)- .attributeMeta(assetQuery.attributeMeta)- .stream()- .collect(Collectors.toList());- }+ if (ruleTriggerStateMap.containsKey(rule.name)) {+ throw new IllegalArgumentException("Rules must have a unique name within a ruleset, rule name '" + rule.name + "' already used");+ }- public JsonRulesBuilder add(Ruleset ruleset, Rule rule) {+ Map<String, RuleTriggerState> triggerStateMap = new HashMap<>();+ ruleTriggerStateMap.put(rule.name, triggerStateMap);+ addRuleTriggerState(rule.when, rule.otherwise != null, 0, triggerStateMap, rule.reset);- Condition condition = buildLhsCondition(ruleset, rule);- Action action = buildAction(ruleset, rule);+ Condition condition = buildLhsCondition(rule, triggerStateMap);+ Action action = buildRhsAction(rule, triggerStateMap);if (condition == null || action == null) {throw new IllegalArgumentException("Error building JSON rule '" + rule.name + "'");@@ -143,369 +349,431 @@ public class JsonRulesBuilder extends RulesBuilder {}).then(action);- Condition resetCondition = buildResetCondition(ruleset, rule);- Action resetAction = buildResetAction();-- if (resetCondition != null && resetAction != null) {- add().name(rule.name + " RESET")- .description(rule.description + " Reset")- .priority(rule.priority + 1)- .when(facts -> {- Object result;- try {- result = resetCondition.evaluate(facts);- } catch (Exception ex) {- throw new RuntimeException("Error evaluating condition of rule '" + rule.name + " RESET': " + ex.getMessage(), ex);- }- if (result instanceof Boolean) {- return result;- } else {- throw new IllegalArgumentException("Error evaluating condition of rule '" + rule.name + " RESET': result is not boolean but " + result);- }- })- .then(resetAction);- }-return this;}- protected Condition buildLhsCondition(Ruleset ruleset, Rule rule) {- if (rule.when == null || (rule.when.asset == null && rule.when.timer == null)) {- return facts -> false;+ protected void addRuleTriggerState(RuleCondition<RuleTrigger> ruleTriggerCondition, boolean trackUnmatched, int index, Map<String, RuleTriggerState> triggerStateMap, RuleTriggerReset defaultReset) {+ if (ruleTriggerCondition != null) {+ if (ruleTriggerCondition.predicates != null && ruleTriggerCondition.predicates.length > 0) {+ for (RuleTrigger ruleTrigger : ruleTriggerCondition.predicates) {+ if (TextUtil.isNullOrEmpty(ruleTrigger.tag)) {+ ruleTrigger.tag = Integer.toString(index);+ }+ if (ruleTrigger.reset == null) {+ ruleTrigger.reset = defaultReset;+ }+ triggerStateMap.put(ruleTrigger.tag, new RuleTriggerState(ruleTrigger, trackUnmatched, timerService));+ index++;+ }+ }+ if (ruleTriggerCondition.conditions != null && ruleTriggerCondition.conditions.length > 0) {+ for (RuleCondition<RuleTrigger> childRuleTriggerCondition : ruleTriggerCondition.conditions) {+ addRuleTriggerState(childRuleTriggerCondition, trackUnmatched, index, triggerStateMap, defaultReset);+ }+ }}+ }- Predicate<RulesFacts> whenPredicate = facts -> false;- Predicate<RulesFacts> andPredicate = facts -> true;+ protected Condition buildLhsCondition(Rule rule, Map<String, RuleTriggerState> triggerStateMap) {+ if (rule.when == null) {+ return facts -> false;+ }- if (rule.when.asset != null) {- // Pull out order and limit so they can be applied after filtering already triggered asset states- BaseAssetQuery.OrderBy orderBy = rule.when.asset.orderBy;- int limit = rule.when.asset.limit;- rule.when.asset.orderBy = null;- rule.when.asset.limit = 0;+ return facts -> {- whenPredicate = facts -> {+ // Update each trigger state+ triggerStateMap.values().forEach(RuleTriggerState::update);- Stream<AssetState> assetStates = facts.matchAssetState(rule.when.asset);+ // Check triggers for matches+ return matches(rule.when, triggerStateMap);+ };+ }- // Apply reset predicate (to prevent re-running a rule on an asset state before the reset has triggered)- assetStates = assetStates.filter(as -> {- String factName = buildResetFactName(ruleset, rule, as);- return !facts.getOptional(factName).isPresent();- });+ protected Action buildRhsAction(Rule rule, Map<String, RuleTriggerState> triggerStateMap) {- if (orderBy != null) {- assetStates = assetStates.sorted(RulesFacts.asComparator(rule.when.asset.orderBy));- }+ return facts -> {- if (limit > 0) {- assetStates = assetStates.limit(limit);- }+ executeRuleActions(rule.then, false, facts, triggerStateMap, assetsFacade, usersFacade, notificationFacade, timerService, assetStorageService, scheduledActionConsumer);- List<AssetState> assetStateList = assetStates.collect(Collectors.toList());+ if (rule.otherwise != null) {+ executeRuleActions(rule.otherwise, true, facts, triggerStateMap, assetsFacade, usersFacade, notificationFacade, timerService, assetStorageService, scheduledActionConsumer);+ }+ };+ }- if (!assetStateList.isEmpty()) {- // Push matched asset states into RHS- facts.bind("assetStates", assetStateList);- return true;+ public static void executeRuleActions(RuleAction[] ruleActions, boolean useUnmatched, RulesFacts facts, Map<String, RuleTriggerState> triggerStateMap, Assets assetsFacade, Users usersFacade, NotificationsFacade notificationsFacade, TimerService timerService, AssetStorageService assetStorageService, BiConsumer<Runnable, Long> scheduledActionConsumer) {++ // Push rule trigger results into the trigger state for future runs and store timestamps where required+ if (triggerStateMap != null) {+ triggerStateMap.values().forEach(ruleTriggerState -> {+ if (ruleTriggerState.lastTriggerResult != null) {++ if (!useUnmatched) {+ // Remove any stale matched asset states+ ruleTriggerState.previouslyMatchedAssetStates.removeAll(ruleTriggerState.lastTriggerResult.matchedAssetStates);+ ruleTriggerState.previouslyMatchedAssetStates.addAll(ruleTriggerState.lastTriggerResult.matchedAssetStates);++ RuleTriggerReset reset = ruleTriggerState.ruleTrigger.reset;+ if (ruleTriggerResetHasTimer(reset)) {+ if (reset.timestampChanges) {+ ruleTriggerState.lastTriggerResult.matchedAssetStates.forEach(assetState ->+ ruleTriggerState.previouslyMatchedExpiryTimes.put(assetState, assetState.getTimestamp()));+ } else if (ruleTriggerState.resetDurationMillis > 0) {+ ruleTriggerState.lastTriggerResult.matchedAssetStates.forEach(assetState ->+ ruleTriggerState.previouslyMatchedExpiryTimes.put(assetState, timerService.getCurrentTimeMillis() + ruleTriggerState.resetDurationMillis));+ }+ }+ } else {+ if (ruleTriggerState.previouslyUnmatchedAssetStates != null) {+ ruleTriggerState.previouslyUnmatchedAssetStates.addAll(ruleTriggerState.lastTriggerResult.unmatchedAssetStates);+ }+ }}-- return false;- };- } else if (!TextUtil.isNullOrEmpty(rule.when.timer)) {- // TODO: Create timer condition+ });}- if (rule.and != null) {- andPredicate = AssetQueryPredicate.asPredicate(timerService, assetStorageService, rule.and);- }+ if (ruleActions != null && ruleActions.length > 0) {- Predicate<RulesFacts> finalWhenPredicate = whenPredicate;- Predicate<RulesFacts> finalAndPredicate = andPredicate;+ long delay = 0L;- return facts -> {- if (!finalWhenPredicate.test(facts)) {- return false;- }+ for (RuleAction ruleAction : ruleActions) {+ JsonRulesBuilder.RuleActionExecution actionExecution = buildRuleActionExecution(+ ruleAction,+ useUnmatched,+ facts,+ triggerStateMap,+ assetsFacade,+ usersFacade,+ notificationsFacade,+ assetStorageService);- if (!finalAndPredicate.test(facts)) {- return false;+ if (actionExecution != null) {+ delay += actionExecution.delay;++ if (delay > 0L) {+ scheduledActionConsumer.accept(actionExecution.runnable, delay);+ } else {+ actionExecution.runnable.run();+ }+ }}+ }+ }- return true;- };+ protected static Collection<String> getUserIds(Users users, UserQuery userQuery) {+ return users.query()+ .tenant(userQuery.tenantPredicate)+ .assetPath(userQuery.pathPredicate)+ .asset(userQuery.assetPredicate)+ .limit(userQuery.limit)+ .getResults();}- protected Action buildAction(Ruleset ruleset, Rule rule) {+ protected static Collection<String> getAssetIds(Assets assets, org.openremote.model.query.AssetQuery assetQuery) {+ return assets.query()+ .ids(assetQuery.ids)+ .name(assetQuery.name)+ .parent(assetQuery.parent)+ .path(assetQuery.path)+ .type(assetQuery.type)+ .attributes(assetQuery.attribute)+ .attributeMeta(assetQuery.attributeMeta)+ .stream()+ .map(Asset::getId)+ .collect(Collectors.toList());+ }- return facts -> {+ protected static RuleActionExecution buildRuleActionExecution(RuleAction ruleAction, boolean useUnmatched, RulesFacts facts, Map<String, RuleTriggerState> triggerStateMap, Assets assetsFacade, Users usersFacade, NotificationsFacade notificationsFacade, AssetStorageService assetStorageService) {- List<AssetState> assetStates = facts.bound("assetStates");+ if (ruleAction instanceof RuleActionNotification) {+ RuleActionNotification notificationAction = (RuleActionNotification) ruleAction;- if (assetStates != null) {- for (AssetState assetState : assetStates) {+ if (notificationAction.notification != null) {- // Add RuleFiredInfo fact to limit re-triggering of the rule for a given asset state- if (rule.reset == null || TextUtil.isNullOrEmpty(rule.reset.timer)) {- facts.put(buildResetFactName(ruleset, rule, assetState), new RuleFiredInfo(assetState));- } else if (!TextUtil.isNullOrEmpty(rule.reset.timer)) {- facts.putTemporary(buildResetFactName(ruleset, rule, assetState), rule.reset.timer, new RuleFiredInfo(assetState));- }+ // Override the notification targets if set in the rule+ Notification.TargetType targetType = Notification.TargetType.ASSET;+ Collection<String> ids = getRuleActionTargetIds(ruleAction.target, useUnmatched, triggerStateMap, assetsFacade, usersFacade);+ if (targetIsNotAssets(ruleAction.target)) {+ targetType = Notification.TargetType.USER;}- }- if (rule.then == null) {- return;- }+ if (ids != null) {+ notificationAction.notification.setTargets(+ new Notification.Targets(targetType, ids));+ }- long delay = 0;+ return new RuleActionExecution(() -> notificationsFacade.send(notificationAction.notification), 0);+ }+ }- for (RuleAction ruleAction : rule.then) {+ if (ruleAction instanceof RuleActionWriteAttribute) {- Runnable action = null;+ if (targetIsNotAssets(ruleAction.target)) {+ return null;+ }- if (ruleAction instanceof RuleActionNotification) {+ RuleActionWriteAttribute attributeAction = (RuleActionWriteAttribute) ruleAction;- RuleActionNotification notificationAction = (RuleActionNotification) ruleAction;+ if (!TextUtil.isNullOrEmpty(attributeAction.attributeName)) {+ Collection<String> ids = getRuleActionTargetIds(ruleAction.target, useUnmatched, triggerStateMap, assetsFacade, usersFacade);- if (notificationAction.notification != null) {+ if (ids != null) {+ return new RuleActionExecution(() ->+ ids.forEach(id ->+ assetsFacade.dispatch(id, attributeAction.attributeName, attributeAction.value)), 0);+ }+ }+ }- // Override the notification targets if set in the rule- if (notificationAction.target != null) {- RuleActionWithTarget.Target target = notificationAction.target;- Notification.TargetType targetType = Notification.TargetType.ASSET;- List<String> ids = null;+ if (ruleAction instanceof RuleActionWait) {+ long millis = ((RuleActionWait) ruleAction).millis;+ if (millis > 0) {+ return new RuleActionExecution(null, millis);+ }+ }- if (target.useAssetsFromWhen && assetStates != null) {- ids = assetStates.stream().map(AssetState::getId).collect(Collectors.toList());- } else if (target.assets != null) {- ids = getAssetIds(assetsFacade, target.assets);- } else if (target.users != null) {- targetType = Notification.TargetType.USER;- ids = getUserIds(usersFacade, target.users);- }+ if (ruleAction instanceof RuleActionUpdateAttribute) {- if (ids != null) {- notificationAction.notification.setTargets(- new Notification.Targets(- targetType,- ids));- }- }+ if (targetIsNotAssets(ruleAction.target)) {+ return null;+ }- action = () -> notificationFacade.send(notificationAction.notification);- }- } else if (ruleAction instanceof RuleActionWriteAttribute) {+ RuleActionUpdateAttribute attributeUpdateAction = (RuleActionUpdateAttribute) ruleAction;- RuleActionWriteAttribute attributeAction = (RuleActionWriteAttribute) ruleAction;+ if (!TextUtil.isNullOrEmpty(attributeUpdateAction.attributeName)) {- if (!TextUtil.isNullOrEmpty(attributeAction.attributeName)) {+ Map<String, Pair<ValueType, Value>> assetAttributeValueMap = new HashMap<>();+ AssetQuery assetQuery = null;- RuleActionWithTarget.Target target = attributeAction.target;- List<String> ids = null;+ if (ruleAction.target == null || ruleAction.target.assets == null) {+ Collection<String> assetIds = getRuleActionTargetIds(ruleAction.target, useUnmatched, triggerStateMap, assetsFacade, usersFacade);- if (target != null) {+ // Try and find the current value within the asset states in memory to avoid a DB call when possible+ Collection<String> assetsToLookup = assetIds.stream().filter(assetId -> {+ Optional<AssetState> assetState = facts.getAssetStates()+ .stream()+ .filter(state -> state.getId().equals(assetId) && state.getAttributeName().equals(attributeUpdateAction.attributeName))+ .findFirst();- // Only assets make sense as the target- if (target.useAssetsFromWhen && assetStates != null) {- ids = assetStates.stream().map(AssetState::getId).collect(Collectors.toList());- } else if (target.assets != null) {- ids = getAssetIds(assetsFacade, target.assets);+ if (assetState.isPresent()) {+ ValueType valueType = assetState.get().getValue().map(Value::getType).orElseGet(() -> assetState.get().getAttributeValueType() != null ? assetState.get().getAttributeValueType().getValueType() : null);+ if (!(valueType == ValueType.ARRAY || valueType == ValueType.OBJECT)) {+ RulesEngine.RULES_LOG.warning("JSON Rule: Rule action target asset '" + assetState.get().getId() + "' cannot determine value type or incompatible value type for attribute: " + attributeUpdateAction.attributeName);+ } else {+ assetAttributeValueMap.put(assetState.get().getId(), new Pair<>(valueType, assetState.get().getValue().orElse(null)));}+ return false;}+ return true;+ }).collect(Collectors.toList());- if (ids != null) {- List<String> finalIds = ids;- action = () ->- finalIds.forEach(id ->- assetsFacade.dispatch(id, attributeAction.attributeName, attributeAction.value));- }+ if (!assetsToLookup.isEmpty()) {+ assetQuery = new AssetQuery().ids(assetsToLookup);}- } else if (ruleAction instanceof RuleActionWait) {- long millis = ((RuleActionWait) ruleAction).millis;- if (millis > 0) {- delay += millis;- }- } else if (ruleAction instanceof RuleActionUpdateAttribute) {- RuleActionUpdateAttribute attributeUpdateAction = (RuleActionUpdateAttribute) ruleAction;-- if (!TextUtil.isNullOrEmpty(attributeUpdateAction.attributeName)) {- RuleActionWithTarget.Target target = attributeUpdateAction.target;-- List<String> ids = null;-- if (target != null) {+ } else {+ assetQuery = ruleAction.target.assets;+ }- // Only assets make sense as the target- if (target.useAssetsFromWhen && assetStates != null) {- ids = assetStates.stream().map(AssetState::getId).collect(Collectors.toList());- } else if (target.assets != null) {- ids = getAssetIds(assetsFacade, target.assets);+ if (assetQuery != null) {+ Stream<Asset> assets = getRuleActionAssets(ruleAction.target, useUnmatched, triggerStateMap, assetsFacade, usersFacade, assetStorageService);+ assets.forEach(asset -> {+ Attribute attribute = asset.getAttribute(attributeUpdateAction.attributeName).orElse(null);+ if (attribute == null) {+ RulesEngine.RULES_LOG.warning("JSON Rule: Rule action target asset '" + asset.getId() + "' doesn't have requested attribute: " + attributeUpdateAction.attributeName);+ } else {+ ValueType valueType = attribute.getValue().map(Value::getType).orElseGet(() -> attribute.getType().map(AttributeValueDescriptor::getValueType).orElse(null));+ if (!(valueType == ValueType.ARRAY || valueType == ValueType.OBJECT)) {+ RulesEngine.RULES_LOG.warning("JSON Rule: Rule action target asset '" + asset.getId() + "' cannot determine value type or incompatible value type for attribute: " + attributeUpdateAction.attributeName);+ } else {+ assetAttributeValueMap.put(asset.getId(), new Pair<>(valueType, attribute.getValue().orElse(null)));}}+ });+ }- if (ids != null) {- List<String> finalIds = ids;- action = () ->- finalIds.forEach(id -> {- ValueType valueType = null;- Value currentValue = null;- if (assetStates != null) {- Optional<AssetState> assetState = assetStates- .stream()- .filter(state -> state.getId().equals(id) && state.getAttributeName().equals(attributeUpdateAction.attributeName))- .findFirst();- if (assetState.isPresent()) {- valueType = assetState.get().getAttributeValueType().getValueType();- if (valueType.equals(ValueType.ARRAY)) {- currentValue = assetState.get().getValue().orElse(Values.createArray());- } else if (valueType.equals(ValueType.OBJECT)) {- currentValue = assetState.get().getValue().orElse(Values.createObject());- } else {- throw new IllegalArgumentException("Only Attributes of type ArrayValue or ObjectValue are allowed for RuleActionUpdateAttribute");+ return new RuleActionExecution(() ->+ assetAttributeValueMap.forEach((id, valueAndType) -> {+ ValueType valueType = valueAndType.key;+ Value value = valueAndType.value;+ boolean skip = false;++ switch (attributeUpdateAction.updateAction) {+ case ADD:+ if (valueType.equals(ValueType.ARRAY)) {+ value = value == null ? Values.createArray() : value;+ ((ArrayValue) value).add(attributeUpdateAction.value);+ } else {+ value = value == null ? Values.createObject() : value;+ ((ObjectValue) value).put(attributeUpdateAction.key, attributeUpdateAction.value);+ }+ break;+ case ADD_OR_REPLACE:+ case REPLACE:+ if (valueType.equals(ValueType.ARRAY)) {+ value = value == null ? Values.createArray() : value;+ ArrayValue arrayValue = (ArrayValue) value;++ if (attributeUpdateAction.index != null && arrayValue.length() >= attributeUpdateAction.index) {+ arrayValue.set(attributeUpdateAction.index, attributeUpdateAction.value);+ } else {+ arrayValue.add(attributeUpdateAction.value);+ }+ } else {+ value = value == null ? Values.createObject() : value;+ if (!TextUtil.isNullOrEmpty(attributeUpdateAction.key)) {+ ((ObjectValue) value).put(attributeUpdateAction.key, attributeUpdateAction.value);+ } else {+ try {+ RulesEngine.RULES_LOG.warning("JSON Rule: Rule action missing required 'key': " + Container.JSON.writeValueAsString(attributeUpdateAction));+ } catch (JsonProcessingException ignored) {}}}-- if (valueType == null || currentValue == null) {- Asset asset = assetStorageService.find(new AssetQuery().select(new BaseAssetQuery.Select(BaseAssetQuery.Include.ONLY_ID_AND_NAME_AND_ATTRIBUTES)).id(id).attributeName(attributeUpdateAction.attributeName));- if (asset != null) {- valueType = asset.getAttribute(attributeUpdateAction.attributeName).flatMap(Attribute::getType).get().getValueType();- if (valueType.equals(ValueType.ARRAY)) {- currentValue = asset.getAttribute(attributeUpdateAction.attributeName).flatMap(AbstractValueHolder::getValueAsArray).orElse(Values.createArray());- } else if (valueType.equals(ValueType.OBJECT)) {- currentValue = asset.getAttribute(attributeUpdateAction.attributeName).flatMap(AbstractValueHolder::getValueAsObject).orElse(Values.createObject());- } else {- throw new IllegalArgumentException("Only Attributes of type ArrayValue or ObjectValue are allowed for RuleActionUpdateAttribute");- }+ break;+ case DELETE:+ if (value == null) {+ skip = true;+ } else {+ if (valueType.equals(ValueType.ARRAY)) {+ ((ArrayValue) value).remove(attributeUpdateAction.index);+ } else {+ ((ObjectValue) value).remove(attributeUpdateAction.key);}}-- if (valueType != null && currentValue != null) {-- switch (attributeUpdateAction.updateAction) {- case ADD:- if (valueType.equals(ValueType.ARRAY)) {- ((ArrayValue) currentValue).add(attributeUpdateAction.value);- } else {- ((ObjectValue) currentValue).put(attributeUpdateAction.key, attributeUpdateAction.value);- }- break;- case ADD_OR_REPLACE:- case REPLACE:- if (valueType.equals(ValueType.ARRAY)) {- ((ArrayValue) currentValue).set(attributeUpdateAction.index, attributeUpdateAction.value);- } else {- ((ObjectValue) currentValue).put(attributeUpdateAction.key, attributeUpdateAction.value);- }- break;- case DELETE:- if (valueType.equals(ValueType.ARRAY)) {- ((ArrayValue) currentValue).remove(attributeUpdateAction.index);- } else {- ((ObjectValue) currentValue).remove(attributeUpdateAction.key);- }- break;- case CLEAR:- if (valueType.equals(ValueType.ARRAY)) {- currentValue = Values.createArray();- } else {- currentValue = Values.createObject();- }- break;+ break;+ case CLEAR:+ if (value == null) {+ skip = true;+ } else {+ if (valueType.equals(ValueType.ARRAY)) {+ value = Values.createArray();+ } else {+ value = Values.createObject();}- assetsFacade.dispatch(id, attributeUpdateAction.attributeName, currentValue);}- });- }- }- }+ break;+ }- if (action != null) {- if (delay > 0) {- scheduledActionConsumer.accept(action, delay);- } else {- action.run();- }- }+ if (!skip) {+ assetsFacade.dispatch(id, attributeUpdateAction.attributeName, value);+ }+ }), 0);}+ }- };+ return null;}- protected Condition buildResetCondition(Ruleset ruleset, Rule rule) {+ protected static Stream<Asset> getRuleActionAssets(RuleActionTarget target, boolean useUnmatched, Map<String, RuleTriggerState> triggerStateMap, Assets assetsFacade, Users usersFacade, AssetStorageService assetStorageService) {+ AssetsFacade.RestrictedQuery query = assetsFacade.query();- // Timer only reset is handled in rule action with temporary fact- if (rule.reset == null || (!rule.reset.triggerNoLongerMatches && !rule.reset.attributeTimestampChange- && !rule.reset.attributeValueChange)) {-- return null;+ if (target == null || target.assets == null) {+ Collection<String> ids = getRuleActionTargetIds(target, useUnmatched, triggerStateMap, assetsFacade, usersFacade);+ if (ids.isEmpty()) {+ return Stream.empty();+ }+ query.ids(ids);+ } else {+ query.ids(target.assets.ids)+ .name(target.assets.name)+ .parent(target.assets.parent)+ .path(target.assets.path)+ .type(target.assets.type)+ .attributes(target.assets.attribute)+ .attributeMeta(target.assets.attributeMeta);}- RuleTriggerReset reset = rule.reset;+ query.select = new BaseAssetQuery.Select(BaseAssetQuery.Include.ONLY_ID_AND_NAME_AND_ATTRIBUTES);+ return assetStorageService.findAll(query).stream();+ }- Predicate<AssetState> noLongerMatchesPredicate = ruleFiredInfo -> false;- BiPredicate<AssetState, RuleFiredInfo> attributeTimestampPredicate = (assetState, ruleFiredInfo) -> false;- BiPredicate<AssetState, RuleFiredInfo> attributeValuePredicate = (assetState, ruleFiredInfo) -> false;+ protected static Collection<String> getRuleActionTargetIds(RuleActionTarget target, boolean useUnmatched, Map<String, RuleTriggerState> triggerStateMap, Assets assetsFacade, Users usersFacade) {+ if (target != null) {+ if (!TextUtil.isNullOrEmpty(target.ruleTriggerTag) && triggerStateMap != null) {+ RuleTriggerState triggerState = triggerStateMap.get(target.ruleTriggerTag);+ if (!useUnmatched) {+ return triggerState != null ? triggerState.getMatchedAssetIds() : Collections.emptyList();+ }++ return triggerState != null ? triggerState.getUnmatchedAssetIds() : Collections.emptyList();+ }- if (reset.triggerNoLongerMatches && rule.when != null && rule.when.asset != null) {- noLongerMatchesPredicate = new AssetQueryPredicate(timerService, assetStorageService, rule.when.asset).negate();+ if (target.assets != null) {+ return getAssetIds(assetsFacade, target.assets);+ }++ if (target.users != null) {+ return getUserIds(usersFacade, target.users);+ }}- if (reset.attributeTimestampChange) {- attributeTimestampPredicate = (assetState, ruleFiredInfo) -> {- long firedTimestamp = ruleFiredInfo.firedAssetState.getTimestamp();- long currentTimestamp = assetState.getTimestamp();- return firedTimestamp != currentTimestamp;- };+ if (triggerStateMap != null) {+ if (!useUnmatched) {+ return triggerStateMap.values().stream().flatMap(triggerState ->+ triggerState != null ? triggerState.getMatchedAssetIds().stream() : Stream.empty()+ ).collect(Collectors.toList());+ }++ return triggerStateMap.values().stream().flatMap(triggerState ->+ triggerState != null ? triggerState.getUnmatchedAssetIds().stream() : Stream.empty()+ ).collect(Collectors.toList());}- if (reset.attributeValueChange) {- attributeValuePredicate = (assetState, ruleFiredInfo) -> {- Value firedValue = ruleFiredInfo.firedAssetState.getValue().orElse(null);- Value currentValue = assetState.getValue().orElse(null);- return !Objects.equals(firedValue, currentValue);- };+ return Collections.emptyList();+ }++ protected static boolean matches(RuleCondition<RuleTrigger> ruleTriggerCondition, Map<String, RuleTriggerState> triggerStateMap) {++ boolean match = false;++ if (conditionIsEmpty(ruleTriggerCondition)) {+ return false;}- Predicate<AssetState> finalNoLongerMatchesPredicate = noLongerMatchesPredicate;- BiPredicate<AssetState, RuleFiredInfo> finalAttributeTimestampPredicate = attributeTimestampPredicate;- BiPredicate<AssetState, RuleFiredInfo> finalAttributeValuePredicate = attributeValuePredicate;+ RuleOperator operator = ruleTriggerCondition.operator == null ? RuleOperator.AND : ruleTriggerCondition.operator;- BiPredicate<AssetState, RuleFiredInfo> resetPredicate = (assetState, ruleFiredInfo) ->- finalNoLongerMatchesPredicate.test(assetState)- || finalAttributeTimestampPredicate.test(assetState, ruleFiredInfo)- || finalAttributeValuePredicate.test(assetState, ruleFiredInfo);+ if (operator == RuleOperator.AND) {+ if (ruleTriggerCondition.predicates != null) {+ match = Arrays.stream(ruleTriggerCondition.predicates)+ .map(ruleTrigger -> triggerStateMap.get(ruleTrigger.tag))+ .allMatch(ruleTriggerState -> ruleTriggerState.lastTriggerResult != null && ruleTriggerState.lastTriggerResult.matches);- return facts -> {- List<String> resetAssetStates = facts.getAssetStates()- .stream()- .filter(as -> {+ if (!match) {+ return false;+ }+ }- String factName = buildResetFactName(ruleset, rule, as);- Optional<RuleFiredInfo> firedInfo = facts.getOptional(factName);+ if (ruleTriggerCondition.conditions != null) {+ match = Arrays.stream(ruleTriggerCondition.conditions)+ .allMatch(condition -> matches(condition, triggerStateMap));+ }+ }- return firedInfo.filter(ruleFiredInfo -> resetPredicate.test(as, ruleFiredInfo)).isPresent();+ if (operator == RuleOperator.OR) {+ if (ruleTriggerCondition.predicates != null) {+ match = Arrays.stream(ruleTriggerCondition.predicates)+ .map(ruleTrigger -> triggerStateMap.get(ruleTrigger.tag))+ .anyMatch(ruleTriggerState -> ruleTriggerState.lastTriggerResult != null && ruleTriggerState.lastTriggerResult.matches);- })- .map(as -> buildResetFactName(ruleset, rule, as))- .collect(Collectors.toList());+ if (match) {+ return true;+ }+ }- if (resetAssetStates.isEmpty()) {- return false;+ if (ruleTriggerCondition.conditions != null) {+ match = Arrays.stream(ruleTriggerCondition.conditions)+ .anyMatch(condition -> matches(condition, triggerStateMap));}+ }- facts.bind("resetAssetStates", resetAssetStates);- return true;- };+ return match;}- protected Action buildResetAction() {- return facts -> {- List<String> resetAssets = facts.bound("resetAssetStates");- if (resetAssets != null) {- resetAssets.forEach(facts::remove);- }- };+ protected static boolean ruleTriggerResetHasTimer(RuleTriggerReset reset) {+ return reset != null && (!TextUtil.isNullOrEmpty(reset.timer) || reset.timestampChanges);+ }++ protected static boolean targetIsNotAssets(RuleActionTarget target) {+ return target != null && target.assets == null && TextUtil.isNullOrEmpty(target.ruleTriggerTag);}}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/RulesBuilder.javaindex 200eb3fb5..bc2046052 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesBuilder.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesBuilder.java@@ -30,6 +30,7 @@ import java.util.List;*/public class RulesBuilder {+ @FunctionalInterfacepublic interface Condition {Object evaluate(RulesFacts facts);}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.javaindex 3488b75a2..34b8c22b8 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java@@ -95,9 +95,12 @@ public class RulesEngine<T extends Ruleset> {final protected InferenceRulesEngine engine;protected boolean running;+ protected long lastFireTimestamp;protected boolean trackLocationPredicates;protected ScheduledFuture fireTimer;protected ScheduledFuture statsTimer;+ protected List<RulesetDeployment> newlyAddedDeployments = new ArrayList<>();+ protected boolean assetStatesChanged = false;// Only used to optimize toString(), contains the details of this engineprotected String deploymentInfo;@@ -208,6 +211,7 @@ public class RulesEngine<T extends Ruleset> {// Check if ruleset is already deployed (maybe an older version)if (deployment != null) {+ deployment.onStop(facts);deployment.stop();LOG.info("Removing ruleset deployment: " + ruleset);deployments.remove(ruleset.getId());@@ -215,6 +219,7 @@ public class RulesEngine<T extends Ruleset> {}deployment = new RulesetDeployment(ruleset, timerService, assetStorageService, executorService, assetsFacade, usersFacade, notificationFacade);+ newlyAddedDeployments.add(deployment);boolean compilationSuccessful = deployment.start();@@ -254,7 +259,6 @@ public class RulesEngine<T extends Ruleset> {deployment.getStatus(),deployment.getErrorMessage());-start();}@@ -262,14 +266,15 @@ public class RulesEngine<T extends Ruleset> {* @return <code>true</code> if this rules engine has no deployments.*/public boolean removeRuleset(Ruleset ruleset) {- if (!deployments.containsKey(ruleset.getId())) {+ RulesetDeployment deployment = deployments.remove(ruleset.getId());++ if (deployment == null) {LOG.finer("Ruleset cannot be retracted as it was never deployed: " + ruleset);return deployments.size() == 0;}stop();-- RulesetDeployment deployment = deployments.remove(ruleset.getId());+ deployment.onStop(facts);deployment.stop();updateDeploymentInfo();@@ -385,8 +390,20 @@ public class RulesEngine<T extends Ruleset> {// Remove any expired temporary factsfacts.removeExpiredTemporaryFacts();+ // Call on start for any newly deployed rulesets+ newlyAddedDeployments.forEach(rulesetDeployment -> rulesetDeployment.onStart(facts));+ newlyAddedDeployments.clear();++ // Notify rulesets if assetstates changed+ boolean notifyAssetStatesChanged = assetStatesChanged;+ assetStatesChanged = false;+for (RulesetDeployment deployment : deployments.values()) {try {+ if (notifyAssetStatesChanged) {+ deployment.onAssetStatesChanged(facts);+ }+RULES_LOG.fine("Firing rules @" + clock + " of: " + deployment);// If full detail logging is enabled@@ -399,6 +416,7 @@ public class RulesEngine<T extends Ruleset> {facts.reset();long startTimestamp = System.currentTimeMillis();+ lastFireTimestamp = startTimestamp;engine.fire(deployment.getRules(), facts);RULES_LOG.fine("Rules executed in: " + (System.currentTimeMillis() - startTimestamp) + "ms");@@ -456,6 +474,7 @@ public class RulesEngine<T extends Ruleset> {}public void updateFact(AssetState assetState, boolean fireImmediately) {+ assetStatesChanged = true;facts.putAssetState(assetState);trackLocationPredicates = trackLocationPredicates || assetState.getAttributeName().equals(AttributeType.LOCATION.getAttributeName());if (fireImmediately) {@@ -464,12 +483,14 @@ public class RulesEngine<T extends Ruleset> {}public void removeFact(AssetState assetState) {+ assetStatesChanged = true;facts.removeAssetState(assetState);trackLocationPredicates = trackLocationPredicates || assetState.getAttributeName().equals(AttributeType.LOCATION.getAttributeName());fire();}public void insertFact(String expires, AssetState assetState) {+ assetStatesChanged = true;facts.insertAssetEvent(expires, assetState);trackLocationPredicates = trackLocationPredicates || assetState.getAttributeName().equals(AttributeType.LOCATION.getAttributeName());fire();diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.javaindex bfa6620b5..dce2dbae4 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java@@ -100,7 +100,7 @@ public class RulesFacts extends Facts implements RuleListener {trackLocationRules = false;Map<String, Set<GeofencePredicate>> assetStateLocationPredicateMap = this.assetStateLocationPredicateMap;this.assetStateLocationPredicateMap = null;- return assetStateLocationPredicateMap.entrySet().stream()+ return assetStateLocationPredicateMap == null ? null : assetStateLocationPredicateMap.entrySet().stream().map(assetStateSetEntry ->new RulesEngine.AssetStateLocationPredicates(assetStateSetEntry.getKey(),diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.javaindex 0ccde3be6..b41ef0e08 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java@@ -54,6 +54,7 @@ import java.util.logging.Level;import java.util.logging.Logger;import static org.openremote.container.concurrent.GlobalLock.withLock;+import static org.openremote.manager.rules.JsonRulesBuilder.executeRuleActions;public class RulesetDeployment {@@ -128,19 +129,34 @@ public class RulesetDeployment {final protected ManagerExecutorService executorService;final protected Assets assetsFacade;final protected Users usersFacade;- final protected NotificationsFacade notificationFacade;+ final protected NotificationsFacade notificationsFacade;final protected List<ScheduledFuture> scheduledRuleActions = new ArrayList<>();protected RulesetStatus status;protected Throwable error;+ protected JsonRulesBuilder jsonRulesBuilder;+ protected JsonRulesetDefinition jsonRulesetDefinition;- public RulesetDeployment(Ruleset ruleset, TimerService timerService, AssetStorageService assetStorageService, ManagerExecutorService executorService, Assets assetsFacade, Users usersFacade, NotificationsFacade notificationFacade) {+ public RulesetDeployment(Ruleset ruleset, TimerService timerService, AssetStorageService assetStorageService, ManagerExecutorService executorService, Assets assetsFacade, Users usersFacade, NotificationsFacade notificationsFacade) {this.ruleset = ruleset;this.timerService = timerService;this.assetStorageService = assetStorageService;this.executorService = executorService;this.assetsFacade = assetsFacade;this.usersFacade = usersFacade;- this.notificationFacade = notificationFacade;+ this.notificationsFacade = notificationsFacade;++ if (ruleset.getLang() == Ruleset.Lang.JSON) {+ try {+ String rulesStr = ruleset.getRules();+ rulesStr = rulesStr.replace("%RULESET_ID%", Long.toString(ruleset.getId()));+ rulesStr = rulesStr.replace("%RULESET_NAME%", ruleset.getName());+ jsonRulesetDefinition = Container.JSON.readValue(rulesStr, JsonRulesetDefinition.class);+ jsonRulesBuilder = new JsonRulesBuilder(timerService, assetStorageService, executorService, assetsFacade, usersFacade, notificationsFacade, this::scheduleRuleAction);+ } catch (IOException e) {+ RulesEngine.LOG.log(Level.SEVERE, "Error evaluating ruleset: " + ruleset, e);+ setError(e);+ }+ }}public long getId() {@@ -163,19 +179,39 @@ public class RulesetDeployment {return rules;}+ /**+ * Called when a ruleset is about to be started (allows for initialisation tasks)+ */+ public void onStart(RulesFacts facts) {+ if (jsonRulesetDefinition != null && jsonRulesetDefinition.rules != null && jsonRulesetDefinition.rules.length > 0) {+ Arrays.stream(jsonRulesetDefinition.rules).forEach(jsonRule ->+ executeRuleActions(jsonRule.onStart, false, facts, null, assetsFacade, usersFacade, notificationsFacade, timerService, assetStorageService, this::scheduleRuleAction));+ }+ }+public boolean start() {RulesEngine.LOG.info("Evaluating ruleset deployment: " + ruleset);switch (ruleset.getLang()) {case JAVASCRIPT:- return startRulesJavascript(ruleset, assetsFacade, usersFacade, notificationFacade);+ return startRulesJavascript(ruleset, assetsFacade, usersFacade, notificationsFacade);case GROOVY:- return startRulesGroovy(ruleset, assetsFacade, usersFacade, notificationFacade);+ return startRulesGroovy(ruleset, assetsFacade, usersFacade, notificationsFacade);case JSON:- return startRulesJson(ruleset, assetsFacade, usersFacade, notificationFacade);+ return startRulesJson(ruleset);}return false;}+ /**+ * Called when a ruleset is about to be stopped (allows for cleanup tasks)+ */+ public void onStop(RulesFacts facts) {+ if (jsonRulesetDefinition != null && jsonRulesetDefinition.rules != null && jsonRulesetDefinition.rules.length > 0) {+ Arrays.stream(jsonRulesetDefinition.rules).forEach(jsonRule ->+ executeRuleActions(jsonRule.onStop, false, facts, null, assetsFacade, usersFacade, notificationsFacade, timerService, assetStorageService, this::scheduleRuleAction));+ }+ }+/*** Called when this deployment is stopped, could be the ruleset is being updated, removed or an error has occurred* during execution@@ -188,6 +224,12 @@ public class RulesetDeployment {}));}+ public void onAssetStatesChanged(RulesFacts facts) {+ if (jsonRulesBuilder != null) {+ jsonRulesBuilder.onAssetStatesChanged(facts);+ }+ }+protected void scheduleRuleAction(Runnable action, long delayMillis) {withLock(toString() + "::scheduleRuleAction", () -> {ScheduledFuture future = executorService.schedule(() ->@@ -199,29 +241,22 @@ public class RulesetDeployment {});}- protected boolean startRulesJson(Ruleset ruleset, Assets assetsFacade, Users usersFacade, NotificationsFacade notificationFacade) {- try {- String rulesStr = ruleset.getRules();- rulesStr = rulesStr.replace("%RULESET_ID%", Long.toString(ruleset.getId()));- rulesStr = rulesStr.replace("%RULESET_NAME%", ruleset.getName());- JsonRulesetDefinition jsonRulesetDefinition = Container.JSON.readValue(rulesStr, JsonRulesetDefinition.class);- JsonRulesBuilder jsonRulesBuilder = new JsonRulesBuilder(timerService, assetStorageService, executorService, assetsFacade, usersFacade, notificationFacade, this::scheduleRuleAction);-- Arrays.stream(jsonRulesetDefinition.rules).forEach(r -> jsonRulesBuilder.add(ruleset, r));+ protected boolean startRulesJson(Ruleset ruleset) {- for (Rule rule : jsonRulesBuilder.build()) {- RulesEngine.LOG.info("Registering JSON rule: " + rule.getName());- rules.register(rule);- }- RulesEngine.LOG.info("Evaluated ruleset deployment: " + ruleset);+ if (jsonRulesetDefinition == null || jsonRulesetDefinition.rules == null || jsonRulesetDefinition.rules.length == 0) {+ RulesEngine.LOG.log(Level.WARNING, "No rules within ruleset so nothing to start: " + ruleset);+ return false;+ }- return true;+ Arrays.stream(jsonRulesetDefinition.rules).forEach(r -> jsonRulesBuilder.add(r));- } catch (IOException e) {- RulesEngine.LOG.log(Level.SEVERE, "Error evaluating ruleset: " + ruleset, e);- setError(e);- return false;+ for (Rule rule : jsonRulesBuilder.build()) {+ RulesEngine.LOG.info("Registering JSON rule: " + rule.getName());+ rules.register(rule);}+ RulesEngine.LOG.info("Evaluated ruleset deployment: " + ruleset);++ return true;}protected boolean startRulesJavascript(Ruleset ruleset, Assets assetsFacade, Users usersFacade, NotificationsFacade consolesFacade) {diff --git a/manager/src/main/java/org/openremote/manager/rules/facade/AssetsFacade.java b/manager/src/main/java/org/openremote/manager/rules/facade/AssetsFacade.javaindex c58f00331..c8b9413df 100644--- a/manager/src/main/java/org/openremote/manager/rules/facade/AssetsFacade.java+++ b/manager/src/main/java/org/openremote/manager/rules/facade/AssetsFacade.java@@ -55,8 +55,8 @@ public class AssetsFacade<T extends Ruleset> extends Assets {if (AssetRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {Asset restrictedAsset = assetStorageService.find(rulesEngineId.getAssetId().orElseThrow(() -> new IllegalStateException("Asset ID missing: " + rulesEngineId)),- true- );+ true);+if (restrictedAsset == null) {throw new IllegalStateException("Asset is no longer available: " + rulesEngineId);}@@ -92,14 +92,13 @@ public class AssetsFacade<T extends Ruleset> extends Assets {}@Override- public String getResult() {+ public Asset getResult() {// TODO: 'Security' checks must be done here as AssetQuery fields can easily be set by a rule- Asset asset = assetStorageService.find(this);- return asset != null ? asset.getId() : null;+ return assetStorageService.find(this);}@Override- public Stream<String> stream() {+ public Stream<Asset> stream() {// TODO: 'Security' checks must be done here as AssetQuery fields can easily be set by a ruleif (this.select == null)@@ -107,7 +106,7 @@ public class AssetsFacade<T extends Ruleset> extends Assets {Include oldValue = this.select.include;this.select.include = Include.ONLY_ID_AND_NAME;try {- return assetStorageService.findAll(this).stream().map(Asset::getId);+ return assetStorageService.findAll(this).stream();} finally {this.select.include = oldValue;}diff --git a/manager/src/main/java/org/openremote/manager/rules/facade/UsersFacade.java b/manager/src/main/java/org/openremote/manager/rules/facade/UsersFacade.javaindex 31f774446..857dc54ec 100644--- a/manager/src/main/java/org/openremote/manager/rules/facade/UsersFacade.java+++ b/manager/src/main/java/org/openremote/manager/rules/facade/UsersFacade.java@@ -33,6 +33,7 @@ import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;+import java.util.stream.Stream;/*** Restricts rule RHS access to the scope of the engine (a rule in asset scope can not access users in global scope).@@ -55,6 +56,11 @@ public class UsersFacade<T extends Ruleset> extends Users {@Overridepublic List<String> getResults() {+ return stream().collect(Collectors.toList());+ }++ @Override+ public Stream<String> stream() {// Do security checks to ensure correct scoping// No restriction for global rulesetsif (TenantRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {@@ -87,8 +93,7 @@ public class UsersFacade<T extends Ruleset> extends Users {}return Arrays.stream(identityService.getIdentityProvider().getUsers(this))- .map(User::getId)- .collect(Collectors.toList());+ .map(User::getId);}}diff --git a/model/src/main/java/org/openremote/model/notification/Notification.java b/model/src/main/java/org/openremote/model/notification/Notification.javaindex c5eace290..8612fd26f 100644--- a/model/src/main/java/org/openremote/model/notification/Notification.java+++ b/model/src/main/java/org/openremote/model/notification/Notification.java@@ -22,7 +22,7 @@ package org.openremote.model.notification;import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonProperty;-import java.util.List;+import java.util.Collection;public class Notification {@@ -45,7 +45,7 @@ public class Notification {protected TargetType type;protected String[] ids;- public Targets(TargetType type, List<String> ids) {+ public Targets(TargetType type, Collection<String> ids) {this(type, ids.toArray(new String[ids.size()]));}diff --git a/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java b/model/src/main/java/org/openremote/model/query/BaseAssetQuery.javaindex d101ed537..ab8e0d90f 100644--- a/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java+++ b/model/src/main/java/org/openremote/model/query/BaseAssetQuery.java@@ -348,8 +348,8 @@ public class BaseAssetQuery<CHILD extends BaseAssetQuery<CHILD>> {return (CHILD) this;}- public CHILD ids(List<String> ids) {- this.ids = ids;+ public CHILD ids(Collection<String> ids) {+ this.ids = ids != null ? new ArrayList<>(ids) : null;return (CHILD) this;}diff --git a/model/src/main/java/org/openremote/model/query/NewAssetQuery.java b/model/src/main/java/org/openremote/model/query/NewAssetQuery.javaindex 2f6d38144..d6fda5fb8 100644--- a/model/src/main/java/org/openremote/model/query/NewAssetQuery.java+++ b/model/src/main/java/org/openremote/model/query/NewAssetQuery.java@@ -20,6 +20,7 @@package org.openremote.model.query;import org.openremote.model.query.filter.*;+import org.openremote.model.rules.json.RuleActionTarget;import org.openremote.model.rules.json.RuleCondition;/**@@ -30,7 +31,7 @@ import org.openremote.model.rules.json.RuleCondition;* The {@link org.openremote.model.asset.Asset}s associated with the returned {@link org.openremote.model.rules.AssetState}s* are then used as the context for any {@link org.openremote.model.rules.json.RuleTriggerReset} to control when the* {@link NewAssetQuery} is allowed to be applied again to the same asset. They can also optionally be used as context- * in the {@link org.openremote.model.rules.json.RuleActionWithTarget.Target}, to apply an action to the matched+ * in the {@link RuleActionTarget}, to apply an action to the matched* {@link org.openremote.model.asset.Asset}s.* <p>* There is an implicit AND between properties and an implicit OR between each value of a property.@@ -49,13 +50,13 @@ public class NewAssetQuery {public RuleCondition<AttributePredicate> attributes;/**- * To be used in combination with {@link #limit} to control the ordering of {@link org.openremote.model.rules.AssetState}s+ * To be used in combination with {@link #limit} to control the ordering of {@link org.openremote.model.asset.Asset}s* before applying the limit.*/public BaseAssetQuery.OrderBy orderBy;/**- * Means that only this number of matching {@link org.openremote.model.rules.AssetState} will be passed through as the context.+ * Means that number of matching {@link org.openremote.model.asset.Asset}s will be limited to this amount.*/public int limit;}diff --git a/model/src/main/java/org/openremote/model/query/filter/ObjectValueKeyPredicate.java b/model/src/main/java/org/openremote/model/query/filter/ObjectValueKeyPredicate.javaindex da1a1faa9..fc8e1942e 100644--- a/model/src/main/java/org/openremote/model/query/filter/ObjectValueKeyPredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/ObjectValueKeyPredicate.java@@ -24,6 +24,7 @@ import org.openremote.model.value.Values;public class ObjectValueKeyPredicate implements ValuePredicate {+ public static final String name = "object-value-key";public boolean negated;public String key;diff --git a/model/src/main/java/org/openremote/model/query/filter/ValueEmptyPredicate.java b/model/src/main/java/org/openremote/model/query/filter/ValueEmptyPredicate.javaindex a0d7bae1e..d171ed25b 100644--- a/model/src/main/java/org/openremote/model/query/filter/ValueEmptyPredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/ValueEmptyPredicate.java@@ -24,6 +24,8 @@ import org.openremote.model.value.Values;public class ValueEmptyPredicate implements ValuePredicate {+ public static final String name = "value-empty";+@Overridepublic ObjectValue toModelValue() {ObjectValue objectValue = Values.createObject();diff --git a/model/src/main/java/org/openremote/model/query/filter/ValueNotEmptyPredicate.java b/model/src/main/java/org/openremote/model/query/filter/ValueNotEmptyPredicate.javaindex 34bed26d7..1160a8805 100644--- a/model/src/main/java/org/openremote/model/query/filter/ValueNotEmptyPredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/ValueNotEmptyPredicate.java@@ -24,6 +24,8 @@ import org.openremote.model.value.Values;public class ValueNotEmptyPredicate implements ValuePredicate {+ public static final String name = "value-not-empty";+@Overridepublic ObjectValue toModelValue() {ObjectValue objectValue = Values.createObject();diff --git a/model/src/main/java/org/openremote/model/query/filter/ValuePredicate.java b/model/src/main/java/org/openremote/model/query/filter/ValuePredicate.javaindex 1fe4e993d..922086fda 100644--- a/model/src/main/java/org/openremote/model/query/filter/ValuePredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/ValuePredicate.java@@ -30,7 +30,10 @@ import org.openremote.model.value.ObjectValue;@JsonSubTypes.Type(value = DateTimePredicate.class, name = DateTimePredicate.name),@JsonSubTypes.Type(value = NumberPredicate.class, name = NumberPredicate.name),@JsonSubTypes.Type(value = RadialGeofencePredicate.class, name = RadialGeofencePredicate.name),- @JsonSubTypes.Type(value = RectangularGeofencePredicate.class, name = RectangularGeofencePredicate.name)+ @JsonSubTypes.Type(value = RectangularGeofencePredicate.class, name = RectangularGeofencePredicate.name),+ @JsonSubTypes.Type(value = ObjectValueKeyPredicate.class, name = ObjectValueKeyPredicate.name),+ @JsonSubTypes.Type(value = ValueEmptyPredicate.class, name = ValueEmptyPredicate.name),+ @JsonSubTypes.Type(value = ValueNotEmptyPredicate.class, name = ValueNotEmptyPredicate.name)})@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,diff --git a/model/src/main/java/org/openremote/model/rules/Assets.java b/model/src/main/java/org/openremote/model/rules/Assets.javaindex 30c83ef69..538f3ab5c 100644--- a/model/src/main/java/org/openremote/model/rules/Assets.java+++ b/model/src/main/java/org/openremote/model/rules/Assets.java@@ -19,6 +19,7 @@*/package org.openremote.model.rules;+import org.openremote.model.asset.Asset;import org.openremote.model.query.BaseAssetQuery;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeExecuteStatus;@@ -34,9 +35,9 @@ public abstract class Assets {public abstract class RestrictedQuery extends BaseAssetQuery<RestrictedQuery> {- abstract public String getResult();+ abstract public Asset getResult();- abstract public Stream<String> stream();+ abstract public Stream<Asset> stream();}abstract public RestrictedQuery query();diff --git a/model/src/main/java/org/openremote/model/rules/Users.java b/model/src/main/java/org/openremote/model/rules/Users.javaindex 67b8bf489..a0136a341 100644--- a/model/src/main/java/org/openremote/model/rules/Users.java+++ b/model/src/main/java/org/openremote/model/rules/Users.java@@ -33,6 +33,8 @@ public abstract class Users {public abstract class RestrictedQuery extends UserQuery<RestrictedQuery> {abstract public List<String> getResults();++ abstract public Stream<String> stream();}abstract public RestrictedQuery query();diff --git a/model/src/main/java/org/openremote/model/rules/json/Rule.java b/model/src/main/java/org/openremote/model/rules/json/Rule.javaindex 47ece9ae3..b64007472 100644--- a/model/src/main/java/org/openremote/model/rules/json/Rule.java+++ b/model/src/main/java/org/openremote/model/rules/json/Rule.java@@ -22,40 +22,51 @@ package org.openremote.model.rules.json;import org.openremote.model.query.NewAssetQuery;/**- * A declarative rule definition (a.k.a. JSON rules); consists of:+ * A declarative rule definition a.k.a. JSON rules; consists of:* <p>* <h2>LHS</li>* <p>- * {@link #when}* - This can be either a {@link RuleTrigger#timer} for time based rules or an {@link NewAssetQuery}- * which will be used to filter {@link org.openremote.model.rules.AssetState}s currently available in the rule engine- * that this rule is loaded into. The {@link NewAssetQuery} will evaluate to true when one or more- * {@link org.openremote.model.rules.AssetState}s are returned by applying the filter. The returned- * {@link org.openremote.model.rules.AssetState}s are then used as context for the RHS and also the- * {@link RuleTriggerReset} (if specified).+ * {@link #when} - A series of grouped {@link RuleTrigger}s to which bitwise operations can be performed based on the+ * {@link RuleCondition#operator} of each group. These are used to filter {@link org.openremote.model.rules.AssetState}s+ * in the RuleEngine that this rule is loaded into to determine which {@link org.openremote.model.asset.Asset}s match;+ * if one or more assets match then the RHS {@link #then} will be triggered. Once a rule is triggered the+ * {@link org.openremote.model.rules.AssetState}s that triggered the rule cannot trigger the rule again until it no longer+ * matches (within the context of the {@link RuleTrigger} i.e. an {@link org.openremote.model.rules.AssetState} can+ * re-trigger a rule if it is matched by a different {@link RuleTrigger})* <p>+ * The {@link #otherwise} {@link RuleAction}s are applied to the {@link org.openremote.model.asset.Asset}s filtered by+ * the query that don't match the {@link NewAssetQuery#attributes} predicates.* {@link #and} - Optional condition that can be used to further restrict the LHS but without affecting the* {@link org.openremote.model.rules.AssetState}s that form the context for the RHS. If this is specified then the* {@link #when} must evaluate to true and this condition must evaluate to true.* <h2>RHS</h2>* <p>- * {@link #then}* - Defines a series of {@link RuleAction}s to perform when the rule triggers.+ * {@link #then} - Defines a series of {@link RuleAction}s to perform when the bitwise result of applying all+ * {@link RuleTrigger}s in the {@link #when} is true.+ * <p>+ * {@link #otherwise} - Defines a series of {@link RuleAction}s to perform when there is one or more asset that matched rule doesn't match the assets specified+ * in the {@link Rule#when}. The list of assets this applies to is the assets filtered by applying the+ * {@link Rule#when} but excluding the {@link NewAssetQuery#attributes} predicates and excluding any assets that match+ * the entire {@link Rule#when}. If the number of these assets is greater than 0 then these {@link RuleAction}s will be+ * executed, if any {@link RuleActionWithTarget#target} is set to {@link RuleActionTarget#useAssetsFromWhen}+ * then the assets that caused this to trigger will be used.* <h2>Reset</h2>* <p>- * {@link #reset}* - Optional logic used to reset the rule, this is applied to each+ * {@link #reset} - Optional logic used to reset the rule, this is applied to each* {@link org.openremote.model.rules.AssetState} that was returned by the {@link #when}. If no reset is specified then* it is assumed that the rule is fire once only per matched {@link org.openremote.model.rules.AssetState}.* <b>NOTE: Rule trigger history is not persisted so on system restart this information is lost and a rule will be* able to fire again.</b>- * <p>- * <b>* = Required</b>*/public class Rule {public String name;public String description;public int priority = Integer.MAX_VALUE-1;- public RuleTrigger when;- public RuleCondition<NewAssetQuery> and;+ public RuleCondition<RuleTrigger> when;public RuleAction[] then;+ public RuleAction[] otherwise;public RuleTriggerReset reset;+ public RuleAction[] onStart;+ public RuleAction[] onStop;}diff --git a/model/src/main/java/org/openremote/model/rules/json/RuleAction.java b/model/src/main/java/org/openremote/model/rules/json/RuleAction.javaindex b042e5899..a616d5938 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleAction.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleAction.java@@ -22,6 +22,9 @@ package org.openremote.model.rules.json;import com.fasterxml.jackson.annotation.JsonSubTypes;import com.fasterxml.jackson.annotation.JsonTypeInfo;+import java.util.List;+import java.util.function.Function;+@JsonSubTypes({@JsonSubTypes.Type(value = RuleActionWait.class, name = "wait"),@JsonSubTypes.Type(value = RuleActionWriteAttribute.class, name = "write-attribute"),@@ -32,6 +35,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;use = JsonTypeInfo.Id.NAME,property = "action")-public interface RuleAction {+public abstract class RuleAction {+ public RuleActionTarget target;}diff --git a/model/src/main/java/org/openremote/model/rules/json/RuleActionNotification.java b/model/src/main/java/org/openremote/model/rules/json/RuleActionNotification.javaindex 99e4d6dc0..9afbc6dd5 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleActionNotification.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleActionNotification.java@@ -21,6 +21,6 @@ package org.openremote.model.rules.json;import org.openremote.model.notification.Notification;-public class RuleActionNotification extends RuleActionWithTarget {+public class RuleActionNotification extends RuleAction {public Notification notification;}diff --git a/model/src/main/java/org/openremote/model/rules/json/RuleActionWithTarget.java b/model/src/main/java/org/openremote/model/rules/json/RuleActionTarget.javasimilarity index 61%rename from model/src/main/java/org/openremote/model/rules/json/RuleActionWithTarget.javarename to model/src/main/java/org/openremote/model/rules/json/RuleActionTarget.javaindex c1d672712..5f6e7e188 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleActionWithTarget.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleActionTarget.java@@ -1,5 +1,5 @@/*- * Copyright 2018, OpenRemote Inc.+ * Copyright 2019, OpenRemote Inc.** See the CONTRIBUTORS.txt file in the distribution for a* full listing of individual contributors.@@ -23,23 +23,15 @@ import org.openremote.model.query.AssetQuery;import org.openremote.model.query.UserQuery;/**- * Indicates that the action should be scoped to the specified {@link Target}.+ * None or one of the options should be set the precedence is:+ * <ol>+ * <li>{@link #ruleTriggerTag}</li>+ * <li>{@link #assets}</li>+ * <li>{@link #users}</li>+ * </ol>*/-public abstract class RuleActionWithTarget implements RuleAction {-- /**- * Only one of the options should be set the precedence is:- * <ol>- * <li>{@link #useAssetsFromWhen}</li>- * <li>{@link #assets}</li>- * <li>{@link #users}</li>- * </ol>- */- public static class Target {- public boolean useAssetsFromWhen;- public AssetQuery assets;- public UserQuery users;- }-- public Target target;+public class RuleActionTarget {+ public String ruleTriggerTag;+ public AssetQuery assets;+ public UserQuery users;}diff --git a/model/src/main/java/org/openremote/model/rules/json/RuleActionUpdateAttribute.java b/model/src/main/java/org/openremote/model/rules/json/RuleActionUpdateAttribute.javaindex d2348f8b2..ceb77edce 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleActionUpdateAttribute.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleActionUpdateAttribute.java@@ -21,13 +21,7 @@ package org.openremote.model.rules.json;import org.openremote.model.value.Value;-public class RuleActionUpdateAttribute extends RuleActionWithTarget {- public String attributeName;- public Value value;- public String key;- public Integer index;- public UpdateAction updateAction;-+public class RuleActionUpdateAttribute extends RuleAction {public enum UpdateAction {ADD,ADD_OR_REPLACE,@@ -35,4 +29,10 @@ public class RuleActionUpdateAttribute extends RuleActionWithTarget {DELETE,CLEAR}++ public String attributeName;+ public Value value;+ public String key;+ public Integer index;+ public UpdateAction updateAction;}diff --git a/model/src/main/java/org/openremote/model/rules/json/RuleActionWait.java b/model/src/main/java/org/openremote/model/rules/json/RuleActionWait.javaindex 14d6007c1..76f2a6cdc 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleActionWait.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleActionWait.java@@ -19,6 +19,6 @@*/package org.openremote.model.rules.json;-public class RuleActionWait implements RuleAction {+public class RuleActionWait extends RuleAction {public long millis;}diff --git a/model/src/main/java/org/openremote/model/rules/json/RuleActionWriteAttribute.java b/model/src/main/java/org/openremote/model/rules/json/RuleActionWriteAttribute.javaindex 544d8a974..a4686a4dd 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleActionWriteAttribute.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleActionWriteAttribute.java@@ -21,7 +21,7 @@ package org.openremote.model.rules.json;import org.openremote.model.value.Value;-public class RuleActionWriteAttribute extends RuleActionWithTarget {+public class RuleActionWriteAttribute extends RuleAction {public String attributeName;public Value value;}diff --git a/model/src/main/java/org/openremote/model/rules/json/RuleTrigger.java b/model/src/main/java/org/openremote/model/rules/json/RuleTrigger.javaindex ac53f2ca4..26da0847a 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleTrigger.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleTrigger.java@@ -22,15 +22,21 @@ package org.openremote.model.rules.json;import org.openremote.model.query.NewAssetQuery;/**- * There are two types of trigger supported and only one should be specified if both are specified then {@link #asset}- * trigger will be used.- * <ul>- * <li>{@link #asset} see {@link NewAssetQuery}</li>- * <li>{@link #timer} Allows timer expressions to be specified and thus creating a time based rule (e.g. "1h")</li>- * </ul>+ * Consists of a timer expression (interval, absolute, CRON etc.) or query for filtering+ * {@link org.openremote.model.rules.AssetState}s, if both are specified then the timer is used.+ * <p>+ * For timer based trigger it evaluates to true if the timer expression matches the current time. For query based trigger+ * it evaluates to true when one or more {@link org.openremote.model.rules.AssetState}s (referencing unique+ * {@link org.openremote.model.asset.Asset}s) are returned after applying the query to the+ * {@link org.openremote.model.rules.AssetState}s in the RuleEngine that this rule is loaded into.+ * <p>+ * The {@link #tag} is used to name the {@link org.openremote.model.asset.Asset}s that are filtered by the query and can+ * be used in the rule RHS to perform actions on these specific assets.*/public class RuleTrigger {- public NewAssetQuery asset;public String timer;+ public NewAssetQuery assets;+ public String tag;+ public RuleTriggerReset reset;}diff --git a/model/src/main/java/org/openremote/model/rules/json/RuleTriggerReset.java b/model/src/main/java/org/openremote/model/rules/json/RuleTriggerReset.javaindex ae2a390b3..2b8bbe7e4 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleTriggerReset.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleTriggerReset.java@@ -19,35 +19,38 @@*/package org.openremote.model.rules.json;-import org.openremote.model.query.NewAssetQuery;-/*** This defines when an {@link org.openremote.model.rules.AssetState} becomes eligible for triggering the rule again once- * if has triggered a rule.+ * if has triggered a rule. If a reset is not specified then {@link #noLongerMatches} is assumed.* <p>* There is an implicit OR condition between each option.*/public class RuleTriggerReset {/**- * Simple timer expression e.g. '1h'+ * Never reset i.e. fire once only for a given {@link org.openremote.model.rules.AssetState}.+ */+ public boolean never;++ /**+ * Simple timer expression e.g. '1h' that is started when*/public String timer;/**- * When the {@link Rule#when} {@link NewAssetQuery} evaluates to true.+ * When the {@link org.openremote.model.rules.AssetState} no longer evaluates to true.*/- public boolean triggerNoLongerMatches;+ public boolean noLongerMatches;/*** When the timestamp of the {@link org.openremote.model.rules.AssetState} changes in comparison to the timestamp- * at the time the rule fired.+ * at the time when it matched.*/- public boolean attributeTimestampChange;+ public boolean timestampChanges;/*** When the value of the {@link org.openremote.model.rules.AssetState} changes in comparison to the value at the- * time the rule fired.+ * time when it matched.*/- public boolean attributeValueChange;+ public boolean valueChanges;}diff --git a/model/src/main/java/org/openremote/model/util/TextUtil.java b/model/src/main/java/org/openremote/model/util/TextUtil.javaindex 1e497590b..668ac4609 100644--- a/model/src/main/java/org/openremote/model/util/TextUtil.java+++ b/model/src/main/java/org/openremote/model/util/TextUtil.java@@ -260,4 +260,15 @@ public class TextUtil {public static String toProperCase(String s, boolean convertUnderscoreToSpace) {return toCamelCase(s, true, convertUnderscoreToSpace);}++ public static Optional<Integer> asInteger(String str) {+ if (isNullOrEmpty(str)) {+ return Optional.empty();+ }+ try {+ return Optional.of(Integer.parseInt(str));+ } catch(NumberFormatException e) {+ return Optional.empty();+ }+ }}diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetModelResourceTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetModelResourceTest.groovyindex 2d6c3d92b..5e71b43d7 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetModelResourceTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetModelResourceTest.groovy@@ -97,8 +97,8 @@ class AssetModelResourceTest extends Specification implements ManagerContainerTrdef attributeValueTypeDescriptors = assetModelResource.getAttributeValueDescriptors(null)- then: "the 50 default value types should be present"- attributeValueTypeDescriptors.size() == 50+ then: "the default value types should be present"+ attributeValueTypeDescriptors.size() == 51when: "a request for Attribute value types is made"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.groovyindex 5e825d2b9..2bbf69bfe 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@@ -187,25 +187,21 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {assert targetIds[0] == consoleRegistration.id}- and: "the rule reset fact should have been created"- conditions.eventually {- assert tenantAEngine.facts.getOptional(ruleset.id + "_" + ruleset.version + "_Test Rule_" + consoleRegistration.id + "_location").isPresent()- }-and: "after a few seconds the rule should not have fired again"new PollingConditions(initialDelay: 3).eventually {assert notificationMessages.size() == 1}when: "the console device moves back inside the home geofence (as defined in the rule)"+ def lastFireTimestamp = tenantAEngine.lastFireTimestampauthenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, ManagerDemoSetup.SMART_BUILDING_LOCATION.toValue().toJson())- then: "the rule reset fact should be removed"+ and: "the engine fires at least one more time"conditions.eventually {- assert !tenantAEngine.facts.getOptional(ruleset.id + "_" + ruleset.version + "_Test Rule_" + consoleRegistration.id + "_location").isPresent()+ assert tenantAEngine.lastFireTimestamp > lastFireTimestamp}- when: "the console device moves outside the home geofence again (as defined in the rule)"+ and: "the console device moves outside the home geofence again (as defined in the rule)"authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d,0d).toValue().toJson())then: "another notification should have been sent to the console"@@ -218,7 +214,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {advancePseudoClock(35, MINUTES, container)authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(0d,0d).toValue().toJson())- then: "another notification should have been sent to the console (because the reset condition includes reset on attributeTimestampChange)"+ then: "another notification should have been sent to the console (because the reset condition includes reset on timestampChanges)"conditions.eventually {assert notificationMessages.size() == 3assert targetIds[2] == consoleRegistration.id@@ -227,7 +223,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console sends a location update with a new location but still outside the geofence"authenticatedAssetResource.writeAttributeValue(null, consoleRegistration.id, LOCATION.attributeName, new GeoJSONPoint(10d,10d).toValue().toJson())- then: "another notification should have been sent to the console (because the reset condition includes reset on attributeValueChange)"+ then: "another notification should have been sent to the console (because the reset condition includes reset on valueChanges)"conditions.eventually {assert notificationMessages.size() == 4assert targetIds[3] == consoleRegistration.iddiff --git a/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json b/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.jsonindex af2893337..bd91ba992 100644--- a/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json+++ b/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json@@ -3,39 +3,43 @@{"name": "Test Rule","when": {- "asset": {- "types": [- {- "predicateType": "string",- "match": "EXACT",- "value": "urn:openremote:asset:console"- }- ],- "attributes": {- "predicates": [- {- "name": {+ "predicates": [+ {+ "assets": {+ "types": [+ {"predicateType": "string","match": "EXACT",- "value": "location"- },- "value": {- "predicateType": "radial",- "radius": 50,- "lat": 51.438000,- "lng": 5.470945,- "negated": true+ "value": "urn:openremote:asset:console"}+ ],+ "attributes": {+ "predicates": [+ {+ "name": {+ "predicateType": "string",+ "match": "EXACT",+ "value": "location"+ },+ "value": {+ "predicateType": "radial",+ "radius": 50,+ "lat": 51.438000,+ "lng": 5.470945,+ "negated": true+ }+ }+ ]}- ]+ }}- }+ ]},"then": [{"action": "notification","target": {- "useAssetsFromWhen": true+ "ruleTriggerTag": "0"},"notification": {"name": "test",@@ -104,9 +108,9 @@}],"reset": {- "triggerNoLongerMatches": true,- "attributeValueChange": true,- "attributeTimestampChange": true+ "noLongerMatches": true,+ "valueChanges": true,+ "timestampChanges": true}}]