I created in DSL some little helpers to shorten several times repeated commands like groups.members.forEach[ if… sendCommmand(…)].
These are global variables like:
val groupState = [ GroupItem g, Object cmd | g?.members.forEach [ i | val strCmd = cmd.toString
if (i.state.toString != strCmd) i.sendCommand(strCmd) ] ]
var rolloUp = [ GenericItem item, Number cmd | if ((item.state as Number) > cmd) item.sendCommand(cmd)]
var rolloDown = [ GenericItem item, Number cmd | if ((item.state as Number) < cmd) item.sendCommand(cmd)]
So I can easily use them in rules like:
rule "..."
when
...
then
groupState.apply(gShutters_sud, 0)
rolloUp.apply(Shutter_OG_Schlafen, 20)
end.
Works smart and efficient. But I have to add the global vals to each .rules file, where I need them.
I am wondering, ig there is any possibility (or maybe will be in future time) to set this global for all .rules files or perhabs as public cache.
I’m afraid not. It’s one of the major limitations of Rules DSL. This is possible in all the other rules languages but not in Rules DSL.
You can get part of the way there using Rules DSL Scripts. These are file placed in the $OH_CONF/scripts folder with a .script extension. However, they have limitations. From the docs
Scripts are small pieces of Rules DSL code that can be called from Rules. However, Scripts have limitations. Scripts cannot accept arguments. Scripts cannot return a value. Any reference to a class that would normally need to be imported in a .rules file, the class needs to be referred to by its full package as imports are not supported.
So they are not really useful for this.
Personal libraries for common code like this is really great and it’s standard programming proactice. But Rules DSL is anything but standard and doesn’t support it.
I wonder if this is actually possible in rulesdsl (xtend), but done in a Java way. I haven’t tested this since I’m on the road at the moment, but in general:
Create a .rules file that contains a class like this:
Rules DSL doesn’t support creating classes even though Xtend does support that. I never knew why classes and real functions were unsupported in Rules DSL. Since they are supported by Xtend it seems like a deliberate choice on OH’s part to not support them.
I should mention that all of the other rules languages do support what you are trying to do. Even Blockly has some ability to create libraries, though there are a few limitations in Blockly. The three big ones, jRuby, JS Scripting, and Python even support using standard third party libraries should you have a use for them.
Both DSL Rules and Xtend inherit from XBase. Xtend handles the class keyword, while DSL Rules and XBase do not handle the class keyword. DSL Rules do not inherit from (are not) Xtend.