Search in sources :

Example 16 with BtrpOperand

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

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

Example 18 with BtrpOperand

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

the class IfStatement method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    BtrpOperand expr = getChild(0).go(this);
    if (expr.equals(BtrpNumber.TRUE)) {
        sTable.pushTable();
        getChild(1).go(this);
        sTable.popTable();
    } else if (getChildCount() == 3) {
        sTable.pushTable();
        getChild(2).go(this);
        sTable.popTable();
    } else {
        return ignoreError(expr + ": not an expression");
    }
    return IgnorableOperand.getInstance();
}
Also used : BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand)

Example 19 with BtrpOperand

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

the class ImportStatement method go.

@Override
@SuppressWarnings("squid:S1166")
public BtrpOperand go(BtrPlaceTree parent) {
    String id = scriptId();
    List<Script> res;
    try {
        res = includes.getScripts(id);
        script.getDependencies().addAll(res);
    } catch (ScriptBuilderException e) {
        int nb = e.getErrorReporter().getErrors().size();
        return ignoreError(Integer.toString(nb) + " error(s) imported through '" + id + "'");
    }
    if (res.isEmpty()) {
        return ignoreError(getChild(0).getToken(), "Unable to locate '" + id + "'");
    }
    if (id.endsWith(".*")) {
        // Prepare the global variable.
        BtrpSet global = new BtrpSet(1, BtrpOperand.Type.VM);
        global.setLabel("$".concat(id.substring(0, id.length() - 2)));
        if (global.size() > 0 && !symTable.declareImmutable(global.label(), global)) {
            return ignoreError("Unable to add variable '" + global.label() + "'");
        }
    }
    for (Script v : res) {
        for (BtrpOperand op : v.getImportables(script.id())) {
            String fqn = v.fullyQualifiedSymbolName(op.label());
            if (!symTable.declareImmutable(fqn, op)) {
                return ignoreError("Unable to import '" + fqn + "': already declared");
            }
        }
    }
    return IgnorableOperand.getInstance();
}
Also used : Script(org.btrplace.btrpsl.Script) BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) ScriptBuilderException(org.btrplace.btrpsl.ScriptBuilderException) BtrpSet(org.btrplace.btrpsl.element.BtrpSet)

Example 20 with BtrpOperand

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

the class NonStrictComparisonOperator method go.

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

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