All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lphy.base.function.IfElse Maven / Gradle / Ivy

Go to download

The standard library of LPhy, which contains the required generative distributions and basic functions.

The newest version!
package lphy.base.function;

import lphy.base.ParameterNames;
import lphy.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;

public class IfElse extends DeterministicFunction {

//    public static final String CONDITION = "cond";
//    public static final String IF_TRUE = "true";
//    public static final String ELSE = "false";

    public IfElse(@ParameterInfo(name = ParameterNames.NoParamName0, description = "the logical condition to determine which value to return")
               Value logicVal,
               @ParameterInfo(name = ParameterNames.NoParamName1, description = "the value to return if the condition is true")
               Value trueVal,
               @ParameterInfo(name = ParameterNames.NoParamName2, description = "the value to return if the condition is false")
               Value falseVal) {
        setInput(ParameterNames.NoParamName0, logicVal);
        setInput(ParameterNames.NoParamName1, trueVal);
        setInput(ParameterNames.NoParamName2, falseVal);
    }

    @GeneratorInfo(name="ifelse", verbClause = "checks",
            description = "Return the 1st value if the condition is true, else return the 2nd value.")
    public Value apply() {
        Value booleanValue = (Value)getParams().get(ParameterNames.NoParamName0);

        if (booleanValue.value()) {
            Value value1 = (Value)getParams().get(ParameterNames.NoParamName1);
            return new Value<>(value1.getId(), value1.value(), this);
        } else {
            Value value2 = (Value)getParams().get(ParameterNames.NoParamName2);
            return new Value<>(value2.getId(), value2.value(), this);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy