Search in sources :

Example 1 with SatConstraintBuilder

use of org.btrplace.btrpsl.constraint.SatConstraintBuilder 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

ArrayList (java.util.ArrayList)1 SatConstraintBuilder (org.btrplace.btrpsl.constraint.SatConstraintBuilder)1 BtrpOperand (org.btrplace.btrpsl.element.BtrpOperand)1 SatConstraint (org.btrplace.model.constraint.SatConstraint)1