[DSL] Add extra commands/abilities to DSL scripts/rules#5481
Conversation
|
Also, consider the specific new commands I've implemented as more of a suggestion. This can be tweaked as desired. @rkoshak Perhaps you have something to add here? |
|
The itest fails because of some unresolved reference. I'm trying to figure it out, but if anybody has any hints of how to figure out the problem, it would be appreciated, because I frankly have no idea how to handle the bndrun resolution. |
|
I don't think the itest fails over dependencies, but that the startlevel is insufficient: If I understand it correctly, So, the question is: Is it actually a problem that |
|
I've set the startlevel in the test to 40, which is required for the script engine to start, and it seems to resolve the problem with the test. I'm still uncertain as to whether reaching startlevel 40 before the Are there scripts being executed before startlevel 40 is reached? |
|
Now there's a new test failure, not sure exactly how it is related yet: |
|
The current test failure seems to have some complicated timing reason. The following line in Resource resource = resourceSet.createResource(URI.createURI(PREFIX_TMP_MODEL + name));This results in an NPE. The question is why it returns
I'm not sure what this mythical factory is and why it suddenly can't be found, but I assume that it has something to do with |
|
I changed the strategy regarding This has resolved the test problems, but comes with the caveat that |
|
In case somebody wants to test it for themselves, this bundle should be all that's needed. |
|
@rkoshak I've added some additional methods for manipulation of metadata. Is that somewhat what you had in mind? I've also reluctantly implemented the varargs "solution" for the maps, so that it's possible to simply call e.g. Since these are just suggestions, I haven't bothered to finish the Javadocs, as I'm sure they'll change, which makes the time I spend writing the documentation a waste. |
|
@rkoshak I've added even more overloads, so not all the metadata methods accept either item name or |
|
@lolodomo There's no a lot of activity here, maybe you're interested in this? This isn't important for me in any way, but seen in light of all the other discussions about DSL versus other scripting languages and the difference in what you can do, I thought that this might be a welcome contribution to close the gap. |
|
Ï've never fully understood the "action" concept in OH. I understand that they are similar to functions/methods, but not what the significance of making things "actions" is. We have for example Why this structure? Is an |
|
I've reorganized The "full versions" are also there, so you can call |
|
This pull request has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/rules-and-rule-templates-yaml-integration/168568/222 |
|
#5484 adds Running afterwards |
Adding That said, the goal of this PR isn't to make it possible to do those things, but to make it reasonably convenient. As long as imports can be resolved, you can always use This PR has public static @Nullable <T> T getInstance(Class<T> clazz) {
Bundle bundle = FrameworkUtil.getBundle(clazz);
if (bundle != null) {
BundleContext bc = bundle.getBundleContext();
if (bc != null) {
ServiceReference<T> ref = bc.getServiceReference(clazz);
if (ref != null) {
return bc.getService(ref);
}
}
}
return null;
}So, I don't see these two as "competing" in any way. |
|
After By using the function |
Rectification: After |
|
I didn't remember that the services were reference counted, so I'll have to make some modifications to accommodate that. But, as I see it, we can't expect script authors to use try/finally to make sure to release the reference. I've looked a bit in core, and it seems like this is "mishandled" other places as well. If you look at JS Scripting, it does the exact same thing: https://openhab.github.io/openhab-js/osgi.js.html#line38 Since it's unrealistic that script authors can manage the reference counting, I think the best thing to do is to release the reference before returning it. That will work just fine as long as something else keeps a "counted reference" to the instance. In OH, this will almost always be the case, there are other components that reference "everything". As far as I understand, OSGi uses this reference counting to automatically stop services that aren't used by anything. The fact that a service is referenced doesn't prevent an explicit shutdown, so it's not like "a lock", it just prevents automatic shutdown. Since the script acquiring the reference should never have the only reference, releasing it before returning should work fine in practice. In the very unlikely scenario that this isn't the case, the script will fail when it tries to use the service if OSGi has shut it down in the meanwhile. I think that is acceptable, given that it is extremely unlikely to happen, and it should be better than to "leak references". |
lolodomo
left a comment
There was a problem hiding this comment.
Very promising contribution.
Just few comments
|
@Nadahar : thank you for that contribution. Why is it still in draft mode ? Does it mean it is not yet finished ? |
|
https://www.openhab.org/docs/configuration/persistence.html#persistence-extensions-in-scripts-and-rules suggests that Likewise I find no statement, that both So im my eyes, an excerpt of all implicit imports is documented, but not all of them. I find also no documentation, stating that So there are plenty of undocumented implicit imports. |
|
I didn’t say the documentation was complete. But there is more then you implied. I seem to remember there was some debate at some point about ChronoUnit.DAYS vs just DAYS. There was some breaking change replacing one by the other. In the end, both were accepted. |
This is because all the actions are also registered as extensions. I've pointed out how I think this is just wrong earlier, but changing it now risks breaking things. I don't think it's documented because This isn't something I've attempted to "clean up" in this PR, ideally it should probably be, but it will be breaking in some way or another. I contemplated making a new annotation or similar, where each action method would need to be explicitly annotated to also register as an extension. Because some of them are meant to be extensions as well, like the persistence methods. |
Exactly. But just because they work doesn't mean we need to explicitly document and promote these. I will already be very happy if we get everything documented that actually makes sense. And that's where I think we are less far off than some of the discussion implies. There are gaps for sure, but there is more documented then just State and Command. |
Found the reference: #4791 |
|
@mherwege I've revised the imports - please check. |
|
I thought that with such a simple change, I couldn't possibly upset spotless, so I didn't run it. I was wrong 😒 |
|
The Java25 failure seems to be unrelated to this PR, in a bundle not touched by this PR. I'd say it's most likely flaky tests, although I didn't investigate further when I saw where it was. |
|
@mherwege I get some strange errors when trying to run this branch now - has something been merged that somehow makes the code incompatible? I don't quite understand it, because I've built all of core successfully on this branch, yet I still get these failures during runtime. I guess I should just rebase it and see if that solves it, but I'm not sure if any review is "pending" here at the moment. |
…ty to run and enable/disable rules to DSL scripts Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
This reverts commit ee8ae1e. Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
- Remove unnecessary, left-over persistence imports - Add potentially useful import considering the new stricter class resolution mechanism in the recently bumped Xtext version Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
…c cleanup Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
|
It seems to have cleared up after a rebase, so the solution is probably just to push the rebased version. |
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
|
I take the lack of response of a sign that nobody is in the middle of something here, so I'll push the rebased version. |
|
I've added two additional commits, one adds access to |
Add access to various system registries, OSGi instances and the ability to run and enable/disable rules to DSL scripts.
This has been considered "impossible" to do, yet I've met no special obstacles, and it seems to work with my (limited) testing. I'm therefore skeptical that there might be something that I've missed, like additional startup issues caused by
ScriptServiceUtilreferencing additional instances, thus depending on them. But, I've observed no such problems.Additional context/information about the historical problems with this is appreciated, but as far as I can see at the moment, this works without complications.