srplab Common Language Extension(CLE)
 

For java calls python, Here presents a new method, which ues cle developed by srplab.

A function “Add” is defined in lua which returns the sum of two input integers. The function also calls java method “Show” to print the result.

There are three steps:

1.       Init cle, create service group, and create service.

2.       Create interface object, and defines a new function “Add” of it.

3.       Write function code of Add.

 

import starpy

--Step1 : Init cle, create service group, and create service

Service = starpy._InitSimple("AddFunctionService","123",0,0);

--Step2 : Create interface object, and define function Add

Obj=Service._New("TestClass");

--Step3 : Add function code

def Obj_Add(self,x,y) :

  --call back to script function

  self.Show(x+y);

  return x+y;

Obj.Add = Obj_Add;

 

For java code calls the above python file, there are four steps;

1.       Init cle, create service group, import python file, and create service.

2.       Define extends class of StarObjectClass, which implement callback function “Show”.

3.       Create Instance of object “TestClass” defined in python.

4.       call function “Add”.

 

import com.srplab.www.starcore.*;

 

//--Step2 : Define extends class of StarObjectClass and implements callback function “Show”.

class MyObjectClass extends StarObjectClass{

   public void Show( StarObjectClass self,int para )

   {

     System.out.println("print from java call back "+para);

     return;

   }

         public MyObjectClass(StarObjectClass srcobj){

                   super(srcobj);

         }     

}

 

public class java_call{

         public static void main(String[] args){

         //--Step1 : Init cle, create service group,import python file,  and create service

                   StarCoreFactory starcore= StarCoreFactory.GetFactory();

                   StarServiceClass Service=starcore._InitSimple("test","123",0,0,"AddFunctionService.py?script=python");

                   //--Step3 : Create Instance of object “TestClass” defined in python.

                   MyObjectClass a = new MyObjectClass( Service._GetObject("TestClass")._New() );

                   //--Step4 : call function “Add”.

        System.out.println("Add returns : "+a._Call("Add",12,34));

        starcore._ModuleExit();

    }

} 

examples code download

Copywright (C) 2005 www.SRPLab.com(Star-River Platform Lab.) All Rights Reserved Email:support@srplab.com