Search in sources :

Example 1 with BtrpNumber

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

the class MaxOnlineBuilder method buildConstraint.

/**
 * Build an online constraint.
 *
 * @param args must be 1 set of nodes. The set must not be empty
 * @return a constraint
 */
@Override
public List<MaxOnline> buildConstraint(BtrPlaceTree t, List<BtrpOperand> args) {
    if (checkConformance(t, args)) {
        @SuppressWarnings("unchecked") List<Node> ns = (List<Node>) params[0].transform(this, t, args.get(0));
        if (ns == null) {
            return Collections.emptyList();
        }
        Set<Node> s = new HashSet<>(ns);
        if (s.size() != ns.size()) {
            // Prevent duplicates
            return Collections.emptyList();
        }
        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 Collections.singletonList(new MaxOnline(new HashSet<>(ns), v, true));
    }
    return Collections.emptyList();
}
Also used : MaxOnline(org.btrplace.model.constraint.MaxOnline) BtrpNumber(org.btrplace.btrpsl.element.BtrpNumber) Node(org.btrplace.model.Node) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with BtrpNumber

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

the class PreserveBuilder method buildConstraint.

@Override
public List<? extends SatConstraint> buildConstraint(BtrPlaceTree t, List<BtrpOperand> args) {
    if (!checkConformance(t, args)) {
        return Collections.emptyList();
    }
    @SuppressWarnings("unchecked") List<VM> s = (List<VM>) params[0].transform(this, t, args.get(0));
    String rcId = (String) params[1].transform(this, t, args.get(1));
    BtrpNumber n = (BtrpNumber) args.get(2);
    if (!n.isInteger()) {
        t.ignoreError("Parameter '" + params[2].getName() + "' expects an integer");
        return Collections.emptyList();
    }
    int v = n.getIntValue();
    if (v < 0) {
        t.ignoreError("Parameter '" + params[2].getName() + "' expects a positive integer (" + v + " given)");
        return Collections.emptyList();
    }
    return s != null && rcId != null ? Preserve.newPreserve(s, rcId, v) : Collections.emptyList();
}
Also used : BtrpNumber(org.btrplace.btrpsl.element.BtrpNumber) VM(org.btrplace.model.VM) List(java.util.List) SatConstraint(org.btrplace.model.constraint.SatConstraint)

Example 3 with BtrpNumber

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

the class ResourceCapacityBuilder 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));
    String rcId = (String) params[1].transform(this, t, args.get(1));
    BtrpNumber n = (BtrpNumber) args.get(2);
    if (!n.isInteger()) {
        t.ignoreError("Parameter '" + params[2].getName() + "' expects an integer");
        return Collections.emptyList();
    }
    int v = n.getIntValue();
    if (v < 0) {
        t.ignoreError("Parameter '" + params[2].getName() + "' expects a positive integer (" + v + " given)");
        return Collections.emptyList();
    }
    return ns != null ? Collections.singletonList(new ResourceCapacity(new HashSet<>(ns), rcId, v)) : Collections.emptyList();
}
Also used : ResourceCapacity(org.btrplace.model.constraint.ResourceCapacity) BtrpNumber(org.btrplace.btrpsl.element.BtrpNumber) Node(org.btrplace.model.Node) List(java.util.List) SatConstraint(org.btrplace.model.constraint.SatConstraint)

Example 4 with BtrpNumber

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

the class ScriptBuilderTest method testSetManipulation.

public void testSetManipulation() throws ScriptBuilderException {
    ScriptBuilder b = new ScriptBuilder(new DefaultModel());
    Script v = b.build(new File(RC_ROOT + "setManip.btrp"));
    BtrpSet t1 = (BtrpSet) v.getImportable("$T1");
    BtrpSet t2 = (BtrpSet) v.getImportable("$T2");
    BtrpSet t3 = (BtrpSet) v.getImportable("$T3");
    BtrpNumber x = (BtrpNumber) v.getImportable("$x");
    BtrpNumber res = (BtrpNumber) v.getImportable("$res");
    BtrpNumber res2 = (BtrpNumber) v.getImportable("$res2");
    BtrpNumber res3 = (BtrpNumber) v.getImportable("$res3");
    BtrpNumber y = (BtrpNumber) v.getImportable("$y");
    Assert.assertEquals(t1.size() + t2.size() + t3.size(), 100);
    Assert.assertEquals(x.getIntValue(), 12);
    Assert.assertEquals(y.getIntValue(), 3);
    BtrpSet C = (BtrpSet) v.getImportable("$C");
    Assert.assertEquals(C.size(), 90);
    BtrpSet a = (BtrpSet) v.getImportable("$a");
    Assert.assertEquals(res, BtrpNumber.TRUE);
    Assert.assertEquals(res2, BtrpNumber.TRUE);
    Assert.assertEquals(res3, BtrpNumber.TRUE);
    Assert.assertEquals(a.degree(), 2);
    Assert.assertEquals(a.size(), 4);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) BtrpNumber(org.btrplace.btrpsl.element.BtrpNumber) BtrpSet(org.btrplace.btrpsl.element.BtrpSet) File(java.io.File)

Example 5 with BtrpNumber

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

the class ScriptBuilderTest method testIfStatement.

public void testIfStatement() {
    ScriptBuilder b = new ScriptBuilder(new DefaultModel());
    try {
        Script v = b.build(new File(RC_ROOT + "ifStatement.btrp"));
        BtrpNumber first = (BtrpNumber) v.getImportable("$first");
        BtrpNumber second = (BtrpNumber) v.getImportable("$second");
        BtrpNumber third = (BtrpNumber) v.getImportable("$third");
        Assert.assertNotNull(first);
        Assert.assertNotNull(second);
        Assert.assertEquals(first, BtrpNumber.TRUE);
        Assert.assertEquals(second, BtrpNumber.TRUE);
        Assert.assertEquals(third.getIntValue(), 5);
    } catch (ScriptBuilderException x) {
        Assert.fail(x.getMessage(), x);
    }
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) BtrpNumber(org.btrplace.btrpsl.element.BtrpNumber) File(java.io.File)

Aggregations

BtrpNumber (org.btrplace.btrpsl.element.BtrpNumber)13 File (java.io.File)4 List (java.util.List)4 DefaultModel (org.btrplace.model.DefaultModel)4 Node (org.btrplace.model.Node)3 SatConstraint (org.btrplace.model.constraint.SatConstraint)3 HashSet (java.util.HashSet)1 BtrpOperand (org.btrplace.btrpsl.element.BtrpOperand)1 BtrpSet (org.btrplace.btrpsl.element.BtrpSet)1 VM (org.btrplace.model.VM)1 MaxOnline (org.btrplace.model.constraint.MaxOnline)1 ResourceCapacity (org.btrplace.model.constraint.ResourceCapacity)1 RunningCapacity (org.btrplace.model.constraint.RunningCapacity)1