Passing receivedEvent and triggeringItem as parameters to a lambda: which type to use?

I want to remove code duplication in a rule by using lambdas. The rule can be trigged by means of a channel Event trigger as well as by an Item change trigger.

One of the lambdas needs to check whether a channel event or an item triggered the rule.

The code without lambdas reads as follows:

	var String eventSource = "Undefined Event or Item Source" // Initialize
	if (receivedEvent !== null) { // A (channel) event triggered the rule
		eventSource = "Event channel '" + receivedEvent.getChannel().asString + "', Event = '" + receivedEvent.getEvent() + "'"
	} else if (triggeringItem !== null) { // An Item triggered the rule (used for debug purposes)
		if (DBG_DebugMode == OFF) {
			eventSource = "DEBUG MODE: Triggering Item '"
				+ ( if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) + "', State = '"
				+ triggeringItem.state.toString + "' - WILL NOT RUN: 'DBG_DebugMode' == 'OFF'"
			// return;
		}
		eventSource = "DEBUG MODE: Triggering Item '"
			+ ( if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) + "', State = '"
			+ triggeringItem.state.toString + "'"
	}

In order to move this code to a lambda, I need to pass it receivedEvent and triggeringItem. However, I need to type them if I want to properly use them.

Obviously, triggeringItem is of type GenericItem, but I can’t get hold of the type of triggeringItem apart from the fact that it appears to be e.g. a ChannelTriggeredEvent which I can’t use as type definition.

What could I use to get the following code work?

val getEventSourceStr = [
	ChannelTriggeredEvent receivedEvent, 	// Does not work - please fix me
	GenericItem triggeringItem
	|
	var String eventSource = "Undefined Event or Item Source"
	if (receivedEvent !== null) {
		eventSource = "Event channel '" + receivedEvent.getChannel().asString + "', Event = '" + receivedEvent.getEvent() + "'"
	} else if (triggeringItem !== null) { // For debug purposes
		if (DBG_DebugMode == OFF) {
			eventSource = "DEBUG MODE: Triggering Item '"
				+ ( if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) + "', State = '"
				+ triggeringItem.state.toString + "' - WILL NOT RUN: 'DBG_DebugMode' == 'OFF'"
			return;
		}
		eventSource = "DEBUG MODE: Triggering Item '"
			+ ( if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) + "', State = '"
			+ triggeringItem.state.toString + "'"
	}
	new String(eventSource)
]

Try logging out receivedEvent.class.getCanonicalName()

That might give you something useful. If not, review https://docs.oracle.com/javase/tutorial/reflect/class/classModifiers.html for other things you can do to discover what type and the inheritance tree of a given Object.

Thanks!

Here’s the first rule I’ve created, with the intention to identify the class name of receivedEvent:
rule “sunset check”

rule "sunset check"
when
	   Channel "astro:sun:local:set#event" triggered
	or Channel "astro:sun:local:civilDusk#event" triggered
	or Channel "astro:sun:local:nauticDusk#event" triggered
	or Channel "astro:sun:local:astroDusk#event" triggered 
	or Channel "astro:sun:local:daylight#event" triggered
then
	val String ruleTitle="event::getCanonicalName()"
	logInfo( ruleTitle, "INFO: receivedEvent.class.getCanonicalName() --> [{}], Event = '{}'", receivedEvent.class.getCanonicalName(), receivedEvent.getEvent() )
end

I get:

2019-03-05 07:24:00.045 [INFO ] [del.script.event::getCanonicalName()] - INFO: receivedEvent.class.getCanonicalName() --> [org.eclipse.smarthome.core.thing.events.ChannelTriggeredEvent], Event channel 'astro:sun:local:daylight#event, Event = 'START'

I’m now importing org.eclipse.smarthome.core.thing.events.ChannelTriggeredEvent at the top of my rules definition, and now it appears I can use ChannelTriggeredEvent as type.