Search in sources :

Example 1 with BtrpOperand

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

the class SelfAssignmentStatement method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    if (getChild(0).getType() == ANTLRBtrplaceSL2Parser.VARIABLE) {
        String lbl = getChild(0).getText();
        if (symbols.isImmutable(lbl)) {
            return ignoreError(lbl + " is an immutable variable. Assignment not permitted");
        }
        BtrpOperand e = getChild(0).go(parent);
        BtrpOperand r = getChild(1).go(this);
        BtrpOperand res;
        try {
            switch(type) {
                case PLUS_EQUALS:
                    res = e.plus(r);
                    break;
                case MINUS_EQUALS:
                    res = e.minus(r);
                    break;
                case TIMES_EQUALS:
                    res = e.times(r);
                    break;
                case DIV_EQUALS:
                    res = e.div(r);
                    break;
                case REMAINDER_EQUALS:
                    res = e.remainder(r);
                    break;
                default:
                    return ignoreError("Unsupported operation: " + type);
            }
        } catch (ArithmeticException ex) {
            return ignoreError(ex);
        }
        symbols.declare(lbl, res);
        res.setLabel(lbl);
        return IgnorableOperand.getInstance();
    }
    return ignoreError(getChild(0).getText() + " is not a variable");
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand)

Example 2 with BtrpOperand

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

the class StrictComparisonOperator method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    BtrpOperand l = getChild(0).go(this);
    BtrpOperand r = getChild(1).go(this);
    if (!reverse) {
        return l.gt(r);
    }
    return r.gt(l);
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand)

Example 3 with BtrpOperand

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

the class TemplateAssignment method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    BtrPlaceTree t = getChild(0);
    String tplName = getChild(1).getText();
    if (!tpls.isAvailable(tplName)) {
        return ignoreError("Unknown template '" + tplName + "'");
    }
    Map<String, String> opts = getTemplateOptions();
    int nType = t.getType();
    if (nType == ANTLRBtrplaceSL2Parser.IDENTIFIER) {
        addVM(tplName, script.id() + "." + t.getText(), opts);
    } else if (nType == ANTLRBtrplaceSL2Parser.NODE_NAME) {
        addNode(tplName, t.getText(), opts);
    } else if (nType == ANTLRBtrplaceSL2Parser.ENUM_ID) {
        BtrpOperand op = ((EnumElement) t).expand();
        if (op == IgnorableOperand.getInstance()) {
            return op;
        }
        for (BtrpOperand o : ((BtrpSet) op).getValues()) {
            addVM(tplName, o.toString(), opts);
        }
    } else if (nType == ANTLRBtrplaceSL2Parser.ENUM_FQDN) {
        BtrpOperand op = ((EnumElement) t).expand();
        if (op == IgnorableOperand.getInstance()) {
            return op;
        }
        for (BtrpOperand o : ((BtrpSet) op).getValues()) {
            addNode(tplName, o.toString(), opts);
        }
    } else if (nType == ANTLRBtrplaceSL2Parser.EXPLODED_SET) {
        @SuppressWarnings("unchecked") List<BtrPlaceTree> children = (List<BtrPlaceTree>) t.getChildren();
        for (BtrPlaceTree child : children) {
            if (child.getType() == ANTLRBtrplaceSL2Parser.IDENTIFIER) {
                addVM(tplName, script.id() + "." + child.getText(), opts);
            } else if (child.getType() == ANTLRBtrplaceSL2Parser.NODE_NAME) {
                addNode(tplName, child.getText(), opts);
            } else {
                return ignoreError("Only VMs or nodes can be declared from templates");
            }
        }
    } else {
        ignoreError("Unable to assign the template to '" + t.getText());
    }
    return IgnorableOperand.getInstance();
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) BtrpSet(org.btrplace.btrpsl.element.BtrpSet) List(java.util.List)

Example 4 with BtrpOperand

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

the class TimesOperator 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 l.times(r);
    }
    return IgnorableOperand.getInstance();
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand)

Example 5 with BtrpOperand

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

the class ScriptTest method testExported.

public void testExported() {
    Script v = new Script();
    v.setFullyQualifiedName("testScript");
    Model mo = new DefaultModel();
    BtrpOperand o1 = new BtrpElement(BtrpOperand.Type.VM, "vm1", mo.newVM());
    v.addExportable("$s", o1, Collections.singleton("*"));
    // No restrictions, so every can access the exported variable
    Assert.assertEquals(v.getImportable("$s"), o1);
    Assert.assertTrue(v.canImport("$s", "foo"));
    Assert.assertTrue(v.canImport("$s", "bar.toto"));
    // Explicit no restriction
    BtrpOperand o2 = new BtrpElement(BtrpOperand.Type.VM, "vm2", mo.newVM());
    v.addExportable("$x", o2, Collections.singleton("*"));
    Assert.assertEquals(v.getImportable("$x"), o2);
    Assert.assertTrue(v.canImport("$x", "foo"));
    Assert.assertTrue(v.canImport("$x", "bar.toto"));
    // Unknown export
    Assert.assertFalse(v.canImport("$z", "foo"));
    Assert.assertFalse(v.canImport("$z", "bar.toto"));
    Set<String> valid = new HashSet<>();
    valid.add("foo");
    valid.add("foo.*");
    valid.add("bar");
    BtrpOperand o3 = new BtrpElement(BtrpOperand.Type.VM, "vm2", mo.newVM());
    v.addExportable("$y", o3, valid);
    Assert.assertNull(v.getImportable("$y"));
    Assert.assertEquals(v.getImportable("$y", "foo"), o3);
    Assert.assertNull(v.getImportable("$y", "zog"));
    Assert.assertEquals(v.getImportable("$y", "foo.bar.fii"), o3);
    Assert.assertEquals(v.getImportable("$y", "bar"), o3);
    Assert.assertEquals(v.getExported().size(), 3);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) BtrpElement(org.btrplace.btrpsl.element.BtrpElement) HashSet(java.util.HashSet)

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