Evaluate CTL expressions in java

Hi,

I am trying to evaluate expressions in java using CTLExpressionEvaluator

CTLExpressionEvaluator evaluator = new CTLExpressionEvaluator();
			try {
				evaluator.evaluate("iif ($in.0.quantity < 0) { $out.0.quantity = 0 }");
			} catch (ParseException e) {
				e.printStackTrace();
			}

it says ‘iif’ is not defined. Can anybody give me some examples to evaluate CTL expressions in java class.

Thank you,

The signature of IFF() function is different than what you are trying to use - see IFF function docs.

Your usage suggest you might want to use normal IF construct, but then it is not an expression, therefore expression evaluator can’t be used.

For more info, look at test.org.jetel.ctl - available as part of CloverETL open source distribution (on sf.net). You will find some good examples there.

Thank you for quick response.

I have downloaded “cloverETL.examples.rel-4-0-0-M1” from sourceforge.net. But I do not see any package “test.org.jetel.ctl” .
Can you please provide link or attach examples here.

yeah I see some test*.ctl files.

What I am looking for is,

public class CustomTransform extends DataRecordTransform {

@Override
public int transform(DataRecord[] sourceRecords, DataRecord[] targetRecords) throws TransformException {
       custom logic.. 
      //here I need to update/calculate field values using available CTL functions, statements etc.,
}
}

So I need an API which can take CTL code like

"if ($in.0.quantity < 0) { $out.0.quantity = 0 }"

and execute it.
CTLExpressionEvaluator is not helping me in the above case.

You have to look inside cloverETL.src.rel-X.zip package - that contains the source code together with JUnit tests. The mentioned package is there. The Interpreter JUnit test contains something quite similar to what you are probably looking for.

Also, source code of component like Reformat (also in that package) is calling CTL interpreter - CTL variant of the “transform” method you listed. Look for Reformat.java in cloveretl.component project.

These are the links to GITHub for reference:
org.jetel.ctl.InterpreterTest
org.jetel.component.Reformat , also look for org.jetel.component.TransformFactory.