From 0edae941f1f1c3d9643f6c241a76a50437d52695 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Wed, 25 Mar 2026 21:22:06 +0100 Subject: [PATCH] [DSL rule] Add tags to DSL file syntax (#5442) * [DSL rule] Add tags to DSL file syntax Closes #5436 Signed-off-by: Laurent Garnier --- .../core/model/rule/runtime/internal/DSLRuleProvider.java | 7 ++++++- .../src/org/openhab/core/model/rule/Rules.xtext | 2 +- .../core/model/rule/runtime/DSLRuleProviderTest.java | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.core.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java b/bundles/org.openhab.core.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java index 035aa22c0..641d53074 100644 --- a/bundles/org.openhab.core.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java +++ b/bundles/org.openhab.core.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java @@ -18,6 +18,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @@ -279,7 +280,11 @@ public class DSLRuleProvider List actions = List.of(ActionBuilder.create().withId("script").withTypeUID("script.ScriptAction") .withConfiguration(cfg).build()); - return RuleBuilder.create(uid).withName(name).withTriggers(triggers).withActions(actions).build(); + List ruleTags = rule.getTags(); + Set tags = ruleTags == null ? Set.of() : Set.copyOf(ruleTags); + + return RuleBuilder.create(uid).withTags(tags).withName(name).withTriggers(triggers).withActions(actions) + .build(); } private String removeIndentation(String script) { diff --git a/bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext b/bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext index 1494c6016..3c5c61e2d 100644 --- a/bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext +++ b/bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext @@ -12,7 +12,7 @@ VariableDeclaration: ; Rule: - 'rule' name=(STRING|ID) + 'rule' name=(STRING|ID) ('[' tags+=(ID|STRING) (',' tags+=(ID|STRING))* ']')? 'when' eventtrigger+=EventTrigger ('or' eventtrigger+=EventTrigger)* 'then' script=XExpressionInClosure 'end' diff --git a/itests/org.openhab.core.model.rule.tests/src/main/java/org/openhab/core/model/rule/runtime/DSLRuleProviderTest.java b/itests/org.openhab.core.model.rule.tests/src/main/java/org/openhab/core/model/rule/runtime/DSLRuleProviderTest.java index 9228865a5..318e33be1 100644 --- a/itests/org.openhab.core.model.rule.tests/src/main/java/org/openhab/core/model/rule/runtime/DSLRuleProviderTest.java +++ b/itests/org.openhab.core.model.rule.tests/src/main/java/org/openhab/core/model/rule/runtime/DSLRuleProviderTest.java @@ -14,6 +14,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.junit.jupiter.api.Assertions.assertNotNull; import java.io.ByteArrayInputStream; @@ -93,7 +94,7 @@ public class DSLRuleProviderTest extends JavaOSGiTest { Collection rules = dslRuleProvider.getAll(); assertThat(rules.size(), is(0)); - String model = "rule RuleNumberOne\n" + // + String model = "rule RuleNumberOne [ Test, \"my test\" ]\n" + // "when\n" + // " System started\n" + // "then\n" + // @@ -117,6 +118,9 @@ public class DSLRuleProviderTest extends JavaOSGiTest { assertThat(firstRule.getUID(), is("dslruletest-1")); assertThat(firstRule.getName(), is("RuleNumberOne")); + assertThat(firstRule.getTags(), hasSize(2)); + assertThat(firstRule.getTags(), hasItem("Test")); + assertThat(firstRule.getTags(), hasItem("my test")); 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() @@ -126,6 +130,7 @@ public class DSLRuleProviderTest extends JavaOSGiTest { assertThat(secondRule.getUID(), is("dslruletest-2")); assertThat(secondRule.getName(), is("Rule Number Two")); + assertThat(secondRule.getTags(), hasSize(0)); assertThat(secondRule.getTriggers().getFirst().getTypeUID(), is(ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID)); assertThat(secondRule.getTriggers().getFirst().getConfiguration().get(ItemStateTriggerHandler.CFG_ITEMNAME), is("X"));