-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Script-Engine: jsr223 script core to allow different scripting engines #2378
Conversation
openhab » openhab #2637 SUCCESS |
openhab » openhab #2650 SUCCESS |
Hi, This is really cool stuff, thanks for it! I was also considering jsr223 support, but couldn't really see how it could fit in best. Your way to deal with actions etc. looks pretty elegant. Your PR brings me in some trouble though: As you are hopefully aware, the evolution of the core takes place at Eclipse SmartHome now, which builds the base for openHAB 2. At Eclipse SmartHome, there is work for a new rule engine framework going on, which is also meant to allow other scripting languages - so your work fits in here nicely. I planned to make the current xtext-based rule syntax compatible with this new rule framework, so that there is no real need for migration for users - their rules could be kept (with maybe only slight modifications). If we now all of a sudden have not only the xtext-based rules, but many other possible syntaxes, I am out of the game with this approach though... That's why I would like to check with you, what you think is the best approach to handle this situation. I mainly see two options:
Regards, |
To be honest, I've not looked at smarthome / openhab2 so far, but it might the right time to do so now. I've read the thread on the eclipse forum and as I see it, it does not differ that much from the current possibilities, only the way of describing them is changed. The same can also be said about the jsr223 implementation. So a "rule" or "automation" can be seen as a sum of triggers (on + if combination) and the execution part, this is nearly the same as I'm currently doing it with jsr223. I already thought about "custom" triggers in this code, but for fast development purposes kept it simple and only used the triggers already available by the Rule system and do not allow additional conditions in them right now. But it would be possible to do this: A trigger consists of:
So basically what I want to tell here is the following: If we see the smarthome json-defintions as "construction"-code for underlying classes, we could do the following: Rule-"Ingredients"
Usage example
Rule generation "brewing"The Rule generation can be done by different code-basis:
These Rules will then be added to the Rule-Engine. The JSON-Description-Parser would do the following:
finally: To illustrate the usage I quickly prototype it. It is not that different from the way I did the script engine right now):
This would allow to make the execution part interchangeable, but does not require to do so. This would therefore allow defining the rules, triggers and conditions in Java, Scripting languages and with the json description "language". In openhab the current definition of the interface could nearly be the same as it is right now. I can already define the interface like that and extend the rule engine a little bit. This engine could than be used in openhab 1 and 2. And last but not least: The scripting engine(s) need the ability to add classes to those factories (triggers, conditions, execution-parts), to be usable in the json-descriptions I would also change it in such a way that not a method "getRules" is called, but rather let the scripts call something like "oh.addRule", "oh.addCondition", "oh.addTrigger", ("oh.addBinding"?, "oh.addAction"?) In my opinion it would be no good idea, to reduce the functionality of how a rule can be defined as they are really powerful in this way and for example one can even generate different triggers based on a supplied configuration if wanted. One of my rules can be seen here |
Any thoughts on my suggestion? Otherwise I would start with it the next days. |
openhab » openhab #2729 SUCCESS |
Sorry, I didn't find the time to read through your proposal yet, but will hopefully provide you feedback by the end of this weekend - thanks for your patience! |
Ok, I read through your ideas, but I think I did not fully get your suggestion yet - what you describe, is that meant to be your suggested solution for openHAB 1 in order to be as compatible as possible with the new Eclipse SmartHome concept? If this is the case, I somehow feel that it would mean quite some duplicate implementation efforts wrt JSON parsing, trigger and engine instantiations etc. - this is all work that will be done for Eclipse SmartHome as well. One of my other concerns is that the "script-based rule brewing" will be hard to get compatible - you will need all those interfaces and factories to be available within the scripting languages. Is that a feature of jsr223 that makes this always possible easily? Aren't there any conceptional mismatches for some scripting languages? |
At the time of writing I did not know, that a first draft of the classes in smarthome were already done, so therefore I did not use them. I looked at the smarthome classes and think that they are too complicated for a scripting language (and even for the java implementation) if all the boilerplate code has to be defined by hand. It would be advisable to create helper classes which help generating them (Builder classes). Furthermore there could be "comfort" methods which take as argument the "basic" rule engine interface (e.g just isSatisfied in case of ConditionHandler) and in addition names/values/configs which are really mandatory to make them available in a modular fashion. The rest is handled by a default implementation. Or maybe use the new "default implementation" feature of interfaces in java? There are two possibilities to proceed here:
Question: [..] you will need all those interfaces and factories to be available within the scripting languages. Is that a feature of jsr223 that makes this always possible easily? Question: Aren't there any conceptional mismatches for some scripting languages? Question: Why do you think it is important to have the "brewing" completely available within scripting? |
Hi @smerschjohann, You are right, besides a JSON reader (be it to read it from within OSGi bundles, from the file system or through the REST API), there can be potentially other sources for rules. But please note that with the new modular concept, the focus is on moving from rules to rule templates. So a rule should actually look like "rule2" in https://github.com/eclipse/smarthome/blob/master/bundles/automation/org.eclipse.smarthome.automation.json/src/main/resources/RuleDefinitions.json#L57. This means it is merely a configuration to instantiate a rule from a given template. The idea of this is to make it much easier to share and exchange rules (that is: rule templates) between users. And then again, these templates are simply a set of modules and the idea is actually to have different modules available for different scripting languages.
I doubt that this will lead to something that is compatible with openHAB 2 in the end - and this is my most important goal. Any new kind of concepts in openHAB 1 will make it harder to provide a migration path to openHAB 2 for users. This is why we actually declared the openHAB 1 runtime to be feature frozen a while ago and only want to accept bug fixes for it. New concepts should be implemented in Eclipse SmartHome (and therefore for openHAB 2). |
What you write would also mean that I stop providing jsr223 for openhab 1. Maybe the best is to stop working on jsr223 for openhab 1. It just keeps the way I have it right now. I could move the package to another namespace and integrate it as an addon. If someone wants to use it, they can - as an addon. No direct migration path will be given for this addon and if something breaks (which will probably happen), they were warned. I guess someone using this addon at this stage would have the competence to migrate the rules later on. And yes we can discuss about the rule engine for smarthome and possible integration points for script engines on the forum. |
Yes, this was exactly my suggestion from above! (See the two bullet points on #2378 (comment)) :-) |
Hm, I am currently looking for a package name but do not really know which would fit here. |
Yes, agreed, the package can stay. So my original suggestion (#2378 (comment)) would still apply, if you agree:
Would you have a chance to check whether it's working with openHAB 2 and what we might have to do on the compat layer? |
I try to check OpenHAB 2 compatiblity when I have a bit spare time. As I do not have a running OpenHAB2 configuration right now, this could take some time (as I don't have much time currently). I just moved the js223 core to the addon package. Hope I didn't miss a spot. I will add a wiki page entry soon. |
Ok, thanks. @teichsta, would you schedule this for the 1.7 release? |
openhab » openhab #2932 FAILURE |
openhab » openhab #2943 FAILURE |
yes, i will! Thanks @smerschjohann! |
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="lib" path="/org.openhab.core.scheduler/lib/quartz-all-2.1.7.jar"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this entry can be removed
org.osgi.framework, | ||
org.osgi.service.cm, | ||
org.osgi.service.event, | ||
org.osgi.util.tracker;version="1.5.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove version (unless you really need this specific version)
Hi @smerschjohann, thanks again for this great contribution which looks quite good already. I've merely had a look at the ScriptXXX classes since many other classes seem to be copied from the existing ScriptEngine. Please find my (very few) review comments inline. Please also squash your commits into one once you've incorporated the feedback. Best, Thomas E.-E. |
yes they were merely copied and modified for my needs. I would even have used them when they were not referencing directly to XText etc. As I did not want to change the current Script-Engine this seemed to be the only choice ;) I have integrated your remarks. Also at https://github.com/openhab/openhab/wiki/Addon%3A-Jsr223-Script-Engine the Script-Engine is documented. If you look at Jython-Installation you see that I modified my local start-script of openhab. Do you have a better idea how to directly integrate it? - And for that matter, make it easier for a new user to use jython with this addon? I will test my modifications during the next days and squash the commits afterwards. |
openhab » openhab #3010 SUCCESS |
hmm … in order to keep the general configuration clean i don't see any other solution than documenting the change to the start.xxx files like you did already.
great, thanks! |
…nguages with jsr223 integration. See Wiki-Entry for more details. (openhab#2378)
looks good, still working as expected. Can be merged now. |
openhab » openhab #3029 FAILURE |
Hi,
I have implemented another script/rule engine to support different script languages. Those script-engines have to support jsr223. There a quiet a lot of choices: Jython, JRuby, Nashorn, etc.) (https://java.net/projects/scripting/sources/svn/show/trunk/engines)
The Scripts need to be located in "configurations/jsr_scripts", the path "jsr_scripts" is currently hardcoded. I had to implement my own FileWatcher as the FileObserver was "integrated" with the models. My FileWatcher is based on the FileWatcher-API provided by Java7 and therefore supports the underlying OS functionality.
I've added jython.jar to the lib-folder of this plugin. There might be a better way. I'm open for any suggestions (I'm not that familiar with the OSGI-architecture)
Script Implementation
Currently I've only checked python support as it is my main focus. I have added a test script as well, which shows how the scripts have to be written.
Rule-Class
Each rule is basically a class in the given scripting language. It needs to implement three functions:
Supported triggers:
Output
Interaction with openhab
In general all interaction is done throw the oh object. It has support for the following
logging
BusEvent
ItemRegistry
Accessing actions:
To access the old known methods (all actions) the openhab interface supports getActions() and getAction(name_of_action_provider)
Global variables/classes