Search in sources :

Example 6 with RealLitExpr

use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.

the class LinearizationRewriter method generateAgreeConstraintForm.

private static NodeDef generateAgreeConstraintForm(LinearizationDef linDef, BoundingSegments segs) {
    NodeDef result = af.createNodeDef();
    result.setName(getConstraintFormName(AgreeUtils.getNodeName(linDef)));
    Arg inputArg = af.createArg();
    PrimType inputArgType = af.createPrimType();
    inputArgType.setName("real");
    inputArg.setType(inputArgType);
    inputArg.setName("inp");
    result.getArgs().add(inputArg);
    Arg outputArg = af.createArg();
    PrimType outputArgType = af.createPrimType();
    outputArgType.setName("real");
    outputArg.setType(outputArgType);
    outputArg.setName("result");
    result.getArgs().add(outputArg);
    Arg constraintArg = af.createArg();
    PrimType constraintArgType = af.createPrimType();
    constraintArgType.setName("bool");
    constraintArg.setType(constraintArgType);
    constraintArg.setName("constraint");
    result.getRets().add(constraintArg);
    NamedElmExpr inputId = af.createNamedElmExpr();
    inputId.setElm(EcoreUtil.copy(inputArg));
    RealLitExpr domainCheckLowerLit = af.createRealLitExpr();
    domainCheckLowerLit.setVal(Double.toString(segs.lower.getFirst().startX));
    BinaryExpr domainCheckLowerExpr = af.createBinaryExpr();
    domainCheckLowerExpr.setOp("<=");
    domainCheckLowerExpr.setLeft(domainCheckLowerLit);
    domainCheckLowerExpr.setRight(EcoreUtil.copy(inputId));
    RealLitExpr domainCheckUpperLit = af.createRealLitExpr();
    domainCheckUpperLit.setVal(Double.toString(segs.lower.getLast().stopX));
    BinaryExpr domainCheckUpperExpr = af.createBinaryExpr();
    domainCheckUpperExpr.setOp("<=");
    domainCheckUpperExpr.setLeft(EcoreUtil.copy(inputId));
    domainCheckUpperExpr.setRight(domainCheckUpperLit);
    BinaryExpr domainCheckExpr = af.createBinaryExpr();
    domainCheckExpr.setOp("and");
    domainCheckExpr.setLeft(domainCheckLowerExpr);
    domainCheckExpr.setRight(domainCheckUpperExpr);
    BoolLitExpr trueLitExpr = af.createBoolLitExpr();
    BooleanLiteral trueLitValue = aadlF.createBooleanLiteral();
    trueLitValue.setValue(true);
    trueLitExpr.setVal(trueLitValue);
    Expr upperBoundExpr = EcoreUtil.copy(trueLitExpr);
    for (Segment seg : segs.upper) {
        BinaryExpr andExpr = af.createBinaryExpr();
        andExpr.setOp("and");
        andExpr.setLeft(upperBoundExpr);
        andExpr.setRight(generateAgreeLinearBoundImplicationExpr(inputArg, outputArg, "<=", seg));
        upperBoundExpr = andExpr;
    }
    Expr lowerBoundExpr = EcoreUtil.copy(trueLitExpr);
    for (Segment seg : segs.lower) {
        BinaryExpr andExpr = af.createBinaryExpr();
        andExpr.setOp("and");
        andExpr.setLeft(lowerBoundExpr);
        andExpr.setRight(generateAgreeLinearBoundImplicationExpr(inputArg, outputArg, ">=", seg));
        lowerBoundExpr = andExpr;
    }
    BinaryExpr boundsCheckExpr = af.createBinaryExpr();
    boundsCheckExpr.setOp("and");
    boundsCheckExpr.setLeft(upperBoundExpr);
    boundsCheckExpr.setRight(lowerBoundExpr);
    BinaryExpr constraintExpr = af.createBinaryExpr();
    constraintExpr.setOp("and");
    constraintExpr.setLeft(domainCheckExpr);
    constraintExpr.setRight(boundsCheckExpr);
    NodeEq constraintEq = af.createNodeEq();
    constraintEq.getLhs().add(constraintArg);
    constraintEq.setExpr(constraintExpr);
    NodeBodyExpr nodeBody = af.createNodeBodyExpr();
    nodeBody.getStmts().add(constraintEq);
    result.setNodeBody(nodeBody);
    NodeLemma domainCheckLemma = af.createNodeLemma();
    domainCheckLemma.setStr(result.getName() + " domain check");
    domainCheckLemma.setExpr(EcoreUtil.copy(domainCheckExpr));
    nodeBody.getStmts().add(domainCheckLemma);
    return result;
}
Also used : BoolLitExpr(com.rockwellcollins.atc.agree.agree.BoolLitExpr) NodeDef(com.rockwellcollins.atc.agree.agree.NodeDef) NodeLemma(com.rockwellcollins.atc.agree.agree.NodeLemma) BooleanLiteral(org.osate.aadl2.BooleanLiteral) BinaryExpr(com.rockwellcollins.atc.agree.agree.BinaryExpr) NodeEq(com.rockwellcollins.atc.agree.agree.NodeEq) Segment(com.rockwellcollins.atc.agree.analysis.linearization.Linearize.Segment) NodeBodyExpr(com.rockwellcollins.atc.agree.agree.NodeBodyExpr) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) Expr(com.rockwellcollins.atc.agree.agree.Expr) BoolLitExpr(com.rockwellcollins.atc.agree.agree.BoolLitExpr) NodeBodyExpr(com.rockwellcollins.atc.agree.agree.NodeBodyExpr) BinaryExpr(com.rockwellcollins.atc.agree.agree.BinaryExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr) Arg(com.rockwellcollins.atc.agree.agree.Arg) PrimType(com.rockwellcollins.atc.agree.agree.PrimType) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr)

Example 7 with RealLitExpr

use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.

the class LinearizationRewriter method generateAgreeLinearBoundImplicationExpr.

private static Expr generateAgreeLinearBoundImplicationExpr(Arg inputArg, Arg resultArg, String relop, Segment seg) {
    RealLitExpr inputMinExpr = af.createRealLitExpr();
    inputMinExpr.setVal(Double.toString(seg.startX));
    RealLitExpr inputMaxExpr = af.createRealLitExpr();
    inputMaxExpr.setVal(Double.toString(seg.stopX));
    RealLitExpr resultOriginExpr = af.createRealLitExpr();
    resultOriginExpr.setVal(Double.toString(seg.startY));
    RealLitExpr resultSlopeExpr = af.createRealLitExpr();
    resultSlopeExpr.setVal(Double.toString((seg.stopY - seg.startY) / (seg.stopX - seg.startX)));
    NamedElmExpr inputId = af.createNamedElmExpr();
    inputId.setElm(EcoreUtil.copy(inputArg));
    NamedElmExpr resultId = af.createNamedElmExpr();
    resultId.setElm(EcoreUtil.copy(resultArg));
    // =======
    // 
    // NestedDotID resultId = af.createNestedDotID();
    // resultId.setBase(EcoreUtil.copy(resultArg));
    // >>>>>>> origin/develop
    BinaryExpr rangeMinExpr = af.createBinaryExpr();
    rangeMinExpr.setOp(">=");
    rangeMinExpr.setLeft(EcoreUtil.copy(inputId));
    rangeMinExpr.setRight(EcoreUtil.copy(inputMinExpr));
    BinaryExpr rangeMaxExpr = af.createBinaryExpr();
    rangeMaxExpr.setOp("<=");
    rangeMaxExpr.setLeft(EcoreUtil.copy(inputId));
    rangeMaxExpr.setRight(EcoreUtil.copy(inputMaxExpr));
    BinaryExpr rangeExpr = af.createBinaryExpr();
    rangeExpr.setOp("and");
    rangeExpr.setLeft(EcoreUtil.copy(rangeMinExpr));
    rangeExpr.setRight(EcoreUtil.copy(rangeMaxExpr));
    BinaryExpr shiftExpr = af.createBinaryExpr();
    shiftExpr.setOp("-");
    shiftExpr.setLeft(EcoreUtil.copy(inputId));
    shiftExpr.setRight(EcoreUtil.copy(inputMinExpr));
    BinaryExpr multiplyExpr = af.createBinaryExpr();
    multiplyExpr.setOp("*");
    multiplyExpr.setLeft(EcoreUtil.copy(resultSlopeExpr));
    multiplyExpr.setRight(shiftExpr);
    BinaryExpr additionExpr = af.createBinaryExpr();
    additionExpr.setOp("+");
    additionExpr.setLeft(EcoreUtil.copy(resultOriginExpr));
    additionExpr.setRight(multiplyExpr);
    BinaryExpr linearBoundExpr = af.createBinaryExpr();
    linearBoundExpr.setOp(relop);
    linearBoundExpr.setLeft(EcoreUtil.copy(resultId));
    linearBoundExpr.setRight(additionExpr);
    BinaryExpr result = af.createBinaryExpr();
    result.setOp("=>");
    result.setLeft(rangeExpr);
    result.setRight(linearBoundExpr);
    return result;
}
Also used : BinaryExpr(com.rockwellcollins.atc.agree.agree.BinaryExpr) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr)

Example 8 with RealLitExpr

use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.

the class Util method getDoubleValue.

public static Double getDoubleValue(Expr expr) {
    Double result = Double.valueOf(0.0);
    assert (AgreeJavaValidator.exprIsConst(expr));
    if (expr instanceof NamedElement) {
        if (expr instanceof ConstStatement) {
            result = getDoubleValue(((ConstStatement) expr).getExpr());
        }
    } else if (expr instanceof SelectionExpr) {
        NamedElement finalId = ((SelectionExpr) expr).getField();
        if (finalId instanceof ConstStatement) {
            result = getDoubleValue(((ConstStatement) finalId).getExpr());
        }
    } else if (expr instanceof RealLitExpr) {
        result = Double.valueOf(((RealLitExpr) expr).getVal());
    } else if (expr instanceof IntLitExpr) {
        result = Double.valueOf(((IntLitExpr) expr).getVal());
    } else if (expr instanceof BinaryExpr) {
        BinaryExpr binExpr = (BinaryExpr) expr;
        Double left = getDoubleValue(binExpr.getLeft());
        Double right = getDoubleValue(binExpr.getRight());
        switch(binExpr.getOp()) {
            case "+":
                result = left + right;
                break;
            case "-":
                result = left - right;
                break;
            case "*":
                result = left * right;
                break;
            case "/":
                result = left / right;
                break;
            case "^":
                result = Math.pow(left, right);
                break;
            default:
                throw new AgreeException("binary expression is not evaluable as integer constant");
        }
        return result;
    } else if (expr instanceof UnaryExpr) {
        UnaryExpr unExpr = (UnaryExpr) expr;
        Double right = getDoubleValue(unExpr.getExpr());
        switch(unExpr.getOp()) {
            case "-":
                result = -right;
                break;
            default:
                throw new AgreeException("unary expression is not evaluable as integer constant");
        }
    } else {
        throw new AgreeException("expression is not evaluable as integer constant");
    }
    return result;
}
Also used : ConstStatement(com.rockwellcollins.atc.agree.agree.ConstStatement) IntLitExpr(com.rockwellcollins.atc.agree.agree.IntLitExpr) BinaryExpr(com.rockwellcollins.atc.agree.agree.BinaryExpr) AgreeException(com.rockwellcollins.atc.agree.analysis.AgreeException) SelectionExpr(com.rockwellcollins.atc.agree.agree.SelectionExpr) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) NamedElement(org.osate.aadl2.NamedElement) UnaryExpr(com.rockwellcollins.atc.agree.agree.UnaryExpr)

Example 9 with RealLitExpr

use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.

the class AgreeValidator method checkWhenHoldsStatement.

@Check(CheckType.FAST)
public void checkWhenHoldsStatement(WhenHoldsStatement when) {
    Expr condition = when.getCondition();
    Expr event = when.getEvent();
    TimeInterval condInterval = when.getConditionInterval();
    checkExprIsIdentifier(condition);
    checkExprIsIdentifier(event);
    if (condInterval != null) {
        Expr lowExpr = condInterval.getLow();
        if (lowExpr instanceof RealLitExpr) {
            RealLitExpr realExpr = (RealLitExpr) lowExpr;
            if (!realExpr.getVal().equals("0.0")) {
                error(lowExpr, "The lower bound of this interval must be zero");
            }
        }
    } else {
        error(when, "Statement most of a cause interval");
    }
    TypeDef type = AgreeTypeSystem.infer(condition);
    if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, type)) {
        error(condition, "The condition of a when statement is of type '" + type + "'" + " but must be of type 'bool'");
    }
    type = AgreeTypeSystem.infer(event);
    if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, type)) {
        error(event, "The effect of a when statement is of type '" + type + "'" + " but must be of type 'bool'");
    }
}
Also used : EnumLitExpr(com.rockwellcollins.atc.agree.agree.EnumLitExpr) IndicesExpr(com.rockwellcollins.atc.agree.agree.IndicesExpr) TimeRiseExpr(com.rockwellcollins.atc.agree.agree.TimeRiseExpr) FlatmapExpr(com.rockwellcollins.atc.agree.agree.FlatmapExpr) TimeFallExpr(com.rockwellcollins.atc.agree.agree.TimeFallExpr) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) GetPropertyExpr(com.rockwellcollins.atc.agree.agree.GetPropertyExpr) TimeOfExpr(com.rockwellcollins.atc.agree.agree.TimeOfExpr) PrevExpr(com.rockwellcollins.atc.agree.agree.PrevExpr) FoldRightExpr(com.rockwellcollins.atc.agree.agree.FoldRightExpr) TagExpr(com.rockwellcollins.atc.agree.agree.TagExpr) EventExpr(com.rockwellcollins.atc.agree.agree.EventExpr) LatchedExpr(com.rockwellcollins.atc.agree.agree.LatchedExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr) SelectionExpr(com.rockwellcollins.atc.agree.agree.SelectionExpr) IfThenElseExpr(com.rockwellcollins.atc.agree.agree.IfThenElseExpr) ArraySubExpr(com.rockwellcollins.atc.agree.agree.ArraySubExpr) PreExpr(com.rockwellcollins.atc.agree.agree.PreExpr) RecordLitExpr(com.rockwellcollins.atc.agree.agree.RecordLitExpr) BinaryExpr(com.rockwellcollins.atc.agree.agree.BinaryExpr) UnaryExpr(com.rockwellcollins.atc.agree.agree.UnaryExpr) ExistsExpr(com.rockwellcollins.atc.agree.agree.ExistsExpr) FoldLeftExpr(com.rockwellcollins.atc.agree.agree.FoldLeftExpr) RecordUpdateExpr(com.rockwellcollins.atc.agree.agree.RecordUpdateExpr) ForallExpr(com.rockwellcollins.atc.agree.agree.ForallExpr) Expr(com.rockwellcollins.atc.agree.agree.Expr) ArrayUpdateExpr(com.rockwellcollins.atc.agree.agree.ArrayUpdateExpr) BoolLitExpr(com.rockwellcollins.atc.agree.agree.BoolLitExpr) NodeBodyExpr(com.rockwellcollins.atc.agree.agree.NodeBodyExpr) IntLitExpr(com.rockwellcollins.atc.agree.agree.IntLitExpr) CallExpr(com.rockwellcollins.atc.agree.agree.CallExpr) ArrayLiteralExpr(com.rockwellcollins.atc.agree.agree.ArrayLiteralExpr) TimeInterval(com.rockwellcollins.atc.agree.agree.TimeInterval) AgreeTypeSystem.nameOfTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.nameOfTypeDef) ArrayTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.ArrayTypeDef) RecordTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.RecordTypeDef) TypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.TypeDef) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) Check(org.eclipse.xtext.validation.Check)

Example 10 with RealLitExpr

use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.

the class AgreeValidator method checkPeriodicStatement.

@Check(CheckType.FAST)
public void checkPeriodicStatement(PeriodicStatement statement) {
    Expr event = statement.getEvent();
    Expr jitter = statement.getJitter();
    Expr period = statement.getPeriod();
    checkExprIsIdentifier(event);
    TypeDef eventType = AgreeTypeSystem.infer(event);
    if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, eventType)) {
        error(event, "Expression is of type '" + eventType + "' but must be of type 'bool'");
    }
    if (jitter != null) {
        if (!(jitter instanceof RealLitExpr || isTimingConst(jitter))) {
            error(jitter, "The specified jitter must be a real literal");
        } else {
            Double val = getRealConstVal(jitter);
            if (val < 0) {
                error(jitter, "The specified jitter must be positive");
            }
        }
    }
    if (!(period instanceof RealLitExpr || isTimingConst(period))) {
        error(period, "The specified period must be a real literal");
    } else {
        Double val = getRealConstVal(period);
        if (val < 0) {
            error(period, "The specified period must be positive");
        }
    }
}
Also used : EnumLitExpr(com.rockwellcollins.atc.agree.agree.EnumLitExpr) IndicesExpr(com.rockwellcollins.atc.agree.agree.IndicesExpr) TimeRiseExpr(com.rockwellcollins.atc.agree.agree.TimeRiseExpr) FlatmapExpr(com.rockwellcollins.atc.agree.agree.FlatmapExpr) TimeFallExpr(com.rockwellcollins.atc.agree.agree.TimeFallExpr) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) GetPropertyExpr(com.rockwellcollins.atc.agree.agree.GetPropertyExpr) TimeOfExpr(com.rockwellcollins.atc.agree.agree.TimeOfExpr) PrevExpr(com.rockwellcollins.atc.agree.agree.PrevExpr) FoldRightExpr(com.rockwellcollins.atc.agree.agree.FoldRightExpr) TagExpr(com.rockwellcollins.atc.agree.agree.TagExpr) EventExpr(com.rockwellcollins.atc.agree.agree.EventExpr) LatchedExpr(com.rockwellcollins.atc.agree.agree.LatchedExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr) SelectionExpr(com.rockwellcollins.atc.agree.agree.SelectionExpr) IfThenElseExpr(com.rockwellcollins.atc.agree.agree.IfThenElseExpr) ArraySubExpr(com.rockwellcollins.atc.agree.agree.ArraySubExpr) PreExpr(com.rockwellcollins.atc.agree.agree.PreExpr) RecordLitExpr(com.rockwellcollins.atc.agree.agree.RecordLitExpr) BinaryExpr(com.rockwellcollins.atc.agree.agree.BinaryExpr) UnaryExpr(com.rockwellcollins.atc.agree.agree.UnaryExpr) ExistsExpr(com.rockwellcollins.atc.agree.agree.ExistsExpr) FoldLeftExpr(com.rockwellcollins.atc.agree.agree.FoldLeftExpr) RecordUpdateExpr(com.rockwellcollins.atc.agree.agree.RecordUpdateExpr) ForallExpr(com.rockwellcollins.atc.agree.agree.ForallExpr) Expr(com.rockwellcollins.atc.agree.agree.Expr) ArrayUpdateExpr(com.rockwellcollins.atc.agree.agree.ArrayUpdateExpr) BoolLitExpr(com.rockwellcollins.atc.agree.agree.BoolLitExpr) NodeBodyExpr(com.rockwellcollins.atc.agree.agree.NodeBodyExpr) IntLitExpr(com.rockwellcollins.atc.agree.agree.IntLitExpr) CallExpr(com.rockwellcollins.atc.agree.agree.CallExpr) ArrayLiteralExpr(com.rockwellcollins.atc.agree.agree.ArrayLiteralExpr) AgreeTypeSystem.nameOfTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.nameOfTypeDef) ArrayTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.ArrayTypeDef) RecordTypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.RecordTypeDef) TypeDef(com.rockwellcollins.atc.agree.AgreeTypeSystem.TypeDef) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) Check(org.eclipse.xtext.validation.Check)

Aggregations

RealLitExpr (com.rockwellcollins.atc.agree.agree.RealLitExpr)10 BinaryExpr (com.rockwellcollins.atc.agree.agree.BinaryExpr)8 BoolLitExpr (com.rockwellcollins.atc.agree.agree.BoolLitExpr)8 IntLitExpr (com.rockwellcollins.atc.agree.agree.IntLitExpr)8 NamedElmExpr (com.rockwellcollins.atc.agree.agree.NamedElmExpr)8 Expr (com.rockwellcollins.atc.agree.agree.Expr)7 UnaryExpr (com.rockwellcollins.atc.agree.agree.UnaryExpr)7 NodeBodyExpr (com.rockwellcollins.atc.agree.agree.NodeBodyExpr)6 SelectionExpr (com.rockwellcollins.atc.agree.agree.SelectionExpr)6 CallExpr (com.rockwellcollins.atc.agree.agree.CallExpr)5 EnumLitExpr (com.rockwellcollins.atc.agree.agree.EnumLitExpr)5 EventExpr (com.rockwellcollins.atc.agree.agree.EventExpr)5 IfThenElseExpr (com.rockwellcollins.atc.agree.agree.IfThenElseExpr)5 PreExpr (com.rockwellcollins.atc.agree.agree.PreExpr)5 PrevExpr (com.rockwellcollins.atc.agree.agree.PrevExpr)5 RecordLitExpr (com.rockwellcollins.atc.agree.agree.RecordLitExpr)5 Check (org.eclipse.xtext.validation.Check)5 ArrayLiteralExpr (com.rockwellcollins.atc.agree.agree.ArrayLiteralExpr)4 ArraySubExpr (com.rockwellcollins.atc.agree.agree.ArraySubExpr)4 ArrayUpdateExpr (com.rockwellcollins.atc.agree.agree.ArrayUpdateExpr)4