Skip to content

armgit / core

Public Imported from GitHub
0

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

Cleanup of rules validity logic and JSON rules sun trigger logic (#1084)

Rich Turner527f852f6ae1
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 1c169dd40..1365c1bb2 100644
--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
@@ -50,7 +50,6 @@ import org.shredzone.commons.suncalc.SunTimes;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
@@ -83,7 +82,7 @@ public class JsonRulesBuilder extends RulesBuilder {
* Stores all state for a given {@link RuleCondition} and calculates which {@link AssetState}s match and don't
* match the condition.
*/
- static class RuleConditionState {
+ class RuleConditionState {
RuleCondition ruleCondition;
final TimerService timerService;
@@ -144,51 +143,41 @@ public class JsonRulesBuilder extends RulesBuilder {
throw e;
}
} else if (ruleCondition.sun != null) {
- SunTimes.Parameters sunCalculator = getSunCalculator(ruleCondition.sun, timerService);
- long offsetMillis = ruleCondition.sun.getOffsetMins() != null ? ruleCondition.sun.getOffsetMins() * 60000 : 0;
+ SunTimes.Parameters sunCalculator = getSunCalculator(jsonRuleset, ruleCondition.sun, timerService);
+ final long offsetMillis = ruleCondition.sun.getOffsetMins() != null ? ruleCondition.sun.getOffsetMins() * 60000 : 0;
+ final boolean useRiseTime = ruleCondition.sun.getPosition() == SunPositionTrigger.Position.SUNRISE || ruleCondition.sun.getPosition().toString().startsWith(SunPositionTrigger.MORNING_TWILIGHT_PREFIX);
// Calculate the next occurrence
AtomicReference<SunTimes> sunTimes = new AtomicReference<>(sunCalculator.execute());
- ZonedDateTime occurrence;
- if(ruleCondition.sun.getPosition() == SunPositionTrigger.Position.SUNRISE || ruleCondition.sun.getPosition().toString().startsWith(SunPositionTrigger.MORNING_TWILIGHT_PREFIX)) {
- occurrence = sunTimes.get().getRise();
- } else {
- occurrence = sunTimes.get().getSet();
- }
- if (occurrence == null) {
- Exception e = new IllegalStateException("Rule condition requested sun position never occurs at the specified location: " + ruleCondition.sun);
- log(Level.SEVERE, e.getMessage(), e);
- throw e;
- }
- try {
- AtomicLong nextExecuteMillis = new AtomicLong(occurrence.toInstant().toEpochMilli() + offsetMillis);
+ Function<Long, Long> nextExecuteMillisCalculator = (time) -> {
+ ZonedDateTime occurrence = useRiseTime ? sunTimes.get().getRise() : sunTimes.get().getSet();
- timePredicate = (time) -> {
- long nextExecute = nextExecuteMillis.get();
- if (time >= nextExecute) {
- // Advance the calculator beyond now or next occurrence (whichever is later)
- long calcMillis = Math.max(sunTimes.get().getSet().toEpochSecond()*1000, time);
- sunTimes.set(sunCalculator.on(new Date(calcMillis)).execute());
- ZonedDateTime nextOccurrence = ruleCondition.sun.getPosition() == SunPositionTrigger.Position.SUNSET ? sunTimes.get().getSet() : sunTimes.get().getRise();
-
- log(Level.INFO, "Rule condition sun position has triggered at: " + timerService.getCurrentTimeMillis());
-
- if (nextOccurrence == null) {
- log(Level.WARNING, "Rule condition requested sun position never occurs at the specified location: " + ruleCondition.sun);
- } else {
- long nextMillis = nextOccurrence.toInstant().toEpochMilli() + offsetMillis;
- log(Level.FINE, "Rule condition sun position next trigger time = " + nextMillis);
- nextExecuteMillis.set(nextMillis);
- }
- return true;
- }
- return false;
- };
- } catch (Exception e) {
- log(Level.SEVERE, "Failed to parse rule condition duration expression: " + ruleCondition.duration, e);
- throw e;
- }
+ if (occurrence == null) {
+ log(Level.WARNING, "Rule condition requested sun position never occurs at the specified location: " + ruleCondition.sun);
+ return Long.MAX_VALUE;
+ }
+
+ long nextMillis = occurrence.toInstant().toEpochMilli() + offsetMillis;
+
+ // If occurrence is before requested time then advance the sun calculator to either reset occurrence or requested time (whichever is later)
+ if (nextMillis < time) {
+ ZonedDateTime resetOccurrence = useRiseTime ? sunTimes.get().getSet() : sunTimes.get().getRise();
+ sunTimes.set(sunCalculator.on(new Date(Math.max(resetOccurrence.toEpochSecond()*1000, time))).execute());
+ }
+
+ return nextMillis;
+ };
+
+ timePredicate = (time) -> {
+ long nextExecute = nextExecuteMillisCalculator.apply(time);
+
+ if (time >= nextExecute) {
+ log(Level.INFO, "Rule condition sun position has triggered at: " + timerService.getCurrentTimeMillis());
+ return true;
+ }
+ return false;
+ };
} else if (ruleCondition.assets != null) {
// Pull out order, limit and attribute predicates so they can be applied at required times
@@ -418,7 +407,7 @@ public class JsonRulesBuilder extends RulesBuilder {
/**
* Stores the state of the overall rule and each {@link RuleCondition}.
*/
- static class RuleState {
+ class RuleState {
protected JsonRule rule;
protected Map<String, RuleConditionState> conditionStateMap = new HashMap<>();
@@ -536,7 +525,7 @@ public class JsonRulesBuilder extends RulesBuilder {
public static final String PLACEHOLDER_RULESET_NAME = "%RULESET_NAME%";
public static final String PLACEHOLDER_TRIGGER_ASSETS = "%TRIGGER_ASSETS%";
final static String TIMER_TEMPORAL_FACT_NAME_PREFIX = "TimerTemporalFact-";
- final static String LOG_PREFIX = "JSON Rules: ";
+ final static String LOG_PREFIX = "JSON Rule '";
final protected AssetStorageService assetStorageService;
final protected TimerService timerService;
final protected Assets assetsFacade;
@@ -595,21 +584,13 @@ public class JsonRulesBuilder extends RulesBuilder {
}
public void start(RulesFacts facts) {
- AtomicBoolean hasTimeTrigger = new AtomicBoolean(false);
Arrays.stream(jsonRules).forEach(jsonRule -> {
- hasTimeTrigger.set(hasTimeTrigger.get() || LogicGroup.getItemsRecursive(jsonRule.when).stream().anyMatch(RuleCondition::hasTimeTrigger));
executeRuleActions(jsonRule, jsonRule.onStart, "onStart", false, facts, null, assetsFacade, usersFacade, notificationsFacade, webhooksFacade, predictedDatapointsFacade, this.scheduledActionConsumer);
});
// Initialise asset states
onAssetStatesChanged(facts, null);
-
- // Insert a temporal fact if we have a time trigger (this will cause the engine to fire periodically)
- if (hasTimeTrigger.get()) {
- String tempFactName = TIMER_TEMPORAL_FACT_NAME_PREFIX + jsonRuleset.getId();
- facts.putTemporary(tempFactName, Long.MAX_VALUE - timerService.getCurrentTimeMillis() - 5000, ""); // current time + expiration shouldn't overflow
- }
}
public void onAssetStatesChanged(RulesFacts facts, RulesEngine.AssetStateChangeEvent event) {
@@ -731,7 +712,7 @@ public class JsonRulesBuilder extends RulesBuilder {
};
}
- public static void executeRuleActions(JsonRule rule, RuleAction[] ruleActions, String actionsName, boolean useUnmatched, RulesFacts facts, RuleState ruleState, Assets assetsFacade, Users usersFacade, Notifications notificationsFacade, Webhooks webhooksFacade, PredictedDatapoints predictedDatapointsFacade, BiConsumer<Runnable, Long> scheduledActionConsumer) {
+ public void executeRuleActions(JsonRule rule, RuleAction[] ruleActions, String actionsName, boolean useUnmatched, RulesFacts facts, RuleState ruleState, Assets assetsFacade, Users usersFacade, Notifications notificationsFacade, Webhooks webhooksFacade, PredictedDatapoints predictedDatapointsFacade, BiConsumer<Runnable, Long> scheduledActionConsumer) {
if (ruleActions != null && ruleActions.length > 0) {
@@ -779,7 +760,7 @@ public class JsonRulesBuilder extends RulesBuilder {
.collect(Collectors.toList());
}
- protected static RuleActionExecution buildRuleActionExecution(JsonRule rule, RuleAction ruleAction, String actionsName, int index, boolean useUnmatched, RulesFacts facts, RuleState ruleState, Assets assetsFacade, Users usersFacade, Notifications notificationsFacade, Webhooks webhooksFacade, PredictedDatapoints predictedDatapointsFacade) {
+ protected RuleActionExecution buildRuleActionExecution(JsonRule rule, RuleAction ruleAction, String actionsName, int index, boolean useUnmatched, RulesFacts facts, RuleState ruleState, Assets assetsFacade, Users usersFacade, Notifications notificationsFacade, Webhooks webhooksFacade, PredictedDatapoints predictedDatapointsFacade) {
if (ruleAction instanceof RuleActionNotification notificationAction) {
@@ -1172,36 +1153,30 @@ public class JsonRulesBuilder extends RulesBuilder {
return target != null && (target.users != null || (target.linkedUsers != null && target.linkedUsers));
}
- protected static void log(Level level, String message) {
- RulesEngine.RULES_LOG.log(level, LOG_PREFIX + message);
+ protected void log(Level level, String message) {
+ RulesEngine.RULES_LOG.log(level, LOG_PREFIX + jsonRuleset.getName() + "': " + message);
}
- protected static void log(Level level, String message, Throwable t) {
- RulesEngine.RULES_LOG.log(level, LOG_PREFIX + message, t);
+ protected void log(Level level, String message, Throwable t) {
+ RulesEngine.RULES_LOG.log(level, LOG_PREFIX + jsonRuleset.getName() + "': " + message, t);
}
- protected static SunTimes.Parameters getSunCalculator(SunPositionTrigger sunPositionTrigger, TimerService timerService) throws IllegalStateException {
+ protected static SunTimes.Parameters getSunCalculator(Ruleset ruleset, SunPositionTrigger sunPositionTrigger, TimerService timerService) throws IllegalStateException {
SunPositionTrigger.Position position = sunPositionTrigger.getPosition();
GeoJSONPoint location = sunPositionTrigger.getLocation();
if (position == null) {
- IllegalStateException e = new IllegalStateException("Rule condition sun requires a position value");
- log(Level.SEVERE, e.getMessage(), e);
- throw e;
+ throw new IllegalStateException(LOG_PREFIX + ruleset.getName() + "': Rule condition sun requires a position value");
}
if (location == null) {
- IllegalStateException e = new IllegalStateException("Rule condition sun requires a location value");
- log(Level.SEVERE, e.getMessage(), e);
- throw e;
+ throw new IllegalStateException(LOG_PREFIX + ruleset.getName() + "': Rule condition sun requires a location value");
}
SunTimes.Twilight twilight = null;
if (position.name().startsWith(SunPositionTrigger.TWILIGHT_PREFIX)) {
String lookupValue = position.name().replace(SunPositionTrigger.MORNING_TWILIGHT_PREFIX, "").replace(SunPositionTrigger.EVENING_TWILIGHT_PREFIX, "").replace(SunPositionTrigger.TWILIGHT_PREFIX, "");
twilight = EnumUtil.enumFromString(SunTimes.Twilight.class, lookupValue).orElseThrow(() -> {
- IllegalStateException e = new IllegalStateException("Rule condition un-supported twilight position value '" + lookupValue + "'");
- log(Level.SEVERE, e.getMessage(), e);
- return e;
+ throw new IllegalStateException(LOG_PREFIX + ruleset.getName() + "': Rule condition un-supported twilight position value '" + lookupValue + "'");
});
}
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
index 38afdfeee..4103706ae 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
@@ -19,7 +19,6 @@
*/
package org.openremote.manager.rules;
-import org.apache.commons.lang3.time.DurationFormatUtils;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.RuleListener;
@@ -47,7 +46,6 @@ import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
-import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -97,9 +95,6 @@ public class RulesEngine<T extends Ruleset> {
// Separate logger for periodic stats printer
public static final Logger STATS_LOG = Logger.getLogger("org.openremote.rules.RulesEngineStats");
- protected static BiConsumer<RulesEngine<?>, RulesetDeployment> UNPAUSE_SCHEDULER = RulesEngine::scheduleUnpause;
- // Here to facilitate testing
- protected static BiConsumer<RulesEngine<?>, RulesetDeployment> PAUSE_SCHEDULER = RulesEngine::schedulePause;
final protected TimerService timerService;
final protected RulesService rulesService;
final protected ScheduledExecutorService executorService;
@@ -116,6 +111,7 @@ public class RulesEngine<T extends Ruleset> {
final protected AssetLocationPredicateProcessor assetLocationPredicatesConsumer;
final protected Map<Long, RulesetDeployment> deployments = new LinkedHashMap<>();
+ final protected Map<Long, RulesetStatus> deploymentStatusMap = new LinkedHashMap<>();
final protected RulesFacts facts;
final protected AbstractRulesEngine engine;
@@ -124,15 +120,10 @@ public class RulesEngine<T extends Ruleset> {
protected boolean trackLocationPredicates;
protected ScheduledFuture<?> fireTimer;
protected ScheduledFuture<?> statsTimer;
- protected Map<Long, ScheduledFuture<?>> pauseTimers = new HashMap<>();
- protected Map<Long, ScheduledFuture<?>> unpauseTimers = new HashMap<>();
// Only used to optimize toString(), contains the details of this engine
protected String deploymentInfo;
- // Only used in tests to prevent scheduled firing of engine
- protected boolean disableTemporaryFactExpiration = false;
-
public RulesEngine(TimerService timerService,
RulesService rulesService,
ManagerIdentityService identityService,
@@ -152,7 +143,13 @@ public class RulesEngine<T extends Ruleset> {
this.assetStorageService = assetStorageService;
this.clientEventService = clientEventService;
this.id = id;
- AssetsFacade<T> assetsFacade = new AssetsFacade<>(id, assetStorageService, assetProcessingService::sendAttributeEvent);
+ AssetsFacade<T> assetsFacade = new AssetsFacade<>(id, assetStorageService, attributeEvent -> {
+ try {
+ assetProcessingService.sendAttributeEvent(attributeEvent);
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE, "Failed to dispatch attribute event");
+ }
+ });
this.assetsFacade = assetsFacade;
this.usersFacade = new UsersFacade<>(id, assetStorageService, notificationService, identityService);
this.notificationFacade = new NotificationsFacade<>(id, notificationService);
@@ -267,35 +264,36 @@ public class RulesEngine<T extends Ruleset> {
removeRuleset(deployment.ruleset);
}
+
deployment = new RulesetDeployment(ruleset, timerService, assetStorageService, executorService, assetsFacade, usersFacade, notificationFacade, webhooksFacade, historicFacade, predictedFacade);
boolean compiled;
- if (TextUtil.isNullOrEmpty(ruleset.getRules())) {
- LOG.finest("Ruleset is empty so no rules to deploy: " + ruleset.getName());
- deployment.setStatus(EMPTY);
- publishRulesetStatus(deployment);
- } else if (!ruleset.isEnabled()) {
- LOG.finest("Ruleset is disabled: " + ruleset.getName());
- deployment.setStatus(DISABLED);
- publishRulesetStatus(deployment);
- } else {
- deployment.updateValidity();
- if (deployment.hasExpired()) {
- LOG.finest("Ruleset validity period has expired: " + ruleset.getName());
- deployment.setStatus(EXPIRED);
+ try {
+ deployment.init();
+
+ if (TextUtil.isNullOrEmpty(ruleset.getRules())) {
+ LOG.finest("Ruleset is empty so no rules to deploy: " + ruleset.getName());
+ deployment.setStatus(EMPTY);
+ publishRulesetStatus(deployment);
+ } else if (!ruleset.isEnabled()) {
+ LOG.finest("Ruleset is disabled: " + ruleset.getName());
+ deployment.setStatus(DISABLED);
publishRulesetStatus(deployment);
- compiled = true;
} else {
compiled = deployment.compile();
- }
- if (!compiled) {
- LOG.log(Level.SEVERE, "Ruleset compilation error: " + ruleset.getName(), deployment.getError());
- deployment.setStatus(COMPILATION_ERROR);
- publishRulesetStatus(deployment);
- } else if (running) {
- startRuleset(deployment);
+ if (!compiled) {
+ LOG.log(Level.SEVERE, "Ruleset compilation error: " + ruleset.getName(), deployment.getError());
+ deployment.setStatus(COMPILATION_ERROR);
+ publishRulesetStatus(deployment);
+ } else if (running) {
+ startRuleset(deployment);
+ }
}
+ } catch (IllegalStateException e) {
+ LOG.log(Level.WARNING, e.getMessage());
+ deployment.setStatus(VALIDITY_PERIOD_ERROR);
+ publishRulesetStatus(deployment);
}
deployments.put(ruleset.getId(), deployment);
@@ -322,12 +320,6 @@ public class RulesEngine<T extends Ruleset> {
deployment.setStatus(REMOVED);
publishRulesetStatus(deployment);
deployments.remove(ruleset.getId());
-
- ScheduledFuture<?> timer = pauseTimers.remove(ruleset.getId());
- if (timer != null) timer.cancel(true);
- timer = unpauseTimers.remove(ruleset.getId());
- if (timer != null) timer.cancel(true);
-
updateDeploymentInfo();
start();
@@ -402,10 +394,6 @@ public class RulesEngine<T extends Ruleset> {
statsTimer.cancel(true);
statsTimer = null;
}
- pauseTimers.values().forEach(pauseTimer -> pauseTimer.cancel(true));
- pauseTimers.clear();
- unpauseTimers.values().forEach(unpauseTimer -> unpauseTimer.cancel(true));
- unpauseTimers.clear();
deployments.values().forEach(this::stopRuleset);
running = false;
@@ -423,30 +411,17 @@ public class RulesEngine<T extends Ruleset> {
return;
}
- if (deployment.getStatus() == COMPILATION_ERROR || deployment.getStatus() == DISABLED || deployment.getStatus() == EXPIRED) {
- return;
- }
+ RulesetStatus status = deployment.getStatus();
- deployment.updateValidity();
-
- if (deployment.hasExpired()) {
- deployment.setStatus(EXPIRED);
+ if (status == COMPILATION_ERROR || status == DISABLED || status == EXPIRED) {
+ LOG.finest("Ruleset '" + deployment.getName() + "': status=" + status);
publishRulesetStatus(deployment);
return;
}
- if (deployment.getValidFrom() > timerService.getCurrentTimeMillis()) {
- // Not ready to start yet so pause
- pauseRuleset(deployment);
- } else {
- deployment.setStatus(DEPLOYED);
- publishRulesetStatus(deployment);
- deployment.start(facts);
-
- if (deployment.getValidTo() != Long.MAX_VALUE) {
- PAUSE_SCHEDULER.accept(this, deployment);
- }
- }
+ deployment.setStatus(DEPLOYED);
+ deployment.start(facts);
+ publishRulesetStatus(deployment);
}
protected synchronized void stopRuleset(RulesetDeployment deployment) {
@@ -491,24 +466,10 @@ public class RulesEngine<T extends Ruleset> {
fireTimer = executorService.schedule(
() -> {
synchronized (RulesEngine.this) {
- // Are temporary facts present before rules are fired?
- boolean hadTemporaryFactsBefore = facts.hasTemporaryFacts();
-
// Process rules for all deployments
fireAllDeployments();
-
- // If there are temporary facts, or if there were some before and
- // now they are gone, schedule a new firing to guarantee processing
- // of expired and removed temporary facts
- if ((facts.hasTemporaryFacts() || (hadTemporaryFactsBefore && !facts.hasTemporaryFacts()))
- && !disableTemporaryFactExpiration) {
- // Schedule call to schedule fire so the fireTimer shows as done
- executorService.schedule(() -> scheduleFire(false), 10, TimeUnit.MILLISECONDS);
- } else if (!disableTemporaryFactExpiration) {
- LOG.finest("No temporary facts present/changed when firing rules on: " + this);
- }
-
fireTimer = null;
+ scheduleFire(false);
}
},
fireTimeMillis,
@@ -516,7 +477,7 @@ public class RulesEngine<T extends Ruleset> {
);
}
- private void doFire(Collection<RulesetDeployment> deploymentList) {
+ protected void fireAllDeployments() {
if (!running) {
return;
}
@@ -529,10 +490,13 @@ public class RulesEngine<T extends Ruleset> {
facts.removeExpiredTemporaryFacts();
long executionTotalMillis = 0L;
- for (RulesetDeployment deployment : deploymentList) {
+ for (RulesetDeployment deployment : deployments.values()) {
try {
- if (deployment.getStatus() == DEPLOYED) {
+ RulesetStatus status = deployment.getStatus();
+ publishRulesetStatus(deployment);
+
+ if (status == DEPLOYED) {
LOG.finest("Executing rules of: " + deployment);
@@ -544,13 +508,12 @@ public class RulesEngine<T extends Ruleset> {
facts.reset();
long startTimestamp = timerService.getCurrentTimeMillis();
- lastFireTimestamp = startTimestamp;
engine.fire(deployment.getRules(), facts);
long executionMillis = (timerService.getCurrentTimeMillis() - startTimestamp);
executionTotalMillis += executionMillis;
LOG.fine("Rules deployment '" + deployment.getName() + "' executed in: " + executionMillis + "ms");
} else {
- LOG.finest("Rules deployment '" + deployment.getName() + "' skipped as status is: " + deployment.getStatus());
+ LOG.fine("Rules deployment '" + deployment.getName() + "' skipped as status is: " + status);
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, "On " + RulesEngine.this + ", error executing rules of: " + deployment, ex);
@@ -567,22 +530,19 @@ public class RulesEngine<T extends Ruleset> {
} finally {
// Reset facts after this firing (loop detection etc.)
facts.reset();
+ lastFireTimestamp = timerService.getCurrentTimeMillis();
}
}
trackLocationPredicates(false);
- if (executionTotalMillis > 100) {
+ if (executionTotalMillis > 500) {
LOG.warning("Rules firing took " + executionTotalMillis + "ms on: " + this);
} else {
- LOG.fine("Rules firing took " + executionTotalMillis + "ms on: " + this);
+ LOG.finer("Rules firing took " + executionTotalMillis + "ms on: " + this);
}
}
- protected void fireAllDeployments() {
- doFire(deployments.values());
- }
-
protected synchronized void notifyAssetStatesChanged(AssetStateChangeEvent event) {
for (RulesetDeployment deployment : deployments.values()) {
if (!deployment.isError()) {
@@ -684,65 +644,26 @@ public class RulesEngine<T extends Ruleset> {
protected void publishRulesetStatus(RulesetDeployment deployment) {
Ruleset ruleset = deployment.ruleset;
+ RulesetStatus previousStatus = deploymentStatusMap.get(ruleset.getId());
String engineId = id == null ? null : id.getRealm().orElse(id.getAssetId().orElse(null));
+ RulesetStatus currentStatus = deployment.getStatus();
- ruleset.setStatus(deployment.getStatus());
- ruleset.setError(deployment.getErrorMessage());
-
- RulesetChangedEvent event = new RulesetChangedEvent(
- timerService.getCurrentTimeMillis(),
- engineId,
- ruleset
- );
-
- LOG.finest("Publishing ruleset status event: " + event);
-
- // Notify clients
- clientEventService.publishEvent(event);
- }
-
- protected void schedulePause(RulesetDeployment deployment) {
- long delay = deployment.getValidTo() - timerService.getCurrentTimeMillis();
- LOG.info("Scheduling pause of ruleset at '" + new Date(deployment.getValidTo()) + " for " + DurationFormatUtils.formatDurationHMS(delay) +": " + deployment.ruleset.getName());
- pauseTimers.put(deployment.getId(), executorService.schedule(() -> pauseRuleset(deployment), delay, TimeUnit.MILLISECONDS));
- }
-
- protected void pauseRuleset(RulesetDeployment deployment) {
- pauseTimers.remove(deployment.getId());
-
- if (!running) {
- return;
- }
-
- LOG.info("Pausing ruleset: " + deployment.getRuleset().getName());
- stopRuleset(deployment);
- deployment.updateValidity();
-
- if (deployment.hasExpired()) {
- deployment.setStatus(EXPIRED);
- publishRulesetStatus(deployment);
- } else {
- deployment.setStatus(PAUSED);
- publishRulesetStatus(deployment);
- UNPAUSE_SCHEDULER.accept(this, deployment);
- }
- }
+ if (currentStatus != previousStatus) {
+ deploymentStatusMap.put(ruleset.getId(), currentStatus);
+ ruleset.setStatus(deployment.getStatus());
+ ruleset.setError(deployment.getErrorMessage());
- protected void scheduleUnpause(RulesetDeployment deployment) {
- long delay = deployment.getValidFrom() - timerService.getCurrentTimeMillis();
- LOG.info("Scheduling un-pause of ruleset at '" + new Date(deployment.getValidFrom()).toString() + "' (" + delay + "ms): " + deployment.ruleset.getName());
- unpauseTimers.put(deployment.getId(), executorService.schedule(() -> unPauseRuleset(deployment), delay, TimeUnit.MILLISECONDS));
- }
+ RulesetChangedEvent event = new RulesetChangedEvent(
+ timerService.getCurrentTimeMillis(),
+ engineId,
+ ruleset
+ );
- protected void unPauseRuleset(RulesetDeployment deployment) {
- unpauseTimers.remove(deployment.getId());
+ LOG.finest("Ruleset '" + deployment.getName() + "': status=" + currentStatus);
- if (!running) {
- return;
+ // Notify clients
+ clientEventService.publishEvent(event);
}
-
- LOG.info("Un-pausing ruleset: " + deployment.getRuleset().getName());
- startRuleset(deployment);
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java
index 934226959..d9d557b03 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesetDeployment.java
@@ -120,6 +120,8 @@ public class RulesetDeployment {
);
}
+ protected static final Pair<Long, Long> ALWAYS_ACTIVE = new Pair<>(0L, Long.MAX_VALUE);
+ protected static final Pair<Long, Long> EXPIRED = new Pair<>(0L, 0L);
final protected Ruleset ruleset;
final protected Rules rules = new Rules();
final protected AssetStorageService assetStorageService;
@@ -153,12 +155,15 @@ public class RulesetDeployment {
this.webhooksFacade = webhooksFacade;
this.historicDatapointsFacade = historicDatapointsFacade;
this.predictedDatapointsFacade = predictedDatapointsFacade;
+ }
+ protected void init() throws IllegalStateException {
if (ruleset.getMeta().has(Ruleset.VALIDITY)) {
validity = ruleset.getValidity();
if (validity == null) {
- RulesEngine.LOG.log(Level.SEVERE, "Ruleset has invalid validity value '" + ruleset.getMeta().get(Ruleset.VALIDITY) + "'");
+ String msg = "Ruleset '" + ruleset.getName() + "' has invalid validity value: " + ruleset.getMeta().get(Ruleset.VALIDITY);
+ throw new IllegalStateException(msg);
}
}
}
@@ -183,27 +188,35 @@ public class RulesetDeployment {
return rules;
}
- public void updateValidity() {
- if (validity != null && !hasExpired()) {
- Pair<Long, Long> fromTo = validity.getNextOrActiveFromTo(new Date(timerService.getCurrentTimeMillis()));
- if (fromTo == null) {
- nextValidity = new Pair<>(Long.MIN_VALUE, Long.MIN_VALUE);
- } else {
- nextValidity = fromTo;
- }
+ protected void updateValidity() {
+ Pair<Long, Long> fromTo = validity.getNextOrActiveFromTo(new Date(timerService.getCurrentTimeMillis()));
+ if (fromTo == null) {
+ nextValidity = EXPIRED;
+ RulesEngine.LOG.log(Level.INFO, "Ruleset deployment '" + getName() + "' has expired");
+ } else {
+ nextValidity = fromTo;
+ RulesEngine.LOG.log(Level.INFO, "Ruleset deployment '" + getName() + "' paused until: " + new Date(fromTo.key));
}
}
- public long getValidFrom() {
- return nextValidity != null ? nextValidity.key : Long.MIN_VALUE;
- }
+ /**
+ * Returns the current or next time window in which this rule is active
+ * @return null if deployment has expired
+ */
+ public Pair<Long, Long> getNextOrActiveFromTo() {
+ if (validity == null) {
+ return ALWAYS_ACTIVE;
+ }
- public long getValidTo() {
- return nextValidity != null ? nextValidity.value : Long.MAX_VALUE;
- }
+ if (nextValidity == EXPIRED) {
+ return nextValidity;
+ }
+
+ if (nextValidity == null || nextValidity.value <= timerService.getCurrentTimeMillis()) {
+ updateValidity();
+ }
- public boolean hasExpired() {
- return validity != null && nextValidity != null && nextValidity.value == Long.MIN_VALUE;
+ return nextValidity;
}
public boolean compile() {
@@ -482,6 +495,22 @@ public class RulesetDeployment {
}
public RulesetStatus getStatus() {
+
+ if (isError()) {
+ return status;
+ }
+
+ Pair<Long, Long> validity = getNextOrActiveFromTo();
+
+ if (validity == EXPIRED) {
+ return RulesetStatus.EXPIRED;
+ }
+ if (validity != ALWAYS_ACTIVE) {
+ if (validity.key > timerService.getCurrentTimeMillis()) {
+ return RulesetStatus.PAUSED;
+ }
+ }
+
return status;
}
@@ -502,7 +531,7 @@ public class RulesetDeployment {
}
public boolean isError() {
- return getStatus() == RulesetStatus.LOOP_ERROR || ((getStatus() == RulesetStatus.EXECUTION_ERROR || getStatus() == RulesetStatus.COMPILATION_ERROR) && !isContinueOnError());
+ return status == RulesetStatus.LOOP_ERROR || ((status == RulesetStatus.EXECUTION_ERROR || status == RulesetStatus.COMPILATION_ERROR) && !isContinueOnError());
}
public boolean isContinueOnError() {
@@ -519,7 +548,7 @@ public class RulesetDeployment {
"id=" + getId() +
", name='" + getName() + '\'' +
", version=" + getVersion() +
- ", status=" + status +
+ ", status=" + getStatus() +
'}';
}
}
diff --git a/model/src/main/java/org/openremote/model/rules/RulesetStatus.java b/model/src/main/java/org/openremote/model/rules/RulesetStatus.java
index 3f4db5b17..f8b6af8f9 100644
--- a/model/src/main/java/org/openremote/model/rules/RulesetStatus.java
+++ b/model/src/main/java/org/openremote/model/rules/RulesetStatus.java
@@ -35,6 +35,11 @@ public enum RulesetStatus {
*/
COMPILATION_ERROR,
+ /**
+ * Validity period is not valid
+ */
+ VALIDITY_PERIOD_ERROR,
+
/**
* Ruleset was executed but there was a runtime error.
*/
diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesImport.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesImport.groovy
index ff3beec0c..7cd4b87b8 100644
--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesImport.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesImport.groovy
@@ -99,14 +99,12 @@ class BasicRulesImport {
globalEngine = rulesService.globalEngine
assert globalEngine != null
- globalEngine.disableTemporaryFactExpiration = true
assert globalEngine.isRunning()
assert globalEngine.deployments.size() == 1
assert globalEngine.deployments.values().any { it -> it.name == "Some global demo rules" && it.status == DEPLOYED }
assert rulesService.realmEngines.size() == 2
masterEngine = rulesService.realmEngines.get(keycloakTestSetup.realmMaster.name)
- masterEngine.disableTemporaryFactExpiration = true
assert masterEngine != null
assert masterEngine.isRunning()
assert masterEngine.deployments.size() == 1
@@ -114,7 +112,6 @@ class BasicRulesImport {
assert masterEngine.deployments.values().iterator().next().status == DEPLOYED
realmBuildingEngine = rulesService.realmEngines.get(keycloakTestSetup.realmBuilding.name)
assert realmBuildingEngine != null
- realmBuildingEngine.disableTemporaryFactExpiration = true
assert realmBuildingEngine.isRunning()
assert realmBuildingEngine.deployments.size() == 1
assert realmBuildingEngine.deployments.values().iterator().next().name == "Some building realm demo rules"
@@ -127,14 +124,12 @@ class BasicRulesImport {
assert apartment1Engine == null
apartment2Engine = rulesService.assetEngines.get(managerTestSetup.apartment2Id)
assert apartment2Engine != null
- apartment2Engine.disableTemporaryFactExpiration = true
assert apartment2Engine.isRunning()
assert apartment2Engine.deployments.size() == 1
assert apartment2Engine.deployments.values().iterator().next().name == "Some apartment 2 demo rules"
assert apartment2Engine.deployments.values().iterator().next().status == DEPLOYED
apartment3Engine = rulesService.assetEngines.get(managerTestSetup.apartment3Id)
assert apartment3Engine != null
- apartment3Engine.disableTemporaryFactExpiration = true
assert apartment3Engine.isRunning()
assert apartment3Engine.deployments.size() == 1
assert apartment3Engine.deployments.values().iterator().next().name == "Some apartment 3 demo rules"
diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
index f5419ed41..6aeddd465 100644
--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
@@ -48,7 +48,6 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
expect: "the rules engines to be ready"
conditions.eventually {
assert rulesImport.assertEnginesReady(rulesService, keycloakTestSetup, managerTestSetup)
- assert noRuleEngineFiringScheduled()
}
when: "a LHS filtering test rule definition is loaded into the Smart Building asset"
@@ -126,7 +125,6 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
expect: "the rules engines to be ready"
conditions.eventually {
assert rulesImport.assertEnginesReady(rulesService, keycloakTestSetup, managerTestSetup)
- assert noRuleEngineFiringScheduled()
}
when: "a Kitchen room asset is inserted into apartment that contains a RULE_STATE = true meta flag"
@@ -172,12 +170,7 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
when: "time advances"
advancePseudoClock(1, TimeUnit.SECONDS, container)
- then: "the rule engines settle down"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
- and: "an attribute event is pushed into the system for an attribute with no RULE_STATE meta"
+ then: "an attribute event is pushed into the system for an attribute with no RULE_STATE meta"
def globalLastFireTimestamp = rulesImport.globalEngine.lastFireTimestamp
def masterLastFireTimestamp = rulesImport.masterEngine.lastFireTimestamp
def realmALastFireTimestamp = rulesImport.realmBuildingEngine.lastFireTimestamp
@@ -224,10 +217,10 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
then: "the rules engines should have executed at least one more time"
conditions.eventually {
assert rulesImport.globalEngine.lastFireTimestamp > globalLastFireTimestamp
- assert rulesImport.masterEngine.lastFireTimestamp == masterLastFireTimestamp
+ assert rulesImport.masterEngine.lastFireTimestamp > masterLastFireTimestamp
assert rulesImport.realmBuildingEngine.lastFireTimestamp > realmALastFireTimestamp
assert rulesImport.apartment2Engine.lastFireTimestamp > apartment2LastFireTimestamp
- assert rulesImport.apartment3Engine.lastFireTimestamp == apartment3LastFireTimestamp
+ assert rulesImport.apartment3Engine.lastFireTimestamp > apartment3LastFireTimestamp
}
then: "no rules should have fired"
@@ -347,7 +340,6 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
expect: "the rules engines to be ready"
conditions.eventually {
assert rulesImport.assertEnginesReady(rulesService, keycloakTestSetup, managerTestSetup)
- assert noRuleEngineFiringScheduled()
}
when: "a broken RHS rule is loaded into the building engine"
@@ -370,11 +362,6 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
})
}
- then: "the engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
when: "an attribute event occurs"
def globalLastFireTimestamp = rulesImport.globalEngine.lastFireTimestamp
def masterLastFireTimestamp = rulesImport.masterEngine.lastFireTimestamp
@@ -389,10 +376,10 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
then: "the rules engines should have executed at least one more time"
conditions.eventually {
assert rulesImport.globalEngine.lastFireTimestamp > globalLastFireTimestamp
- assert rulesImport.masterEngine.lastFireTimestamp == masterLastFireTimestamp
- assert rulesImport.realmBuildingEngine.lastFireTimestamp == realmALastFireTimestamp
+ assert rulesImport.masterEngine.lastFireTimestamp > masterLastFireTimestamp
+ assert rulesImport.realmBuildingEngine.lastFireTimestamp > realmALastFireTimestamp
assert rulesImport.apartment2Engine.lastFireTimestamp > apartment2LastFireTimestamp
- assert rulesImport.apartment3Engine.lastFireTimestamp == apartment3LastFireTimestamp
+ assert rulesImport.apartment3Engine.lastFireTimestamp > apartment3LastFireTimestamp
}
}
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 9dc405832..d86742f89 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
@@ -17,7 +17,10 @@ import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.notification.EmailNotificationHandler
import org.openremote.manager.notification.NotificationService
import org.openremote.manager.notification.PushNotificationHandler
-import org.openremote.manager.rules.*
+import org.openremote.manager.rules.JsonRulesBuilder
+import org.openremote.manager.rules.RulesEngine
+import org.openremote.manager.rules.RulesService
+import org.openremote.manager.rules.RulesetStorageService
import org.openremote.manager.rules.geofence.ORConsoleGeofenceAssetAdapter
import org.openremote.manager.setup.SetupService
import org.openremote.manager.webhook.WebhookService
@@ -56,7 +59,6 @@ import java.time.temporal.ChronoUnit
import java.util.concurrent.TimeUnit
import static java.util.concurrent.TimeUnit.HOURS
-import static java.util.concurrent.TimeUnit.MILLISECONDS
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
import static org.openremote.model.rules.RulesetStatus.*
import static org.openremote.model.util.ValueUtil.parse
@@ -448,32 +450,11 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
assert pushTargetsAndMessages.findAll {it.v2.title == "Test title" && it.v1.id == consoleRegistration.id}.size() == 4
}
- when: "the Rules PAUSE_SCHEDULER is overridden to facilitate testing"
- RulesEngine engine
- RulesetDeployment deployment
- def originalPause = RulesEngine.PAUSE_SCHEDULER
- RulesEngine.PAUSE_SCHEDULER = {e, d ->
- engine = e
- deployment = d
- long delay = d.getValidTo() - timerService.getCurrentTimeMillis()
- // Don't actually schedule just simulate time passing and let test decide when to move on
- advancePseudoClock(delay, MILLISECONDS, container)
-
- }
- def originalUnpause = RulesEngine.UNPAUSE_SCHEDULER
- RulesEngine.UNPAUSE_SCHEDULER = {e, d ->
- engine = e
- deployment = d
- long delay = d.getValidFrom() - timerService.getCurrentTimeMillis()
- // Don't actually schedule just simulate time passing and let test decide when to move on
- advancePseudoClock(delay, MILLISECONDS, container)
- }
-
- and: "a validity period is added to the ruleset (fictional times to ensure firing in sensible time within test)"
+ when: "a validity period is added to the ruleset"
version = ruleset.version
def validityStart = Instant.ofEpochMilli(getClockTimeOf(container)).plus(2, ChronoUnit.HOURS)
def validityEnd = Instant.ofEpochMilli(getClockTimeOf(container)).plus(4, ChronoUnit.HOURS)
- def recur = new Recur(Recur.DAILY, 3)
+ def recur = new Recur(Recur.Frequency.DAILY, 3)
recur.setInterval(2)
def calendarEvent = new CalendarEvent(
Date.from(validityStart),
@@ -502,10 +483,10 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
and: "no notification should have been sent as outside the validity period"
assert pushTargetsAndMessages.findAll {it.v2.title == "Test title"}.size() == 4
- when: "the pause elapses"
- engine.unPauseRuleset(deployment)
+ when: "time advances to inside the validity period"
+ advancePseudoClock(3, HOURS, container)
- then: "eventually the ruleset should be unpaused (1st occurrence)"
+ then: "the ruleset should be unpaused (1st occurrence)"
conditions.eventually {
assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == DEPLOYED
}
@@ -526,8 +507,8 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
assert pushTargetsAndMessages.findAll {it.v2.title == "Test title"}.size() == 5
}
- when: "the un-pause elapses"
- engine.pauseRuleset(deployment)
+ when: "time advances past the validity period"
+ advancePseudoClock(1, HOURS, container)
then: "the ruleset should become paused again (until next occurrence)"
conditions.eventually {
@@ -548,32 +529,32 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
and: "no notification should have been sent as outside the validity period"
assert pushTargetsAndMessages.findAll {it.v2.title == "Test title"}.size() == 5
- when: "the pause elapses"
- engine.unPauseRuleset(deployment)
+ when: "time advances to inside the next validity period"
+ advancePseudoClock(47, HOURS, container)
then: "eventually the ruleset should be unpaused (next occurrence)"
conditions.eventually {
assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == DEPLOYED
}
- when: "the un-pause elapses"
- engine.pauseRuleset(deployment)
+ when: "time advances past the validity period"
+ advancePseudoClock(1, HOURS, container)
then: "the ruleset should become paused again (until last occurrence)"
conditions.eventually {
assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == PAUSED
}
- when: "the pause elapses"
- engine.unPauseRuleset(deployment)
+ when: "time advances to inside the next validity period"
+ advancePseudoClock(47, HOURS, container)
then: "eventually the ruleset should be unpaused (last occurrence)"
conditions.eventually {
assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == DEPLOYED
}
- when: "the un-pause elapses"
- engine.pauseRuleset(deployment)
+ when: "time advances past the validity period"
+ advancePseudoClock(1, HOURS, container)
then: "the ruleset should expire"
conditions.eventually {
@@ -581,12 +562,6 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
}
cleanup: "static variables are reset and the mock is removed"
- if (originalPause != null) {
- RulesEngine.PAUSE_SCHEDULER = originalPause
- }
- if (originalUnpause != null) {
- RulesEngine.UNPAUSE_SCHEDULER = originalUnpause
- }
if (originalDebounceMillis != null) {
ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_DEBOUNCE_MILLIS = originalDebounceMillis
}
@@ -629,7 +604,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
and: "the pseudo clock is stopped"
stopPseudoClock()
- and: "a ruleset with sunset trigger condition is added whose action updates the thing asset"
+ when: "a ruleset with sunset trigger condition is added whose action updates the thing asset"
def rulesStr = getClass().getResource("/org/openremote/test/rules/JsonRuleSunset.json").text
def rule = parse(rulesStr, JsonRulesetDefinition.class).orElseThrow()
Ruleset ruleset = new RealmRuleset(
@@ -638,11 +613,11 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
Ruleset.Lang.JSON,
rulesStr)
ruleset = rulesetStorageService.merge(ruleset)
- def sunsetCalculator = JsonRulesBuilder.getSunCalculator(rule.rules[0].when.groups[0].items[0].sun, timerService)
+ def sunsetCalculator = JsonRulesBuilder.getSunCalculator(ruleset, rule.rules[0].when.groups[0].items[0].sun, timerService)
def sunTimes = sunsetCalculator.execute()
def lastFireTimestamp = 0
- expect: "the rule engines to become available and be running with asset states inserted"
+ 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
@@ -650,6 +625,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
assert realmBuildingEngine.assetStates.size() == DEMO_RULE_STATES_SMART_BUILDING
assert realmBuildingEngine.lastFireTimestamp == timerService.getNow().toEpochMilli()
lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
+ assert sunsetCalculator != null
}
and: "the next sunset should be calculated and should be in the future"
@@ -735,7 +711,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
Ruleset.Lang.JSON,
rulesStr)
ruleset = rulesetStorageService.merge(ruleset)
- sunsetCalculator = JsonRulesBuilder.getSunCalculator(rule.rules[0].when.groups[0].items[0].sun, timerService)
+ sunsetCalculator = JsonRulesBuilder.getSunCalculator(ruleset, rule.rules[0].when.groups[0].items[0].sun, timerService)
sunTimes = sunsetCalculator.execute()
then: "the rule engines to become available and be running with asset states inserted"
@@ -790,7 +766,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
Ruleset.Lang.JSON,
rulesStr)
ruleset = rulesetStorageService.merge(ruleset)
- sunsetCalculator = JsonRulesBuilder.getSunCalculator(rule.rules[0].when.groups[0].items[0].sun, timerService)
+ sunsetCalculator = JsonRulesBuilder.getSunCalculator(ruleset, rule.rules[0].when.groups[0].items[0].sun, timerService)
sunTimes = sunsetCalculator.execute()
then: "the rule engines to become available and be running with asset states inserted"
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy
index 32768430c..cad13283a 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy
@@ -54,7 +54,6 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
assert apartment1Engine != null
assert apartment1Engine.isRunning()
assert apartment1Engine.assetStates.size() == DEMO_RULE_STATES_APARTMENT_1
- assert noRuleEngineFiringScheduled()
}
and: "the ventilation should be off"
@@ -69,11 +68,6 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
new AttributeEvent(managerTestSetup.apartment1Id, "ventilationAuto", true)
)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "auto ventilation should be on"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -101,11 +95,6 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
advancePseudoClock(2, MINUTES, container)
}
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "ventilation level of the apartment should be MEDIUM"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -118,11 +107,6 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
)
simulatorProtocol.putValue(co2LevelDecrement)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "the decreasing CO2 should have been detected in rules"
conditions.eventually {
assert apartment1Engine.assetEvents.any() {
@@ -160,11 +144,6 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
advancePseudoClock(2, MINUTES, container)
}
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "ventilation level of the apartment should be HIGH"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -186,11 +165,6 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
and: "time advances"
advancePseudoClock(20, MINUTES, container)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "ventilation level of the apartment should be MEDIUM"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -212,11 +186,6 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
and: "time advances"
advancePseudoClock(15, MINUTES, container)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "ventilation level of the apartment should be LOW"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy
index da8a6c8a5..64d3ec48f 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy
@@ -285,67 +285,39 @@ class ResidenceNotifyAlarmTriggerTest extends Specification implements ManagerCo
rulesetDeployment = apartment1Engine.deployments.get(ruleset.getId())
assert rulesetDeployment != null
assert rulesetDeployment.status == RulesetStatus.PAUSED
- assert apartment1Engine.pauseTimers.size() == 0
- assert apartment1Engine.unpauseTimers.size() == 1
- // Assert scheduled future delay is correct
- assert fromTo[0].getTime() - getClockTimeOf(container) - apartment1Engine.unpauseTimers.get(ruleset.getId()).getDelay(TimeUnit.MILLISECONDS) <= 10000
}
when: "time advances to within the first valid time period"
advancePseudoClock(fromTo[0].getTime()-getClockTimeOf(container)+10000, TimeUnit.MILLISECONDS, container)
- apartment1Engine.unPauseRuleset(rulesetDeployment)
- then: "the ruleset status should be un-paused"
+ then: "the ruleset status should become un-paused"
conditions.eventually {
assert rulesetDeployment.status == RulesetStatus.DEPLOYED
}
- and: "the next pause of the ruleset should be scheduled"
- assert apartment1Engine.pauseTimers.size() == 1
- assert apartment1Engine.unpauseTimers.size() == 0
- assert fromTo[1].getTime() - getClockTimeOf(container) - apartment1Engine.pauseTimers.get(ruleset.getId()).getDelay(TimeUnit.MILLISECONDS) <= 10000
-
when: "time advances past the first expiry the ruleset is paused again"
advancePseudoClock((fromTo[1].getTime()-getClockTimeOf(container))+10000, TimeUnit.MILLISECONDS, container)
- apartment1Engine.pauseRuleset(rulesetDeployment)
then: "the ruleset status should be paused"
conditions.eventually {
assert rulesetDeployment.status == RulesetStatus.PAUSED
}
- when: "the next occurrence is calculated"
- fromTo = getOccurrenceFromTo(baseTime, 1)
-
- then: "the next unpause of the ruleset should be scheduled"
- assert apartment1Engine.pauseTimers.size() == 0
- assert apartment1Engine.unpauseTimers.size() == 1
- assert fromTo[0].getTime() - getClockTimeOf(container) - apartment1Engine.unpauseTimers.get(ruleset.getId()).getDelay(TimeUnit.MILLISECONDS) <= 10000
-
when: "time advances to within the next valid time period"
+ fromTo = getOccurrenceFromTo(baseTime, 1)
advancePseudoClock(fromTo[0].getTime() - getClockTimeOf(container) + (61*60000), TimeUnit.MILLISECONDS, container)
- apartment1Engine.unPauseRuleset(rulesetDeployment)
then: "the ruleset status should be un-paused"
conditions.eventually {
assert rulesetDeployment.status == RulesetStatus.DEPLOYED
}
- and: "the next pause of the ruleset should be scheduled"
- assert apartment1Engine.pauseTimers.size() == 1
- assert apartment1Engine.unpauseTimers.size() == 0
- // Assert scheduled future delay is correct
- assert fromTo[1].getTime() - getClockTimeOf(container) - apartment1Engine.pauseTimers.get(ruleset.getId()).getDelay(TimeUnit.MILLISECONDS) <= 10000
-
when: "time advances past the second expiry the ruleset is paused again"
advancePseudoClock(fromTo[1].getTime() - getClockTimeOf(container) + (45*60000), TimeUnit.MILLISECONDS, container)
- apartment1Engine.pauseRuleset(rulesetDeployment)
then: "the ruleset status should be expired"
conditions.eventually {
assert rulesetDeployment.status == RulesetStatus.EXPIRED
- assert apartment1Engine.pauseTimers.size() == 0
- assert apartment1Engine.unpauseTimers.size() == 0
}
cleanup: "the mock is removed"
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
index a7f6c36e1..cc8c7f9ca 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
@@ -54,11 +54,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
assert apartment1Engine.assetStates.size() == DEMO_RULE_STATES_APARTMENT_1
}
- and: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
and: "the presence detected flag and timestamp should not be set"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -82,11 +77,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
)
simulatorProtocol.updateSensor(hallwayMotionSensorEvent)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "presence should be detected in all rooms"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -109,11 +99,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
)
simulatorProtocol.updateSensor(kitchenMotionSensorEvent)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "presence should be detected in all rooms and timestamp should be updated of one room"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -135,11 +120,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
)
simulatorProtocol.updateSensor(kitchenMotionSensorEvent)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "some presence should still be detected and timestamps are still available"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -161,11 +141,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
)
simulatorProtocol.updateSensor(hallwayMotionSensorEvent)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "no presence should be detected but the last timestamp still available"
conditions.eventually {
def apartment = assetStorageService.find(managerTestSetup.apartment1Id, true)
@@ -209,11 +184,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
assert apartment1Engine.assetStates.size() == DEMO_RULE_STATES_APARTMENT_1
}
- and: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
and: "the presence detected flag and timestamp of the room should not be set"
conditions.eventually {
def roomAsset = assetStorageService.find(managerTestSetup.apartment1LivingroomId, true)
@@ -260,11 +230,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
}
*/
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "presence should be detected and the last motion sensor trigger is the last detected timestamp"
conditions.eventually {
def roomAsset = assetStorageService.find(managerTestSetup.apartment1LivingroomId, true)
@@ -295,10 +260,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
}
advancePseudoClock(5, MINUTES, container)
-
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
}
then: "presence should be detected and the last motion sensor trigger is the last detected timestamp"
@@ -311,11 +272,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
when: "there is no CO2 level increase for a while (nobody resting in the room)"
advancePseudoClock(20, MINUTES, container)
- then: "the rule engines should settle"
- conditions.eventually {
- assert noRuleEngineFiringScheduled()
- }
-
then: "presence should be gone but the last timestamp still available"
conditions.eventually {
def roomAsset = assetStorageService.find(managerTestSetup.apartment1LivingroomId, true)
diff --git a/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy b/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy
index d4dbd8a3f..23f22479b 100644
--- a/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy
+++ b/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy
@@ -6,7 +6,6 @@ import org.openremote.container.Container
import org.openremote.container.message.MessageBrokerService
import org.openremote.container.timer.TimerService
import org.openremote.manager.mqtt.MQTTBrokerService
-import org.openremote.manager.rules.RulesService
import org.openremote.model.ContainerService
import spock.util.concurrent.PollingConditions
@@ -14,7 +13,6 @@ import java.util.concurrent.TimeUnit
import java.util.stream.Collectors
import static org.openremote.container.timer.TimerService.Clock.PSEUDO
-import static org.openremote.container.timer.TimerService.Clock.REAL
import static org.openremote.container.timer.TimerService.TIMER_CLOCK_TYPE
import static org.openremote.container.web.WebService.OR_WEBSERVER_LISTEN_PORT
import static org.openremote.manager.mqtt.MQTTBrokerService.MQTT_SERVER_LISTEN_HOST
@@ -50,23 +48,6 @@ trait ManagerContainerTrait extends ContainerTrait {
defaultServices(Arrays.asList(additionalServices))
}
- /**
- * Use wall clock instead of pseudo clock.
- */
- Container startContainerWithoutPseudoClock(Map<String, String> config, Iterable<ContainerService> services) {
- startContainer(config << [(TIMER_CLOCK_TYPE): REAL.name()], services)
- }
-
- boolean noRuleEngineFiringScheduled() {
- if (!this.container) {
- return false
- }
-
- def rulesService = this.container.getService(RulesService.class)
-
- return (rulesService.globalEngine == null || rulesService.globalEngine.fireTimer == null) && rulesService.realmEngines.values().every {it.fireTimer == null} && rulesService.assetEngines.values().every {it.fireTimer == null}
- }
-
/**
* Execute pseudo clock operations in Container.
*/
diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.ts
index 31d8d5b40..2ce002eb4 100644
--- a/ui/component/core/src/util.ts
+++ b/ui/component/core/src/util.ts
@@ -635,7 +635,7 @@ export function getValueAsString(value: any, formatProvider: () => ValueFormat |
} else if (format.asDate) {
// Assume UNIX timestamp in ms
value = new Date(value);
- }
+ }
} else if (typeof(value) === "boolean" && format.asNumber) {
value = value ? 1 : 0;
}
@@ -801,11 +801,11 @@ function mergeObjectKey(destination: object, path: string[], key: string, value:
if (value === undefined || value === null) {
delete dest[key];
} else if (Array.isArray(dest[key])) {
- if (mergeArrays) {
- dest[key] = [...dest[key], ...value];
- } else {
- dest[key] = [...value];
- }
+ if (mergeArrays) {
+ dest[key] = [...dest[key], ...value];
+ } else {
+ dest[key] = [...value];
+ }
} else if (typeof(value) === "object") {
dest[key] = mergeObjects({...dest[key]}, value, mergeArrays);
} else {
@@ -834,7 +834,7 @@ function getValueFormatConstraintOrUnits<T>(lookup: WellknownMetaItems.FORMAT |
matched = JSON.parse(str) as T;
if (matched) {
if (lookup === WellknownMetaItems.FORMAT) {
- formats.push(matched);
+ formats.push(matched as ValueFormat);
} else {
return matched;
}
@@ -847,7 +847,7 @@ function getValueFormatConstraintOrUnits<T>(lookup: WellknownMetaItems.FORMAT |
if (matched) {
if (lookup === WellknownMetaItems.FORMAT) {
- formats.push(matched);
+ formats.push(matched as ValueFormat);
} else {
return matched;
}
@@ -857,7 +857,7 @@ function getValueFormatConstraintOrUnits<T>(lookup: WellknownMetaItems.FORMAT |
if (descriptor && typeof(descriptor) !== "string" && (descriptor as any).hasOwnProperty(lookup)) {
matched = (descriptor as any)[lookup] as T;
if (lookup === WellknownMetaItems.FORMAT) {
- formats.push(matched);
+ formats.push(matched as ValueFormat);
} else {
return matched;
}
@@ -867,7 +867,7 @@ function getValueFormatConstraintOrUnits<T>(lookup: WellknownMetaItems.FORMAT |
const valueDescriptor = AssetModelUtil.getValueDescriptor((descriptor as AbstractNameValueDescriptorHolder).type);
matched = (valueDescriptor as any)[lookup] as T;
if (lookup === WellknownMetaItems.FORMAT) {
- formats.push(matched);
+ formats.push(matched as ValueFormat);
} else {
return matched;
}