Search in sources :

Example 6 with BtrpOperand

use of org.btrplace.btrpsl.element.BtrpOperand in project scheduler by btrplace.

the class DefaultSatConstraintBuilder method checkConformance.

/**
 * Check if the provided parameters match the constraint signature
 *
 * @param t   the constraint token
 * @param ops the constraint arguments
 * @return {@code true} iff the arguments match the constraint signature
 */
public boolean checkConformance(BtrPlaceTree t, List<BtrpOperand> ops) {
    // Arity error
    if (ops.size() != getParameters().length) {
        t.ignoreError("'" + pretty(ops) + "' cannot be casted to '" + getSignature() + "'");
        return false;
    }
    // Type checking
    for (int i = 0; i < ops.size(); i++) {
        BtrpOperand o = ops.get(i);
        ConstraintParam<?> p = params[i];
        if (o == IgnorableOperand.getInstance()) {
            return false;
        }
        if (!p.isCompatibleWith(t, o)) {
            t.ignoreError("'" + pretty(ops) + "' cannot be casted to '" + getSignature() + "'");
            return false;
        }
    }
    return true;
}
Also used : DefaultBtrpOperand(org.btrplace.btrpsl.element.DefaultBtrpOperand) BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand)

Example 7 with BtrpOperand

use of org.btrplace.btrpsl.element.BtrpOperand in project scheduler by btrplace.

the class AssignmentStatement method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    // We evaluate right operand
    try {
        BtrpOperand res = getChild(1).go(this);
        if (res == IgnorableOperand.getInstance()) {
            // We declare the variable to reduce the number of errors
            symbols.declare(getChild(0).getText(), res);
            return res;
        }
        if (getChild(0).getType() == ANTLRBtrplaceSL2Parser.VARIABLE) {
            return declareVariable(getChild(0).getText(), res);
        } else if (getChild(0).getType() == ANTLRBtrplaceSL2Parser.EXPLODED_SET) {
            List<BtrpOperand> vals = ((BtrpSet) res).getValues();
            BtrPlaceTree t = getChild(0);
            for (int i = 0; i < t.getChildCount(); i++) {
                switch(t.getChild(i).getType()) {
                    case ANTLRBtrplaceSL2Parser.VARIABLE:
                        if (i < vals.size()) {
                            declareVariable(t.getChild(i).getText(), vals.get(i));
                        }
                        break;
                    case ANTLRBtrplaceSL2Parser.BLANK:
                        break;
                    default:
                        return ignoreError("Unsupported type for decomposition: " + t);
                }
            }
        } else if (getChild(0).getType() == ANTLRBtrplaceSL2Parser.ENUM_VAR) {
            List<BtrpOperand> vals = ((BtrpSet) res).getValues();
            BtrpOperand op = ((EnumVar) getChild(0)).expand();
            if (op == IgnorableOperand.getInstance()) {
                return op;
            }
            BtrpSet vars = (BtrpSet) op;
            for (int i = 0; i < vars.getValues().size(); i++) {
                BtrpOperand o = vars.getValues().get(i);
                if (i < vals.size()) {
                    declareVariable(o.toString(), vals.get(i));
                } else {
                    break;
                }
            }
        } else {
            return ignoreError("Unsupported decomposition");
        }
    } catch (UnsupportedOperationException e) {
        return ignoreError(e);
    }
    return IgnorableOperand.getInstance();
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) List(java.util.List) BtrpSet(org.btrplace.btrpsl.element.BtrpSet)

Example 8 with BtrpOperand

use of org.btrplace.btrpsl.element.BtrpOperand in project scheduler by btrplace.

the class AssignmentStatement method declareVariable.

private BtrpOperand declareVariable(String lbl, BtrpOperand res) {
    if (symbols.isImmutable(lbl)) {
        return ignoreError(lbl + " is an immutable variable. Assignment not permitted");
    }
    BtrpOperand cpy = res.copy();
    cpy.setLabel(lbl);
    symbols.declare(lbl, cpy);
    return IgnorableOperand.getInstance();
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand)

Example 9 with BtrpOperand

use of org.btrplace.btrpsl.element.BtrpOperand in project scheduler by btrplace.

the class BooleanBinaryOperation method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    BtrpOperand l = getChild(0).go(this);
    BtrpOperand r = getChild(1).go(this);
    if (l == IgnorableOperand.getInstance() || r == IgnorableOperand.getInstance()) {
        return IgnorableOperand.getInstance();
    }
    if (!(l instanceof BtrpNumber)) {
        return ignoreError("Expression expected, but was '" + l + "'");
    }
    if (!(r instanceof BtrpNumber)) {
        return ignoreError("Expression expected, but was '" + r + "'");
    }
    boolean b1 = BtrpNumber.TRUE.equals(l);
    boolean b2 = BtrpNumber.TRUE.equals(r);
    return eval(b1, b2);
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) BtrpNumber(org.btrplace.btrpsl.element.BtrpNumber)

Example 10 with BtrpOperand

use of org.btrplace.btrpsl.element.BtrpOperand in project scheduler by btrplace.

the class ConstraintStatement method go.

/**
 * Build the constraint.
 * The constraint is built if it exists in the catalog and if the parameters
 * are compatible with the constraint signature.
 *
 * @param parent the parent of the root
 * @return {@code Content.empty} if the constraint is successfully built.
 * {@code Content.ignore} if an error occurred (the error is already reported)
 */
@Override
public BtrpOperand go(BtrPlaceTree parent) {
    String cname = getText();
    if (catalog == null) {
        return ignoreError("No constraints available");
    }
    SatConstraintBuilder b = catalog.getConstraint(cname);
    if (b == null) {
        ignoreError("Unknown constraint '" + cname + "'");
    }
    // Get the params
    int i = 0;
    boolean discrete = false;
    if (">>".equals(getChild(0).getText())) {
        i = 1;
        discrete = true;
    }
    List<BtrpOperand> params = new ArrayList<>();
    for (; i < getChildCount(); i++) {
        params.add(getChild(i).go(this));
    }
    if (b != null) {
        List<? extends SatConstraint> constraints = b.buildConstraint(this, params);
        for (SatConstraint c : constraints) {
            if (c != null) {
                if (discrete) {
                    if (!c.setContinuous(false)) {
                        return ignoreError("Discrete restriction is not supported by constraint '" + cname + "'");
                    }
                } else {
                    // force the continuous mode, if available
                    c.setContinuous(true);
                }
                script.addConstraint(c);
            }
        }
    }
    return IgnorableOperand.getInstance();
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) SatConstraintBuilder(org.btrplace.btrpsl.constraint.SatConstraintBuilder) SatConstraint(org.btrplace.model.constraint.SatConstraint) ArrayList(java.util.ArrayList) SatConstraint(org.btrplace.model.constraint.SatConstraint)

Aggregations

BtrpOperand (org.btrplace.btrpsl.element.BtrpOperand)22 BtrpSet (org.btrplace.btrpsl.element.BtrpSet)8 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BtrpString (org.btrplace.btrpsl.element.BtrpString)2 DefaultBtrpOperand (org.btrplace.btrpsl.element.DefaultBtrpOperand)2 Script (org.btrplace.btrpsl.Script)1 ScriptBuilderException (org.btrplace.btrpsl.ScriptBuilderException)1 SatConstraintBuilder (org.btrplace.btrpsl.constraint.SatConstraintBuilder)1 BtrpElement (org.btrplace.btrpsl.element.BtrpElement)1 BtrpNumber (org.btrplace.btrpsl.element.BtrpNumber)1 DefaultModel (org.btrplace.model.DefaultModel)1 Model (org.btrplace.model.Model)1 SatConstraint (org.btrplace.model.constraint.SatConstraint)1