Errors using org.eclipse.xtext.xbase.lib.Procedures or Functions

Hi,

I am getting errors when starting up Openhab 2 (I am on 2.3 stable) in function and procedure definitions, like:

2018-10-14 20:53:11.151 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Change Light_FF_GuestBedroom_Rear status if power reported is changing': The name 'updateLightsStatus' cannot be 
resolved to an item or type; line 896, column 3, length 18

‘updateLightsStatus’ is a lambda function:

val Procedure2<SwitchItem,NumberItem> updateLightsStatus = [
	SwitchItem theItem,
	NumberItem theItem_Power |
	logInfo("FILE","--> Evaluating the power update for "+theItem.name)
	if ( (theItem_Power.state as DecimalType).intValue >= 1 && (theItem.state == OFF || theItem.state == NULL) ) {
		logInfo("FILE","--> Power is on, change "+theItem.name+" from OFF to ON.")
		theItem.postUpdate(ON)
	} else if ( (theItem_Power.state as DecimalType).intValue < 1 && (theItem.state == ON) ) {
		logInfo("FILE","--> Power is off, change "+theItem.name+" from ON to OFF.")
		theItem.postUpdate(OFF)
	}
]

rule "Change Light_FF_LivingRoom_Rear status if power reported is changing"
	when Item Light_FF_LivingRoom_Rear_Power changed then
		updateLightsStatus.apply(Light_FF_LivingRoom_Rear,Light_FF_LivingRoom_Rear_Power)
	end

from import org.eclipse.xtext.xbase.lib.Procedures.Procedure2. As a matter of fact, I am getting this error for all such lambda functions in my rules file.

Any idea how to fix this ?

Thanks in advance, Markus

import org.eclipse.xtext.xbase.lib.Functions

val Procedure2<SwitchItem,NumberItem> updateLightsStatus = [ SwitchItem theItem, NumberItem theItem_Power |
    logInfo("FILE","--> Evaluating the power update for "+theItem.name)
    if ( (theItem_Power.state as DecimalType).intValue >= 1 && (theItem.state == OFF || theItem.state == NULL) 
        logInfo("FILE","--> Power is on, change "+theItem.name+" from OFF to ON.")
        theItem.postUpdate(ON)
    } else if ( (theItem_Power.state as DecimalType).intValue < 1 && (theItem.state == ON) ) {
        logInfo("FILE","--> Power is off, change "+theItem.name+" from ON to OFF.")
        theItem.postUpdate(OFF)
    }
]

rule "Change Light_FF_LivingRoom_Rear status if power reported is changing"
when
    Item Light_FF_LivingRoom_Rear_Power changed
then
    updateLightsStatus.apply(Light_FF_LivingRoom_Rear,Light_FF_LivingRoom_Rear_Power)
end

yes, of course I have the import in my rules, this one is:

import org.eclipse.xtext.xbase.lib.Procedures.Procedure2

The error seems to be when the rule is loaded, it seems not to recognise the signature of the procedure.

It works for my procedures and functions with just:

import org.eclipse.xtext.xbase.lib.Functions

Interesting, checking…

nop, seems not to work

The import 'org.eclipse.xtext.xbase.lib.Functions' is never used.
The field Tmp_HomeRules.offsetToTime refers to the missing type Object
The field Tmp_HomeRules.offsetToTime refers to the missing type Object
The field Tmp_HomeRules.offsetToTime refers to the missing type Object
The field Tmp_HomeRules.offsetToTime refers to the missing type Object
...

I missed that:

val Procedures$Procedure2<SwitchItem,NumberItem> updateLightsStatus = [ SwitchItem theItem, NumberItem theItem_Power |

The imports seem to be ok, but both functions and procedures are not resolved when loading to figure out the return types. I didn’t have that problem in OH2.1, but now in OH2.3. I noticed some updates where needed to rules between the 2 versions…

btw, the rules seem to work never the less (still debugging to be sure). Strange…

I have that problem too. It’s not an issue

Just import Procedures

 import org.eclipse.xtext.xbase.lib.Procedures

and use

Procedures$Procedure2

But the “real” solution is don’t import anything.

val updateLightStatus = [ SwitchItem theItem, NumberItem theItem_Power |
    	logInfo("FILE","--> Evaluating the power update for "+theItem.name)
	if ( (theItem_Power.state as DecimalType).intValue >= 1 && (theItem.state == OFF || theItem.state == NULL) ) {
		logInfo("FILE","--> Power is on, change "+theItem.name+" from OFF to ON.")
		theItem.postUpdate(ON)
	} else if ( (theItem_Power.state as DecimalType).intValue < 1 && (theItem.state == ON) ) {
		logInfo("FILE","--> Power is off, change "+theItem.name+" from ON to OFF.")
		theItem.postUpdate(OFF)
	}
]

That will create a Procedure2 with the proper types for the arguments.

If you want a Functions$FunctionX, make sure the last line executed returns something and specify the type.

You don’t need the import.

You need to import Procedures to create Procedures$Procedure2. Functions only works for Functions$FunctionX

There are things that have always been a problem with Rules that the Rules parser used to let go. In 2.3 they made the syntax checking more strict so that we can get more meaningful errors so a lot of things that have always been on the bad side of acceptable are not full blown syntax errors.

I really hate to contradict you but in the rules you provided for the Islamic Prayer Times you use both a function and a procedure2 and a procedure5 and the only import is Functions. And it works!!

Are you sure because it makes no sense that it works, unless they started to include Pricedures$ProcedureX by default, in which case I bet it will continue to work without any imports what so ever.

Functions and Procedures are separate classes. You can use Procedures if you only import Functions because they are not the same thing. Just like I can’t use ArrayList when I only import HashMap.

So the only way it can work is if Procedures is already imported by default. And if Procedures is imported then Functions almost certainly is too.

Yes just checked again
I’ll try to remove the Functions import and let’s see what happens…

@rlkoshak
Yep, it works without any imports!!

1 Like

Sorry for being offline yesterday, I’ll hope to test things myself later tonight.

Importing just

import org.eclipse.xtext.xbase.lib.Procedures
import org.eclipse.xtext.xbase.lib.Functions

and using

Procedures$Procedure2

or

Functions$Function2

still gives errors at system startup. If just refreshing rules on a running system, there are no errors. The startup errors seem not to be 100% reproducible, as if they depend on the startup status somehow. System runs fine though.

Yes it will do that. I have the same problem and the system runs fine. I think it’s to do with types in the arguments