Last active
April 23, 2018 09:22
-
-
Save echebbi/4c381b1c2924f0dc30d59794c6627646 to your computer and use it in GitHub Desktop.
Demonstrates programmatic use of Py4J script engine within an Eclipse plug-in thanks to EASE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Greets people. | |
*/ | |
public class Greeting { | |
private String name; | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public void greet() { | |
System.out.println("Hello " + name + "!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import org.eclipse.ease.IScriptEngine; | |
import org.eclipse.ease.service.EngineDescription; | |
import org.eclipse.ease.service.IScriptService; | |
import org.eclipse.ui.PlatformUI; | |
/** | |
* Greets people using a Java Greeting instance from Python. | |
* | |
* <h3>Example of Use</h3> | |
* | |
* The following examples prints "Hello Lorem!" then "Hello Ipsum!": | |
* | |
* <pre> | |
* List<String> names = Arrays.asList("Lorem", "Ipsum"); | |
* | |
* GreetingInPython greeting = new GreetingInPython(); | |
* greeting.greet(names); | |
* </pre> | |
* | |
* <h3>Dependencies</h3> | |
* | |
* Requires following dependencies in MANIFEST.MF: | |
* <ul> | |
* <li>org.eclipse.ui,</li> | |
* <li>org.eclipse.ease</li> | |
* </ul> | |
* | |
* Following plug-ins should be available as well (within Eclipse or via target | |
* platform): | |
* <ul> | |
* <li>org.eclipse.ease.lang.python,</li> | |
* <li>org.eclipse.ease.lang.python.py4j</li> | |
* </ul> | |
*/ | |
public class GreetingInPython { | |
public void greet(List<String> names) { | |
// Retrieve the engine aimed to execute Python's code | |
final IScriptService scriptService = PlatformUI.getWorkbench().getService(IScriptService.class); | |
EngineDescription engineDescription = scriptService.getEngineByID("org.eclipse.ease.lang.python.py4j.engine"); | |
IScriptEngine engine = engineDescription.createEngine(); | |
// Create a new Greeting instance and make it available from Python | |
Greeting greet = new Greeting(); | |
engine.setVariable("greeting", greet); | |
// Make the list of the names to greet available from Python | |
engine.setVariable("names", names); | |
try { | |
// Execute Python code | |
engine.executeSync( | |
"for name in names:\n" + | |
" greeting.setName(name)\n" + | |
" greeting.greet()" | |
); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
Thread.currentThread().interrupt(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment