Java调用Jython的方法

来源:互联网 发布:实验数据作图软件 编辑:程序博客网 时间:2024/05/01 13:25
1、一个可以和 Jython 对象映射的 Java 借口类
  1. package com.newbee;
  2. public interface EmployeeType {
  3.     
  4.     public String getEmployeeFirst();
  5.     public String getEmployeeLast();
  6.     public String getEmployeeId();
  7.     public void setEmployeeId();
  8.     
  9. }

2、一个从 Jython 文件中获取 Java对象、Jython对象和 Jython函数对象的工厂方法类
  1. package com.newbee;
  2. import java.util.HashMap;
  3. import org.python.core.PyFunction;
  4. import org.python.core.PyObject;
  5. import org.python.util.PythonInterpreter;
  6. public class JythonFactory {
  7.     
  8.     private static final JythonFactory instance = new JythonFactory();
  9.     private static final HashMap<String, PythonInterpreter> piMap = new HashMap<String, PythonInterpreter>();
  10.     public static JythonFactory getInstance() {
  11.         return instance;
  12.     }
  13.     public Object getJavaObjectFromJythonFile(String interfaceName, String pathToJythonModule) {
  14.         Object javaObject = null;
  15.         
  16.         PythonInterpreter interpreter = cacheInterpreter(pathToJythonModule);
  17.         
  18.         String tempName = pathToJythonModule.substring(pathToJythonModule.lastIndexOf("/") + 1);
  19.         tempName = tempName.substring(0, tempName.indexOf("."));
  20.         System.out.println(tempName);
  21.         String instanceName = tempName.toLowerCase();
  22.         String javaClassName = tempName.substring(01).toUpperCase() + tempName.substring(1);
  23.         String objectDef = "=" + javaClassName + "()";
  24.         interpreter.exec(instanceName + objectDef);
  25.         try {
  26.             Class JavaInterface = Class.forName(interfaceName);
  27.             javaObject = interpreter.get(instanceName).__tojava__(JavaInterface);
  28.         } catch (ClassNotFoundException ex) {
  29.             ex.printStackTrace(); // Add logging here
  30.         }
  31.         return javaObject;
  32.     }
  33.     public PyObject getPyObjectFromJythonFile(String typeName, String pathToJythonModule) {
  34.         PyObject pyObject = null;
  35.         PythonInterpreter interpreter = cacheInterpreter(pathToJythonModule);
  36.         
  37.         String instanceName = typeName.toLowerCase();
  38.         String objectDef = "=" + typeName + "()";
  39.         interpreter.exec(instanceName + objectDef);
  40.         pyObject = interpreter.get(instanceName);
  41.         return pyObject;
  42.     }
  43.     
  44.     public PyFunction getPyFunctionFromJythonFile(String funcName, String pathToJythonModule) {
  45.         PyFunction pyFunction = null;
  46.         PythonInterpreter interpreter = cacheInterpreter(pathToJythonModule);
  47.         pyFunction = (PyFunction)interpreter.get(funcName,PyFunction.class);  
  48.         return pyFunction;
  49.     }
  50.     
  51.     private PythonInterpreter cacheInterpreter(String pathToJythonModule) {
  52.         PythonInterpreter interpreter = null;
  53.         if (piMap.get(pathToJythonModule)!=null) {
  54.             interpreter = piMap.get(pathToJythonModule);
  55.         } else {
  56.             interpreter = new PythonInterpreter();
  57.             interpreter.execfile(pathToJythonModule);
  58.         }
  59.         return interpreter;
  60.     }
  61.     
  62. }
3、Jython脚本文件 Employee.py
  1. # Jython source file
  2. from com.newbee import EmployeeType
  3. class Employee(EmployeeType):
  4.     def __init__(self):
  5.         self.first = "Sean"
  6.         self.last  = "Guo"
  7.         self.id = "731"
  8.     def getEmployeeFirst(self):
  9.         return self.first
  10.     def getEmployeeLast(self):
  11.         return self.last
  12.     def getEmployeeId(self):
  13.         return self.id
  14.   
  15.     def setEmployeeId(self, newId):
  16.         self.id = newId
  17.         
  18. def getNunmberValue(seed):
  19.     v0 = 0
  20.     for i in range(1,seed+1):
  21.         v0 +=i
  22.     return v0
4、使用上述类的方法
  1. package com.newbee;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. import org.python.core.PyException;
  5. import org.python.core.PyFunction;
  6. import org.python.core.PyInteger;
  7. import org.python.core.PyObject;
  8. import org.python.core.PyString;
  9. public class SimpleEmbedded {
  10.     public static void main(String[] args) throws PyException {
  11.         
  12.         // Java
  13.         EmployeeType eType = (EmployeeType) JythonFactory.getInstance().getJavaObjectFromJythonFile("com.newbee.EmployeeType""jylib/Employee.py");
  14.         System.out.println("Employee Name: " + eType.getEmployeeFirst() + " " + eType.getEmployeeLast());
  15.         System.out.println("Employee ID: " + eType.getEmployeeId());
  16.         
  17.         // Jython
  18.         PyObject pyObject = JythonFactory.getInstance().getPyObjectFromJythonFile("Employee""jylib/Employee.py");
  19.         System.out.println("+++="+pyObject.invoke("getEmployeeId"));
  20.         pyObject.invoke("setEmployeeId",new PyString("1999"));
  21.         System.out.println("+++="+pyObject.invoke("getEmployeeId"));
  22.         
  23.         // Jython Function
  24.         PyFunction pyFunction = JythonFactory.getInstance().getPyFunctionFromJythonFile("getNunmberValue""jylib/Employee.py");
  25.         System.out.println("***="+pyFunction.__call__(new PyInteger(10)));
  26.     }
  27. }

原创粉丝点击