//Note 2. If the input was of a type for which there was no special typed policy
//we could use InputPolicy.makeTypedDefaultInputPolicy to create one, e.g.
//InputPolicy.makeTypedDefaultInputPolicy(SourceModel.TypeExprDefn.TypeCons.make(CAL_Prelude.TypeConstructors.Int))
EntryPointSpec addSpec = EntryPointSpec.make(QualifiedName.make(CAL_Prelude.MODULE_NAME, "add"), new InputPolicy[] {
InputPolicy.INT_INPUT_POLICY,
InputPolicy.DEFAULT_INPUT_POLICY
}, OutputPolicy.DEFAULT_OUTPUT_POLICY);
Object addResult = calServices.runFunction(addSpec, new Object[] { new Integer(1), new Integer(2) });
System.out.println("Example 1 output: " + addResult.toString());
}
{
//Example 2: Inferring input types
//This example again shows how the Prelude.add function
//can be invoked. We provide a type (Int) for the output. This will
//allow CAL to infer the types of the inputs, so default input
//policies can be used.
EntryPointSpec addSpec = EntryPointSpec.make(QualifiedName.make(CAL_Prelude.MODULE_NAME, "add"), new InputPolicy[] {
InputPolicy.DEFAULT_INPUT_POLICY,
InputPolicy.DEFAULT_INPUT_POLICY
}, OutputPolicy.INT_OUTPUT_POLICY);
Object addResult = calServices.runFunction(addSpec, new Object[] { new Integer(3), new Integer(4) });
System.out.println("Example 2 output: " + addResult.toString());
}
{
//Example 3: CAL Value Output
//This example shows the use of CAL_VALUE_OUTPUT to output a CAL value as an opaque type in Java.
//the opaque output is then used as an input to another CAL function.
//Call the allPrimes and get the resulting infinite list as an opaque CAL Value
EntryPointSpec allPrimesSpec = EntryPointSpec.make(QualifiedName.make(CALPlatformTestModuleNames.M1, "allPrimes"), new InputPolicy[0], OutputPolicy.CAL_VALUE_OUTPUT_POLICY);
Object allPrimesResult = calServices.runFunction(allPrimesSpec, new Object[0]);
//Use List.Take to get 5 values from the primes list.
//As List.take is a polymorphic function we must specify the type of the CAL input
EntryPointSpec primesSpec = EntryPointSpec.make(QualifiedName.make(CAL_List.MODULE_NAME, "take"), new InputPolicy[] {
InputPolicy.DEFAULT_INPUT_POLICY,
InputPolicy.makeTypedCalValueInputPolicy(SourceModel.TypeExprDefn.List.make(SourceModel.TypeExprDefn.TypeCons.make(CAL_Prelude.TypeConstructors.Int)))}, OutputPolicy.DEFAULT_OUTPUT_POLICY);
List<?> primesList = (List<?>) calServices.runFunction(primesSpec, new Object[] { new Integer(5), allPrimesResult});