Add optional rule UID to DSL Rule file syntax#5449
Add optional rule UID to DSL Rule file syntax#5449lolodomo wants to merge 19 commits intoopenhab:mainfrom
Conversation
Closes openhab#5437 Allow avoiding iussue described in openhab#5415 by setting an UID. Also avoid notifying the rule registry for isolated models. Signed-off-by: Laurent Garnier <lg.hc@free.fr>
|
Going into draft mode until I check what happens if several same UID are used. |
|
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/rules-and-rule-templates-yaml-integration/168568/143 |
No problem, it is detected by the registry (as for other stuff - items, things, ...). I realized that the syntax did not accept hyphen inside the UID and not a number at the beginning of the ID, I will have to extend that and implement the same control on the UID as what @Nadahar implemented in the YAML provider (":" also accepted). @florian-h05 or @jsjames : can you please tell me what is the syntax accepted in Main UI for the rule UID ? We need the same to allow "conversions". |
|
So Main UI accepts leading hypen and does not accept colon |
It might be that it's time to formalize the format for the UID. I think it's probably "a bit random" what has been done until now, when I made "my choice", I did so by looking around and trying to "sum it up". It probably shouldn't be very restricted though, to avoid breaking existing rules. |
|
For compatibility reason, I propose to at least cover Main UI pattern. |
I support that. |
|
I plan to create a new RuleUID extending AbstractUID. |
Unless you're a C programmer 😝 |
I'm not sure what the benefit is of such encoding. I mean, as far as I know, "any string" will work here (as that's basically what we have today). We have URL encoding to apply if used as a URL parameter, JSON has its way to encode this. I think we should ideally only restrict what can cause problems somewhere. I'm not sure what that is, but some of the "symbols" can probably collide with something somewhere..? |
|
I propose to be consistent with other UID like thing UID. |
Isn't that much more restrictive than it is now, and will break a lot of existing rules. Is that really "worth it"? |
...ore.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java
Outdated
Show resolved
Hide resolved
...ore.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java
Outdated
Show resolved
Hide resolved
...ore.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java
Outdated
Show resolved
Hide resolved
If your file has only comments for example. |
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
|
I added a last change, inspired from what @mherwege did for sitemaps. Before notifying the removal of a rule with a certain UID, first check if it exists with this UID in another model file and then rather notify a modification. |
...ore.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java
Outdated
Show resolved
Hide resolved
|
Irrespective of DSL, what is supposed to happen, if a rule is created, which has the same UID, as a different, pre-existing rule? Is there a test for DSL Rules, verifying the above behaviour, when two distinct |
In JS and I imagine the other languages as well I think which ever rule is created second will overwrite the first one. In the UI, I think MainUI will prevent you from creating the rule if you try to give it the same UID as an existing rule. |
The (rule) registry is checking unicity of id, it will accept the first and rejects the next ones with the same id, a warning is logged. It is true if rules are coming from the same provider or different providers. It is the same principle as for items or things, in fact a general principle for any of our registries |
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
|
@lolodomo As I said, it wasn't important to move the variable declaration back 😉 What is more important is to get this merged, so that I can continue with the file conversion. What exactly is this hanging on? |
Probably someone available that can take few minutes to review the PR. |
|
The Java 25 build failed, but I'm not convinced that it actually has anything to do with this PR: |
|
It feels a little unnatural to have the (required) name in the syntax followed by an optional uid, without indication of what is what (except for the documentation). |
I think this is how it will have to be because of backwards compatibility. UIDs will still be autogenerated if not specified, and the name will still be the name, so that all existing rules will be interpreted the same way as they are today. If you turn this around, all existing rules users have (and that they refer to from other scripts/rules) will get new UIDs and mayhem will ensue. That would make this a huge breaking change instead of a relatively minor enhancement. |
I understand that. But does anyone call file based DSL rules from other rules? Is it even possible in an easy way? You are working on supporting this in another PR. Are there any other places we rely on the current exact UID (except for the enable/disable bug)? If this is unlikely, now is the time to adapt. If it is common pattern, we should go with the guaranteed backward compatible change, at the cost of not entirely solving the bug, because it can still happen for the auto-generated UIDs. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...ore.model.rule.runtime/src/org/openhab/core/model/rule/runtime/internal/DSLRuleProvider.java
Show resolved
Hide resolved
| String name = rule.getName(); | ||
| String uid = modelName + "-" + index; | ||
| String uid = rule.getUid(); | ||
| if (uid == null || uid.isBlank()) { | ||
| uid = modelName + "-" + index; | ||
| } |
There was a problem hiding this comment.
User-provided rule UIDs from the DSL are accepted verbatim (only blank/null falls back to auto-generated). Because rule UIDs are embedded directly into event topics (e.g., openhab/rules/{ruleID}/...), a UID containing '/' will produce an invalid topic that RuleEventFactory cannot parse (it expects exactly 4 topic segments). Add validation/sanitization for configured UIDs (at minimum reject '/' and fall back to the auto-generated UID, ideally with a warning log) to prevent runtime event failures.
There was a problem hiding this comment.
There is a separate PR, #5467, that deals with this.
There was a problem hiding this comment.
User-provided rule UIDs from the DSL are accepted verbatim (only blank/null falls back to auto-generated). Because rule UIDs are embedded directly into event topics (e.g., openhab/rules/{ruleID}/...), a UID containing '/' will produce an invalid topic that RuleEventFactory cannot parse (it expects exactly 4 topic segments). Add validation/sanitization for configured UIDs (at minimum reject '/' and fall back to the auto-generated UID, ideally with a warning log) to prevent runtime event failures.
I think this comment from copilot is the best reason to reject my wild idea. It does correctly point out the UID should be validated more.
There was a problem hiding this comment.
@Nadahar : maybe it is good to exclude the / in that PR, even if there is a more general discussion in parallel in another PR?
With |
bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext
Outdated
Show resolved
Hide resolved
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bundles/org.openhab.core.model.rule/src/org/openhab/core/model/rule/Rules.xtext
Outdated
Show resolved
Hide resolved
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
|
|
||
| Rule: | ||
| 'rule' name=(STRING|ID) ('uid=' uid=(STRING|ID))? ('[' tags+=(ID|STRING) (',' tags+=(ID|STRING))* ']')? | ||
| 'rule' name=(STRING|ID) ('uid' '='? uid=(STRING|ID))? ('[' tags+=(ID|STRING) (',' tags+=(ID|STRING))* ']')? |
There was a problem hiding this comment.
Why make the = sign optional? I see potential for parser issues when anything is ever changed to the syntax (add a field).
There was a problem hiding this comment.
No equal sign - neither mandatory, nor optional - is also a viable approach.
There was a problem hiding this comment.
No equal sign - neither mandatory, nor optional - is also a viable approach.
I think that would be inconsistent with what has been done in other DSL formats.
There was a problem hiding this comment.
After rule, after when and after then there is no equal sign. So why should after uid be an =?
There was a problem hiding this comment.
I was referring to the other formats, e.g.
I also think it's "safer" to have a more explicit syntax, it allows more flexibility for potential future changes to the format.
There was a problem hiding this comment.
I am not far from forgiving and closing that PR...
There was a problem hiding this comment.
My suggestion, make = required to make this explicit and be done with it. I am fine with all else, so when I find the time we can proceed.



Closes #5437
Allow avoiding issue described in #5415 by setting an UID.
Also avoid notifying the rule registry for isolated models.
Also Refactor change notification.