Is there anything like global rules?

rules
scripts
items
Tags: #<Tag:0x00007fadf97e8728> #<Tag:0x00007fadf97e8548> #<Tag:0x00007fadf97e8390>

(Alex) #1

Good evening!

I’m the C++ developer, and I want to simplify my current rules. I’m looking for something like header files in C++. I want to declare global rules and use them in different files (rule files). For example like int GetMinTemperature(…) and based on current time of year, time of day, etc return minimal allowed temperature


(Scott Rushworth) #2

https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html

The OH DSL allows for lambdas and scripts. Separating rule files is really just for logical arrangement… otherwise, there is no benefit. Scripts can be shared across rule files, but have their own context, so no access to lambdas/global variables. The closest thing OH offers to what you are looking for is a lambda, but can’t be shared across rule files.


(Alex) #3

Thanks, Scott!
It is quite ridiculous :frowning: There is one awesome rule of thumb. More code = more bugs. I have almost same code which I would like to reuse in ~5-6 rules but seems I can’t. Moreover… seems if I want to change something in my global logic then I will need to change it everywhere… in 99% cases, I will forget to do it somewhere.

But okay. Thanks for explanation


(Hans-Jörg Merk) #4

There might be a misunderstanding. If You define this reusable code as a lambda (function), it can be used in all rules you have in your .rules file. What you can’t do is sharing this over splitted rules files.

You can find a good example on using labdas here :


(Alex) #5

I see, but I have plenty rules which should be global (for all files). Like exchange rate will be same for all .rules file. And, for example, if I want to calculate my monthly payment for utilities then I will need to copy-paste same functions to all .rules files.


(Scott Rushworth) #6

You can have more than one rule in a rules file. In fact, I find it best to have them all in one rule file, and only put them into separate files to group functionality (ie, Action.rules, Alert.rules, Cron.rules, …).