mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-31 13:34:24 +02:00
[DSL rule] Add conditions to DSL Rule file syntax (#5453)
Supports all conditions except the execution of script. Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
+120
@@ -15,6 +15,7 @@ package org.openhab.core.model.rule.runtime;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -22,6 +23,7 @@ import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.xtext.naming.QualifiedName;
|
||||
@@ -32,13 +34,19 @@ import org.junit.jupiter.api.Test;
|
||||
import org.openhab.core.automation.Rule;
|
||||
import org.openhab.core.automation.RuleProvider;
|
||||
import org.openhab.core.automation.internal.module.handler.ChannelEventTriggerHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.DayOfWeekConditionHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.EphemerisConditionHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.GenericCronTriggerHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.GroupCommandTriggerHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.GroupStateTriggerHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.IntervalConditionHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.ItemCommandTriggerHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.ItemStateConditionHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.ItemStateTriggerHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.SystemTriggerHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.ThingStatusConditionHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.ThingStatusTriggerHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.TimeOfDayConditionHandler;
|
||||
import org.openhab.core.automation.internal.module.handler.TimeOfDayTriggerHandler;
|
||||
import org.openhab.core.automation.module.script.internal.handler.AbstractScriptModuleHandler;
|
||||
import org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler;
|
||||
@@ -122,6 +130,7 @@ public class DSLRuleProviderTest extends JavaOSGiTest {
|
||||
assertThat(firstRule.getTags(), hasSize(2));
|
||||
assertThat(firstRule.getTags(), hasItem("Test"));
|
||||
assertThat(firstRule.getTags(), hasItem("my test"));
|
||||
assertThat(firstRule.getConditions(), hasSize(0));
|
||||
assertThat(firstRule.getTriggers().getFirst().getTypeUID(), is(SystemTriggerHandler.STARTLEVEL_MODULE_TYPE_ID));
|
||||
assertThat(firstRule.getActions().getFirst().getTypeUID(), is(ScriptActionHandler.TYPE_ID));
|
||||
assertThat(firstRule.getActions().getFirst().getConfiguration()
|
||||
@@ -135,6 +144,7 @@ public class DSLRuleProviderTest extends JavaOSGiTest {
|
||||
assertThat(secondRule.getTriggers().getFirst().getTypeUID(), is(ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID));
|
||||
assertThat(secondRule.getTriggers().getFirst().getConfiguration().get(ItemStateTriggerHandler.CFG_ITEMNAME),
|
||||
is("X"));
|
||||
assertThat(secondRule.getConditions(), hasSize(0));
|
||||
assertThat(secondRule.getActions().getFirst().getTypeUID(), is(ScriptActionHandler.TYPE_ID));
|
||||
assertThat(secondRule.getActions().getFirst().getConfiguration()
|
||||
.get(AbstractScriptModuleHandler.CONFIG_SCRIPT_TYPE), is(DSLScriptEngine.MIMETYPE_OPENHAB_DSL_RULE));
|
||||
@@ -245,6 +255,116 @@ public class DSLRuleProviderTest extends JavaOSGiTest {
|
||||
is("START"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllConditions() {
|
||||
Collection<Rule> rules = dslRuleProvider.getAll();
|
||||
assertThat(rules.size(), is(0));
|
||||
|
||||
String model = "rule RuleWithAllConditions\n" + //
|
||||
"when\n" + //
|
||||
" Item X received command ON\n" + //
|
||||
"but only if\n" + //
|
||||
" Time is between 19:20 and 22:10 and\n" + //
|
||||
" Day is Thursday or Tuesday or Sunday and\n" + //
|
||||
" Day is a weekday and\n" + //
|
||||
" Day is a weekend and\n" + //
|
||||
" Day offset 3 is a holiday and\n" + //
|
||||
" Day offset = -2 is not a holiday and\n" + //
|
||||
" Day is in \"my-dayset\" and\n" + //
|
||||
" Thing \"T1\" is ONLINE and\n" + //
|
||||
" Thing \"T2\" is not UNKNOWN and\n" + //
|
||||
" Item W = \"a value\" and\n" + //
|
||||
" Item X ON and\n" + //
|
||||
" Item Y != 0 and\n" + //
|
||||
" Item Z > 5.25 and\n" + //
|
||||
" 15000 ms between executions\n" + //
|
||||
"then\n" + //
|
||||
" logInfo('Test', 'Test')\n" + //
|
||||
"end\n\n";
|
||||
|
||||
modelRepository.addOrRefreshModel(TESTMODEL_NAME,
|
||||
new ByteArrayInputStream(model.getBytes(StandardCharsets.UTF_8)));
|
||||
Collection<Rule> actualRules = dslRuleProvider.getAll();
|
||||
|
||||
assertThat(actualRules.size(), is(1));
|
||||
|
||||
Iterator<Rule> it = actualRules.iterator();
|
||||
|
||||
Rule rule = it.next();
|
||||
|
||||
assertThat(rule.getUID(), is("dslruletest-1"));
|
||||
assertThat(rule.getName(), is("RuleWithAllConditions"));
|
||||
assertThat(rule.getConditions().size(), is(14));
|
||||
|
||||
assertThat(rule.getConditions().getFirst().getTypeUID(), is(TimeOfDayConditionHandler.MODULE_TYPE_ID));
|
||||
assertThat(rule.getConditions().getFirst().getConfiguration().get(TimeOfDayConditionHandler.CFG_START_TIME),
|
||||
is("19:20"));
|
||||
assertThat(rule.getConditions().getFirst().getConfiguration().get(TimeOfDayConditionHandler.CFG_END_TIME),
|
||||
is("22:10"));
|
||||
|
||||
assertThat(rule.getConditions().get(1).getTypeUID(), is(DayOfWeekConditionHandler.MODULE_TYPE_ID));
|
||||
Object days = rule.getConditions().get(1).getConfiguration().get(DayOfWeekConditionHandler.CFG_DAYS);
|
||||
assertThat(days, instanceOf(List.class));
|
||||
assertThat((List<String>) days, hasSize(3));
|
||||
assertThat((List<String>) days, contains("THU", "TUE", "SUN"));
|
||||
|
||||
assertThat(rule.getConditions().get(2).getTypeUID(), is(EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID));
|
||||
assertThat(rule.getConditions().get(2).getConfiguration().get("offset"), is(new BigDecimal(0)));
|
||||
|
||||
assertThat(rule.getConditions().get(3).getTypeUID(), is(EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID));
|
||||
assertThat(rule.getConditions().get(3).getConfiguration().get("offset"), is(new BigDecimal(0)));
|
||||
|
||||
assertThat(rule.getConditions().get(4).getTypeUID(), is(EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID));
|
||||
assertThat(rule.getConditions().get(4).getConfiguration().get("offset"), is(new BigDecimal(3)));
|
||||
|
||||
assertThat(rule.getConditions().get(5).getTypeUID(), is(EphemerisConditionHandler.NOT_HOLIDAY_MODULE_TYPE_ID));
|
||||
assertThat(rule.getConditions().get(5).getConfiguration().get("offset"), is(new BigDecimal(-2)));
|
||||
|
||||
assertThat(rule.getConditions().get(6).getTypeUID(), is(EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID));
|
||||
assertThat(rule.getConditions().get(6).getConfiguration().get("dayset"), is("my-dayset"));
|
||||
assertThat(rule.getConditions().get(6).getConfiguration().get("offset"), is(new BigDecimal(0)));
|
||||
|
||||
assertThat(rule.getConditions().get(7).getTypeUID(), is(ThingStatusConditionHandler.THING_STATUS_CONDITION));
|
||||
assertThat(rule.getConditions().get(7).getConfiguration().get(ThingStatusConditionHandler.CFG_THING_UID),
|
||||
is("T1"));
|
||||
assertThat(rule.getConditions().get(7).getConfiguration().get(ThingStatusConditionHandler.CFG_OPERATOR),
|
||||
is("="));
|
||||
assertThat(rule.getConditions().get(7).getConfiguration().get(ThingStatusConditionHandler.CFG_STATUS),
|
||||
is("ONLINE"));
|
||||
|
||||
assertThat(rule.getConditions().get(8).getTypeUID(), is(ThingStatusConditionHandler.THING_STATUS_CONDITION));
|
||||
assertThat(rule.getConditions().get(8).getConfiguration().get(ThingStatusConditionHandler.CFG_THING_UID),
|
||||
is("T2"));
|
||||
assertThat(rule.getConditions().get(8).getConfiguration().get(ThingStatusConditionHandler.CFG_OPERATOR),
|
||||
is("!="));
|
||||
assertThat(rule.getConditions().get(8).getConfiguration().get(ThingStatusConditionHandler.CFG_STATUS),
|
||||
is("UNKNOWN"));
|
||||
|
||||
assertThat(rule.getConditions().get(9).getTypeUID(), is(ItemStateConditionHandler.ITEM_STATE_CONDITION));
|
||||
assertThat(rule.getConditions().get(9).getConfiguration().get(ItemStateConditionHandler.ITEM_NAME), is("W"));
|
||||
assertThat(rule.getConditions().get(9).getConfiguration().get(ItemStateConditionHandler.OPERATOR), is("="));
|
||||
assertThat(rule.getConditions().get(9).getConfiguration().get(ItemStateConditionHandler.STATE), is("a value"));
|
||||
|
||||
assertThat(rule.getConditions().get(10).getTypeUID(), is(ItemStateConditionHandler.ITEM_STATE_CONDITION));
|
||||
assertThat(rule.getConditions().get(10).getConfiguration().get(ItemStateConditionHandler.ITEM_NAME), is("X"));
|
||||
assertThat(rule.getConditions().get(10).getConfiguration().get(ItemStateConditionHandler.OPERATOR), is("="));
|
||||
assertThat(rule.getConditions().get(10).getConfiguration().get(ItemStateConditionHandler.STATE), is("ON"));
|
||||
|
||||
assertThat(rule.getConditions().get(11).getTypeUID(), is(ItemStateConditionHandler.ITEM_STATE_CONDITION));
|
||||
assertThat(rule.getConditions().get(11).getConfiguration().get(ItemStateConditionHandler.ITEM_NAME), is("Y"));
|
||||
assertThat(rule.getConditions().get(11).getConfiguration().get(ItemStateConditionHandler.OPERATOR), is("!="));
|
||||
assertThat(rule.getConditions().get(11).getConfiguration().get(ItemStateConditionHandler.STATE), is("0"));
|
||||
|
||||
assertThat(rule.getConditions().get(12).getTypeUID(), is(ItemStateConditionHandler.ITEM_STATE_CONDITION));
|
||||
assertThat(rule.getConditions().get(12).getConfiguration().get(ItemStateConditionHandler.ITEM_NAME), is("Z"));
|
||||
assertThat(rule.getConditions().get(12).getConfiguration().get(ItemStateConditionHandler.OPERATOR), is(">"));
|
||||
assertThat(rule.getConditions().get(12).getConfiguration().get(ItemStateConditionHandler.STATE), is("5.25"));
|
||||
|
||||
assertThat(rule.getConditions().get(13).getTypeUID(), is(IntervalConditionHandler.MODULE_TYPE_ID));
|
||||
assertThat(rule.getConditions().get(13).getConfiguration().get(IntervalConditionHandler.CFG_MIN_INTERVAL),
|
||||
is(new BigDecimal(15000)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVars() {
|
||||
Collection<Rule> rules = dslRuleProvider.getAll();
|
||||
|
||||
Reference in New Issue
Block a user