Search in sources :

Example 1 with BinaryExpr

use of com.rockwellcollins.atc.agree.agree.BinaryExpr in project VERDICT by ge-high-assurance.

the class Agree2Vdm method getVdmExpressionFromAgreeExpression.

// method to translate expression in Agree to expression in vdm
private Expression getVdmExpressionFromAgreeExpression(Expr agreeExpr, HashSet<String> dataTypeDecl, HashSet<String> nodeDecl, Model model) {
    Expression vdmExpr = new Expression();
    if (agreeExpr instanceof IfThenElseExpr) {
        IfThenElseExpr ifexpr = (IfThenElseExpr) agreeExpr;
        // for vdm model
        IfThenElse ifThenElseVal = new IfThenElse();
        Expression condExpr = getVdmExpressionFromAgreeExpression(ifexpr.getA(), dataTypeDecl, nodeDecl, model);
        ifThenElseVal.setCondition(condExpr);
        Expression thenBranchExpr = getVdmExpressionFromAgreeExpression(ifexpr.getB(), dataTypeDecl, nodeDecl, model);
        ifThenElseVal.setThenBranch(thenBranchExpr);
        Expression elseBranchExpr = getVdmExpressionFromAgreeExpression(ifexpr.getC(), dataTypeDecl, nodeDecl, model);
        ifThenElseVal.setElseBranch(elseBranchExpr);
        vdmExpr.setConditionalExpression(ifThenElseVal);
    } else if (agreeExpr instanceof CallExpr) {
        CallExpr callExpr = (CallExpr) agreeExpr;
        DoubleDotRef ddref = (DoubleDotRef) callExpr.getRef();
        if (ddref.getElm() instanceof NodeDef) {
            NodeDef nodeDef = (NodeDef) ddref.getElm();
            addNodeDefToTypeDeclarations(nodeDef, dataTypeDecl, nodeDecl, model);
            // create node call in vdm model
            NodeCall vdmNodeCall = new NodeCall();
            // setting node name
            vdmNodeCall.setNodeId(nodeDef.getName());
            EList<Expr> callExprArgs = callExpr.getArgs();
            // below are the parameters passed to the function call
            for (Expr callExprArg : callExprArgs) {
                Expression argExpr = getVdmExpressionFromAgreeExpression(callExprArg, dataTypeDecl, nodeDecl, model);
                // setting node arguments
                vdmNodeCall.getArgument().add(argExpr);
            }
            vdmExpr.setCall(vdmNodeCall);
        } else {
            System.out.println("Unmapped Typed");
        }
    } else if (agreeExpr instanceof NamedElmExpr) {
        NamedElmExpr nmExpr = (NamedElmExpr) agreeExpr;
        vdmExpr.setIdentifier(nmExpr.getElm().getName());
        // define corresponding types in the VDM if not already defined
        if (nmExpr.getElm() instanceof Arg) {
            Arg nmElmArg = (Arg) nmExpr.getElm();
            // define corresponding type in the VDM if not already defined
            Type argType = nmElmArg.getType();
            defineDataTypeDataImplementationTypeInVDM(argType, dataTypeDecl, model);
        } else if (nmExpr.getElm() instanceof Port) {
            Port nmElmPort = (Port) nmExpr.getElm();
            // define corresponding type in the VDM if not already defined
            if (nmElmPort instanceof DataPortImpl) {
                DataPort nmElmDataPort = (DataPort) nmElmPort;
                DataSubcomponentType dSubCompType = nmElmDataPort.getDataFeatureClassifier();
                defineDataTypeDataImplementationTypeInVDM(dSubCompType, dataTypeDecl, model);
            } else if (nmElmPort instanceof EventDataPortImpl) {
                EventDataPort nmElmDataPort = (EventDataPort) nmElmPort;
                DataSubcomponentType dSubCompType = nmElmDataPort.getDataFeatureClassifier();
                defineDataTypeDataImplementationTypeInVDM(dSubCompType, dataTypeDecl, model);
            } else {
                if (!(nmElmPort instanceof EventPort)) {
                    System.out.println("Unresolved Port Type");
                }
            }
        } else if (nmExpr.getElm() instanceof ConstStatement) {
            ConstStatement nmElmConstStatement = (ConstStatement) nmExpr.getElm();
            String nmElmConstStatementName = nmElmConstStatement.getName();
            // add const declaration to VDM if not already defined
            if (!dataTypeDecl.contains(nmElmConstStatementName)) {
                dataTypeDecl.add(nmElmConstStatementName);
                ConstantDeclaration vdmConstDeclaration = new ConstantDeclaration();
                vdmConstDeclaration.setName(nmElmConstStatementName);
                vdmConstDeclaration.setDefinition(getVdmExpressionFromAgreeExpression(nmElmConstStatement.getExpr(), dataTypeDecl, nodeDecl, model));
                vdmConstDeclaration.setDataType(getVdmTypeFromAgreeType(nmElmConstStatement.getType(), dataTypeDecl, model));
                LustreProgram lustreProgram = model.getDataflowCode();
                lustreProgram.getConstantDeclaration().add(vdmConstDeclaration);
                model.setDataflowCode(lustreProgram);
            }
        } else {
            System.out.println("Unresolved/unmapped NamedExprElm: " + nmExpr.getElm().getName());
        }
    } else if (agreeExpr instanceof SelectionExpr) {
        // selection expression corresponds to record projection in VDM
        RecordProjection vdmRecordProj = new RecordProjection();
        SelectionExpr selExpr = (SelectionExpr) agreeExpr;
        if (selExpr.getField() == null) {
            System.out.println("Null Selection Expr field: " + selExpr.getField());
        } else {
            NamedElement field = selExpr.getField();
            // set record-projection's reference
            if (selExpr.getTarget() != null) {
                // target can be NamedElmExpr or a SelectionExpr
                vdmRecordProj.setRecordReference(getVdmExpressionFromAgreeExpression(selExpr.getTarget(), dataTypeDecl, nodeDecl, model));
            }
            // set record-projection's field id
            vdmRecordProj.setFieldId(field.getName());
            // also set the name of the field's type as the record-projection's record-type
            if (field instanceof DataPortImpl) {
                DataPort dport = (DataPort) field;
                DataSubcomponentType dSubCompType = dport.getDataFeatureClassifier();
                defineDataTypeDataImplementationTypeInVDM(dSubCompType, dataTypeDecl, model);
                vdmRecordProj.setRecordType(dSubCompType.getName());
            } else if (field instanceof DataSubcomponentImpl) {
                DataSubcomponent dSubComp = (DataSubcomponent) field;
                DataSubcomponentType dSubCompType = dSubComp.getDataSubcomponentType();
                defineDataTypeDataImplementationTypeInVDM(dSubCompType, dataTypeDecl, model);
                vdmRecordProj.setRecordType(dSubCompType.getName());
            } else if (field instanceof ArgImpl) {
                Arg arg = (Arg) field;
                Type argType = arg.getType();
                defineDataTypeDataImplementationTypeInVDM(argType, dataTypeDecl, model);
                if (argType instanceof PrimType) {
                    vdmRecordProj.setRecordType(getDataTypeName(argType));
                } else {
                    System.out.println("Unresolved Arg Type so not setting record-type in record-projection.");
                }
            } else {
                System.out.println("Unresolved type of field.");
            }
        }
        vdmExpr.setRecordProjection(vdmRecordProj);
    } else if (agreeExpr instanceof BinaryExpr) {
        BinaryExpr binExpr = (BinaryExpr) agreeExpr;
        // for vdm
        BinaryOperation binoper = new BinaryOperation();
        // set left operand
        Expression leftOperand = getVdmExpressionFromAgreeExpression(binExpr.getLeft(), dataTypeDecl, nodeDecl, model);
        binoper.setLhsOperand(leftOperand);
        // set right operand
        Expression rightOperand = getVdmExpressionFromAgreeExpression(binExpr.getRight(), dataTypeDecl, nodeDecl, model);
        binoper.setRhsOperand(rightOperand);
        // set appropriate operator
        String operator = binExpr.getOp();
        if (operator.equalsIgnoreCase("->")) {
            vdmExpr.setArrow(binoper);
        } else if (operator.equalsIgnoreCase("=>")) {
            vdmExpr.setImplies(binoper);
        } else if (operator.equalsIgnoreCase("and")) {
            vdmExpr.setAnd(binoper);
        } else if (operator.equalsIgnoreCase("or")) {
            vdmExpr.setOr(binoper);
        } else if (operator.equalsIgnoreCase("=")) {
            vdmExpr.setEqual(binoper);
        } else if (operator.equalsIgnoreCase(">")) {
            vdmExpr.setGreaterThan(binoper);
        } else if (operator.equalsIgnoreCase("<")) {
            vdmExpr.setLessThan(binoper);
        } else if (operator.equalsIgnoreCase(">=")) {
            vdmExpr.setGreaterThanOrEqualTo(binoper);
        } else if (operator.equalsIgnoreCase("<=")) {
            vdmExpr.setLessThanOrEqualTo(binoper);
        } else if (operator.equalsIgnoreCase("+")) {
            vdmExpr.setPlus(binoper);
        } else if (operator.equalsIgnoreCase("-")) {
            vdmExpr.setMinus(binoper);
        } else if (operator.equalsIgnoreCase("!=") || operator.equalsIgnoreCase("<>")) {
            vdmExpr.setNotEqual(binoper);
        } else if (operator.equalsIgnoreCase("/")) {
            vdmExpr.setDiv(binoper);
        } else if (operator.equalsIgnoreCase("*")) {
            vdmExpr.setTimes(binoper);
        } else {
            System.out.println("Unmapped binary operator: " + operator);
        }
    } else if (agreeExpr instanceof UnaryExpr) {
        UnaryExpr unExpr = (UnaryExpr) agreeExpr;
        Expression singleOperand = getVdmExpressionFromAgreeExpression(unExpr.getExpr(), dataTypeDecl, nodeDecl, model);
        String operator = unExpr.getOp();
        if (operator.equalsIgnoreCase("this")) {
            vdmExpr.setCurrent(singleOperand);
        } else if (operator.equalsIgnoreCase("not")) {
            vdmExpr.setNot(singleOperand);
        } else if (operator.equalsIgnoreCase("-")) {
            vdmExpr.setNegative(singleOperand);
        } else {
            System.out.println("Unmapped unary operator.");
        }
    } else if (agreeExpr instanceof BoolLitExpr) {
        BoolLitExpr boolExpr = (BoolLitExpr) agreeExpr;
        vdmExpr.setBoolLiteral(boolExpr.getVal().getValue());
    } else if (agreeExpr instanceof EnumLitExpr) {
        EnumLitExpr enumExpr = (EnumLitExpr) agreeExpr;
        // check if elm is DataImplementationImpl or DataTypeImpl -- if yes add definition to type declarations if not already present
        DoubleDotRef enumType = enumExpr.getEnumType();
        if (enumType.getElm() instanceof DataTypeImpl) {
            org.osate.aadl2.DataType aadlDType = (org.osate.aadl2.DataType) enumType.getElm();
            resolveAADLDataType(aadlDType, dataTypeDecl, model);
        } else {
            System.out.println("Unexpected Elm type for EnumLitExpr");
        }
        vdmExpr.setIdentifier(enumExpr.getValue());
    } else if (agreeExpr instanceof PreExpr) {
        PreExpr preExpr = (PreExpr) agreeExpr;
        Expression expr = getVdmExpressionFromAgreeExpression(preExpr.getExpr(), dataTypeDecl, nodeDecl, model);
        vdmExpr.setPre(expr);
    } else if (agreeExpr instanceof RecordLitExpr) {
        RecordLiteral vdmRecordLiteral = new RecordLiteral();
        RecordLitExpr recLitExpr = (RecordLitExpr) agreeExpr;
        if (recLitExpr.getRecordType() instanceof DoubleDotRef) {
            DoubleDotRef recType = (DoubleDotRef) recLitExpr.getRecordType();
            if (recType.getElm().getName() != null) {
                // check if elm is DataImplementationImpl -- if yes add definition to type declarations if not already present
                if (recType.getElm() instanceof DataImplementation) {
                    org.osate.aadl2.DataImplementation aadlDImpl = (org.osate.aadl2.DataImplementation) recType.getElm();
                    resolveAADLDataImplementationType(aadlDImpl, dataTypeDecl, model);
                } else {
                    System.out.println("Unexpected Elm type for EnumLitExpr");
                }
                // set name of the record literal in the vdm model
                vdmRecordLiteral.setRecordType(recType.getElm().getName());
                // get args and arg-expr and set them as field identifier and value in the vdm model
                EList<NamedElement> recLitArgs = recLitExpr.getArgs();
                EList<Expr> recLitArgsExpr = recLitExpr.getArgExpr();
                // below are the values set to variable
                for (int ind = 0; ind < recLitArgs.size(); ind++) {
                    FieldDefinition fieldDef = new FieldDefinition();
                    fieldDef.setFieldIdentifier(recLitArgs.get(ind).getName());
                    fieldDef.setFieldValue(getVdmExpressionFromAgreeExpression(recLitArgsExpr.get(ind), dataTypeDecl, nodeDecl, model));
                    // set field definitions in the record literal in the vdm model
                    vdmRecordLiteral.getFieldDefinition().add(fieldDef);
                }
                vdmExpr.setRecordLiteral(vdmRecordLiteral);
            } else {
                System.out.println("Unexpected Literal's Record Type that has null named elm.");
            }
        } else {
            System.out.println("Unresolved or unmapped record literal expression.");
        }
    } else if (agreeExpr instanceof IntLitExpr) {
        IntLitExpr intLitExpr = (IntLitExpr) agreeExpr;
        BigInteger bigInt = new BigInteger(intLitExpr.getVal());
        vdmExpr.setIntLiteral(bigInt);
    } else if (agreeExpr instanceof RealLitExpr) {
        RealLitExpr realLitExpr = (RealLitExpr) agreeExpr;
        BigDecimal bigDecimal = new BigDecimal(realLitExpr.getVal());
        vdmExpr.setRealLiteral(bigDecimal);
    } else if (agreeExpr instanceof EventExpr) {
        EventExpr eventExpr = (EventExpr) agreeExpr;
        if (eventExpr.getPort() != null) {
            vdmExpr.setEvent(getVdmExpressionFromAgreeExpression(eventExpr.getPort(), dataTypeDecl, nodeDecl, model));
        } else {
            System.out.println("EventExpr does not have port infornation.");
        }
    } else if (agreeExpr instanceof RealCast) {
        RealCast realCastExpr = (RealCast) agreeExpr;
        vdmExpr.setToReal(getVdmExpressionFromAgreeExpression(realCastExpr.getExpr(), dataTypeDecl, nodeDecl, model));
    } else {
        System.out.println("Unresolved/umapped agree expr" + agreeExpr.toString());
    }
    return vdmExpr;
}
Also used : BinaryOperation(verdict.vdm.vdm_lustre.BinaryOperation) EventPort(org.osate.aadl2.EventPort) DataPort(org.osate.aadl2.DataPort) Port(org.osate.aadl2.Port) EventDataPort(org.osate.aadl2.EventDataPort) FieldDefinition(verdict.vdm.vdm_lustre.FieldDefinition) ConstantDeclaration(verdict.vdm.vdm_lustre.ConstantDeclaration) SelectionExpr(com.rockwellcollins.atc.agree.agree.SelectionExpr) DataPort(org.osate.aadl2.DataPort) EventDataPort(org.osate.aadl2.EventDataPort) RealCast(com.rockwellcollins.atc.agree.agree.RealCast) RecordProjection(verdict.vdm.vdm_lustre.RecordProjection) DoubleDotRef(com.rockwellcollins.atc.agree.agree.DoubleDotRef) PrimType(com.rockwellcollins.atc.agree.agree.PrimType) CallExpr(com.rockwellcollins.atc.agree.agree.CallExpr) EventDataPort(org.osate.aadl2.EventDataPort) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr) RecordLiteral(verdict.vdm.vdm_lustre.RecordLiteral) EnumLitExpr(com.rockwellcollins.atc.agree.agree.EnumLitExpr) EventDataPortImpl(org.osate.aadl2.impl.EventDataPortImpl) RecordLitExpr(com.rockwellcollins.atc.agree.agree.RecordLitExpr) UnaryExpr(com.rockwellcollins.atc.agree.agree.UnaryExpr) IfThenElseExpr(com.rockwellcollins.atc.agree.agree.IfThenElseExpr) ConstStatement(com.rockwellcollins.atc.agree.agree.ConstStatement) DataSubcomponentType(org.osate.aadl2.DataSubcomponentType) PreExpr(com.rockwellcollins.atc.agree.agree.PreExpr) EnumLitExpr(com.rockwellcollins.atc.agree.agree.EnumLitExpr) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) EventExpr(com.rockwellcollins.atc.agree.agree.EventExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr) SelectionExpr(com.rockwellcollins.atc.agree.agree.SelectionExpr) IfThenElseExpr(com.rockwellcollins.atc.agree.agree.IfThenElseExpr) 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) Expr(com.rockwellcollins.atc.agree.agree.Expr) 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) IntLitExpr(com.rockwellcollins.atc.agree.agree.IntLitExpr) Arg(com.rockwellcollins.atc.agree.agree.Arg) BigInteger(java.math.BigInteger) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) NamedElement(org.osate.aadl2.NamedElement) ArgImpl(com.rockwellcollins.atc.agree.agree.impl.ArgImpl) BoolLitExpr(com.rockwellcollins.atc.agree.agree.BoolLitExpr) NodeDef(com.rockwellcollins.atc.agree.agree.NodeDef) EventExpr(com.rockwellcollins.atc.agree.agree.EventExpr) DataSubcomponentImpl(org.osate.aadl2.impl.DataSubcomponentImpl) EventPort(org.osate.aadl2.EventPort) NodeCall(verdict.vdm.vdm_lustre.NodeCall) DataPortImpl(org.osate.aadl2.impl.DataPortImpl) EventDataPortImpl(org.osate.aadl2.impl.EventDataPortImpl) BinaryExpr(com.rockwellcollins.atc.agree.agree.BinaryExpr) DataImplementation(org.osate.aadl2.DataImplementation) BigDecimal(java.math.BigDecimal) PrimType(com.rockwellcollins.atc.agree.agree.PrimType) DataSubcomponentType(org.osate.aadl2.DataSubcomponentType) Type(com.rockwellcollins.atc.agree.agree.Type) EnumType(verdict.vdm.vdm_data.EnumType) RecordType(verdict.vdm.vdm_data.RecordType) SystemType(org.osate.aadl2.SystemType) ComponentType(verdict.vdm.vdm_model.ComponentType) EList(org.eclipse.emf.common.util.EList) PropertyExpression(org.osate.aadl2.PropertyExpression) Expression(verdict.vdm.vdm_lustre.Expression) LustreProgram(verdict.vdm.vdm_lustre.LustreProgram) DataSubcomponent(org.osate.aadl2.DataSubcomponent) DataTypeImpl(org.osate.aadl2.impl.DataTypeImpl) IfThenElse(verdict.vdm.vdm_lustre.IfThenElse)

Example 2 with BinaryExpr

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

the class AgreeValidator method checkBinaryExpr.

@Check(CheckType.FAST)
public void checkBinaryExpr(BinaryExpr binExpr) {
    checkTypeExists(binExpr.getLeft());
    checkTypeExists(binExpr.getRight());
    TypeDef typeLeft = AgreeTypeSystem.infer(binExpr.getLeft());
    TypeDef typeRight = AgreeTypeSystem.infer(binExpr.getRight());
    String op = binExpr.getOp();
    Expr rightSide = binExpr.getRight();
    Expr leftSide = binExpr.getLeft();
    boolean isInLinearizationBodyExpr = isInLinearizationBody(binExpr);
    boolean rightSideConst = exprIsConst(rightSide);
    boolean leftSideConst = exprIsConst(leftSide);
    switch(op) {
        case "->":
            if (isInLinearizationBodyExpr) {
                error(binExpr, "Arrow '->' expressions are not allowed in linearization body expressions.");
            } else {
                if (!AgreeTypeSystem.typesEqual(typeRight, typeLeft)) {
                    error(binExpr, "left and right sides of binary expression '" + op + "' are of type '" + nameOfTypeDef(typeLeft) + "' and '" + nameOfTypeDef(typeRight) + "', but must be of the same type");
                }
            }
            return;
        case "=>":
        case "<=>":
        case "and":
        case "or":
            if (isInLinearizationBodyExpr) {
                error(binExpr, "Logical expressions (like '" + op + "') are not allowed in linearization body expressions.");
            } else {
                if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, typeLeft)) {
                    error(binExpr, "left side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeLeft) + "' but must be of " + "type 'bool'");
                }
                if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, typeRight)) {
                    error(binExpr, "right side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeRight) + "' but must be of" + " type 'bool'");
                }
            }
            return;
        case "=":
        case "<>":
        case "!=":
            if (isInLinearizationBodyExpr) {
                error(binExpr, "Logical comparison expressions (like '" + op + "') are not allowed in linearization body expressions.");
            } else {
                if (!AgreeTypeSystem.typesEqual(typeRight, typeLeft)) {
                    error(binExpr, "left and right sides of binary expression '" + op + "' are of type '" + nameOfTypeDef(typeLeft) + "' and '" + nameOfTypeDef(typeRight) + "', but must be of the same type");
                }
            }
            return;
        case "<":
        case "<=":
        case ">":
        case ">=":
            if (isInLinearizationBodyExpr) {
                error(binExpr, "Comparison expressions (like '" + op + "') are not allowed in linearization body expressions.");
            } else {
                if (!AgreeTypeSystem.typesEqual(typeRight, typeLeft)) {
                    error(binExpr, "left and right sides of binary expression '" + op + "' are of type '" + nameOfTypeDef(typeLeft) + "' and '" + nameOfTypeDef(typeRight) + "', but must be of the same type");
                }
                if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.IntTypeDef, typeLeft) && !AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.RealTypeDef, typeLeft)) {
                    error(binExpr, "left side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeLeft) + "' but must be of type" + "'int' or 'real'");
                }
                if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.IntTypeDef, typeRight) && !AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.RealTypeDef, typeRight)) {
                    error(binExpr, "right side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeRight) + "' but must be of type" + "'int' or 'real'");
                }
            }
            return;
        case "+":
        case "-":
        case "*":
            if (!AgreeTypeSystem.typesEqual(typeRight, typeLeft)) {
                error(binExpr, "left and right sides of binary expression '" + op + "' are of type '" + nameOfTypeDef(typeLeft) + "' and '" + nameOfTypeDef(typeRight) + "', but must be of the same type");
            }
            if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.IntTypeDef, typeLeft) && !AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.RealTypeDef, typeLeft)) {
                error(binExpr, "left side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeLeft) + "' but must be of type" + "'int' or 'real'");
            }
            if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.IntTypeDef, typeRight) && !AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.RealTypeDef, typeRight)) {
                error(binExpr, "right side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeRight) + "' but must be of type" + "'int' or 'real'");
            }
            if (op.equals("*") && !isInLinearizationBodyExpr) {
                if (!rightSideConst && !leftSideConst) {
                    warning(binExpr, "neither the right nor the left side of binary expression '" + op + "' is constant'.  Non-linear expressions are allowed only with z3 and dReal." + " With z3 they are not recomended.");
                }
            }
            return;
        case "mod":
        case "div":
            if (isInLinearizationBodyExpr) {
                error(binExpr, "Integer operation expressions (like '" + op + "') are not allowed in linearization body expressions.");
            } else {
                if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.IntTypeDef, typeLeft)) {
                    error(binExpr, "left side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeLeft) + "' but must be of type 'int'");
                }
                if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.IntTypeDef, typeRight)) {
                    error(binExpr, "right side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeRight) + "' but must be of type 'int'");
                }
                if (!rightSideConst) {
                    warning(binExpr, "right side of binary expression '" + op + "' is not constant." + " Non-linear expressions are allowed only with z3." + " Even with z3 they are not recomended...");
                }
            }
            return;
        case "/":
            if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.RealTypeDef, typeLeft)) {
                error(binExpr, "left side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeLeft) + "' but must be of type 'real'");
            }
            if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.RealTypeDef, typeRight)) {
                error(binExpr, "right side of binary expression '" + op + "' is of type '" + nameOfTypeDef(typeRight) + "' but must be of type 'real'");
            }
            if (!rightSideConst && !isInLinearizationBodyExpr) {
                warning(binExpr, "right side of binary expression '" + op + "' is not constant." + " Non-linear expressions are allowed only with z3." + " Even with z3 they are not recomended...");
            }
            return;
        default:
            assert (false);
    }
}
Also used : 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) 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) Check(org.eclipse.xtext.validation.Check)

Example 3 with BinaryExpr

use of com.rockwellcollins.atc.agree.agree.BinaryExpr 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 4 with BinaryExpr

use of com.rockwellcollins.atc.agree.agree.BinaryExpr 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 5 with BinaryExpr

use of com.rockwellcollins.atc.agree.agree.BinaryExpr 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)

Aggregations

BinaryExpr (com.rockwellcollins.atc.agree.agree.BinaryExpr)6 NamedElmExpr (com.rockwellcollins.atc.agree.agree.NamedElmExpr)5 RealLitExpr (com.rockwellcollins.atc.agree.agree.RealLitExpr)5 IntLitExpr (com.rockwellcollins.atc.agree.agree.IntLitExpr)4 SelectionExpr (com.rockwellcollins.atc.agree.agree.SelectionExpr)4 UnaryExpr (com.rockwellcollins.atc.agree.agree.UnaryExpr)4 BoolLitExpr (com.rockwellcollins.atc.agree.agree.BoolLitExpr)3 Expr (com.rockwellcollins.atc.agree.agree.Expr)3 NodeBodyExpr (com.rockwellcollins.atc.agree.agree.NodeBodyExpr)3 Arg (com.rockwellcollins.atc.agree.agree.Arg)2 CallExpr (com.rockwellcollins.atc.agree.agree.CallExpr)2 ConstStatement (com.rockwellcollins.atc.agree.agree.ConstStatement)2 EnumLitExpr (com.rockwellcollins.atc.agree.agree.EnumLitExpr)2 EventExpr (com.rockwellcollins.atc.agree.agree.EventExpr)2 IfThenElseExpr (com.rockwellcollins.atc.agree.agree.IfThenElseExpr)2 NodeDef (com.rockwellcollins.atc.agree.agree.NodeDef)2 PreExpr (com.rockwellcollins.atc.agree.agree.PreExpr)2 PrimType (com.rockwellcollins.atc.agree.agree.PrimType)2 RecordLitExpr (com.rockwellcollins.atc.agree.agree.RecordLitExpr)2 ArrayTypeDef (com.rockwellcollins.atc.agree.AgreeTypeSystem.ArrayTypeDef)1