package me.soulmachine;

import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;


/**
 * A simple Jython example to execute Python scripts from Java.
 */
final class JythonExample {
  private JythonExample() {}

  /**
   * Main entrypoint.
   *
   * @param args arguments
   * @throws ScriptException ScriptException
   */
  public static void main(final String[] args) throws ScriptException {
    listEngines();

    final ScriptEngineManager mgr = new ScriptEngineManager();
    final ScriptEngine pyEngine = mgr.getEngineByName("python");

    try {
      pyEngine.eval("print \"Python - Hello, world!\"");
    } catch (ScriptException ex) {
      ex.printStackTrace();
    }

    final PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec("print \"Python - Hello, world!\"");

    final PyObject result = interpreter.eval("2 + 3");
    System.out.println(result.toString());
  }

  /**
   * Display all script engines.
   */
  public static void listEngines() {
    final ScriptEngineManager mgr = new ScriptEngineManager();
    final List<ScriptEngineFactory> factories =
        mgr.getEngineFactories();
    for (final ScriptEngineFactory factory: factories) {
      System.out.println("ScriptEngineFactory Info");

      final String engName = factory.getEngineName();
      final String engVersion = factory.getEngineVersion();
      final String langName = factory.getLanguageName();
      final String langVersion = factory.getLanguageVersion();

      System.out.printf("\tScript Engine: %s (%s)\n", engName, engVersion);

      final List<String> engNames = factory.getNames();
      for (final String name: engNames) {
        System.out.printf("\tEngine Alias: %s\n", name);
      }
      System.out.printf("\tLanguage: %s (%s)\n", langName, langVersion);
    }
  }
}

@@ -51,16 +51,49 @@
 Object;%0A
+import org.python.core.PyString;%0A
 import o
@@ -635,24 +635,67 @@
 Engines();%0A%0A
+    // Integrate with ScriptEngine%0A    %7B%0A  
     final Sc
@@ -737,32 +737,34 @@
 ngineManager();%0A
+  
     final Script
@@ -821,14 +821,18 @@
     
+ 
+ 
 try %7B%0A
+  
     
@@ -888,16 +888,18 @@
 %22);%0A    
+  
 %7D catch 
@@ -927,16 +927,18 @@
 %7B%0A      
+ 
+ 
 ex.print
@@ -947,24 +947,32 @@
 ackTrace();%0A
+      %7D%0A
     %7D%0A%0A    f
@@ -1096,22 +1096,16 @@
 );%0A%0A    
-final 
 PyObject
@@ -1183,16 +1183,361 @@
 ring());
+%0A%0A    // Define a function and call it from Java%0A    interpreter.exec(%22def myLowerCase(s):%5Cn%5Ctreturn s.lower()%5Cn%22);%0A    final PyObject myLowerCase = interpreter.get(%22myLowerCase%22);%0A    result = myLowerCase.__call__(new PyString(%22TEST!%22));%0A    final String realResult = (String)result.__tojava__(String.class);%0A    System.out.println(realResult);
 %0A  %7D%0A%0A  

add an example: define a Python function then call it from Java