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;
}
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;
}
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;
}
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();
}
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();
}
Aggregations