Search in sources :

Example 6 with Expression

use of verdict.vdm.vdm_lustre.Expression in project VERDICT by ge-high-assurance.

the class VerdictLustreListener method exitBinaryExpr.

/**
 * Extract a binary operation.
 */
@Override
public void exitBinaryExpr(LustreParser.BinaryExprContext ctx) {
    BinaryOperation binaryOperation = new BinaryOperation();
    binaryOperation.setLhsOperand(ctx.expr(0).expression);
    binaryOperation.setRhsOperand(ctx.expr(1).expression);
    ctx.expression = new Expression();
    switch(ctx.op.getText()) {
        case "^":
            ctx.expression.setCartesianExpression(binaryOperation);
            break;
        case "when":
            ctx.expression.setWhen(binaryOperation);
            break;
        case "*":
            ctx.expression.setTimes(binaryOperation);
            break;
        case "/":
            ctx.expression.setDiv(binaryOperation);
            break;
        case "%":
        case "mod":
            ctx.expression.setMod(binaryOperation);
            break;
        case "div":
            ctx.expression.setIntDiv(binaryOperation);
            break;
        case "+":
            ctx.expression.setPlus(binaryOperation);
            break;
        case "-":
            ctx.expression.setMinus(binaryOperation);
            break;
        case "<":
            ctx.expression.setLessThan(binaryOperation);
            break;
        case "<=":
            ctx.expression.setLessThanOrEqualTo(binaryOperation);
            break;
        case "=":
            ctx.expression.setEqual(binaryOperation);
            break;
        case ">=":
            ctx.expression.setGreaterThanOrEqualTo(binaryOperation);
            break;
        case ">":
            ctx.expression.setGreaterThan(binaryOperation);
            break;
        case "<>":
            ctx.expression.setNotEqual(binaryOperation);
            break;
        case "and":
            ctx.expression.setAnd(binaryOperation);
            break;
        case "or":
            ctx.expression.setOr(binaryOperation);
            break;
        case "xor":
            ctx.expression.setXor(binaryOperation);
            break;
        case "=>":
            ctx.expression.setImplies(binaryOperation);
            break;
        case "->":
        case "fby":
            ctx.expression.setArrow(binaryOperation);
            break;
        case "|":
            ctx.expression.setConcat(binaryOperation);
            break;
    }
}
Also used : Expression(verdict.vdm.vdm_lustre.Expression) BinaryOperation(verdict.vdm.vdm_lustre.BinaryOperation)

Example 7 with Expression

use of verdict.vdm.vdm_lustre.Expression in project VERDICT by ge-high-assurance.

the class VerdictLustreListener method exitIdExpr.

/**
 * Extract an identifier.
 */
@Override
public void exitIdExpr(LustreParser.IdExprContext ctx) {
    ctx.expression = new Expression();
    ctx.expression.setIdentifier(ctx.identifier().getText());
}
Also used : Expression(verdict.vdm.vdm_lustre.Expression)

Example 8 with Expression

use of verdict.vdm.vdm_lustre.Expression in project VERDICT by ge-high-assurance.

the class VDM2Lustre method eventExpression.

protected Expression eventExpression(Expression expr, boolean event_type) {
    Expression eventExpression = new Expression();
    if (expr != null) {
        RecordProjection recordProject = new RecordProjection();
        if (event_type) {
            recordProject.setFieldId("is_present");
        } else {
            recordProject.setFieldId("value");
        }
        recordProject.setRecordReference(expr);
        eventExpression.setRecordProjection(recordProject);
    }
    return eventExpression;
}
Also used : Expression(verdict.vdm.vdm_lustre.Expression) RecordProjection(verdict.vdm.vdm_lustre.RecordProjection)

Example 9 with Expression

use of verdict.vdm.vdm_lustre.Expression in project VERDICT by ge-high-assurance.

the class VDM2Lustre method visitExpression.

protected Expression visitExpression(Expression expr) {
    Expression u_Expr = new Expression();
    // Binary Operators
    if (expr != null) {
        BinaryOperation op = expr.getEqual();
        if (op != null) {
            u_Expr.setEqual(binaryOP(op));
        }
        op = expr.getNotEqual();
        if (op != null) {
            u_Expr.setNotEqual(binaryOP(op));
        }
        op = expr.getImplies();
        if (op != null) {
            u_Expr.setImplies(binaryOP(op));
        }
        op = expr.getAnd();
        if (op != null) {
            u_Expr.setAnd(binaryOP(op));
        }
        op = expr.getOr();
        if (op != null) {
            u_Expr.setOr(binaryOP(op));
        }
        op = expr.getXor();
        if (op != null) {
            u_Expr.setXor(binaryOP(op));
        }
        op = expr.getArrow();
        if (op != null) {
            u_Expr.setArrow(binaryOP(op));
        }
        op = expr.getLessThanOrEqualTo();
        if (op != null) {
            u_Expr.setLessThanOrEqualTo(binaryOP(op));
        }
        op = expr.getLessThan();
        if (op != null) {
            u_Expr.setLessThan(binaryOP(op));
        }
        op = expr.getGreaterThan();
        if (op != null) {
            u_Expr.setGreaterThan(binaryOP(op));
        }
        op = expr.getGreaterThanOrEqualTo();
        if (op != null) {
            u_Expr.setGreaterThanOrEqualTo(binaryOP(op));
        }
        op = expr.getMinus();
        if (op != null) {
            u_Expr.setMinus(binaryOP(op));
        }
        op = expr.getPlus();
        if (op != null) {
            u_Expr.setPlus(binaryOP(op));
        }
        op = expr.getDiv();
        if (op != null) {
            u_Expr.setDiv(binaryOP(op));
        }
        op = expr.getTimes();
        if (op != null) {
            u_Expr.setTimes(binaryOP(op));
        }
        op = expr.getMod();
        if (op != null) {
            u_Expr.setMod(binaryOP(op));
        }
        op = expr.getCartesianExpression();
        if (op != null) {
            u_Expr.setCartesianExpression(binaryOP(op));
        }
        IfThenElse cond_op = expr.getConditionalExpression();
        if (cond_op != null) {
            Expression cond_expr = cond_op.getCondition();
            cond_expr = visitExpression(cond_expr);
            Expression then_expr = cond_op.getThenBranch();
            then_expr = visitExpression(then_expr);
            Expression else_expr = cond_op.getElseBranch();
            else_expr = visitExpression(else_expr);
            cond_op.setCondition(cond_expr);
            cond_op.setThenBranch(then_expr);
            cond_op.setElseBranch(else_expr);
            u_Expr.setConditionalExpression(cond_op);
        }
        // Record Literal
        RecordLiteral recordLiteral = expr.getRecordLiteral();
        if (recordLiteral != null) {
            Expression fieldExpr;
            for (FieldDefinition fieldDef : recordLiteral.getFieldDefinition()) {
                fieldExpr = fieldDef.getFieldValue();
                visitExpression(fieldExpr);
            }
            u_Expr.setRecordLiteral(recordLiteral);
        }
        // Record Project
        RecordProjection recordProj = expr.getRecordProjection();
        if (recordProj != null) {
            Expression recordRef = recordProj.getRecordReference();
            recordRef = visitExpression(recordRef);
            recordProj.setRecordReference(recordRef);
            u_Expr.setRecordProjection(recordProj);
        }
        // Unary Operators
        Expression notExpr = expr.getNot();
        if (notExpr != null) {
            notExpr = visitExpression(notExpr);
            u_Expr.setNot(notExpr);
        }
        Expression negExpr = expr.getNegative();
        if (negExpr != null) {
            negExpr = visitExpression(negExpr);
            u_Expr.setNegative(negExpr);
            ;
        }
        Expression preExpr = expr.getPre();
        if (preExpr != null) {
            preExpr = visitExpression(preExpr);
            u_Expr.setPre(preExpr);
        }
        Expression toIntExpr = expr.getToInt();
        if (toIntExpr != null) {
            toIntExpr = visitExpression(toIntExpr);
            u_Expr.setToInt(toIntExpr);
        }
        Expression toRealExpr = expr.getToReal();
        if (toRealExpr != null) {
            toRealExpr = visitExpression(toRealExpr);
            u_Expr.setToReal(toRealExpr);
        }
        Boolean b = expr.isBoolLiteral();
        if (b != null) {
            u_Expr.setBoolLiteral(b);
        }
        BigInteger int_value = expr.getIntLiteral();
        if (int_value != null) {
            u_Expr.setIntLiteral(int_value);
        }
        BigDecimal real_value = expr.getRealLiteral();
        if (real_value != null) {
            u_Expr.setRealLiteral(real_value);
        }
        // Identifier
        String identifier = expr.getIdentifier();
        if (identifier != null) {
            if (eventDeclarations.containsKey(identifier)) {
                u_Expr = eventExpression(expr, false);
            } else {
                u_Expr.setIdentifier(identifier);
            }
        }
        // NodeCall
        NodeCall nodeCall = expr.getCall();
        if (nodeCall != null) {
            u_Expr.setCall(nodeCall);
            List<Expression> arguments = new Vector<Expression>();
            for (Expression argExpr : nodeCall.getArgument()) {
                argExpr = visitExpression(argExpr);
                arguments.add(argExpr);
            }
            nodeCall.getArgument().clear();
            nodeCall.getArgument().addAll(arguments);
            u_Expr.setCall(nodeCall);
        }
        // Event Expression
        Expression event = expr.getEvent();
        if (event != null) {
            u_Expr = eventExpression(event, true);
        // System.out.println(expr + "^^^ Updated to Event ^^^ " + event);
        }
        ExpressionList expList = expr.getExpressionList();
        if (expList != null) {
            ExpressionList uList = new ExpressionList();
            for (Expression aexpr : expList.getExpression()) {
                expr = visitExpression(aexpr);
                uList.getExpression().add(expr);
            }
            expList.getExpression().clear();
            u_Expr.setExpressionList(uList);
        }
    // ExpressionList arrayExpList = expr.getArrayExpression();
    // 
    // if(expList != null) {
    // List<Expression> uList = new ArrayList<Expression>();
    // for(Expression aexpr: arrayExpList.getExpression()) {
    // expr = visitExpression(aexpr);
    // uList.add(expr);
    // }
    // arrayExpList.getExpression().clear();
    // arrayExpList.getExpression().addAll(uList);
    // u_Expr.setArrayExpression(arrayExpList);
    // }
    }
    return u_Expr;
}
Also used : RecordLiteral(verdict.vdm.vdm_lustre.RecordLiteral) NodeCall(verdict.vdm.vdm_lustre.NodeCall) BinaryOperation(verdict.vdm.vdm_lustre.BinaryOperation) FieldDefinition(verdict.vdm.vdm_lustre.FieldDefinition) BigDecimal(java.math.BigDecimal) Expression(verdict.vdm.vdm_lustre.Expression) RecordProjection(verdict.vdm.vdm_lustre.RecordProjection) BigInteger(java.math.BigInteger) IfThenElse(verdict.vdm.vdm_lustre.IfThenElse) Vector(java.util.Vector) ExpressionList(verdict.vdm.vdm_lustre.ExpressionList)

Example 10 with Expression

use of verdict.vdm.vdm_lustre.Expression in project VERDICT by ge-high-assurance.

the class VDM2Lustre method visit.

// Connect -> NodeCall
public void visit(Connection connection, NodeBody nodeBody) {
    // R.H.S
    ConnectionEnd src = connection.getSource();
    // Source Connection
    Port src_component_port = src.getComponentPort();
    // L.H.S.
    ConnectionEnd dest = connection.getDestination();
    // Destination Connection
    Port dest_component_port = dest.getComponentPort();
    if (dest_component_port != null) {
        // Destination = Component (z3)
        // Source = SubComponent (my_b:B _z2)
        NodeEquation neq = new NodeEquation();
        NodeEquationLHS eq_lhs = new NodeEquationLHS();
        eq_lhs.getIdentifier().add(dest_component_port.getName());
        neq.setLhs(eq_lhs);
        CompInstancePort compInstancePort = src.getSubcomponentPort();
        ComponentInstance componentInstance = compInstancePort.getSubcomponent();
        src_component_port = compInstancePort.getPort();
        String src_portID = src_component_port.getName();
        Expression expr = new Expression();
        List<VariableDeclaration> vars = nodeBody.getVariableDeclaration();
        boolean match = false;
        String id_expr = componentInstance.getId() + "_port_" + src_portID;
        for (VariableDeclaration var : vars) {
            if (var.getName().equals(id_expr)) {
                match = true;
                break;
            }
        }
        if (match) {
            expr.setIdentifier(id_expr);
        } else {
            String inst_cmp = "(.+)_instrumented";
            Pattern inst_pattern = Pattern.compile(inst_cmp);
            // System.out.println(src_portID);
            Matcher m = inst_pattern.matcher(src_portID);
            if (m.matches()) {
                src_portID = m.group(1);
            }
            id_expr = componentInstance.getId() + "_port_" + src_portID;
            expr.setIdentifier(id_expr);
        // System.out.println(id_expr);
        // System.out.println(">>>>>>>>>>>>>Identifiers: " +
        // expr.getIdentifier());
        }
        neq.setRhs(expr);
        nodeBody.getEquation().add(neq);
    } else if (src_component_port != null) {
        CompInstancePort compInstancePort = dest.getSubcomponentPort();
        // X1
        dest_component_port = compInstancePort.getPort();
        // my_a1 : A
        ComponentInstance componentInstance = compInstancePort.getSubcomponent();
        String arg_value = src_component_port.getName();
        // called node Identifier.
        String called_node_ID = null;
        ComponentType componentType = componentInstance.getSpecification();
        ComponentImpl componentImpl = componentInstance.getImplementation();
        if (componentType == null) {
            componentType = componentImpl.getType();
            called_node_ID = componentType.getName() + "_dot_Impl";
        }
        if (componentType != null && componentImpl != null) {
            componentType = componentImpl.getType();
            called_node_ID = componentType.getName() + "_dot_Impl";
            String inst_cmp = "(.+)_instrumented";
            Pattern inst_pattern = Pattern.compile(inst_cmp);
            // System.out.println(arg_value);
            Matcher m = inst_pattern.matcher(arg_value);
            if (m.matches()) {
                arg_value = m.group(1);
            }
        } else {
            called_node_ID = componentType.getName();
            arg_value = src_component_port.getName();
        // System.out.println(arg_value);
        }
        for (Port port : componentType.getPort()) {
            // MODE
            PortMode port_mode = port.getMode();
            if (port_mode == PortMode.OUT) {
                // EQ L.H.S Variables *called Node return values
                String expr_id = componentInstance.getId() + "_port_" + port.getName();
                // System.out.print(">>>" + expr_id);
                NodeEquation n_eq = getNodeEq(expr_id, nodeBody);
                if (n_eq != null) {
                    Expression eq_rhs = n_eq.getRhs();
                    NodeCall node_called = eq_rhs.getCall();
                    String inst_cmp = "(.+)_Inst_.*";
                    Pattern inst_pattern = Pattern.compile(inst_cmp);
                    // String node_id = "";
                    // if (node_called != null) {
                    // node_id = node_called.getNodeId();
                    // }
                    // System.out.println(" = " + node_id + " (" + arg_value
                    // +
                    // ")");
                    Matcher m = inst_pattern.matcher(node_called.getNodeId());
                    IfThenElse ifelse = new IfThenElse();
                    Expression called_expr = new Expression();
                    // Condition
                    Expression gps_expr = new Expression();
                    if (m.matches()) {
                        // Instrumented component Instance ID
                        String component_id = m.group(1);
                        gps_expr.setIdentifier(component_id);
                    }
                    ifelse.setCondition(gps_expr);
                    // Then
                    ifelse.setThenBranch(called_expr);
                    // Else
                    Expression arg = new Expression();
                    arg.setIdentifier(src_component_port.getName());
                    ifelse.setElseBranch(arg);
                    // NodeCalled Expr
                    called_expr.setCall(node_called);
                    Expression instrumented_expr = new Expression();
                    instrumented_expr.setConditionalExpression(ifelse);
                    if (node_called != null) {
                        if (node_called.getNodeId().equals(called_node_ID)) {
                            for (Expression arg_expr : node_called.getArgument()) {
                                if (arg_expr.getIdentifier().equals(dest_component_port.getName())) {
                                    arg_expr.setIdentifier(arg_value);
                                } else if (node_called.getArgument().size() == 1) {
                                    arg_expr.setIdentifier(arg_value);
                                }
                            }
                        }
                    }
                }
            }
        }
    } else {
        CompInstancePort compInstancePort = src.getSubcomponentPort();
        ComponentInstance componentInstance = compInstancePort.getSubcomponent();
        src_component_port = compInstancePort.getPort();
        Expression arg_expr = new Expression();
        // my_a1_y1
        String src_portID = src_component_port.getName();
        String componentInstanceID = componentInstance.getId();
        arg_expr.setIdentifier(componentInstanceID + "_port_" + src_portID);
        // called node Identifier.
        String called_node_ID = null;
        compInstancePort = dest.getSubcomponentPort();
        componentInstance = compInstancePort.getSubcomponent();
        dest_component_port = compInstancePort.getPort();
        ComponentType componentType = componentInstance.getSpecification();
        ComponentImpl componentImpl = componentInstance.getImplementation();
        String old_portID = null;
        if (componentType == null) {
            componentType = componentImpl.getType();
            called_node_ID = componentType.getName();
        }
        if (componentType != null && componentImpl != null) {
            componentType = componentImpl.getType();
            called_node_ID = componentType.getName() + "_dot_Impl";
            String inst_cmp = "(.+)_instrumented";
            Pattern inst_pattern = Pattern.compile(inst_cmp);
            // System.out.print(src_portID + " ==> ");
            Matcher m = inst_pattern.matcher(src_portID);
            if (m.matches()) {
                old_portID = src_portID;
                src_portID = m.group(1);
                arg_expr.setIdentifier(componentInstanceID + "_port_" + src_portID);
            // System.out.println(old_portID + " -- " + src_portID);
            }
        // System.out.println(arg_expr.getIdentifier() + " <== " +
        // src_portID);
        } else {
            called_node_ID = componentType.getName();
        }
        String node_arg = "(.+)_Inst_.*";
        Pattern arg_pattern = Pattern.compile(node_arg);
        Matcher m_arg = arg_pattern.matcher(called_node_ID);
        if (!m_arg.matches() && old_portID != null) {
            arg_expr.setIdentifier(componentInstanceID + "_port_" + old_portID);
        // System.out.println("Node ID =>" + called_node_ID + "(" +
        // arg_expr.getIdentifier() + ")");
        }
        // System.out.println(called_node_ID);
        for (Port port : componentType.getPort()) {
            // MODE
            PortMode port_mode = port.getMode();
            if (port_mode == PortMode.OUT) {
                // EQ L.H.S Variables *called Node return values
                String expr_id = componentInstance.getId() + "_port_" + port.getName();
                NodeEquation n_eq = getNodeEq(expr_id, nodeBody);
                if (n_eq != null) {
                    Expression eq_rhs = n_eq.getRhs();
                    NodeCall node_called = eq_rhs.getCall();
                    String inst_cmp = "(.+)_Inst_.*";
                    Pattern inst_pattern = Pattern.compile(inst_cmp);
                    String node_id = "";
                    if (node_called != null) {
                        node_id = node_called.getNodeId();
                    }
                    Matcher m = inst_pattern.matcher(node_id);
                    IfThenElse ifelse = new IfThenElse();
                    Expression called_expr = new Expression();
                    // Condition
                    Expression g_expr = new Expression();
                    if (m.matches()) {
                        // Instrumented component Instance ID
                        String component_id = m.group(1);
                        g_expr.setIdentifier(component_id);
                    }
                    ifelse.setCondition(g_expr);
                    // Then
                    ifelse.setThenBranch(called_expr);
                    // Else
                    // Expression arg = new Expression();
                    ifelse.setElseBranch(arg_expr);
                    // NodeCalled Expr
                    called_expr.setCall(node_called);
                    Expression instrumented_expr = new Expression();
                    instrumented_expr.setConditionalExpression(ifelse);
                    if (node_called != null) {
                        if (node_called.getNodeId().equals(called_node_ID)) {
                            for (Expression a_expr : node_called.getArgument()) {
                                if (a_expr.getIdentifier().equals(dest_component_port.getName())) {
                                    a_expr.setIdentifier(arg_expr.getIdentifier());
                                } else if (node_called.getArgument().size() == 1) {
                                    a_expr.setIdentifier(arg_expr.getIdentifier());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ComponentType(verdict.vdm.vdm_model.ComponentType) NodeCall(verdict.vdm.vdm_lustre.NodeCall) Matcher(java.util.regex.Matcher) PortMode(verdict.vdm.vdm_model.PortMode) CompInstancePort(verdict.vdm.vdm_model.CompInstancePort) CompInstancePort(verdict.vdm.vdm_model.CompInstancePort) Port(verdict.vdm.vdm_model.Port) NodeEquationLHS(verdict.vdm.vdm_lustre.NodeEquationLHS) ComponentImpl(verdict.vdm.vdm_model.ComponentImpl) NodeEquation(verdict.vdm.vdm_lustre.NodeEquation) Expression(verdict.vdm.vdm_lustre.Expression) ComponentInstance(verdict.vdm.vdm_model.ComponentInstance) ConnectionEnd(verdict.vdm.vdm_model.ConnectionEnd) VariableDeclaration(verdict.vdm.vdm_lustre.VariableDeclaration) IfThenElse(verdict.vdm.vdm_lustre.IfThenElse)

Aggregations

Expression (verdict.vdm.vdm_lustre.Expression)45 ContractItem (verdict.vdm.vdm_lustre.ContractItem)9 BinaryOperation (verdict.vdm.vdm_lustre.BinaryOperation)8 NodeCall (verdict.vdm.vdm_lustre.NodeCall)7 DataType (verdict.vdm.vdm_data.DataType)6 ComponentType (verdict.vdm.vdm_model.ComponentType)6 PropertyExpression (org.osate.aadl2.PropertyExpression)5 ConstantDeclaration (verdict.vdm.vdm_lustre.ConstantDeclaration)5 IfThenElse (verdict.vdm.vdm_lustre.IfThenElse)5 NodeEquation (verdict.vdm.vdm_lustre.NodeEquation)5 RecordLiteral (verdict.vdm.vdm_lustre.RecordLiteral)5 SymbolDefinition (verdict.vdm.vdm_lustre.SymbolDefinition)5 CompInstancePort (verdict.vdm.vdm_model.CompInstancePort)5 ComponentImpl (verdict.vdm.vdm_model.ComponentImpl)5 Port (verdict.vdm.vdm_model.Port)5 BinaryExpr (com.rockwellcollins.atc.agree.agree.BinaryExpr)4 BoolLitExpr (com.rockwellcollins.atc.agree.agree.BoolLitExpr)4 CallExpr (com.rockwellcollins.atc.agree.agree.CallExpr)4 EnumLitExpr (com.rockwellcollins.atc.agree.agree.EnumLitExpr)4 EventExpr (com.rockwellcollins.atc.agree.agree.EventExpr)4