Skip to content

Commit

Permalink
Only simulate rules that are enabled.
Browse files Browse the repository at this point in the history
Signed-off-by: Sönke Küper <soenkekueper@gmx.de>
  • Loading branch information
soenkekueper committed Apr 2, 2021
1 parent e433bef commit 3da3d6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ public RuleExecutionSimulator(RuleRegistry ruleRegistry, RuleEngineImpl ruleEngi
public Stream<RuleExecution> simulateRuleExecutions(ZonedDateTime fromDate, ZonedDateTime untilDate) {
logger.debug("Simulating rules from {} until {}.", fromDate, untilDate);

return ruleRegistry.stream().filter(RulePredicates.hasAllTags(TAG_SCHEDULE))
.map((Rule r) -> simulateExecutionsForRule(r, fromDate, untilDate)).flatMap(List::stream).sorted();
return ruleRegistry.stream() //
.filter(RulePredicates.hasAllTags(TAG_SCHEDULE)) //
.filter((Rule r) -> ruleEngine.isEnabled(r.getUID())) //
.map((Rule r) -> simulateExecutionsForRule(r, fromDate, untilDate)) //
.flatMap(List::stream).sorted();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

/**
* Test the simulation of rule executions with the {@link org.openhab.core.automation.internal.RuleExecutionSimulator}
*
*
* @author Sönke Küper - Created tests for rule simulation
*/
@NonNullByDefault
Expand Down Expand Up @@ -104,6 +104,7 @@ public void testSimulateRuleWithCronTrigger() {
logger.info("assert that rules are simulated correct");

final Rule cronRuleWithTimeOfDayCondition = createRuleWithCronExpressionTrigger();
final Rule disabledRule = createRuleWithCronExpressionTrigger();
final Rule timeOfDayTriggerWithDayOfWeekCondition = createRuleWithTimeOfDayTrigger();
final Rule timeOfDayTriggerWithEphemerisCondition = createRuleWithEphemerisCondition();

Expand All @@ -115,19 +116,25 @@ public void addProviderChangeListener(ProviderChangeListener<Rule> listener) {
@Override
public Collection<Rule> getAll() {
return Set.of(cronRuleWithTimeOfDayCondition, timeOfDayTriggerWithDayOfWeekCondition,
timeOfDayTriggerWithEphemerisCondition);
timeOfDayTriggerWithEphemerisCondition, disabledRule);
}

@Override
public void removeProviderChangeListener(ProviderChangeListener<Rule> listener) {
}
};

// Register the RuleProvider, so that the rules are registered within the ruleRegistry.
registerService(ruleProvider);
assertThat(ruleRegistry.get(cronRuleWithTimeOfDayCondition.getUID()), is(notNullValue()));
assertThat(ruleRegistry.get(disabledRule.getUID()), is(notNullValue()));
assertThat(ruleRegistry.get(timeOfDayTriggerWithDayOfWeekCondition.getUID()), is(notNullValue()));
assertThat(ruleRegistry.get(timeOfDayTriggerWithEphemerisCondition.getUID()), is(notNullValue()));

// Disable one rule, so it must not be contained within the simulation
ruleEngine.setEnabled(disabledRule.getUID(), false);
assertFalse(ruleEngine.isEnabled(disabledRule.getUID()));

// Simulate for two weeks
final ZonedDateTime from = ZonedDateTime.of(2021, 1, 4, 0, 0, 0, 0, ZoneId.systemDefault());
final ZonedDateTime until = ZonedDateTime.of(2021, 1, 17, 23, 59, 59, 0, ZoneId.systemDefault());
Expand Down

0 comments on commit 3da3d6b

Please sign in to comment.