GreenDelta / olca-ipc.py

Mozilla Public License 2.0
29 stars 17 forks source link

Syntax Error importing module olca in Jython #12

Closed TKatzwinkel closed 3 years ago

TKatzwinkel commented 3 years ago

When I try import olca in a Jython script, I get the Error Message:

mismatched input ':' expecting RPAREN (__init__.py, line 12) SyntaxError

This happens with the actual master branch v0.0.9. I tried in Jython 2.5.3, Jython 2.7.2 and even native Python 2.7.18

My suggestion is that the type declaration of variables in the modules do not match the Python 2.x syntax somehow? Maybe related to Python 3.x vs Python 2.x issues?

As olca-ipc is meant for Jython in the first place, I wonder how this Error occurs.

Any suggestions?

msrocka commented 3 years ago

As olca-ipc is meant for Jython ...

No, olca-ipc runs on standard Python, see the README:

The olca-ipc package provides a convenience API for using this IPC protocol from standard Python (Cpython v3.6+) ...

TKatzwinkel commented 3 years ago

Oh, I am sorry for not getting this right. The Error does make sense in that case, as it is meant for Cpython 3.6+ Thanks for correcting me there.

Any suggestions on how to use the IPC server of openLCA by a Jython Script from the outside?

msrocka commented 3 years ago

It depends a bit on what you want to do but with Jython you are already in the JVM and then it should be much easier to use the Java API of openLCA directly instead of doing this via the olca-ipc interface. With the IPC interface you can call only a selected set of functions that we expose via an JSON-RPC over HTTP protocol. So you would need to write an HTTP client that interprets these JSON-RPC messages in Jython. While this is possible you can call these functions (and more) via the Java API directly.

TKatzwinkel commented 3 years ago

It depends a bit on what you want to do

I am trying to combine a Model-based Systems Engineering approach (using a Java-based SysML Modeler tool) with openLCA, so I can calculate single manufacturing processes inside openLCA and return the CO2 footprint result into the SysML-Modeler Application. In the best case, I want to hide openLCA completely in the background and just invoke the calculation process from a script basis.

Simply put, the Modeler tool (Cameo Systems Modeler from NoMagic Company) is a Java based application and provides Script execution. Here, various scripting languages like Jython, Jruby or JavaScript are provided.

As far as I digged into it, I see two options from here:

1) I can Import the Java API from openLCA into the JVM from the Modeler tool and try to automate the process calculations using the APi within the Jython script directly.

2) As a fallback, I could invoke a system shell from the Modeler tool with Jython, and then use the Python olca-ipc API with standard Python running on the local system. This would not be my first aim though, as it seems to be quite complicated.

Can you give me some quick feedback on these ideas? If I go the Java API way, I could not find any tutorial or example. Is there something similar for the Java API like the last example from here https://github.com/GreenDelta/openlca-python-tutorial/blob/master/examples.md ?

Thanks for pointing me the right way...

msrocka commented 3 years ago

I can Import the Java API from openLCA into the JVM from the Modeler tool and try to automate the process calculations using the APi within the Jython script directly.

This sounds good. If you can add the libraries from openLCA/plugins/olca-app_<version>/libs to the classpath of the Jython interpreter, this could work.

If I go the Java API way, I could not find any tutorial or example.

The Jython examples in the openLCA-Python-tutorial repository are already using the openLCA Java API; so this is how your scripts would look like when you manage it to get the libraries into your Jython interpreter loaded. However, the examples there are outdated (most of them were created for a developer training a few years ago) and the API changed quite a bit. Also, we are working on openLCA 2.0 and we try to make the API more simple to use.

TKatzwinkel commented 3 years ago

Thank you so much for the great support!

Also, we are working on openLCA 2.0 and we try to make the API more simple to use.

I am impressed to hear that and look forward to it...

My "issue" is solved by now, though. Therefor I close it.