use of org.btrplace.model.constraint.SatConstraint in project scheduler by btrplace.
the class RunningCapacityBuilder method buildConstraint.
@Override
public List<? extends SatConstraint> buildConstraint(BtrPlaceTree t, List<BtrpOperand> args) {
if (!checkConformance(t, args)) {
return Collections.emptyList();
}
@SuppressWarnings("unchecked") List<Node> ns = (List<Node>) params[0].transform(this, t, args.get(0));
BtrpNumber n = (BtrpNumber) args.get(1);
if (!n.isInteger()) {
t.ignoreError("Parameter '" + params[1].getName() + "' expects an integer");
return Collections.emptyList();
}
int v = n.getIntValue();
if (v < 0) {
t.ignoreError("Parameter '" + params[1].getName() + "' expects a positive integer (" + v + " given)");
return Collections.emptyList();
}
return ns != null ? Collections.singletonList(new RunningCapacity(new HashSet<>(ns), v)) : Collections.emptyList();
}
use of org.btrplace.model.constraint.SatConstraint 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();
}
use of org.btrplace.model.constraint.SatConstraint in project scheduler by btrplace.
the class ScriptBuilderTest method testMeUsage.
public void testMeUsage() {
ScriptBuilder b = new ScriptBuilder(new DefaultModel());
try {
Script v = b.build("namespace foo; VM[1..5] : tiny;\nVM[6..10] : small;\n lonely($me); ");
SatConstraint cs = v.getConstraints().iterator().next();
Assert.assertEquals(cs.getInvolvedVMs().size(), 10);
} catch (ScriptBuilderException x) {
Assert.fail(x.getMessage(), x);
}
}
Aggregations