Help..."Cannot reference the field 'OFF' before it is defined"

I’ve upgraded to 2.3 from 2.0 and one of my rules suddenly stopped working. I only get warning messages but suspect it is linked to the cause of the issue

I get the following warnings: “Cannot reference the field ‘OFF’ before it is defined” and " Function2 is a raw type. References to generic type Function2<P1, P2, Result> should be parameterized"

Any idea? Simplified text of rule below:


// Imports
import org.eclipse.xtext.xbase.lib.Functions
import java.util.HashMap
import java.util.LinkedHashMap

var java.util.concurrent.locks.ReentrantLock lock  = new java.util.concurrent.locks.ReentrantLock()

var HashMap<String, LinkedHashMap<String, Object>> scenes = newHashMap(
   "0" -> (newLinkedHashMap(
            "Light_LR_Kitchen" -> OFF,
            "Light_LR_Bulb" -> OFF,
            "Light_LR_Table" -> OFF)),

    "100" -> (newLinkedHashMap(
            "Light_LR_Kitchen" -> 100,
            "Light_LR_Bulb" -> 100,
            "Light_LR_Table" -> 100))
)

val org.eclipse.xtext.xbase.lib.Functions$Function2 updateLRLight = [String fixture, String fixtureValue, Number result |
    logInfo("scene", "lambda: " + fixture + " = " + fixtureValue)
    sendCommand(GF_Living_All.allMembers.findFirst[name.equals(fixture)], fixtureValue)
]


/**
 * Selection item=Scene_LivingRoom mappings=[0=Off, 8=TV, 12=Soft,14=AllOn]
 */
rule "Scene LivingRoom"
»       when
»       »       Item Scene_LivingRoom received command
»       then
»       »       lock.lock()
»       »       try {
»       »       logInfo("scene", "LR selection changed")
»       »       switch (receivedCommand) {
»       »       »       default : { 
»       »       »       »       for (String fixtureName : scenes.get(receivedCommand.toString()).keySet()) {»   
»       »       »       »       »       var fixtureValue = scenes.get(receivedCommand.toString()).get(fixtureName).toString() as String
»       »       »       »       »       logInfo("scene", "setting " + fixtureName + " = " + fixtureValue)
»       »       »       »       »       updateLRLight.apply(fixtureName, fixtureValue)
»       »       »       »       }
»       »       »       }
»       »       }
»       »       } finally {
»       »       »       lock.unlock()
»       »       }
end

That one’s fairly clear.

The first one is a known bug. Based on timing of startup, roles can start running before the Rules Engine is ready. I assume the error only occurs during startup and not every time.

The second one is just a warning. You are not using the best syntax for defining the lambda. See Reusable Functions: A simple lambda example with copious notes.

Also, see 3 different methods to use scenes with Google Home & openHAB and A simple scene management: finally a good use of scripts for far better approaches to implement lighting scenes than HashMaps.