Overview Package Class Tree Deprecated Index Help
PREV CLASS NEXT CLASS FRAMES NO FRAMES All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD

jni
Class JNIUtilities

java.lang.Object
 extended byjni.JNIUtilities

public class JNIUtilities
extends java.lang.Object

A collection of utilities for generating Java Native Interface (JNI) classes.

For information about JNI, see http://java.sun.com/docs/books/tutorial/native1.1/concepts/index.html

For information about how to create shared libraries under Windows using Cygwin, see http://cygwin.com/cygwin-ug-net/dll.html

For information about using JNI with Cygwin, see http://www.inonit.com/cygwin/jni/helloWorld/c.html

How to call an actor that calls C code that returns a double

In this example, we call a C method called meaningOfLife() that returns a double.
  1. Create the C file called meaningOfLife.c that contains
     // Return the answer to "the meaning of life, the universe, and everything"
     double meaningOfLife() {
     return 42.0;
     }
     
  2. Create meaningOfLife.h that contains
     extern "C" double meaningOfLife();
     
  3. Compile and create the shared library. Under Windows, we create a .dll
     gcc -shared -o meaningOfLife.dll meaningOfLife.c
     
  4. Set the CLASSPATH to include the current directory
     CLASSPATH=.
     export CLASSPATH
     
  5. Start up Vergil with the JNI interface enabled.
     $PTII/bin/vergil -jni
     
    Note that the -jni option may go away in the future if we merge the jni facility into the main tree
  6. Create a new model with File -> New -> Graph Editor
  7. Drag in the JNIActor from the jni folder
  8. Right click on the actor and select Configure Arguments.
  9. Fill in the form as follows
    Name:
    output
    C or C++ type:
    double
    Kind:
    return
  10. Select Ok and then Commit to close the argument configurer
  11. Right Click on the actor to edit the parameters
    libraryDirectory
    ""
    nativeFunction:
    "meaningOfLife"
    nativeLibrary
    "meaningOfLife"
  12. Select Commit to close the Edit Parameters window
  13. Select JNI from the menu and then select Generate C Interface
  14. FIXME: Copy the dlls into a directory that is in the path
     cp meaningOfLife.dll $PTII/bin
     cp jni/jnimeaningOfLife/JnijnimeaningOfLife.dll $PTII/bin
     
  15. Save the model.
    FIXME: Because of an apparent bug, it is necessary to save the model for the port we just created to appear
  16. Add a Display actor from the Sink folder in the Actor Library and connect the input of the Display actor to the output of the meaningOfLife Actor
  17. FIXME: Because of a bug in the Ptolemy interface to the JNI actor, you must specify the type of the output of the meaningOfLife actor by right clicking on the actor and selecting Configure Ports and then entering double for output type
  18. Drag in a SDF director and set the number of iterations to 1.
  19. Select Run
  20. The answer to "the meaning of life, the universe, and everything" should appear in the Display actor.
If you get an error like:
 /cygdrive/c/Program Files/j2sdk1.4.1_01/include/win32/jni_md.h:16: syntax error before `;'
 
See http://java.sun.com/products/jdk/faq/jni-j2sdk-faq.html#compiler" and then follow these steps:
  1. Edit the file jdk_root>/include/win32/jni_md.h, Where jdk_root is the installation root (eg., c:/Program Files/Java/jdk1.5.0_01
  2. Replace the segment:
 typedef __int64 jlong;
 
with:
 #ifdef __GNUC__
 typedef long long jlong;
 #else
 typedef __int64 jlong;
 #endif
 

Since:
Ptolemy II 2.3
Version:
$Id: JNIUtilities.java,v 1.85.2.1 2006年12月30日 22:18:05 cxh Exp $
Author:
Vincent Arnould (vincent.arnould@thalesgroup.com), contributor Christopher Hylands
Accepted Rating:
Red (vincent.arnould)
Proposed Rating:
Red (vincent.arnould)

Method Summary
protected static java.io.File _exportCInterfaceFile(GenericJNIActor actor, java.lang.String destinationDirectory)
Create the JNI C file.
protected static void _exportDSP(GenericJNIActor actor, java.lang.String destinationDirectory)
Export the Visual Studio project.
protected static java.io.File _exportJavaInterfaceFile(GenericJNIActor actor, java.lang.String destinationDirectory)
Create the JNI Java file.
protected static void _exportMakefile(GenericJNIActor actor, java.lang.String destinationDirectory)
Export a makefile.
protected static java.util.Vector _getArguments(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn)
Get the arguments belonging to a GenericJNIActor.
protected static java.lang.String _getArguments(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
Get the args belonging to this entity.
protected static java.util.Vector _getArgumentsIn(GenericJNIActor actor)
Get the args In belonging to this entity.
protected static java.lang.String _getArgumentsIn(GenericJNIActor actor, java.lang.String separator)
Get the args In name belonging to this entity.
protected static java.util.Vector _getArgumentsInOut(GenericJNIActor actor)
Get the args InOut belonging to this entity.
protected static java.lang.String _getArgumentsInOut(GenericJNIActor actor, java.lang.String separator)
Get the args InOut belonging to this entity.
protected static java.lang.String _getArgumentsInOutWithCType(GenericJNIActor actor, java.lang.String separator)
Get the args In belonging to a GenericJNIActor with their c type.
protected static java.lang.String _getArgumentsInOutWithJNIType(GenericJNIActor actor, java.lang.String separator)
Get the InOut args belonging to a GenericJNIActor entity with their JNI type.
protected static java.lang.String _getArgumentsInOutWithJType(GenericJNIActor actor, java.lang.String separator)
Get the InOut args belonging to a GenericJNIActor entity with their java type.
protected static java.lang.String _getArgumentsInWithCType(GenericJNIActor actor, java.lang.String separator)
Get the In args belonging to a GenericJNIActor entity with their c type.
protected static java.lang.String _getArgumentsInWithJNIType(GenericJNIActor actor, java.lang.String separator)
Get the In args belonging to a GenericJNIActor entity with their JNI type, excluding the out arguments.
protected static java.lang.String _getArgumentsInWithJType(GenericJNIActor actor, java.lang.String separator)
Get the In args belonging to a GenericJNIActor entity with their java type.
protected static java.util.Vector _getArgumentsOut(GenericJNIActor actor)
Get the Out args belonging a GenericJNIActor entity.
protected static java.lang.String _getArgumentsOut(GenericJNIActor actor, java.lang.String separator)
Get the names of the out args name belonging to a GenericJNIActor.
protected static java.lang.String _getArgumentsOutWithCType(GenericJNIActor actor, java.lang.String separator)
Get the Out args belonging to a GenericJNIActor entity with their c type.
protected static java.lang.String _getArgumentsOutWithJNIType(GenericJNIActor actor, java.lang.String separator)
Get the args In belonging to this entity with their JNI type, excluding the out arguments.
protected static java.lang.String _getArgumentsOutWithJType(GenericJNIActor actor, java.lang.String separator)
Get the args Out belonging to this entity with their java type.
protected static java.lang.String _getArgumentsWithCType(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
Get the args belonging to this entity with their c type.
protected static java.lang.String _getArgumentsWithCTypeCast(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
Get the arguments belonging to this entity with their c type.
protected static java.lang.String _getArgumentsWithJNIType(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
Get the arguments In belonging to this entity with their JNI type, excluding the out arguments.
protected static java.lang.String _getArgumentsWithJType(GenericJNIActor actor, boolean isInput, boolean isOutput, boolean isReturn, java.lang.String separator)
Get the arguments belonging to this entity with their java type.
protected static java.lang.String _signature(java.lang.String typ)
Return the signature of the interface function.
protected static java.lang.String _signatureSendResults(GenericJNIActor actor)
Return the signature of the interface function.
static boolean generateJNI(CompositeEntity model)
Given a model, generate JNI files for all GenericJNIActors.
static void generateJNI(CompositeEntity model, GenericJNIActor actor)
Generate JNI files for one actor in a model.
static ExecuteCommands getExecuteCommands()
Get the command executor, which can be either non-graphical or graphical.
static java.lang.String getNativeLibrary(GenericJNIActor actor)
Return the value of the nativeLibrary attribute with the double quotes stripped off.
static void setExecuteCommands(ExecuteCommands executeCommands)
Set the command executor, which can be either non-graphical or graphical.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Method Detail

generateJNI

public static boolean generateJNI(CompositeEntity model)
 throws java.lang.Exception
Given a model, generate JNI files for all GenericJNIActors.

Parameters:
model - The model to generate JNI files for any contained GenericJNIActors.
Returns:
true if a GenericJNIActor was found.
Throws:
java.lang.Exception - If there was a problem creating the JNI files.

generateJNI

public static void generateJNI(CompositeEntity model,
 GenericJNIActor actor)
 throws java.lang.Exception
Generate JNI files for one actor in a model.

Parameters:
model - The model that contains the actor.
actor - The actor for which to generate JNI files.
Throws:
java.lang.Exception - If an actor with the JNI name already exists, if there is a problem creating or compiling the JNI files, or if there is a problem creating the ports that correspond with the native method parameters.

getNativeLibrary

public static java.lang.String getNativeLibrary(GenericJNIActor actor)
 throws IllegalActionException
Return the value of the nativeLibrary attribute with the double quotes stripped off.

Parameters:
actor - The actor that contains a nativeLibrary attribute.
Returns:
The value of the nativeLibrary attribute with the leading and trailing double quotes removed.
Throws:
IllegalActionException - If there is a problem getting the nativeLibrary attribute.

getExecuteCommands

public static ExecuteCommands getExecuteCommands()
Get the command executor, which can be either non-graphical or graphical. The initial default is non-graphical, which means that stderr and stdout from subcommands is written to the console.

Returns:
executeCommands The subprocess command executor.
See Also:
setExecuteCommands(ExecuteCommands)

setExecuteCommands

public static void setExecuteCommands(ExecuteCommands executeCommands)
Set the command executor, which can be either non-graphical or graphical. The initial default is non-graphical, which means that stderr and stdout from subcommands is written to the console.

Parameters:
executeCommands - The subprocess command executor.
See Also:
getExecuteCommands()

_exportCInterfaceFile

protected static java.io.File _exportCInterfaceFile(GenericJNIActor actor,
 java.lang.String destinationDirectory)
 throws IllegalActionException,
 java.io.IOException
Create the JNI C file.

Parameters:
actor - Actor to generate a JNI C file for.
destinationDirectory - Directory to create the file in.
Returns:
a reference to the C file.
Throws:
IllegalActionException - If there is a problem accessing parameters or ports.
java.io.IOException - If there is a problem writing the C files.

_exportDSP

protected static void _exportDSP(GenericJNIActor actor,
 java.lang.String destinationDirectory)
 throws IllegalActionException,
 java.io.IOException
Export the Visual Studio project.

Parameters:
actor - Actor to generate a JNI Java file for.
destinationDirectory - Directory to create the file in.
Throws:
IllegalActionException - If there is a problem accessing parameters or ports.
java.io.IOException - If there is a problem writing the Visual Studio files.

_exportJavaInterfaceFile

protected static java.io.File _exportJavaInterfaceFile(GenericJNIActor actor,
 java.lang.String destinationDirectory)
 throws IllegalActionException,
 java.io.IOException
Create the JNI Java file.

Parameters:
actor - Actor to generate a JNI Java file for.
destinationDirectory - Directory to create the file in.
Returns:
a reference to the Java file.
Throws:
IllegalActionException - If there is a problem accessing parameters or ports.
java.io.IOException - If there is a problem writing the Java file.

_exportMakefile

protected static void _exportMakefile(GenericJNIActor actor,
 java.lang.String destinationDirectory)
 throws IllegalActionException,
 java.io.IOException
Export a makefile.

Parameters:
actor - Actor to generate a makefile for.
destinationDirectory - Directory to create the file in.
Throws:
IllegalActionException - If there is a problem accessing parameters or ports.
java.io.IOException - If there is a problem writing the makefile.

_getArguments

protected static java.util.Vector _getArguments(GenericJNIActor actor,
 boolean isInput,
 boolean isOutput,
 boolean isReturn)
Get the arguments belonging to a GenericJNIActor.

Parameters:
actor - The GenericJNIActor object
isInput - True if we are searching for input arguments.
isOutput - True if we are searching for output arguments.
isReturn - True if we are searchin for return arguments.
Returns:
a vector of arguments.

_getArguments

protected static java.lang.String _getArguments(GenericJNIActor actor,
 boolean isInput,
 boolean isOutput,
 boolean isReturn,
 java.lang.String separator)
Get the args belonging to this entity.

Parameters:
actor - The GenericJNIActor object.
isInput - True if we are searching for input arguments.
isOutput - True if we are searching for output arguments.
isReturn - True if we are searchin for return arguments.
separator - The separator used between multiple arguments.
Returns:
the name of each argument.

_getArgumentsIn

protected static java.util.Vector _getArgumentsIn(GenericJNIActor actor)
Get the args In belonging to this entity.

Parameters:
actor - The GenericJNIActor object
Returns:
a vector of out arguments, excluding the in arguments.

_getArgumentsIn

protected static java.lang.String _getArgumentsIn(GenericJNIActor actor,
 java.lang.String separator)
Get the args In name belonging to this entity.

Parameters:
actor - The GenericJNIActor object
separator - The separator used between multiple arguments.
Returns:
the name of the out arguments, excluding the in arguments.

_getArgumentsInOut

protected static java.util.Vector _getArgumentsInOut(GenericJNIActor actor)
Get the args InOut belonging to this entity.

Parameters:
actor - The GenericJNIActor object
Returns:
a vector of inout arguments.

_getArgumentsInOut

protected static java.lang.String _getArgumentsInOut(GenericJNIActor actor,
 java.lang.String separator)
Get the args InOut belonging to this entity.

Parameters:
actor - The GenericJNIActor object
separator - The separator used between multiple arguments.
Returns:
the name of the InOut arguments.

_getArgumentsInOutWithCType

protected static java.lang.String _getArgumentsInOutWithCType(GenericJNIActor actor,
 java.lang.String separator)
Get the args In belonging to a GenericJNIActor with their c type.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the c type and name of each in arguments.

_getArgumentsInOutWithJNIType

protected static java.lang.String _getArgumentsInOutWithJNIType(GenericJNIActor actor,
 java.lang.String separator)
Get the InOut args belonging to a GenericJNIActor entity with their JNI type.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the JNI type and name of each in arguments.

_getArgumentsInOutWithJType

protected static java.lang.String _getArgumentsInOutWithJType(GenericJNIActor actor,
 java.lang.String separator)
Get the InOut args belonging to a GenericJNIActor entity with their java type.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the name and the java type of the out arguments, excluding the in arguments.

_getArgumentsInWithCType

protected static java.lang.String _getArgumentsInWithCType(GenericJNIActor actor,
 java.lang.String separator)
Get the In args belonging to a GenericJNIActor entity with their c type.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the c type of the out arguments.

_getArgumentsInWithJNIType

protected static java.lang.String _getArgumentsInWithJNIType(GenericJNIActor actor,
 java.lang.String separator)
Get the In args belonging to a GenericJNIActor entity with their JNI type, excluding the out arguments.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the JNI type and name of each in arguments.

_getArgumentsInWithJType

protected static java.lang.String _getArgumentsInWithJType(GenericJNIActor actor,
 java.lang.String separator)
Get the In args belonging to a GenericJNIActor entity with their java type.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the name and the java type of the out arguments.

_getArgumentsOut

protected static java.util.Vector _getArgumentsOut(GenericJNIActor actor)
Get the Out args belonging a GenericJNIActor entity.

Parameters:
actor - The GenericJNIActor object.
Returns:
a vector of out arguments.

_getArgumentsOut

protected static java.lang.String _getArgumentsOut(GenericJNIActor actor,
 java.lang.String separator)
Get the names of the out args name belonging to a GenericJNIActor.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the name of the out arguments.

_getArgumentsOutWithCType

protected static java.lang.String _getArgumentsOutWithCType(GenericJNIActor actor,
 java.lang.String separator)
Get the Out args belonging to a GenericJNIActor entity with their c type.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the c type of the out arguments.

_getArgumentsOutWithJNIType

protected static java.lang.String _getArgumentsOutWithJNIType(GenericJNIActor actor,
 java.lang.String separator)
Get the args In belonging to this entity with their JNI type, excluding the out arguments.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the JNI type and name of each in arguments.

_getArgumentsOutWithJType

protected static java.lang.String _getArgumentsOutWithJType(GenericJNIActor actor,
 java.lang.String separator)
Get the args Out belonging to this entity with their java type.

Parameters:
actor - The GenericJNIActor object.
separator - The separator used between multiple arguments.
Returns:
the name and the java type of the out arguments.

_getArgumentsWithCType

protected static java.lang.String _getArgumentsWithCType(GenericJNIActor actor,
 boolean isInput,
 boolean isOutput,
 boolean isReturn,
 java.lang.String separator)
Get the args belonging to this entity with their c type.

Parameters:
actor - The GenericJNIActor object.
isInput - True if we are searching for input arguments.
isOutput - True if we are searching for output arguments.
isReturn - True if we are searchin for return arguments.
separator - The separator used between multiple arguments.
Returns:
the c type and name of each in arguments.

_getArgumentsWithCTypeCast

protected static java.lang.String _getArgumentsWithCTypeCast(GenericJNIActor actor,
 boolean isInput,
 boolean isOutput,
 boolean isReturn,
 java.lang.String separator)
Get the arguments belonging to this entity with their c type.

Parameters:
actor - The GenericJNIActor object.
isInput - True if we are searching for input arguments.
isOutput - True if we are searching for output arguments.
isReturn - True if we are searchin for return arguments.
separator - The separator used between multiple arguments.
Returns:
the c type and name of each in arguments.

_getArgumentsWithJNIType

protected static java.lang.String _getArgumentsWithJNIType(GenericJNIActor actor,
 boolean isInput,
 boolean isOutput,
 boolean isReturn,
 java.lang.String separator)
Get the arguments In belonging to this entity with their JNI type, excluding the out arguments.

Parameters:
actor - The GenericJNIActor object.
isInput - True if we are searching for input arguments.
isOutput - True if we are searching for output arguments.
isReturn - True if we are searchin for return arguments.
separator - The separator used between multiple arguments.
Returns:
the JNI type and name of each in arguments.

_getArgumentsWithJType

protected static java.lang.String _getArgumentsWithJType(GenericJNIActor actor,
 boolean isInput,
 boolean isOutput,
 boolean isReturn,
 java.lang.String separator)
Get the arguments belonging to this entity with their java type.

Parameters:
actor - The GenericJNIActor object.
isInput - True if we are searching for input arguments.
isOutput - True if we are searching for output arguments.
isReturn - True if we are searchin for return arguments.
separator - The separator used between multiple arguments.
Returns:
the java type and name of each in arguments

_signature

protected static java.lang.String _signature(java.lang.String typ)
Return the signature of the interface function.

Parameters:
typ - The interface function declaration.
Returns:
the Java JNI signature of the interface function.

_signatureSendResults

protected static java.lang.String _signatureSendResults(GenericJNIActor actor)
Return the signature of the interface function.

Parameters:
actor - The GenericJNIActor object.
Returns:
the signature of the interface function.

Overview Package Class Tree Deprecated Index Help
PREV CLASS NEXT CLASS FRAMES NO FRAMES All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD

AltStyle によって変換されたページ (->オリジナル) /