Using "rule name" string inside rule logic

I’ll make it short. As we all know, the basic structure if a rule is

rule "Control time machine"
when
    <TRIGGER CONDITION1> or
    <TRIGGER_CONDITION2>
then
    logInfo("myrule", "Control time machine")
end

Is there any way to use the string for the rule name inside the rule? I often find myself repeating it as shown in the example.
@rlkoshak maybe?

2 Likes

I don’t think so. I know of no way. I think the name is considered metadara and unavailable in the logic.

that’s a shame. @Kai any idea how the access the rule name? Otherwise do you think it would be something easily implemented?

1 Like

I took a quick look at the RuleEngineImpl implementation and it appears to require a 1 line code change to set the name in the execution context.

reading the name would be more than enough :wink: Could you PR that change?

1 Like

Not 100% sure if it is that easy. Note that this will introduce a new keyword and thus is potentially a not-backward-compatible change. Also it would have to be made known to the designer, so that it does not complain about the syntax.

Well yes, isn’t that the same as with any other improvement? Besides, this is an addition and doesn’t change anything, making it an uncritical change. Correct? Adding one more keyword to the big list of things the Designer doesn’t know (no offense) doesn’t hurt, does it?

I’m trying to stay away from the core development (time reasons). Would one of you be willing to look into this?

Thanks.

No, as I mentioned it is a breaking change. Introducing new keywords means that if somebody currently uses this string as a variable or whatever will see his rule becoming invalid.

Adding one more keyword to the big list of things the Designer doesn’t know (no offense) doesn’t hurt, does it?

The Designer will automatically know about it if you add it to the DSL grammar.

I’m trying to stay away from the core development (time reasons). Would one of you be willing to look into this?

Doing correct changes to the Xtext DSL isn’t easy and quite error-prone. I’d rather suggest to spend the energy in moving to the new rule engine and bringing all features that we wish to it.

Just so I understand, what grammar would need to be modified? I’m wondering since the rule name is obviously parsed by the rule parser and it appears to be saved in the related model objects (based on what I see in the Rules.xtext file). I’m assuming you mean there’s another Xtext grammar file (maybe specific to the Designer?) that would need to be modified.

Hm, you are right - my mistake. The grammar does not need a change, because where you want to access it is within the Xbase script (the part between then…end) and to make it available there it would need to be defined as an implicit scope variable. So that might be feasible without too much hassle.

2 Likes

@Kai @steve1 before this idea gets lost… how do you want to continue? Do you want me to open an issue (where)?

I’d recommend creating an feature request in the smarthome repository. I don’t use the XText rules or OH2 (until the JSR223 PR is approved) so it wouldn’t make sense for me to implement the change. I was just trying to help by doing some quick research into how it would be done.

Is this point fixed?
Ideally would be a string with the rule-file-name and one with the rule-name.

1 Like