Is there an interactive tool for writing java scripts?

My environment is RPI/OpenHAB/Samba and I use XCode Editor on Mac via Samba.

My java skills are rusty and I have several python scripts I’d like to turn into rules in openHAB. (I realize I could install//use Jython but at this time I prefer to convert my code to Java.)

Is there an environment I could install that would let me test my code semi-interactively? What I mean is I’d like to write a main with a function and build up from that while testing along the way. (I’d also like to minimize the openhab footprint on my editing platform if possible.)

The goal will be to have some sophisticated rule logic I’m currently using in Python on another HA platform.

First, Java isn’t script based. It is a compiled programming language so interactive programming really isn’t a thing for Java like you might be used to in languages like Python.

Second, openHAB rules are not written in Java so anything you would try to write in Java that you would entend to use in rules would be wasted effort. Rules can only be written in openHAB’s Domain Specific Language (DSL) which is based on Xbase and similar to Xtend, or by using the JSR233 addon you can write rules in Jython or JavaScript. So if you don’t want to use Jython you will need to convert your rules to the DSL, not Java.

There is an IDE for openHAB rules and configurations called openHAB Designer. It is based on Eclipse and can be downloaded from openHAB’s main site. I strongly recommend using Designer if you plan on using the DSL.

One thing to keep in mind, openHAB right now, whether you are using the DSL, Jython, or JavaScript, does not yet support generic reusable code very well. What I mean by this is that your rules and code will be strongly tied to your specific Items Groups, and other openHAB configurations. So it really is exceptionally difficult if not impossible to develop rules externally from the openHAB environment. This is particularly true for the DSL, less so for Jython and JavaScript where you have more tools techniques available to you like unit testing frameworks.

Therefore trying to build something up external from openHAB with its own runtime that you later plan on converting to rules inside of openHAB will likely not work very well. As it exists right now your best bet is to develop inside of an openHAB. This means using Designer, setting up Items, Persistence, Rules and Scripts and testing them on a running openHAB. Building something standalone with a “main” function is not an option.

I do keep saying things like “right now” and “as it exists” because I know Kai is working on a new rules engine for OH 2 which is going to support a way to write rules that are more generic and sharable. But until that is done…

Personally, I would suggest you develop in Jython instead of trying to convert to the DSL. It will much better support your desire to build up your rules separately and create unit tests and such. Though spend the time to figure out HOW openHAB works and ties everything together (it is an event based environment that follows a more actor based programming approach as opposed to a more traditional structured programming or OO approach). I bring this up as it may guide your approach and will limit how you structure your code.

All that being said, I am firm in my opinion that the DSL is an excellent language for writing HA rules and logic and advocate its use. I believe it is well worth learning and using. But because it is closer to an event driven actor based language developers who come to it and try to write their rules in a structured or OO manner end up fighting against the language instead of progressing. And that is one reason I suggest you might be happier using Jython.

I sincerely thank you for the thoughtful reply and cogent argument for Jython. I think you’ve been a great help to me in organizing my approach.

Installed openHAB Designer on my mac.
Is there a way to step through a script and see variables?
I don’t seem to be able to get Xtext to work.
I apologize if this is out of scope but would appreciate any links to openHAB scripting tutorial similar to this Xtext tutorial.

No debuger or anything similar is available. It is pretty much just a syntax checking editor. You pretty much have to rely on log statements. And there is nothing separate to download and set up to work in the DSL. You write the code in Designer, run the code in openHAB, and use the sitemap and log files to verify and debug. On the one hand it is kind of archaic sounding but the DSL is really high level and manages so much for you that the kinds of problems you will run into are pretty small when compared to a general purpose coding environment.

When you install openHAB it will have a configurations folder (where that folder is depends on the platform and installation method). You point Designer at that configurations folder. As you make changes openHAB will reload the files as they are saved.

The rules engine is a DSL based on Xtext. It is actually closer to Xtend and they have good documentation there and a tutorial at the link. But being a DSL, it is not either of these languages. For example, there is no support for classes or arrays in the DSL (given the nature of the problem domain you shouldn’t be using either anyway) but there is in Xtend.

Your best bet for learning the DSL (and how openHAB works in general) is reading the wiki,downloading and experimenting with the Demo, and looking at the examples on the wiki (bottom right column). A straight up DSL tutorial was talked about but given the changes that are in the works for OH 2 Kai and the others have suggested waiting would be better than spending a lot of effort on documentation that will become deprecated and/or need massive updates once OH 2 is released.