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

org.btrplace.scheduler.choco.duration.ActionDurationFromOptionalAttribute Maven / Gradle / Ivy

/*
 * Copyright  2020 The BtrPlace Authors. All rights reserved.
 * Use of this source code is governed by a LGPL-style
 * license that can be found in the LICENSE.txt file.
 */

package org.btrplace.scheduler.choco.duration;

import org.btrplace.model.Attributes;
import org.btrplace.model.Element;
import org.btrplace.model.Model;

/**
 * A duration evaluator that try to get a duration from an attribute
 * if exists. Otherwise, it relies on a "parent" evaluator.
 * The retrieved attribute value must be an {@link Integer} object.
 *
 * @author Fabien Hermenier
 */
public class ActionDurationFromOptionalAttribute implements ActionDurationEvaluator {

    private ActionDurationEvaluator parent;

  private final String key;

    /**
     * Make a new evaluator.
     *
     * @param attrId the attribute identifier. The associated value must be an {@link Integer}.
     * @param dev    the evaluator to rely on if the attribute is not set or invalid
     */
    public ActionDurationFromOptionalAttribute(String attrId, ActionDurationEvaluator dev) {
        parent = dev;
        key = attrId;
    }

    @Override
    public int evaluate(Model mo, E e) {
        Attributes attrs = mo.getAttributes();
        if (attrs.isSet(e, key)) {
            return attrs.get(e, key, -1);
        }
        return parent.evaluate(mo, e);
    }

    /**
     * Get the parent evaluator to use when
     * the attribute is not set.
     *
     * @return an evaluator.
     */
    public ActionDurationEvaluator getParent() {
        return parent;
    }

    /**
     * Set the parent evaluator.
     *
     * @param dev the evaluator to use
     */
    public void setParent(ActionDurationEvaluator dev) {
        parent = dev;
    }

    /**
     * Get the attribute identifier.
     *
     * @return a String
     */
    public String getAttributeKey() {
        return key;
    }

    @Override
    public String toString() {
        return "d= attr[" + key + "]? attr[" + key + "]:" + parent;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy