Debugging Jython or Groovy rules

Hi All,

I just begin to work with openHAB 1.8.3. Since we need generic rules, I tested Jython with a simple one, it worked well. OK, I then I translated a complicated DSL rule to Jython. After correcting the import errors, the rule (or a part of it) began to work and do something in execute. Unfortunately, there was an unknown identifier, so I got a Nameerror exception.

At this point I realized that the whole script is not parsed at the beginning for unknown identifiers, they only raise problem when the control gets there. I know it because this exception was thrown each time the cron triggered execution, but not immediately, so right after startup nothing happened.

This makes debugging extremely slow and painful, because I have to cover all execution paths with test cases. Is there a way to overcome this limitation?

What is the situation with Groovy? Does it parse the whole script fully after reading?
If not, is it possible in a simple way to wrap it in a Groovy program which imports everything openHAB would do, compile independently with a Groovy compiler to reveal all errors in just one pass?

Again about import errors: in Jython it was also awkward that it reported only one such error. After correcting it, there was a next one to be reported in the next openHAB startup. Starting it over and over for each error takes too much time. Do you know a better way to debug it?

Thank you in advance.
Best regards: Balazs

The script will be reloaded each time it is saved to the disk. If you are monitoring the log file for errors this is much faster than restarting OH.

Thank you, I will try it so.

That is standard Python behavior.

This is also standard Python behavior.

However, does updating the file not automatically cause OH to reload the rules? With pretty much every other file changing it causes OH to automatically reload it. I don’t use JSR233 so don’t know if it fails to do that.

Yes, a JSR223 script (which may contain rules) is reloaded if Java detects the file has changed.

Hi All,

After some experiments I have found the solution: Groovy static compilation. This way all typos, type errors etc will be signed compile time. I have also found the solution for the script reload problem (it does happen with me with scripts in scripts directory): The JSR223 bundle must be reloaded using the r command on the OSGi console.

An example: this Groovy file, as a generic rule comes in scripts/lib:

import org.openhab.core.jsr223.internal.shared.*
import groovy.transform.CompileStatic
import org.slf4j.Logger
class GenericCronLog implements Rule {

This script instantiates the rules:

import org.openhab.core.jsr223.internal.shared.*

RuleSet getRules() {
    Class<?> clazz = ClazzLoader.parse("GenericCronLog.groovy");
    return new RuleSet(
        clazz.newInstance(["3 * * * * ?", "third second."] as Object[]),
        clazz.newInstance(["5 * * * * ?", "fifth second."] as Object[])

Here ClazzLoader is a handy little class which is compiled and placed in scripts/lib. By including that directory in the openHAB class path the class file will be found.

class ClazzLoader {
    static Class<?> parse(String name) {
        GroovyClassLoader classLoader = new GroovyClassLoader();
        return classLoader.parseClass(new File("configurations/scripts/lib/" + name));

Best regards: Balazs