Groovy rules no longer work after update to 2.5#1535

I just updated from 2.4 to 2.5#1535. I now have the following errors in the log for my groovy rules:

Script3.groovy: 142: unable to resolve class SimpleRule
Script3.groovy: 146: unable to resolve class Action

I have checked if I have to adapt something due to the reintegration of ESH, but could not find anything. Is this a regression or have I overlooked something?

Please post an example rule (and any libraries). I havenā€™t updated to a snapshot after the reintegration, but others have mentioned issues, and I expect some imports will need to be changed in scripts. Iā€™m looking into it now. Tests with the raw API seemed to work.

If youā€™re not doing anything really complex in your rules, you will probably be fine just changing references of org.eclipse.smarthome.automation to org.openhab.core.automation. However, any of the remaining org.eclipse.smarthome.core packages (the migration has not fully completed), I have not yet found a way to access.

The most simple example does not work:

scriptExtension.importPreset(ā€œRuleSimpleā€)
def r = new SimpleRule();

It does not help if I try to import either
org.openhab.automation.module.script.rulesupport.shared.simple.SimpleRule or
org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleRule.

I have just updated to S1566 which reportedly contains openhab core modules built on 04/01. Even though this is older than expected, it should contain the DynamicImport commit you did, shouldnā€™t it?

It did not change anything for my rules, still exactly the same behaviour.

Edit: forget what I wrote, I had a wrong package in my import. Just corrected this and everything works fine again. Thank you!

Unfortunately, not all classes are found. It can find SimpleRule and Action, but it does not find e.g. DecimalType.
It parses the script without showing any errors, but when the rule is executed, it fails with ClassNotFoundException:

2019-04-11 12:13:50.580 [WARN ] [e.automation.internal.RuleEngineImpl] - Fail to execute action: 1
java.lang.NoClassDefFoundError: org/eclipse/smarthome/core/library/types/DecimalType
	at Script12$3.execute(Script12.groovy:213) ~[?:?]
	at org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandlerDelegate.execute(SimpleRuleActionHandlerDelegate.java:34) ~[?:?]
	at org.openhab.core.automation.module.script.rulesupport.internal.delegates.SimpleActionHandlerDelegate.execute(SimpleActionHandlerDelegate.java:60) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1199) [229:org.openhab.core.automation:2.5.0.201904010547]
	at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1047) [229:org.openhab.core.automation:2.5.0.201904010547]

I checked openhab-core source code and the package seems to be correct, any idea what else the problem might be? Does this not work with org.openhab.core

Iā€™m using S1566 in my production environment and havenā€™t had an issue. However, some of the packages that got renamed in the reintegration were put into ā€˜internalā€™ packages, so they will not be accessible even with the DynamicImport.

Could you please supply an example of a script that is failing?

I stripped down my rule as much as possible:

import org.eclipse.smarthome.core.library.types.DecimalType
import org.openhab.core.automation.Action
import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")

def r = new SimpleRule() {
    Object execute(Action module, Map<String, ?> inputs) {
        items.get("myNumItem") as DecimalType;
    }
}
r.setName("JSR223ClassNotFound");

automationManager.addRule(r);

This fails with ClassNotFound during runtime. You can use it as it is, not need to create the item. To test you can start it from paperUI->rules.

@5iver, did you have a chance to look into this?

Yesā€¦ will report back soon, hopefully with a solution. Iā€™m working on it.

@vbier, there appears to be an issue with the classloader. It also looks like there has always been a problem with how the scope variables are loaded for the Groovy script engine. DecimalType is included in the default scope, Action and Configuration are in RuleSupport, and SimpleRule is in RuleSimple, so they should not have to be imported.

import org.openhab.core.automation.*
import org.openhab.core.automation.module.script.rulesupport.shared.simple.*
import org.eclipse.smarthome.config.core.Configuration

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")

import org.slf4j.*
def log = LoggerFactory.getLogger('jsr223.groovy')

def testGroovyRule = new SimpleRule() {
    Object execute(Action module, Map<String, ?> inputs) {
        //log.warn("Hello World from Groovy")
        log.warn("\n{}", (binding.variables as String).replace(",", ",\n"))
    }
}

testGroovyRule.setTriggers([
    TriggerBuilder.create()
        .withId("aTimerTrigger")
        .withTypeUID("timer.GenericCronTrigger")
        .withConfiguration(new Configuration([cronExpression: "0/10 * * * * ?"]))
        .build()
    ])

testGroovyRule.setName("Test Groovy: display binding variables")
automationManager.addRule(testGroovyRule)

This rule logs everything available in the context, and shows DecimalType is available, but everything that is a class is not accessible from Groovy. This just could be my inexperience with Groovy though. The NashornScriptEngineFactory has a JS specific workaround for this particular issue (compare this to what is used by Groovy and Jython), so maybe there is something similar that can be done for Groovy. Iā€™ve had to work on some other things and havenā€™t had a chance to dig into this yet.

[voice:org.eclipse.smarthome.core.voice.internal.VoiceManagerImpl@bf01298,
 DOWN:DOWN,
 ActionType:class org.eclipse.smarthome.automation.type.ActionType,
 Configuration:class org.eclipse.smarthome.config.core.Configuration,
 FileUtils:class org.apache.commons.io.FileUtils,
 StringType:class org.eclipse.smarthome.core.library.types.StringType,
 rules:org.eclipse.smarthome.automation.core.internal.RuleRegistryImpl@1078852,
 ruleRegistry:org.eclipse.smarthome.automation.module.script.rulesupport.shared.RuleSupportRuleRegistryDelegate@5306b84,
 NextPreviousType:class org.eclipse.smarthome.core.library.types.NextPreviousType,
 PLAY:PLAY,
 ImperialUnits:class org.eclipse.smarthome.core.library.unit.ImperialUnits,
 SimpleRule:class org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleRule,
 ModuleType:class org.eclipse.smarthome.automation.type.ModuleType,
 Trigger:interface org.eclipse.smarthome.automation.Trigger,
 INCREASE:INCREASE,
 OpenClosedType:class org.eclipse.smarthome.core.library.types.OpenClosedType,
 things:org.eclipse.smarthome.core.thing.internal.ThingRegistryImpl@49340cca,
 UP:UP,
 RawType:class org.eclipse.smarthome.core.library.types.RawType,
 StringListType:class org.eclipse.smarthome.core.library.types.StringListType,
 events:org.eclipse.smarthome.automation.module.script.defaultscope.internal.ScriptBusEvent@425bb3a6,
 SIUnits:class org.eclipse.smarthome.core.library.unit.SIUnits,
 itemRegistry:org.eclipse.smarthome.core.internal.items.ItemRegistryImpl@193c064d,
 TriggerType:class org.eclipse.smarthome.automation.type.TriggerType,
 NULL:NULL,
 STOP:STOP,
 UnDefType:class org.eclipse.smarthome.core.types.UnDefType,
 ir:org.eclipse.smarthome.core.internal.items.ItemRegistryImpl@193c064d,
 TriggerBuilder:class org.eclipse.smarthome.automation.core.util.TriggerBuilder,
 RewindFastforwardType:class org.eclipse.smarthome.core.library.types.RewindFastforwardType,
 DateTimeType:class org.eclipse.smarthome.core.library.types.DateTimeType,
 QuantityType:class org.eclipse.smarthome.core.library.types.QuantityType,
 State:interface org.eclipse.smarthome.core.types.State,
 DecimalType:class org.eclipse.smarthome.core.library.types.DecimalType,
 IncreaseDecreaseType:class org.eclipse.smarthome.core.library.types.IncreaseDecreaseType,
 Visibility:class org.eclipse.smarthome.automation.Visibility,
 Rule:interface org.eclipse.smarthome.automation.Rule,
 File:class java.io.File,
 items:[Virtual_Number_1:5],
 actions:org.eclipse.smarthome.automation.module.script.defaultscope.internal.ScriptThingActions@7f23e6a7,
 Action:interface org.eclipse.smarthome.automation.Action,
 CLOSED:CLOSED,
 ActionBuilder:class org.eclipse.smarthome.automation.core.util.ActionBuilder,
 MetricPrefix:class org.eclipse.smarthome.core.library.unit.MetricPrefix,
 DECREASE:DECREASE,
 OFF:OFF,
 StopMoveType:class org.eclipse.smarthome.core.library.types.StopMoveType,
 PREVIOUS:PREVIOUS,
 PAUSE:PAUSE,
 se:org.eclipse.smarthome.automation.module.script.internal.ScriptExtensionManagerWrapper@10ffffa,
 SimpleConditionHandler:class org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleConditionHandler,
 StringUtils:class org.apache.commons.lang.StringUtils,
 NEXT:NEXT,
 REWIND:REWIND,
 audio:org.eclipse.smarthome.core.audio.internal.AudioManagerImpl@67b87a8e,
 URLEncoder:class java.net.URLEncoder,
 HSBType:class org.eclipse.smarthome.core.library.types.HSBType,
 SimpleTriggerHandler:class org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleTriggerHandler,
 ON:ON,
 Condition:interface org.eclipse.smarthome.automation.Condition,
 SmartHomeUnits:class org.eclipse.smarthome.core.library.unit.SmartHomeUnits,
 scriptExtension:org.eclipse.smarthome.automation.module.script.internal.ScriptExtensionManagerWrapper@10ffffa,
 ModuleBuilder:class org.eclipse.smarthome.automation.core.util.ModuleBuilder,
 UpDownType:class org.eclipse.smarthome.core.library.types.UpDownType,
 PercentType:class org.eclipse.smarthome.core.library.types.PercentType,
 FilenameUtils:class org.apache.commons.io.FilenameUtils,
 OPEN:OPEN,
 automationManager:org.eclipse.smarthome.automation.module.script.rulesupport.shared.ScriptedAutomationManager@440d61bf,
 SimpleActionHandler:class org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleActionHandler,
 ConditionBuilder:class org.eclipse.smarthome.automation.core.util.ConditionBuilder,
 Command:interface org.eclipse.smarthome.core.types.Command,
 OnOffType:class org.eclipse.smarthome.core.library.types.OnOffType,
 MOVE:MOVE,
 PlayPauseType:class org.eclipse.smarthome.core.library.types.PlayPauseType,
 FASTFORWARD:FASTFORWARD,
 PointType:class org.eclipse.smarthome.core.library.types.PointType,
 ConfigDescriptionParameter:class org.eclipse.smarthome.config.core.ConfigDescriptionParameter,
 UNDEF:UNDEF]

I saw the same results with 2.5.6 and 2.4.12. Out of curiosity, which version of Groovy are you using?

Thanks for all the effort you put into this. I also use 2.4.12. I found this related issue:


Maybe somebody that helped to close it has an idea on what the problem might be? Or might this once again be the same problem?

1 Like

It looks like I found a workaround. It helps if i add

org.eclipse.smarthome.core.library.types.DecimalType dt = Class.forName(ā€œorg.eclipse.smarthome.core.library.types.DecimalTypeā€).newInstance();

before the instantiation of the SimpleRule. It then executes without error.

1 Like

Thanks a lot

//workaround: make OpenClosedType visible to script
def oct = Class.forName("org.eclipse.smarthome.core.library.types.OpenClosedType");

does the same for enums