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

lphy.base.function.ArgI 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.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;

import java.util.TreeMap;

public class ArgI extends DeterministicFunction {

    private static final String nameParamName = "name";
    private static final String defaultParamName = "default";
    private Value name;
    private Value defaultValue;

    private static java.util.Map integerArguments = new TreeMap<>();


    public ArgI(@ParameterInfo(name = nameParamName, description = "the name of the integer argument.") Value name,
                @ParameterInfo(name = defaultParamName, description = "the default value.", optional = true) Value defaultValue) {

        this.name = name;
        if (name == null) throw new IllegalArgumentException("The mean value can't be null!");
        this.defaultValue = defaultValue;
    }

    @GeneratorInfo(name = "argi", description = "The arg function for reading an integer.")
    public Value apply() {
        Integer value = integerArguments.get(name.value());

        if (value != null) {
            return new Value<>(value, this);
        }

        return defaultValue;
    }

    public java.util.Map getParams() {
        return new TreeMap<>() {{
            put(nameParamName, name);
            put(defaultParamName, defaultValue);
        }};
    }

    public void setParam(String paramName, Value value) {
        if (paramName.equals(nameParamName)) name = value;
        else if (paramName.equals(defaultParamName)) defaultValue = value;
        else throw new RuntimeException("Unrecognised parameter name: " + paramName);
    }

    public static void putArgument(String name, int value) {
        integerArguments.put(name, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy