Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
querdenker2k committed Mar 11, 2024
1 parent 92266fd commit e3a5920
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleChannelExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleItemChangeExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleItemExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleItemReceivedCommandExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleItemReceivedUpdateExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRulePreconditionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleThingExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleTimeTimerExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleTimedCronExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.*;
import org.openhab.automation.jrule.internal.module.JRuleModuleEntry;
import org.openhab.automation.jrule.rules.JRuleMemberOf;
import org.openhab.automation.jrule.things.JRuleThingStatus;
Expand All @@ -55,6 +47,7 @@ public class JRuleBuilder {

final private List<PreCondition> preConditions = new ArrayList<>();

final private List<WhenStartupTrigger> whenStartupTriggers = new ArrayList<>();
final private List<WhenThingTrigger> whenThingTriggers = new ArrayList<>();
final private List<WhenChannelTrigger> whenChannelTriggers = new ArrayList<>();
final private List<WhenItemReceivedCommand> whenItemReceivedCommandTriggers = new ArrayList<>();
Expand Down Expand Up @@ -104,6 +97,11 @@ public JRuleBuilder preCondition(String itemName, Condition condition) {
return this;
}

public JRuleBuilder whenStartupTrigger(int level) {
whenStartupTriggers.add(new WhenStartupTrigger(level));
return this;
}

public JRuleBuilder whenThingTrigger(String thingName, @Nullable JRuleThingStatus from,
@Nullable JRuleThingStatus to) {
whenThingTriggers.add(new WhenThingTrigger(thingName, from, to));
Expand Down Expand Up @@ -164,6 +162,15 @@ public boolean build() {
Optional.ofNullable(data.condition.neq)))
.toList();

whenStartupTriggers.forEach(data -> {
JRuleStartupExecutionContext context = new JRuleStartupExecutionContext(uid, logName, loggingTags,
invocationCallback, preconditionContexts, timedLock, delayed, data.level);
jRuleEngine.addToContext(context, enableRule);
jRuleEngine.ruleLoadingStatistics.addStartupTrigger();
ruleModuleEntry.addJRuleWhenStartupTrigger(context);
addedToContext.set(true);
});

whenThingTriggers.forEach(data -> {
JRuleThingExecutionContext context = new JRuleThingExecutionContext(uid, logName, loggingTags,
invocationCallback, Optional.ofNullable(data.thingName), Optional.ofNullable(data.from),
Expand Down Expand Up @@ -260,6 +267,9 @@ private JRuleItemExecutionContext.JRuleConditionContext toJRuleConditionContext(
private record PreCondition(String itemName, Condition condition) {
}

private record WhenStartupTrigger(int level) {
}

private record WhenThingTrigger(String thingName, JRuleThingStatus from, JRuleThingStatus to) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* <p>
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* <p>
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* <p>
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.jrule.internal.engine;
Expand Down Expand Up @@ -39,32 +39,13 @@
import org.openhab.automation.jrule.exception.JRuleRuntimeException;
import org.openhab.automation.jrule.internal.JRuleConfig;
import org.openhab.automation.jrule.internal.JRuleLog;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleChannelExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleItemExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleThingExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleTimedExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.*;
import org.openhab.automation.jrule.internal.engine.timer.JRuleTimerExecutor;
import org.openhab.automation.jrule.internal.events.JRuleEventSubscriber;
import org.openhab.automation.jrule.internal.handler.JRuleTimerHandler;
import org.openhab.automation.jrule.internal.module.JRuleModuleEntry;
import org.openhab.automation.jrule.internal.module.JRuleRuleProvider;
import org.openhab.automation.jrule.rules.JRule;
import org.openhab.automation.jrule.rules.JRuleCondition;
import org.openhab.automation.jrule.rules.JRuleDebounce;
import org.openhab.automation.jrule.rules.JRuleDelayed;
import org.openhab.automation.jrule.rules.JRuleLogName;
import org.openhab.automation.jrule.rules.JRuleMemberOf;
import org.openhab.automation.jrule.rules.JRuleName;
import org.openhab.automation.jrule.rules.JRulePrecondition;
import org.openhab.automation.jrule.rules.JRuleTag;
import org.openhab.automation.jrule.rules.JRuleWhenChannelTrigger;
import org.openhab.automation.jrule.rules.JRuleWhenCronTrigger;
import org.openhab.automation.jrule.rules.JRuleWhenItemChange;
import org.openhab.automation.jrule.rules.JRuleWhenItemReceivedCommand;
import org.openhab.automation.jrule.rules.JRuleWhenItemReceivedUpdate;
import org.openhab.automation.jrule.rules.JRuleWhenThingTrigger;
import org.openhab.automation.jrule.rules.JRuleWhenTimeTrigger;
import org.openhab.automation.jrule.rules.*;
import org.openhab.automation.jrule.rules.event.JRuleEvent;
import org.openhab.automation.jrule.things.JRuleThingStatus;
import org.openhab.core.events.AbstractEvent;
Expand Down Expand Up @@ -223,34 +204,9 @@ private void add(Method method, JRule jRule, boolean enableRule) {
.orElse(null),
Optional.of(jRuleWhen.from()).filter(s -> s != JRuleThingStatus.THING_UNKNOWN).orElse(null),
Optional.of(jRuleWhen.to()).filter(s -> s != JRuleThingStatus.THING_UNKNOWN).orElse(null)));
Arrays.stream(method.getAnnotationsByType(JRuleWhenThingTrigger.class)).forEach(jRuleWhen -> {
JRuleThingExecutionContext context = new JRuleThingExecutionContext(jRule, logName, loggingTags, method,
Optional.of(jRuleWhen.thing()).filter(StringUtils::isNotEmpty).filter(s -> !s.equals("*")),
Optional.of(jRuleWhen.from()).filter(s -> s != JRuleThingStatus.THING_UNKNOWN),
Optional.of(jRuleWhen.to()).filter(s -> s != JRuleThingStatus.THING_UNKNOWN),
jRulePreconditionContexts, timedLock, delayed);
addToContext(context, enableRule);
ruleLoadingStatistics.addThingTrigger();
ruleModuleEntry.addJRuleWhenThingTrigger(context);
addedToContext.set(true);
});

// Check if rule was actually activated, i.e. if triggers are present
if (!jRuleBuilder.build()) {
Arrays.stream(method.getAnnotationsByType(JRuleWhenStartup.class)).forEach(jRuleWhen -> {
JRuleStartupExecutionContext context = new JRuleStartupExecutionContext(jRule, logName, loggingTags, method,
jRulePreconditionContexts, timedLock, delayed, jRuleWhen.level());
addToContext(context, enableRule);
ruleLoadingStatistics.addStartupTrigger();
ruleModuleEntry.addJRuleWhenStartupTrigger(context);
addedToContext.set(true);
});

// Check if any rule triggers are present
if (!addedToContext.get()) {
logWarn("Skipping rule method {} on class {} with no JRuleWhenXXX annotation triggers", method.getName(),
jRule.getClass().getName());
}
Arrays.stream(method.getAnnotationsByType(JRuleWhenStartup.class))
.forEach(jRuleWhen -> jRuleBuilder.whenStartupTrigger(jRuleWhen.level()));
}

boolean addToContext(JRuleExecutionContext context, boolean enableRule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
*/
package org.openhab.automation.jrule.internal.engine.excutioncontext;

import java.lang.reflect.Method;
import java.time.Duration;
import java.util.List;

import org.openhab.automation.jrule.rules.JRule;
import org.openhab.automation.jrule.internal.engine.JRuleInvocationCallback;
import org.openhab.automation.jrule.rules.event.JRuleEvent;
import org.openhab.automation.jrule.rules.event.JRuleStartupEvent;
import org.openhab.core.events.AbstractEvent;
Expand All @@ -31,10 +30,10 @@ public class JRuleStartupExecutionContext extends JRuleExecutionContext {

private final int startupLevel;

public JRuleStartupExecutionContext(JRule rule, String logName, String[] loggingTags, Method method,
List<JRulePreconditionContext> preconditionContextList, Duration timedLock, Duration delayed,
int startupLevel) {
super(rule, logName, loggingTags, method, preconditionContextList, timedLock, delayed);
public JRuleStartupExecutionContext(String uid, String logName, String[] loggingTags,
JRuleInvocationCallback invocationCallback, List<JRulePreconditionContext> preconditionContextList,
Duration timedLock, Duration delayed, int startupLevel) {
super(uid, logName, loggingTags, invocationCallback, preconditionContextList, timedLock, delayed);
this.startupLevel = startupLevel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.openhab.automation.jrule.internal.engine.JRuleBuilder.Condition;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleChannelExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleItemChangeExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleItemReceivedCommandExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleItemReceivedUpdateExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleThingExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleTimeTimerExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.JRuleTimedCronExecutionContext;
import org.openhab.automation.jrule.internal.engine.excutioncontext.*;
import org.openhab.automation.jrule.internal.module.JRuleRuleProvider;
import org.openhab.automation.jrule.internal.rules.JRuleAbstractTest;
import org.openhab.automation.jrule.rules.JRuleMemberOf;
Expand Down Expand Up @@ -206,6 +200,16 @@ public void testPreconditions() {
verify(ruleProvider, times(1)).add(argThat(entry -> entry.getTriggers().size() == 1));
}

@Test
public void testWhenStartupTrigger() {

boolean result = jRuleBuilder.whenStartupTrigger(10).whenStartupTrigger(30).build();

Assertions.assertTrue(result);
verify(jRuleEngine, times(2)).addToContext(isA(JRuleStartupExecutionContext.class), eq(false));
verify(ruleProvider, times(1)).add(argThat(entry -> entry.getTriggers().size() == 2));
}

@Test
public void testWhenThingTrigger() {

Expand Down

0 comments on commit e3a5920

Please sign in to comment.