Java Snippet

I have tried the below code in Java snippet, but it didnt really worked. it gives result as final else statement i gave but not really picking other rules i gave before, can someone help me? what i am doing wrong? knime is not showing any errors but answers are wrong.

// system imports
import org.knime.base.node.jsnippet.expression.AbstractJSnippet;
import org.knime.base.node.jsnippet.expression.Abort;
import org.knime.base.node.jsnippet.expression.Cell;
import org.knime.base.node.jsnippet.expression.ColumnException;
import org.knime.base.node.jsnippet.expression.TypeException;
import static org.knime.base.node.jsnippet.expression.Type.*;
import java.util.Date;
import java.util.Calendar;
import org.w3c.dom.Document;

// Your custom imports:

// system variables
public class JSnippet extends AbstractJSnippet {
// Fields for input columns
/** Input column: “Cust Mapping” /
public String c_CustMapping;
* Input column: “Customer Type Name” /
public String c_CustomerTypeName;
* Input column: “Customer Type” /
public String c_CustomerType;
* Input column: “Source System” */
public String c_SourceSystem;

// Fields for output columns
/** Output column: “Cust Type” */
public String out_CustType;

// Your custom variables:

// expression start
public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:

if (c_CustMapping == “Inside Fire”) {out_CustType = “Inside Fire”;
} else if (c_CustomerTypeName == “Internal”) {out_CustType = “Outside Fire”;
} else if (c_CustomerTypeName == “External”) {out_CustType = “External”;
} else if (c_CustomerType == “EXTERNAL” ) {out_CustType = “External”;
} else if (c_SourceSystem ==“SAP”) {out_CustType = “Outside Fire”;
} else {out_CustType = “Outside Fire”;

// expression end


1 Like

btw.: I’d probably not do that as a Java Snippet. The Rule Engine might be more appropriate:

1 Like

It might be good to capture a possible null in the first line

if (c_CustMapping == null) {out_CustType = "_NA_";}
else if (c_CustomerTypeName.equals("Internal")) {out_CustType = "Outside Fire"; }

And in this case strangely the == is the better fit.

1 Like

Can you tell me how to write the same example in rule engine?

that works with Rule engine.

when i tried similar one using rule engine it does not even show the options to choose.

if (c_ProcType1==“Factored”) {out_RetwSO = (c_W2WMargin*0.8);
} else {out_RetwSO = 0.0;}

below is the example am trying