Search in sources :

Example 6 with BtrpSet

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

the class EnumVar method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    String head = getChild(0).getText().substring(0, getChild(0).getText().length() - 1);
    String tail = getChild(getChildCount() - 1).getText().substring(1);
    BtrpSet res = null;
    for (int i = 1; i < getChildCount() - 1; i++) {
        BtrpOperand op = getChild(i).go(this);
        if (op == IgnorableOperand.getInstance()) {
            return op;
        }
        BtrpSet s = (BtrpSet) op;
        for (BtrpOperand o : s.getValues()) {
            // Compose
            String id = head + o.toString() + tail;
            // lookup
            BtrpOperand var = syms.getSymbol(id);
            if (var == null) {
                return ignoreError(parent.getToken(), "Unknown variable '" + id + "'");
            }
            if (res == null) {
                res = new BtrpSet(2, var.type());
            }
            res.getValues().add(var);
        }
    }
    return res;
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) BtrpSet(org.btrplace.btrpsl.element.BtrpSet) BtrpString(org.btrplace.btrpsl.element.BtrpString)

Example 7 with BtrpSet

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

the class EnumVar method expand.

/**
 * Expand the enumeration.
 * Variables are not evaluated nor checked
 *
 * @return a set of string or an error
 */
public BtrpOperand expand() {
    String head = getChild(0).getText().substring(0, getChild(0).getText().length() - 1);
    String tail = getChild(getChildCount() - 1).getText().substring(1);
    BtrpSet res = new BtrpSet(1, BtrpOperand.Type.STRING);
    for (int i = 1; i < getChildCount() - 1; i++) {
        BtrpOperand op = getChild(i).go(this);
        if (op == IgnorableOperand.getInstance()) {
            return op;
        }
        BtrpSet s = (BtrpSet) op;
        for (BtrpOperand o : s.getValues()) {
            // Compose
            res.getValues().add(new BtrpString(head + o.toString() + tail));
        }
    }
    return res;
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) BtrpSet(org.btrplace.btrpsl.element.BtrpSet) BtrpString(org.btrplace.btrpsl.element.BtrpString) BtrpString(org.btrplace.btrpsl.element.BtrpString)

Example 8 with BtrpSet

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

the class ExplodedSetTree method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    if (getChildCount() == 0) {
        return ignoreError("Empty sets not allowed");
    }
    BtrpOperand t0 = getChild(0).go(this);
    if (t0 == IgnorableOperand.getInstance()) {
        return t0;
    }
    BtrpSet s = new BtrpSet(t0.degree() + 1, t0.type());
    Set<BtrpOperand> viewed = new HashSet<>();
    for (int i = 0; i < getChildCount(); i++) {
        BtrpOperand tx = getChild(i).go(this);
        // s.getIntValue().add() is not safe at all. So preconditions have to be check
        if (tx == IgnorableOperand.getInstance()) {
            return tx;
        }
        if (tx.degree() != s.degree() - 1) {
            return ignoreError(tx + " has type '" + tx.prettyType() + "'. It should be a '" + DefaultBtrpOperand.prettyType(s.degree() - 1, s.type()) + "' to be insertable into a '" + s.prettyType() + "'");
        }
        if (tx.type() != s.type()) {
            return ignoreError("Unable to add '" + tx.type() + "' elements in a set of '" + s.type() + "' elements");
        }
        if (viewed.add(tx)) {
            s.getValues().add(tx);
        } else {
            return ignoreError(tx + " ignored");
        }
    }
    return s;
}
Also used : DefaultBtrpOperand(org.btrplace.btrpsl.element.DefaultBtrpOperand) BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) BtrpSet(org.btrplace.btrpsl.element.BtrpSet) HashSet(java.util.HashSet)

Example 9 with BtrpSet

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

the class ExportStatement method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    Set<String> scope = new HashSet<>();
    List<BtrpOperand> toAdd = new ArrayList<>();
    for (int i = 0; i < getChildCount(); i++) {
        if (getChild(i).getType() == ANTLRBtrplaceSL2Parser.ENUM_VAR) {
            BtrpOperand r = getChild(i).go(this);
            if (r == IgnorableOperand.getInstance()) {
                return r;
            }
            for (BtrpOperand o : ((BtrpSet) r).getValues()) {
                toAdd.add(o);
            }
        } else if (getChild(i).getType() == ANTLRBtrplaceSL2Parser.VARIABLE) {
            toAdd.add(getChild(i).go(this));
        } else if (getChild(i).getType() == ANTLRBtrplaceSL2Parser.TIMES) {
            scope.add("*");
        } else if (getChild(i).getType() == ANTLRBtrplaceSL2Parser.IDENTIFIER) {
            scope.add(getChild(i).getText());
        } else {
            BtrpOperand e = getChild(i).go(this);
            if (e == IgnorableOperand.getInstance()) {
                return e;
            }
            toAdd.addAll(flatten(e));
        }
    }
    try {
        for (BtrpOperand op : toAdd) {
            script.addExportable(op.label(), op, scope);
        }
    } catch (UnsupportedOperationException ex) {
        return ignoreError(ex);
    }
    return IgnorableOperand.getInstance();
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) ArrayList(java.util.ArrayList) BtrpSet(org.btrplace.btrpsl.element.BtrpSet) HashSet(java.util.HashSet)

Example 10 with BtrpSet

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

the class ForStatement method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    // First child is variable, second is set we iterate over, third is a block
    if (this.getChildCount() != 3) {
        return ignoreError("Malformed iteration loop");
    }
    table.pushTable();
    String inVar = getChild(0).getText();
    if (table.isDeclared(inVar)) {
        return ignoreError("Variable " + inVar + " already declared");
    }
    BtrpOperand c = getChild(1).go(this);
    if (c == IgnorableOperand.getInstance()) {
        return c;
    }
    if (c.degree() < 1) {
        return ignoreError("The literal to iterate one must be a set");
    }
    BtrpSet set = (BtrpSet) c;
    for (Object elem : set.getValues()) {
        table.declare(inVar, (BtrpOperand) elem);
        getChild(2).go(this);
    // TODO a good solution to avoid to iterate once an iteration fail?
    }
    if (!table.popTable()) {
        return ignoreError("Unable to Pop the symbol table");
    }
    return IgnorableOperand.getInstance();
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) 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