Re: What is the purpose of CompilationCustomizer.getPhase(); Can importCustomizer() add imports, after groovy file execution has started, based on the instructions in that groovy file?

Дилян Палау зов Via Dev <dev-GSC0n/0aZo1d/SJB6HiN2Ni2O/[email protected]>
Newsgroups gmane.comp.lang.groovy.devel
Message-ID <[email protected]>
Hello,

I will narrow down my questions.  If a groovy script in openHAB calls `scriptExtension.importPreset("RuleSupport")`, then the system executes essentially in JSR 223 context:

scriptEngine.put("Configuration", org.openhab.core.config.core.Configuration.class)
scriptEngine.put("TriggerBuilder", org.openhab.core.automation.util.TriggerBuilder.class)

where scriptEngine was returned by GroovyScriptEngineImpl() (before `scriptExtension.importPreset("RuleSupport")` was executed).

Why afterwards this works from that started groovy script:

scriptExtension.importPreset("RuleSupport")
var r = TriggerBuilder.create().withId("trig2").withTypeUID("core.ItemStateChangeTrigger").build()
var c = Configuration.class

but this produces an error:

scriptExtension.importPreset("RuleSupport")
var c = new Configuration()

Error during evaluation of script '/etc/openhab/automation/jsr223/u.groovy': org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/etc/openhab/automation/jsr223/u.groovy: 2: unable to resolve class Configuration
 @ line 2, column 9.
   var c = new Configuration()
           ^

1 error

How can this be made working?

Greetings // Дилян

-----Original Message-----
From: Дилян Палаузов <[email protected]>
To: dev-GSC0n/0aZo1d/SJB6HiN2Ni2O/[email protected]
Subject: What is the purpose of CompilationCustomizer.getPhase(); Can importCustomizer() add imports, after groovy file execution has started, based on the instructions in that groovy file?
Date: 04/10/25 21:47:31

Hello,

* what is the purpose of CompilationCustomizer.getPhase(), which is not described at https://docs.groovy-lang.org/4.0.0/html/api/org/codehaus/groovy/control/customizers/CompilationCustomizer.html#getPhase() ?

OpenHAB integrates Groovy 4.0.28 ala JSR223 by utilizing two files - https://github.com/openhab/openhab-addons/tree/main/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal .

When a .groovy input file is proceeded, first some default things are inserted into the scripting engine in GroovyScriptEngineFactory.scopeValues().  If the thing is a variable, then it is inserted with scriptEngine.put(variable-name, value).  If the thing is a class, then the class is added to an ImportCustomizer, and the latter is at the end .addCompilationCustomizers(importCustomizer).  This works!

After the script execution has started, in OpenHAB JSR223 one can execute `se.importPreset("RuleSupport")` - cf https://www.openhab.org/docs/configuration/jsr223.html - to add further things in the context.  Currently GroovyScriptEngineFactory.scopeValues() does again for classes create a new ImportCustomizer, addImport, e.g. addImport("TriggerBuilder", org.openhab.core.automation.util.TriggerBuilder).  This does not work, however, and the question is why: with the content of file t.groovy:n

se.importPreset("RuleSupport")
var k = TriggerBuilder.create()

produces an error:

javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: TriggerBuilder for class: t

* As far as I see ImportCustomizer has always phase=CONVERSION.  Does this mean, that ImportCustomizer can only be adjusted before script execution has started?
* What should above `se.importPreset("RuleSupport")` do, so that the second line assumes that `import org.openhab.core.automation.util.TriggerBuilder;` is executed after the first line and before the second line?

Thanks // Дилян
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.