Initializing help system before first use

Passing an array from a Mosel model to a Java method


Description: Demonstrates constructing an array of reals in a Mosel model, then passing this to a Java method which returns the sum of the values, and finally displaying the returned sum from Mosel. You must compile the Java file before running the example - e.g. javac PassArray.java
File(s): passarray.mos, PassArray.java


passarray.mos
(!*******************************************************
   file passarray.mos
   ``````````````````
   a model that demonstrates passing an array from Mosel to a Java method

   This example requires an installation of Java, see
   the manual 'mosjvm Module for Mosel' for
   compatible versions and setup instructions.
   
   The compiled PassArray.class file must be in the current working
   directory.

   (c) 2016 Fair Isaac Corporation
       author: J. Farmer, 2016
  *******************************************************!)


model myModel
  uses 'mosjvm'
  
  declarations
    classAndMethodName = "PassArray.sumValues"
    result: real
    values: dynamic array(0..9) of real
  end-declarations
 
  ! Set to initial array elements
  values(0) := 1
  values(1) := 5
  values(2) := 10
  values(7) := 10
 
  ! Abort model if we encounter a Java exception
  setparam('jvmabortonexception',true)

  ! Tell Java to look for classes in work directory
  setparam('jvmclasspath',getparam('workdir'))

  ! Call into Java to sum values
  ! (missing values in the array will be passed to Java as 0)
  result := jvmcallreal( classAndMethodName, values )
  
  ! Output result
  writeln('Sum of values: ',result)
end-model

PassArray.java
/**
 * Example Java class, containing a method that returns a value based on an array passed to it
 *
 * @author  J.Farmer, (c) Fair Isaac Corporation, 2016
 **/
public class PassArray {
	public static double sumValues(double[] values) {
		double sum = 0;
		for (int i=0;i<values.length;i++) {
			sum += values[i];
		}
		return sum;
	}
}

© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.