Search in sources :

Example 1 with BtrpSet

use of org.btrplace.btrpsl.element.BtrpSet 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 2 with BtrpSet

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

the class ScriptBuilderTest method testStringSupport.

public void testStringSupport() throws ScriptBuilderException {
    ScriptBuilder b = new ScriptBuilder(new DefaultModel());
    Script v = b.build("namespace foo; VM[1..10] : tiny;\n$arr = {\"foo\",\"bar\", \"baz\"};$arr2 = $arr + {\"git\"}; $out = \"come \" + \"out \" + 5 + \" \" + VM1; export $arr2,$out to *;");
    BtrpString out = (BtrpString) v.getImportable("$out");
    BtrpSet arr2 = (BtrpSet) v.getImportable("$arr2");
    Assert.assertEquals(out.toString(), "come out 5 foo.VM1");
    Assert.assertEquals(arr2.size(), 4);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) BtrpString(org.btrplace.btrpsl.element.BtrpString) BtrpSet(org.btrplace.btrpsl.element.BtrpSet)

Example 3 with BtrpSet

use of org.btrplace.btrpsl.element.BtrpSet 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 4 with BtrpSet

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

the class ScriptBuilderTest method testVariablesInElementRange.

public void testVariablesInElementRange() throws ScriptBuilderException {
    ScriptBuilder b = new ScriptBuilder(new DefaultModel());
    Script v = b.build(new File(RC_ROOT + "range.btrp"));
    BtrpSet s = (BtrpSet) v.getImportable("$foo");
    System.out.println(s);
    Assert.assertEquals(s.size(), 9);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) BtrpSet(org.btrplace.btrpsl.element.BtrpSet) File(java.io.File)

Example 5 with BtrpSet

use of org.btrplace.btrpsl.element.BtrpSet 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)

Aggregations

BtrpSet (org.btrplace.btrpsl.element.BtrpSet)13 BtrpOperand (org.btrplace.btrpsl.element.BtrpOperand)8 DefaultModel (org.btrplace.model.DefaultModel)4 BtrpString (org.btrplace.btrpsl.element.BtrpString)3 File (java.io.File)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ArrayList (java.util.ArrayList)1 Script (org.btrplace.btrpsl.Script)1 ScriptBuilderException (org.btrplace.btrpsl.ScriptBuilderException)1 BtrpNumber (org.btrplace.btrpsl.element.BtrpNumber)1 DefaultBtrpOperand (org.btrplace.btrpsl.element.DefaultBtrpOperand)1