Search in sources :

Example 1 with Expression

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);
    }
}
Also used : VariableReference(fr.lip6.move.gal.VariableReference) Comparison(fr.lip6.move.gal.Comparison) Constant(fr.lip6.move.gal.Constant) And(fr.lip6.move.gal.And)

Example 2 with Expression

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.");
    }
}
Also used : Add(fr.lip6.move.coloane.projects.its.expression.Add) Infinity(fr.lip6.move.coloane.projects.its.expression.Infinity) Constant(fr.lip6.move.gal.Constant) IVariable(fr.lip6.move.coloane.projects.its.expression.IVariable) BinaryIntExpression(fr.lip6.move.gal.BinaryIntExpression) IntExpression(fr.lip6.move.gal.IntExpression) NaryExpression(fr.lip6.move.coloane.projects.its.expression.NaryExpression) BinaryIntExpression(fr.lip6.move.gal.BinaryIntExpression) ParamRef(fr.lip6.move.gal.ParamRef) UnaryMinus(fr.lip6.move.coloane.projects.its.expression.UnaryMinus) Fixpoint(fr.lip6.move.gal.Fixpoint) Div(fr.lip6.move.coloane.projects.its.expression.Div) Mult(fr.lip6.move.coloane.projects.its.expression.Mult) Minus(fr.lip6.move.coloane.projects.its.expression.Minus) UnaryMinus(fr.lip6.move.coloane.projects.its.expression.UnaryMinus)

Example 3 with Expression

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);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) IntegerExpressionParserParser(fr.lip6.move.coloane.projects.its.expression.parser.IntegerExpressionParserParser) ErrorReporter(fr.lip6.move.coloane.projects.its.antlrutil.ErrorReporter) IntegerExpressionParserLexer(fr.lip6.move.coloane.projects.its.expression.parser.IntegerExpressionParserLexer) ArrayList(java.util.ArrayList) RecognitionException(org.antlr.runtime.RecognitionException)

Example 4 with Expression

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);
                }
            }
        }
    }
}
Also used : Variable(fr.lip6.move.coloane.projects.its.expression.Variable) IVariable(fr.lip6.move.coloane.projects.its.expression.IVariable) IModelVariable(fr.lip6.move.coloane.projects.its.variables.IModelVariable) PlaceMarkingVariable(fr.lip6.move.coloane.projects.its.variables.PlaceMarkingVariable) TransitionClockVariable(fr.lip6.move.coloane.projects.its.variables.TransitionClockVariable) Infinity(fr.lip6.move.coloane.projects.its.expression.Infinity) Constant(fr.lip6.move.coloane.projects.its.expression.Constant) IVariable(fr.lip6.move.coloane.projects.its.expression.IVariable) IntegerExpression(fr.lip6.move.coloane.projects.its.expression.IntegerExpression) ExpressionParseResult(fr.lip6.move.coloane.projects.its.expression.ExpressionParseResult)

Example 5 with Expression

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;
}
Also used : VariableOrConstantReference(fr.lip6.move.divine.divine.VariableOrConstantReference) ArrayReference(fr.lip6.move.divine.divine.ArrayReference) Variable(fr.lip6.move.divine.divine.Variable) VariableReference(fr.lip6.move.gal.VariableReference) Constant(fr.lip6.move.divine.divine.Constant) ArrayVarAccess(fr.lip6.move.divine.divine.ArrayVarAccess) IntExpression(fr.lip6.move.gal.IntExpression)

Aggregations

Expression (fr.lip6.move.gal.structural.expr.Expression)53 ArrayList (java.util.ArrayList)52 List (java.util.List)25 SparseIntArray (android.util.SparseIntArray)23 IOException (java.io.IOException)18 HashSet (java.util.HashSet)18 BooleanExpression (fr.lip6.move.gal.BooleanExpression)16 IntExpression (fr.lip6.move.gal.IntExpression)15 Property (fr.lip6.move.gal.structural.Property)15 StructuralReduction (fr.lip6.move.gal.structural.StructuralReduction)14 File (java.io.File)14 Set (java.util.Set)13 SparsePetriNet (fr.lip6.move.gal.structural.SparsePetriNet)12 TGBA (fr.lip6.ltl.tgba.TGBA)11 VariableReference (fr.lip6.move.gal.VariableReference)11 ISparsePetriNet (fr.lip6.move.gal.structural.ISparsePetriNet)11 BitSet (java.util.BitSet)10 IntMatrixCol (fr.lip6.move.gal.util.IntMatrixCol)9 GlobalPropertySolvedException (fr.lip6.move.gal.structural.GlobalPropertySolvedException)8 BinOp (fr.lip6.move.gal.structural.expr.BinOp)8