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

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

/**
 * @author Walter Xie
 */
public class Get extends DeterministicFunction {

    public static final String KEY = "key";
    public static final String MAP = "map";

    public Get(@ParameterInfo(name = KEY, description = "the key of the map as String") Value keyVal,
               @ParameterInfo(name = MAP, description = "the map") Value> mapVal) {
        setParam(KEY, keyVal);
        setParam(MAP, mapVal);
    }

    @GeneratorInfo(name="get", verbClause = "is the value from",
            description = "Get the value from a map given a string ID as the key.")
    public Value apply() {
        String key = getKey().value();
        java.util.Map map = getMap().value();
        if (!map.containsKey(key))
            LoggerUtils.log.severe("The map (" + getMap().getId() + ") does not contain the key (" +
                    key + "), all keys = " + map.keySet());

        V val = map.get(key);
        if (val instanceof Value value)
            return new Value<>(value.getId(), (V) value.value(), this);
        return new Value<>(null, val, this);
    }

    public Value getKey() {
        return (Value)paramMap.get(KEY);
    }

    public Value> getMap() {
        return (Value>) paramMap.get(MAP);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy