Multiple case in switch statement

I am already using a switch in my Rules:

rule "HK_Pumpe_Status an/aus berechnen"
when
	System started or
	Item PU_HK_Heizkreispumpe received update
then
	val String status = PU_HK_Heizkreispumpe.state.toString

	switch status
	{
		case "Ein": PU_HK_Pumpe_Status.postUpdate(ON)

		case "Aus": PU_HK_Pumpe_Status.postUpdate(OFF)

		case "Aus Übertemperatur":
		{
			PU_HK_Pumpe_Status.postUpdate(OFF)
			logWarn ("my.rules", "PU: HK-Pumpe wegen Übertemperatur ausgeschaltet!")
		}

		case "Undefined": { }
		case "Uninitialized": { }

		default:
		{
			logWarn ("my.rules", "PU: Bug: HK-Pumpe-Status '"+status+"' nicht Ein oder Aus!")
		}
	}
end

But I am wondering, is there a way to have multiple case-statements in the same line, like:

		case "Undefined", "Uninitialized": { }

or (for Number items)

		case 1..2: // do something

Sorry, if the “multiple case” has been discussed here already, but I wasn’t able to find it when searching for it.

JavaDoc also mentions something like:

		case "Undefined": case "Uninitialized": { }

but this doen’t work. I am always getting:

2019-03-04 16:31:44.043 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Test': Ambiguous methods

Stefano

    case "Undefined",
    case "Uninitialized": { }
1 Like

Hi Rich,
thanks, but it still logs an error:

2019-03-04 18:09:19.419 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Test': Ambiguous methods [protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAssignment(org.eclipse.xtext.xbase.XAssignment,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object [...] org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateWhileExpression(org.eclipse.xtext.xbase.XWhileExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator)] for params [null, org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext@2a273aa4, org.eclipse.xtext.util.CancelIndicator$1@7509eeaf]

Maybe, I forgot to say, I am running openHAB 1.8.2 :grimacing:
Stefan

I’m not going to be much help to you then. There has been like three years of changes since 1.8. I can barely keep up with changes from one version to the next.

The ambiguous methods error is not related to the Switch statement or even coming from this Rule. Something in a Rule named “Test” is ambiguous to the Rules Engine. For example if you try to sendCommand a DecimalType to a Number Item you will have this error because DecimalType is also of type Number and there is a sendCommand(DecimalType) and a sendCommand(Number) on NumberItem and the Rules Engine doesn’t know which one you mean.

Hi Rich,
I think multiple cases just don’t work.

The following code works for h = 23, 11, 12, 17, and 22, so for lines that have a command, but it does not for h = 5:

rule "Test"
when
	Time cron "*/30 * * * * ?" or
	System started
then
	logInfo ("Test", "0")

	val int h = 5

	switch h
	{
		case 0,
		case 1,
		case 2,
		case 3,
		case 4,
		case 5,
		case 23: logInfo ("Test", "night")

		case 6,
		case 7,
		case 8,
		case 9,
		case 10,
		case 11: logInfo ("Test", "morning")

		case 12: logInfo ("Test", "noon")

		case 13,
		case 14,
		case 15,
		case 16,
		case 17: logInfo ("Test", "afternoon")

		case 18,
		case 19,
		case 20,
		case 21,
		case 22: logInfo ("Test", "evening")
	}
end

And this is, what is logged:

2019-03-06 10:11:43.877 [INFO ] [o.model.script.openHAB started] - System started
2019-03-06 10:12:10.330 [INFO ] [org.openhab.model.script.Test ] - 0
2019-03-06 10:12:10.373 [ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule 'Test': Ambiguous methods [protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAssignment(org.eclipse.xtext.xbase.XAssignment,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBlockExpression(org.eclipse.xtext.xbase.XBlockExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBooleanLiteral(org.eclipse.xtext.xbase.XBooleanLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateCastedExpression(org.eclipse.xtext.xbase.XCastedExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateClosure(org.eclipse.xtext.xbase.XClosure,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateConstructorCall(org.eclipse.xtext.xbase.XConstructorCall,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected org.eclipse.xtext.xbase.interpreter.IEvaluationResult org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateDoWhileExpression(org.eclipse.xtext.xbase.XDoWhileExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateForLoopExpression(org.eclipse.xtext.xbase.XForLoopExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateIfExpression(org.eclipse.xtext.xbase.XIfExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateInstanceOf(org.eclipse.xtext.xbase.XInstanceOfExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateNullLiteral(org.eclipse.xtext.xbase.XNullLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateNumberLiteral(org.eclipse.xtext.xbase.XNumberLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateReturnExpression(org.eclipse.xtext.xbase.XReturnExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateStringLiteral(org.eclipse.xtext.xbase.XStringLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateSwitchExpression(org.eclipse.xtext.xbase.XSwitchExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateThrowExpression(org.eclipse.xtext.xbase.XThrowExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateTryCatchFinallyExpression(org.eclipse.xtext.xbase.XTryCatchFinallyExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateTypeLiteral(org.eclipse.xtext.xbase.XTypeLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateWhileExpression(org.eclipse.xtext.xbase.XWhileExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator)] for params [null, org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext@347130cd, org.eclipse.xtext.util.CancelIndicator$1@42bc36f]
2019-03-06 10:12:22.373 [INFO ] [org.openhab.model.script.Test ] - 0
2019-03-06 10:12:22.378 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Test
java.lang.IllegalStateException: Ambiguous methods [protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateAssignment(org.eclipse.xtext.xbase.XAssignment,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBlockExpression(org.eclipse.xtext.xbase.XBlockExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBooleanLiteral(org.eclipse.xtext.xbase.XBooleanLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateCastedExpression(org.eclipse.xtext.xbase.XCastedExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateClosure(org.eclipse.xtext.xbase.XClosure,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateConstructorCall(org.eclipse.xtext.xbase.XConstructorCall,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected org.eclipse.xtext.xbase.interpreter.IEvaluationResult org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateDoWhileExpression(org.eclipse.xtext.xbase.XDoWhileExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateForLoopExpression(org.eclipse.xtext.xbase.XForLoopExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateIfExpression(org.eclipse.xtext.xbase.XIfExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateInstanceOf(org.eclipse.xtext.xbase.XInstanceOfExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateNullLiteral(org.eclipse.xtext.xbase.XNullLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateNumberLiteral(org.eclipse.xtext.xbase.XNumberLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateReturnExpression(org.eclipse.xtext.xbase.XReturnExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateStringLiteral(org.eclipse.xtext.xbase.XStringLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateSwitchExpression(org.eclipse.xtext.xbase.XSwitchExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateThrowExpression(org.eclipse.xtext.xbase.XThrowExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateTryCatchFinallyExpression(org.eclipse.xtext.xbase.XTryCatchFinallyExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateTypeLiteral(org.eclipse.xtext.xbase.XTypeLiteral,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator), protected java.lang.Object org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateWhileExpression(org.eclipse.xtext.xbase.XWhileExpression,org.eclipse.xtext.xbase.interpreter.IEvaluationContext,org.eclipse.xtext.util.CancelIndicator)] for params [null, org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext@28504489, org.eclipse.xtext.util.CancelIndicator$1@42bc36f]
	at org.eclipse.xtext.util.PolymorphicDispatcher.handleAmbigousMethods(PolymorphicDispatcher.java:308) ~[na:na]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:283) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateSwitchExpression(XbaseInterpreter.java:360) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._evaluateBlockExpression(XbaseInterpreter.java:321) ~[na:na]
	at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
	at org.eclipse.xtext.util.PolymorphicDispatcher.invoke(PolymorphicDispatcher.java:291) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:218) ~[na:na]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:204) ~[na:na]
	at org.openhab.model.script.internal.engine.ScriptImpl.execute(ScriptImpl.java:59) ~[na:na]
	at org.openhab.model.rule.internal.engine.ExecuteRuleJob.execute(ExecuteRuleJob.java:55) ~[na:na]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-all-2.1.7.jar:na]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557) [quartz-all-2.1.7.jar:na]
2019-03-06 10:12:32.719 [INFO ] [org.openhab.model.script.Test ] - 0

Best regards,
Stefano

I just copied your code in my config and it is working.

But I’ll bet you are not OH 1.8. Useful to know, though.

@rossko57: I am running openHAB 1.8.2

@HomeAutomation: Which OH version?

I’m running OH 2.4. Could be the case.

I suspect case statements like this was added after OH 1.8 then.