Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DSL] Cannot import org.openhab.core.util namespace #3743

Closed
jsetton opened this issue Aug 7, 2023 · 21 comments · Fixed by #3749
Closed

[DSL] Cannot import org.openhab.core.util namespace #3743

jsetton opened this issue Aug 7, 2023 · 21 comments · Fixed by #3749
Labels
bug An unexpected problem or unintended behavior of the Core

Comments

@jsetton
Copy link

jsetton commented Aug 7, 2023

It is not possible to import the org.openhab.core.util namespace with the DSL rule engine.

import org.openhab.core.util.ColorUtil

rule "ColorItem Changes"
when
  Item ColorItem changed
then
  val rgb = ColorUtil.hsbToRgb(ColorItem.state as HSBType)
  logInfo("ColorItem", "RGB: {},{},{}", rgb.get(0), rgb.get(1), rgb.get(2))
end

Each time this rule is called, the following error is generated:

[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-1' failed: The name 'ColorUtil' cannot be resolved to an item or type; line 7, column 12, length 9 in test
@jsetton jsetton added the bug An unexpected problem or unintended behavior of the Core label Aug 7, 2023
@openhab-bot
Copy link
Collaborator

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/oh4-hsbtype-is-deprecated-warning/148080/12

@lolodomo
Copy link
Contributor

lolodomo commented Aug 7, 2023

Did you try?
val rgb = hsbToRgb(ColorItem.state as HSBType)

@jsetton
Copy link
Author

jsetton commented Aug 7, 2023

Same error.

The name 'hsbToRgb' cannot be resolved to an item or type

@lolodomo
Copy link
Contributor

lolodomo commented Aug 8, 2023

Sorry for my stupid suggestion.
ColorUtil is the class mame and org.openhab.core.util the package.
This package is public.
I hope there is not somewhere a hardcoded list of accepted packages by DSL rules.

@lolodomo
Copy link
Contributor

lolodomo commented Aug 8, 2023

@lolodomo
Copy link
Contributor

lolodomo commented Aug 8, 2023

I will do this change and then test if your rule example is working without the import.

@lolodomo
Copy link
Contributor

lolodomo commented Aug 8, 2023

@lolodomo
Copy link
Contributor

Unfortunately, I do not succeed to make it work.

10:18:15.371 [ERROR] [.xtext.validation.CompositeEValidator] - Error executing EValidator
java.lang.NoClassDefFoundError: org/openhab/core/util/ColorUtil
        at org.openhab.core.model.script.scoping.ScriptImplicitlyImportedTypes.getStaticImportClasses(ScriptImplicitlyImportedTypes.java:119) ~[?:?]
        at org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedFeatures.getStaticImportClasses(ImplicitlyImportedFeatures.java:67) ~[?:?]

...

Caused by: java.lang.ClassNotFoundException: org.openhab.core.util.ColorUtil cannot be found by org.openhab.core.model.script_4.1.0.202308110815

Even if the changed code is compiling, at runtime, org.openhab.core.model.script is not "seeing" org.openhab.core.util.
I do not understand why !

@jsetton
Copy link
Author

jsetton commented Aug 11, 2023

Shouldn't the package org.openhab.core.util be added as Import-Package to the org.openhab.core.model.script bnd file?

@lolodomo
Copy link
Contributor

Shouldn't the package org.openhab.core.util be added as Import-Package to the org.openhab.core.model.script bnd file?

Probably. I will try.

@lolodomo
Copy link
Contributor

Ok, it solved my exception.

Unfortunately, my change has no impact, I still get: The name 'ColorUtil' cannot be resolved to an item or type

@lolodomo
Copy link
Contributor

lolodomo commented Aug 11, 2023

By the way, I searched in my existing rules the syntax to call static methods and apparently the syntax would be ColorUtil::hsbToRgb(val)
Example:

var int idx = Integer::parseInt(params.get(0))

But it does not solve the issue !

@lolodomo
Copy link
Contributor

Do you have another working example with a call to a static method provided by openHAB ?

@jsetton
Copy link
Author

jsetton commented Aug 11, 2023

Do you have another working example with a call to a static method provided by openHAB ?

Is this relevant at this stage of the issue? I would expect an error at the method level once the org.openhab.core.util.ColorUtil package can be imported. Nevertheless, I think you might be right in the fact it should be called as ColorUtil::hsbToRgb.

@lolodomo
Copy link
Contributor

If I had a working example, I could try to understand what is the difference.
What annoys me a little is that we are even not sure what is the expected syntax!

@jsetton
Copy link
Author

jsetton commented Aug 11, 2023

Apparently, it doesn't seem to matter how it is called.

https://eclipse.dev/Xtext/xtend/documentation/203_xtend_expressions.html#static-access

@lolodomo
Copy link
Contributor

Maybe the only solution would be to add actions napping ColorUtil methods.

https://github.com/openhab/openhab1-addons/wiki/Actions

I think you can't with DSL rules import any Java package you like. That is certainly the root cause.
I thought my proposed change would allow importing a specific class but I was not able to make it work.

@jsetton
Copy link
Author

jsetton commented Aug 11, 2023

I don't have enough knowledge to weigh in on your proposed solution. It seems a bit overkill though.

The org.openhab.core.library packages from the same module are imported.

@lolodomo
Copy link
Contributor

It should be definitively possible to make ColorUtil class implicitly imported. It is what I am trying to achieve.
It is what we are already doing for example for Exec class (method executeCommandLine) or Log class (method logInfo, ...).
Once done, no need to use ColorUtil, just use the method name.
Normally, what I have done should be sufficient. There is probably something obvious that I miss !

@jsetton
Copy link
Author

jsetton commented Aug 11, 2023

There are other classes in org.openhab.core.util that would be useful.

@lolodomo
Copy link
Contributor

lolodomo commented Aug 11, 2023

Ok, I am able to make it work but I need to define a class in org.openhab.core.model.script.actions mapping all static methods of ColorUtil.

But then, we can take care to also expose the methods to UI. Edit: makes probably no sense to expose these utils directly as actions for the user in UI. But of course, they will be usable inside a UI script.

lolodomo added a commit to lolodomo/openhab-core that referenced this issue Aug 11, 2023
lolodomo added a commit to lolodomo/openhab-core that referenced this issue Aug 11, 2023
lolodomo added a commit to lolodomo/openhab-core that referenced this issue Aug 11, 2023
lolodomo added a commit to lolodomo/openhab-core that referenced this issue Aug 12, 2023
kaikreuzer pushed a commit that referenced this issue Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of the Core
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants