Introduction into Rules DSL

Hello All,

This is a more abstract question, and very conciously so:
I know a [edit: TINY] bit of coding in python, java, c# yadda yadda - and I fail horribly with even the basic things I’d like to do in the rulesets.
The trigger example for this post was my effort to sort a list of four dates so that the soonest is always first.
Plain forward, two-dimensional array or a linkedHashMap, use Collection.sort() or .orderBy() and be done for. Only that this is wrong and it seems that the DSL doesn’t know those methods or variable types.

Now I don’t want this specific item fixed but I’d like to learn how I can help myself.
The rules-guides, help posts and so on are not really introduction in the do’s and don’ts of this language.
Or do I misunderstand the basic design of openhab and any more advanced data manipulation like sorting should be put into scripts which then are triggered by rules?

So basically I have two questions:

  1. is it the goal of the rules to provide data manipulation which I can’t do (easily) within the frontend (e.g. pre-sorting of items, data aggregation, etc)?
  2. What’s my best approach to learn about the limitations and possibilities of the rules DSL (not syntax) ?

Thanks a lot for any pointers!



Sources I tried (but with the goal of learning the language, not fixing this example):



This beautiful community
github and stackoverflow searches
https://www.eclipse.org/xtend/
https://www.eclipse.org/Xtext/#xbase
(from the latter two I got the ideas with the 2d-arrays and linkedHashMaps for example)

No, the fundamental main point of the Rule DSL is to provide a method to handle events that can happen with your items within Openhab, and based on a whole range of conditions, perform some actions that are a result of that.

There are multiple sources to learn about Xtend, of which this forum is one.

To be honest, that might not so much be a “basic thing” as you describe it.
To avoid going into lengthy discussions to find out this really is a XY problem, my suggestion would be to explain what you need and why in a separate topic, such that people can think along there.

1 Like

Yes you somewhat misunderstand. No, scripts essentially aren’t different.
Don’t think of rules DSL as an equivalent to a full-scale programming language but rather as a domain specific “glue” between OH components.

Not really. Your thinking is typical for a programmer, but this is a little counterproductive in openHAB.
Yes, if there really is a need to manipulate data you should do it in rules DSL, but only when the OH contained methods cannot provide what you want.
But most of the time it’s not really needed.
Don’t try programming things yourself from ‘the ground up’, you should make much more use of the existing components of openHAB such as groups, transformation and persistence services.

Forget about Xtend docs. Stop thinking like a traditional programmer does.
I suggest reading some of @rlkoshak’s Design Patterns which are applicable to many problems you might be trying to solve.
Reuse the data structures given there. Browse the examples section, search the community for keywords of your problem. Find and copy code snippets, play with them, extend them.

2 Likes

Have you looked into jsr233?


you say that you already know some python and java so why not use that knowledge.
Have a look at

or

especially the Jython simple rule decorators are awesome

1 Like

Thank you guys for the answers!
I followed @RolfV hint and created a dedicated topic for the specific case - most likely you’re right and it is easiest to learn by example (link).

@mstormi, The design patterns are an exciting resource on a first glance! I’ll give them a look for sure :slight_smile: In the example-section I didn’t manage to find an example for ordering datetime though - but as RolfV said perhaps I’m already having too many wrong assumptions (several of which you pointed out, thanks again for that!).

@jgkk I stumbled upon jsr233 but I have only little knowledge but that of everything - because of that I was hesitant to now put something more on top before having understood the basics :slight_smile:

Rules DSL can do this. All those structures are really just using the Java core classes. However, usually when one is trying to do this one is probably doing something in a way that the Rules DSL doesn’t like. The Rules DSL is very opinionated in how it wants you to code Rules. When you fight against that things get difficult for you.

Honestly, you may know enough about programming where adapting to Rules DSL may not be worth the effort for you to do. Instead you will probably be happier over all using JSR223 Rules. This will let you write Rules in Jython, JavaScript, or Groovy.

The best you will get are the Design Pattern postings.

No, and this is one of the things programmers have the biggest problem with the Rules DSL. It is designed first and foremost to process events. What data processing it is good at supporting is primarily through processing members of Groups. See Design Pattern: Working with Groups in Rules.

The Design Pattern posts.

While the Rules DSL is based on these and share much in common with them, it really is a Domain Specific Language and has many differences and limitations.

And now I see that everyone else has already said all of the above. I’ll second what everyone else said. :wink:

1 Like