Rules DSL Binary 'AND' operations

I’m trying to do some bit manipulation in a DSL Rules script.

rule "Test Rule"
when
    Item Something changed
then
    var int i1 = -2147457441

    var int i2 = i1 & 0x0FFFFFFF

    // then do something with them...
end

However, the Rules DSL doesn’t appear to like the ‘&’ operator and I get this error:

2021-02-08 18:47:27.988 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'rulefile.rules' has errors, therefore ignoring it: [207,15]: no viable alternative at input 'i1'

The line number refers to the line with the attempted ‘&’ operation.

Does anyone know how I can achieve this in Rules DSL?

I think this old post is still relevant.

No bitwise operators, use instead methods of the variable.

var Integer i1 = -2147457441
var Integer i2 = i1.bitwiseAnd(0x0FFFFFFF)

@rossko57 Thanks for the info - that worked! I found that the primitives worked too e.g.

var int i1 = -2147457441
var int i2 = i1.bitwiseAnd(0x0FFFFFFF)

For some odd reason in the interpreter bowels, the use of primitives can impact DSL performance and use is best kept to the minimum.

Dear Community,

The example above with the bitwiseAnd operation on Integer variables works also fine on my setup of OpenHab 3.0.1. However, I fail to adapt this example to apply a bitwise and operation to a received number state.

val Number HvacState = APPEG_Zimmer_Betriebsartstatus_HVAC.state as Number
var Integer HvacMode = HvacState.bitwiseAnd(0x0F)

The result is:

2021-03-21 12:43:00.157 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'knx-1' failed: 'bitwiseAnd' is not a member of 'java.lang.Number'; line 11, column 26, length 25 in knx

I am not familiar with the proper casting of states. When I try to read the state as integer, I receive a different error message…

Does anybody has a hint as how to properly apply bitwise operations onto received number states?

Best regards,
Peter

Number type doesn’t have a bitwise method. Try

val HvacState = (APPEG_Zimmer_Betriebsartstatus_HVAC.state as Number).intValue

Dear Rossko57,

Thank you very much for your hint. Unfortunately, I receive a slightly different error message when applying the suggested change:

	[line 38] val HvacState = (APPEG_Bad_Betriebsartstatus_HVAC.state as Number).intvalue
	[line 39] var Integer HvacMode = HvacState.bitwiseAnd(0x0F)

The result is:

2021-03-21 14:42:19.480 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'knx-2' failed: 'intvalue' is not a member of 'java.lang.Number'; line 38, column 18, length 59 in knx

Best regards,
Peter

All this stuff is case sensitive

Thank you very much for your hint!

1 Like