use of fr.lip6.move.gal.structural.expr.Expression in project ITSTools by lip6.
the class BoundApplier method createBooleanExpressionFromReferences.
/**
* Function to create the BooleanExpression in order to bind the transition from wich we've collected the Set references.
*
* @param expression
* @param references
* @param bound
* @return
*/
private static BooleanExpression createBooleanExpressionFromReferences(And expression, Set<VariableReference> references, int bound) {
if (references.size() == 1) {
Comparison comparison = GalFactory.eINSTANCE.createComparison();
Constant boundConstant = GalFactory.eINSTANCE.createConstant();
Iterator<VariableReference> iterator = references.iterator();
boundConstant.setValue(bound);
comparison.setLeft(iterator.next());
comparison.setRight(boundConstant);
comparison.setOperator(ComparisonOperators.LE);
expression.setRight(comparison);
return expression;
} else {
Comparison comparison = GalFactory.eINSTANCE.createComparison();
Constant boundConstant = GalFactory.eINSTANCE.createConstant();
And and = GalFactory.eINSTANCE.createAnd();
Iterator<VariableReference> iterator = references.iterator();
boundConstant.setValue(bound);
comparison.setLeft(iterator.next());
comparison.setRight(boundConstant);
comparison.setOperator(ComparisonOperators.LE);
iterator.remove();
expression.setRight(comparison);
and.setLeft(expression);
expression = and;
return createBooleanExpressionFromReferences(expression, references, bound);
}
}
use of fr.lip6.move.gal.structural.expr.Expression in project ITSTools by lip6.
the class ExportToGAL method convertToGAL.
private IntExpression convertToGAL(IntegerExpression expr, Map<IVariable, ConstParameter> varMap) {
if (expr instanceof fr.lip6.move.coloane.projects.its.expression.Constant) {
int val = ((fr.lip6.move.coloane.projects.its.expression.Constant) expr).getValue();
return constant(val);
} else if (expr instanceof Infinity) {
// Infinity inf = (Infinity) expr;
return constant(-1);
} else if (expr instanceof IVariable) {
IVariable var = (IVariable) expr;
ParamRef pr = GalFactory.eINSTANCE.createParamRef();
pr.setRefParam(varMap.get(var));
return pr;
} else if (expr instanceof UnaryMinus) {
UnaryMinus um = (UnaryMinus) expr;
IntExpression child = convertToGAL(um.getChildren().get(0), varMap);
fr.lip6.move.gal.UnaryMinus minus = GalFactory.eINSTANCE.createUnaryMinus();
minus.setValue(child);
return minus;
} else if (expr instanceof NaryExpression) {
NaryExpression add = (NaryExpression) expr;
BinaryIntExpression bin = GalFactory.eINSTANCE.createBinaryIntExpression();
if (add instanceof Add) {
bin.setOp("+");
} else if (add instanceof Div) {
bin.setOp("/");
} else if (add instanceof Minus) {
bin.setOp("-");
} else if (add instanceof Mult) {
bin.setOp("*");
}
bin.setLeft(convertToGAL(add.getChildren().get(0), varMap));
bin.setRight(convertToGAL(add.getChildren().get(1), varMap));
return bin;
} else {
throw new RuntimeException("Unexpected unknown Expression type when parsing net attribute.");
}
}
use of fr.lip6.move.gal.structural.expr.Expression in project ITSTools by lip6.
the class ExpressionFactory method parseExpression.
public static ExpressionParseResult parseExpression(String expr) {
if (expr != null && !"".equals(expr)) {
// if (name.equals("marking")
// || name.equals("earliestFiringTime")
// || name.equals("latestFiringTime")
// || name.equals("valuation")
// || name.equals("size")) {
IntegerExpressionParserLexer lexer;
lexer = new IntegerExpressionParserLexer(new ANTLRStringStream(expr));
ErrorReporter report = new ErrorReporter();
lexer.setErrorReporter(report);
CommonTokenStream tokens = new CommonTokenStream(lexer);
IntegerExpressionParserParser parser = new IntegerExpressionParserParser(tokens);
parser.setErrorReporter(report);
try {
IntegerExpression pexpr = parser.prog();
List<String> errors = new ArrayList<String>();
for (String err : report) {
errors.add(err);
}
return new ExpressionParseResult(pexpr, errors);
} catch (RecognitionException e) {
String msg = "Syntax error parsing integer expression for \"" + expr + "\". Use $ to prefix variables, and arithmetic operations only.\n";
msg += e.getLocalizedMessage();
return new ExpressionParseResult(null, Collections.singletonList(msg));
}
}
return new ExpressionParseResult(null, null);
}
use of fr.lip6.move.gal.structural.expr.Expression in project ITSTools by lip6.
the class TypeDeclaration method parseIntExpression.
/**
* Do the actual loading of a given attribute value = parse an int
* expression
*
* @param attrib
* attrib to load or null
* @param context
* the current context (can be updated)
* @throws ExtensionException
* if syntax errors occur
*/
private void parseIntExpression(IAttribute attrib, IEvaluationContext context) throws ExtensionException {
if (attrib == null) {
return;
}
String mark = attrib.getValue();
if (mark != null && !"".equals(mark)) {
ExpressionParseResult epr = ExpressionFactory.parseExpression(mark);
int nberr = epr.getErrorCount();
if (nberr != 0) {
context.declareVariable(new Variable("SYNTAX ERRORS IN MODEL, PLEASE RUN SYNTAX CHECK" + epr.getErrors()));
} else {
IntegerExpression expr = epr.getExpression();
if (!(expr instanceof Constant) && expr != null && !(expr instanceof Infinity)) {
// dont store the mapping for trivial integers
attribs.put(attrib, expr);
// could be empty for simple expressions, eg 3+ 2
for (IVariable var : expr.supportingVariables()) {
context.declareVariable(var);
}
}
}
}
}
use of fr.lip6.move.gal.structural.expr.Expression in project ITSTools by lip6.
the class Converter method convertToGalRefInt.
private IntExpression convertToGalRefInt(Expression e) {
fr.lip6.move.gal.IntExpression target = null;
// e is a reference of variabe or constant
if (e instanceof VariableOrConstantReference) {
VariableOrConstantReference ref = (VariableOrConstantReference) e;
if (ref.getRef() instanceof Variable) {
Variable var = (Variable) ref.getRef();
// check if global var
target = gvarmap.get(var);
if (target != null)
return EcoreUtil.copy(target);
else {
// local var
target = lvarmap.get(var);
if (target == null)
throw new UnsupportedOperationException("Unmapped variable " + var);
return EcoreUtil.copy(target);
}
} else if (ref.getRef() instanceof Constant) {
Constant c = (Constant) ref.getRef();
target = constmap.get(c);
if (target != null)
return EcoreUtil.copy(target);
throw new UnsupportedOperationException("Unmapped constant " + c);
} else
throw new UnsupportedOperationException("Unmapped variable or constant " + ref);
} else // e is a reference of array
if (e instanceof ArrayReference) {
ArrayReference ref = (ArrayReference) e;
if (ref.getRef() instanceof ArrayVarAccess) {
ArrayVarAccess ava = (ArrayVarAccess) ref.getRef();
fr.lip6.move.gal.VariableReference gava;
// check if global array
gava = garraymap.get(ava.getPrefix());
if (gava != null) {
gava.setIndex(convertToGalInt(ava.getIndex()));
return EcoreUtil.copy(gava);
} else {
// local array
gava = larraymap.get(ava.getPrefix());
if (gava != null) {
gava.setIndex(convertToGalInt(ava.getIndex()));
return EcoreUtil.copy(gava);
} else
throw new UnsupportedOperationException("Unmapped array " + ava);
}
} else
throw new UnsupportedOperationException("Unmapped array " + ref);
}
return target;
}
Aggregations