Foreach loops

The wife and I have recently finished a living room renovation project which included new ceiling lights. We opted for Philips Hue spots as accent lights. One thought led to another and I wondered how one would control theses lights in a way that a column would light up sequentially, as in a light show - one light turning on, then off, after another. I suspected that a for-loop would be the most practical way and started experimenting with rules (this was just an exercise to expand my knowledge of rules. I have no intention of turning the living room into a disco!) After googling I learned that the Rules DSL equivilant is a forEach loop. When I wrote the rule the order of the lights turning on were out of order (lights 3, 4, 5, 1, 2 turned on sequentially.) After more googling I added a line to sort the items in the group…"gLights.members.sortby[name] - that line resulted in the lights firing in order of 3, 4, 5, 2, 1…very strange. After more googling I finally got the code needed to successfully accomplish the task - provided by non other than @rikoshak:

   Hallway_Spots.members
        .sortBy[name]
        .forEach [spot |
        spot.sendCommand(ON)
            Thread::sleep(150)
            spot.sendCommand(OFF)
            Thread::sleep(100)
      ]

With this success I was interested in learning more about the “members.sortBy” and “members.forEach” statements, but I couldn’t find any documentation. Is there a source where I could learn more about these, and other statements available in the Rules DSL? I tried looking at X-Text, X-base, and X-tend sources but they didn’t match up with openHAB’s rules DSL.

thanks,
Chet

  • Platform information:
    • Hardware: i7 based miniPC
    • OS: Ubuntu 20.04
    • Java Runtime Environment: openJDK 11
  • Issue of the topic: lessons learned from forEach loops

They do actually but the Xtend docs are a little lacking on this front. But Rules DSL is using the Xtend way to do these sorts of operations. Under the covers I suspect it’s the Java Streaming API. Stream (Java SE 11 & JDK 11 )

I collected most of the info I was able to figure out through the Xtend movies example and elsewhere in Design Pattern: Working with Groups in Rules