TIP: The JWAVE wrapper API functions are described in Appendix A, JWAVE Wrapper API.
Example 5-1 Simple JWAVE wrapper function
FUNCTION PASSARR, client_data ; Unpack parameters received from the client. arr = GETPARAM(client_data, 'ARRAY', /Value) ; Change the array. mydata = arr * 1.5 ; Return the changed array. RETURN, mydata END
The Input Parameter: client_data
client_data
. The
client_data
parameter passes parameter name(s) and data that were "packaged" by the client Java program. (JWAVE client developers use the setParam
method of the JWaveExecute class to "package" the parameters.) When the Java client calls the JWaveExecute.execute
method, the packaged parameters are automatically sent in an array to the appropriate JWAVE wrapper function. (The Java client application also specifies the name of the wrapper function it intends to execute.)
The
client_data
parameter. In Example 5-1, GETPARAM happens to retrieve an array. The GETPARAM arguments include:
client_data
The parameter information that was passed to the wrapper function from the client.
The name of the parameter to unpack. This name was assigned in the client Java program at the time the parameter was "packaged" (with the
setParam
method). The Value keyword tells GETPARAM to retrieve just the value of the parameter that was sent. (See the following note.)
NOTE: In other situations, it is necessary for GETPARAM to return more than the value. For instance, in many cases, the JWAVE wrapper function will be used to execute one or more PV-WAVE functions. Typically, this is accomplished with the PV-WAVE EXECUTE command, and parameters from the client that were unpacked by GETPARAM are used to "build" a string containing the command for EXECUTE. To facilitate these cases, GETPARAM can return a string that is formatted appropriately. For example, if the Positional keyword were used instead of Value, GETPARAM would return a string of the form:",
param_reference"
(where param_reference is a symbolic reference to a value). The result could then be used directly in an EXECUTE statement. For example:
x1=GETPARAM(client_data, 'ARRAY', /Positional) status=EXECUTE("PLOT"+ x1)
The
DATA
. See Returning Multiple Results to the Client for information on sending multiple results back to the client.
.cpr
files using the PV-WAVE COMPILE procedure.
For example, a typical series of commands that you can use to compile a single PV-WAVE routine is:
WAVEDELPROC, /All WAVE
DELFUNC, /All WAVE
.RUN mywrapper.pro WAVE
COMPILE, /All, File='mywrapper.cpr'
TIP: We have provided a set of routines that you can use to test your JWAVE wrapper functions before compiling them and publishing them on your Web server. See Testing Wrapper Functions for more information.
For instance, if your JWAVE client application displays a 2D plot, you might send the following parameter information to the server:
X
TITLE
LINESTYLE
setParam
methods used on the client to set these parameters might look like this:
setParam("X", anarray) setParam("TITLE", "Peak Concentrations") setParam("LINESTYLE", 1)
execute
method, the JWAVE wrapper on the server receives this information through its input argument, usually called client_data
. You must decide how to unpack this data. In this case, the goal is to execute a PV-WAVE PLOT command with the parameters that were received from the client. For example, you might want to run a PV-WAVE PLOT command with these parameters and keywords:
PLOT, X, Title=thetitle, Linestyle=thestyle
x_val = GETPARAM( client_data, 'X', /Value )
NOTE:client_data
is the single parameter passed to a JWAVE wrapper function, as in:FUNCTION MY_WRAPPER, client_data
X
parameter (set by a client's setParam
call) is assigned to the PV-WAVE variable x_val
. Then, x_val
can be used in any valid context. For example:
PLOT, SQRT( x_val )
Using the Default Keyword
For example, if the client sets parameters as follows:
setParam("X", anarray) setParam("TITLE", "Peak Concentrations") setParam("LINESTYLE", 1)
x_val = GETPARAM
( client_data, 'X', /Value, Default=FINDGEN(10) )
the_title = GETPARAM( client_data, 'TITLE', /Value, Default='' )
lstyle = GETPARAM( client_data, 'LINESTYLE', /Value, Default=0 )
PLOT, x_val, Title=the_title, Linestyle=lstyle
X
, then the default dataset FINDGEN(10)
is used. The default plot title is an empty string, and the default linestyle is 0 (solid).
The Expect* Keywords
execute
method throws an exception.) The Expect* keyword options are:
/ExpectScalar
/ExpectArray
or ExpectArray
= array_of_dimensions
ExpectType
= wave_type_code
/ExpectNumeric
/ExpectString
For examples that use the Expect* keywords, see Using the Expect Keywords.
There are two distinct kinds of parameters in PV-WAVE: positional and keyword. Thus there are two methods used to get these strings from GETPARAM. Positional and keyword parameters will be discussed in more detail in the next section, but as an example, the JWAVE wrapper code we saw in the previous section might be re-written as follows:
cmd = 'PLOT' cmd = cmd + GETPARAM( client_data, 'X', /Positional ) cmd = cmd + GETPARAM( client_data, [ 'TITLE', 'LINESTYLE' ] ) status = EXECUTE( cmd )
X
is used as a positional parameter, and LINESTYLE
and TITLE
are used as keywords. The resulting string cmd
might look something like this:
"PLOT, x_reference, TITLE=title_reference, LINESTYLE=linestyle_reference"
NOTE: GETPARAM does not extract actual data fromclient_data
; rather, it extracts a symbolic reference to data. The data that is referenced might have been sent by the client or retrieved from memory on the server. Therefore, the command:
x = GETPARAM(client_data, 'X', /Positional)
Positional vs. Keyword Parameters
X
, and two keyword parameters, Title
and Linestyle
. In PV-WAVE, positional parameters are of the form:
",
param"
",
KeywordName=
Value"
The way in which you unpack parameters in the JWAVE wrapper depends on whether you are unpacking a positional or a keyword parameter.
Unpacking Positional Parameters
The first parameter to unpack is the data parameter
X
. Note that X
is a positional parameter in the PV-WAVE PLOT command. To unpack a positional parameter, use the GETPARAM function with the Positional keyword, as follows:
param1=GETPARAM(client_data, 'X', /Positional)
X
is an array, this function might return the following string in the param1
variable.
" ,
array_reference "
As noted before, this returned string is in the expected form of a positional parameter in a PV-WAVE function:
" ,
param"
ret=EXECUTE('PLOT' + param1)
Unpacking Keyword Parameters
", KeywordName=
keyword_reference"
For example, to unpack the Title keyword, use the following GETPARAM call:
title=GETPARAM(client_data, 'TITLE')
title
variable:
" , TITLE=
title_reference "
NOTE: Again, remember that GETPARAM does not extract and return an actual value fromclient_data
. Rather, the function builds a symbolic reference to a value into the string.
For example:
command= new JWaveView(connection, "JWAVE_PLOT") int[] data = {1,2,3,4,5}; command.setParam("X", data); command.setParam("TITLE", "CO2 Data"); command.setParam("LINESTYLE", 1);
result=GETPARAM(client_data, 'X', /Positional)
keywords=GETPARAM(client_data, ['TITLE', 'LINESTYLE']
)
status=EXECUTE("PLOT" +result + keywords)
To package multiple results on the server, create and return an associative array of data name/value pairs. For example:
Example 5-2 JWAVE wrapper function that returns multiple results to the client
FUNCTION PASSFFT, client_data ; Get the data from the client. arr = GETPARAM(client_data, 'ARRAY', /Value) ;Process the data, creating two separate variable results freq = FLOAT(ABS(FFT(arr, -1))) distrib = HISTOGRAM(freq) ;Create an associative array to send results back to the client. RETURN, ASARR('FDATA', freq, 'HIST', distrib) END
freq
and distrib
are packaged are returned to the client in the associative array. The associative array keys become the names of the returned data referenced by the client with the getReturnData
method. The names of the data returned by this example are FDATA
and HIST
. The following example shows a Java code fragment used to retrieve the parameters sent from the server and to properly cast the values.
Example 5-3 Client calls to unpack the associative array sent from the server.
//Get the FFT data float[] d = (float[]) command.getReturnData("F
DATA"); //Get the histogram data int[] h = (int[]) command.getReturnData("
HIST");
JWaveView class to request that the server return graphical data in addition to numerical data. Whenever JWaveView is used to execute a client request to the server, the server automatically creates a
NOTE: The client developer uses the
Viewable
object. This object is then packaged and streamed back to the client where it can be displayed. If the client calls the wrapper with the JWaveExecute class, the graphics are discarded, and only the data are returned. For more information on JWaveView and graphics, see Chapter 4, JWAVE Graphics. Example 5-4 Simple JWAVE wrapper that returns a 2D plot
FUNCTION APLOT, client_data ; Unpack the parameters and data from the client. arr = GETPARAM(client_data, 'ARRAY', /Value) PLOT, arr RETURN, 0 END
NOTE: You must make sure that all coordinate system information is correct before you return a plot to the client. Also, note that the SURFACE and AXIS procedures create a temporary axis transformation that is not automatically saved by the PV-WAVE session. To ensure that the correct transformation and coordinate system information is sent back to the client, use the Save keyword with these procedures. This causes the correct transformation information to be sent automatically to the client. For more information on coordinate transformations, see Coordinate System Transformations.
This JWAVE wrapper function retrieves positional and keyword parameters from the client, unpacks the parameters, and builds a command string for use in a PV-WAVE EXECUTE function. This example also demonstrates the GET_NAMED_COLOR function, which allows colors to be passed by name from the client to the JWAVE wrapper.
TIP: Refer to the PV-WAVE Reference for information on the EXECUTE, PLOT, and OPLOT commands. These PV-WAVE commands are used in the following example.
jwave_plot.pro
, which can be found in:
jwave-3_0/lib
jwave-3_0\lib
Example 5-5 JWAVE wrapper that unpacks positional and keyword parameters and builds a command string
FUNCTION JWAVE_PLOT_SIMPLE, client_data ; Determine plot type (Can also be done with [XY]Type) CASE GETPARAM(client_data, 'SCALING', /Value, Default=0) OF 1: cmd = 'PLOT_OI' 2: cmd = 'PLOT_IO' 3: cmd = 'PLOT_OO' ELSE: cmd = 'PLOT' ENDCASE ; Get colors -- default is black lines on white background back = GET_NAMED_COLOR('BACKGROUND', Default='000000'xL) fore = GET_NAMED_COLOR('COLOR', Default = 'ffffff'xL) acol = GET_NAMED_COLOR('AXIS', Default = fore) color_kwds = ', Background=back, Color=fore' ; Get allowed keywords for the PLOT command plot_kwds = GETPARAM(client_data, $ [ 'Box', 'Charsize', 'Charthick', 'Clip', 'Gridstyle', $ 'Linestyle', 'Noclip', 'Nsum', 'Polar', 'Position', 'Psym', $ 'Solid_Psym', 'Subtitle', 'Symsize', 'Thick', 'Tickformat', $ 'Ticklen', 'Title', 'XCharsize', 'XGridstyle', 'XMargin', $ 'XMinor', 'XRange', 'XStyle', 'XTickformat', 'XTicklen', $ 'XTickname', 'XTicks', 'XTickv', 'XTitle', 'XType', $ 'YCharsize', 'YGridstyle', 'YMargin', 'YMinor', $ 'YNozero', 'YRange', 'YStyle', 'YTickformat', $ 'YTicklen', 'YTickname', 'YTicks', 'YTickv', $ 'YTitle', 'YType' ] ) ; Get positional data y = GETPARAM(client_data, 'Y', /Positional) ; REQUIRED IF y EQ '' THEN $ MESSAGE, 'Parameter Y is required.' x = GETPARAM(client_data, 'X', /Positional) ; Optional ; Execute the plotting function to draw the axes status = EXECUTE( cmd + x + y + color_kwds + plot_kwds ) RETURN, 0 END
Parameter names are not case sensitive. They must begin with a letter, and can contain only alphanumeric characters and the underscore character (_).
NOTE:
NOTE: A client application that provides controls for generating and modifying the appearance of plots or other kinds of graphics probably needs to communicate positional and keyword parameter information to the server. The client user might use option menus to change the colors used in a plot, text fields to add plot titles, push buttons to add axes, and so on. The client developer must retrieve the parameter names and values from the user interface, package those parameters (withsetParam
andsetNamedColor
method calls), and send them to the server. As shown here, the JWAVE wrapper function then unpacks the parameters, generates the plot, and sends back a graphic for display on the client.
Unpacking Values
SCALING
parameter that was passed from the client. As in previous examples, the Value keyword specifies that a simple value be returned. Also, the Default keyword is used to return a value, 0, if no SCALING
parameter was sent from the client. In this case, no scaling causes the CASE statement to save the regular PLOT command.
CASE GETPARAM(client_data, 'SCALING', /Value, Default=0) OF 1: cmd = 'PLOT_OI' 2: cmd = 'PLOT_IO' 3: cmd = 'PLOT_OO' ELSE: cmd = 'PLOT' ENDCASE
Unpacking Keywords
plot_kwds = GETPARAM(client_data, $ [ 'Box', 'Charsize', 'Charthick', 'Clip', 'Gridstyle', $ 'Linestyle', 'Noclip', 'Nsum', 'Polar', 'Position', 'Psym', $ 'Solid_Psym', 'Subtitle', 'Symsize', 'Thick', 'Tickformat', $ 'Ticklen', 'Title', 'XCharsize', 'XGridstyle', 'XMargin', $ 'XMinor', 'XRange', 'XStyle', 'XTickformat', 'XTicklen', $ 'XTickname', 'XTicks', 'XTickv', 'XTitle', 'XType', $ 'YCharsize', 'YGridstyle', 'YMargin', 'YMinor', $ 'YNozero', 'YRange', 'YStyle', 'YTickformat', $ 'YTicklen', 'YTickname', 'YTicks', 'YTickv', $ 'YTitle', 'YType' ] )
client_data
any keywords that were specified in the string array and their associated values. With this usage, the GETPARAM function returns a string array in the following form:
", keyname_1=value_reference, keyname_2=value_reference, ... "
For example, the client sends keyword/value pairs that the PLOT command expects, and GETPARAM unpacks them into a string array, containing keyword names and references to keyword values.
" , Title=title_ref, Ticklen=tick_ref, Charsize=charsize_ref "
NOTE: The list of keywords given in this GETPARAM function example represents all of the keywords that can be extracted. Any keywords in the list that are not sent by the client are simply ignored by GETPARAM. If no keywords are sent, this GETPARAM function would return a null (empty) string.
TIP: We recommend that you use a string array (as was done in this example) to specify which keywords you wish to retrieve with GETPARAM. By specifying a string of names in GETPARAM, rather than using the /All keyword, you prevent your program from failing if the client sends unexpected information.
Unpacking Positional Parameters
y = GETPARAM(client_data, 'Y', /Positional) ; REQUIRED IF y EQ '' THEN $ MESSAGE, 'Parameter Y is required.' x = GETPARAM(client_data, 'X', /Positional)
XTitle="Units Sold"
. Positional parameters, on the other hand, of the form: paramname. For example, the PLOT command takes a required positional parameter (Y
), an optional positional parameter (X
), and numerous optional keywords. For example:
PLOT, y, Title="CO2 Content", Charsize=3
When the Positional keyword is specified, GETPARAM returns the specified parameter in a string in the format: "
,
param_reference
", where param_reference is a symbolic reference to the data. The leading comma is needed because when this string is used in an EXECUTE command, it is concatenated with other strings to "build" a command. If the required y parameter is not supplied by the client, this error is "trapped" (because GETPARAM returns an empty string) and the MESSAGE procedure is called. The effect of the MESSAGE procedure is discussed in Error Handling.
Color
object. This object is sent to the JWAVE wrapper and GET_NAMED_COLOR translates that color object into a color that can be used by PV-WAVE. In this example, GET_NAMED_COLOR retrieves three colors that were specified by the client:
BACKGROUND
. COLOR
, and AXIS
.
TIP: For more information on JWAVE graphics and color parameters, see Chapter 4, JWAVE Graphics. See also Managing the Color Table on page A-18.
Example 5-6 JWAVE wrapper calls retrieve colors sent from the client.
back = GET_NAMED_COLOR('BACKGROUND', Default='000000'xL) fore = GET_NAMED_COLOR('COLOR', Default = 'ffffff'xL) axis = GET_NAMED_COLOR('AXIS', Default = fore)
TIP: The following lines show the corresponding calls that were made in the Java client program to set the colors retrieved in Example 5-6:
myJWaveView.setNamedColor("BACKGROUND", java.awt.Color.lightGray) myJWaveView.setNamedColor("COLOR", java.awt.Color.red) myJWaveView.setNamedColor("AXIS", java.awt.Color.black)
BACKGROUND
) with a Java color object (e.g., Color.lightGray
). These colors are packaged with the rest of the parameters and sent to the client.
For example, the following GETPARAM calls retrieve parameters from the client:
foo = GETPARAM(client_data, 'foo') bar = GETPARAM(client_data, /All)
foo
parameter is retrieved, it cannot be retrieved again in the subsequent call with the /All keyword. Therefore, in this case, the bar
command string will not contain the foo
parameter.
TIP: You can use the Ignore_Used keyword with GETPARAM to request that all requested parameters will be returned whether they have been used or not.
JWaveWrapperException
object). The MESSAGE procedure also causes the wrapper function to stop processing (by default). For detailed information on MESSAGE, refer to the PV-WAVE Reference.
ON_ERROR and ON_ERROR_GOTO are described in the PV-WAVE Reference.
These keywords all begin with the word Expect. They are:
Type Code | Data Type |
---|---|
1 | Byte |
2 | Integer |
3 | Longword integer |
4 | Floating point |
5 | Double precision floating |
7 | String |
The keyword ExpectScalar produces an error if the input parameter is not a scalar or a 1-element array. If the parameter is a 1-element array, then the function returns that element as a scalar.
; y must be a numerical array y = GETPARAM(client_data, 'Y', /Value, /ExpectArray, /ExpectNumeric ) ; xcnt and ycnt must be numerical scalars xcnt = GETPARAM(client_data, 'XCENTER', /Value, Default = 0.5, $ /ExpectScalar, /ExpectNumeric ) ycnt = GETPARAM(client_data, 'YCENTER', /Value, Default = 0.5, $ /ExpectScalar, /ExpectNumeric ) ; title must be a scalar string. title = GETPARAM(client_data, 'TITLE', /Value, Default = 'The Title', $ /ExpectScalar, /ExpectString)
simple.pro
that we discussed in Chapter 1, JWAVE System Introduction. This wrapper accepts a number and returns the square root of the number to the Java client. To test this wrapper without writing the Java client application yet, you can run the following test procedures in PV-WAVE (assuming that the directory that contains simple.pro
is in the !Path system variable of PV-WAVE):
WAVEWRAPPER_TEST_INIT, 'SIMPLE' WAVE
WRAPPER_TEST_SETPARAM, 'NUMBER', 2 WAVE
WRAPPER_TEST_EXECUTE WAVE
WRAPPER_TEST_RETURN_INFO DATA FLOAT = 1.41421 WAVE
![]()
The procedures used to test this wrapper correspond to the Java methods used to set parameters, and so on, in the client application. The following table shows the correspondence between these PV-WAVE test routines and the Java methods they imitate:
Here is an example that tests the wrapper
jwave_plot.pro
. You can find this wrapper function in:
VNI_DIR/jwave-3_0/lib
VNI_DIR\jwave-3_0\lib
VNI_DIR
is the main Visual Numerics installation directory.
WAVEWRAPPER_TEST_INIT, 'JWAVE_PLOT', 300, 300 WAVE
WRAPPER_TEST_SETCOLOR, 'BACKGROUND', '000000'xL ; black WAVE
WRAPPER_TEST_SETCOLOR, 'LINE', 'ff0000'xL ; blue WAVE
WRAPPER_TEST_SETPARAM, 'Y', HANNING(20,20) WAVE
WRAPPER_TEST_SETPARAM, 'PSYM', 1 WAVE
WRAPPER_TEST_EXECUTE
For detailed information on each of the WRAPPER_TEST_* routines, see Appendix A, JWAVE Wrapper API.
You can use standard PV-WAVE error trapping routines to trap errors in a JWAVE wrapper function. If you trap and handle errors within the wrapper function, be sure to reset the !Error system variable to 0 after the error is trapped to prevent an error message from being returned to the client.
Remember that JWAVE wrapper functions, and any PV-WAVE functions that run on the JWAVE server, must be compiled with the PV-WAVE COMPILE command.