use of verdict.vdm.vdm_lustre.IfThenElse in project VERDICT by ge-high-assurance.
the class VDMInstrumentor method instrument_link.
// public void instrument_link(Connection connection) {
// // Connection Source
// ConnectionEnd src = connection.getSource();
//
// // Connection Destination
// ConnectionEnd dest = connection.getDestination();
//
// // Source Component
// Port src_port = src.getComponentPort();
// // Destination Component
// Port dest_port = dest.getComponentPort();
//
// if (src_port == null && dest_port == null) {
// // Both are sub-compon
// System.out.println("Both are subcomponents.");
// }
// if (src_port == null && dest_port != null) {
// // Only one is Subcomponent
// System.out.println(dest_port.getId() + " -- " + dest_port.getName());
// }
// if (src_port != null && dest_port == null) {
// // One Subcomponent
// System.out.println(src_port.getId() + " -- " + src_port.getName());
// }
// }
// public void create_link(Connection old_channel, ComponentInstance
// src_componentInstance,
// ComponentInstance dest_componentInstance) {
//
// ComponentInstance instrumented_componentInstance = new ComponentInstance();
//
// String component_ID = src_componentInstance.getName() + "_Inst_" +
// dest_componentInstance.getName();
// instrumented_componentInstance.setId(component_ID + "_Instance");
// instrumented_componentInstance.setName(component_ID);
//
// instrumented_componentInstance.setSpecification(value);
// instrumented_componentInstance.setImplementation(value);
//
// ComponentType instrumented_component = new ComponentType();
// instrumented_component.setId(component_ID);
// instrumented_component.setName(component_ID);
//
//
//
// Connection inst_channel = new Connection();
//
// //Update Old connection Destination
// old_channel.setDestination(value);
//
// //Add New Connection Source
// inst_channel.setSource(value);
// //Add New Connection Destination
// inst_channel.setDestination(value);
//
//
// }
public String instrument_link(String compID, Connection connection, BlockImpl blockImpl) {
// instrument_link(connection);
// System.out.println("Instrumented Link ***" + connection.getName());
// Default Block Implementation
ComponentImpl compImpl = null;
if (compID != null) {
compImpl = retrieve_cmp_impl(compID);
}
// Connections without Components Instrumentation.
if (compImpl == null) {
compImpl = retrieve_main_cmp_impl();
}
ComponentType instrumented_cmp = new ComponentType();
// R.H.S
ConnectionEnd src = connection.getSource();
ComponentInstance src_componentInstance = new ComponentInstance();
// Source Connection
Port src_port = src.getComponentPort();
if (src_port != null) {
String identifier = compImpl.getId();
// identifier = identifier.replace(".I", "_I");
identifier = identifier.replace(".", "_dot_");
identifier = identifier.replace("::", "_double_colon_");
src_componentInstance.setId(identifier);
src_componentInstance.setName(identifier);
src_componentInstance.setImplementation(compImpl);
}
// if (src_port == instrumented_port) {
CompInstancePort compInstancePort = src.getSubcomponentPort();
if (compInstancePort != null) {
src_componentInstance = compInstancePort.getSubcomponent();
src_port = compInstancePort.getPort();
}
// R.H.S
ConnectionEnd dest = connection.getDestination();
ComponentInstance dest_componentInstance = new ComponentInstance();
// Source Connection
Port dest_port = dest.getComponentPort();
if (dest_port != null) {
String identifier = compImpl.getId();
// identifier = identifier.replace(".I", "_I");
identifier = identifier.replace(".", "_dot_");
identifier = identifier.replace("::", "_double_colon_");
dest_componentInstance.setId(identifier);
dest_componentInstance.setName(identifier);
dest_componentInstance.setImplementation(compImpl);
}
// if (dest_port == instrumented_port) {
compInstancePort = dest.getSubcomponentPort();
if (compInstancePort != null) {
dest_componentInstance = compInstancePort.getSubcomponent();
dest_port = compInstancePort.getPort();
}
String instrument_cmp_Id = src_componentInstance.getName() + "_Inst_" + dest_componentInstance.getName() + "_port_" + dest_port.getName();
instrument_cmp_Id = instrument_cmp_Id.replace(".", "_dot_");
// Setting Component IDs
instrumented_cmp.setId(instrument_cmp_Id);
instrumented_cmp.setName(instrument_cmp_Id);
// output port
Port instrumented_port_dest = new Port();
instrumented_port_dest.setId(dest_port.getId());
instrumented_port_dest.setName(dest_port.getName());
instrumented_port_dest.setMode(dest_port.getMode());
instrumented_port_dest.setType(dest_port.getType());
if (dest_port.isEvent() != null && dest_port.isEvent()) {
instrumented_port_dest.setEvent(true);
} else {
instrumented_port_dest.setEvent(false);
}
instrumented_cmp.getPort().add(instrumented_port_dest);
// Input port
Port instrumented_port_src = new Port();
instrumented_port_src.setId(src_port.getId());
instrumented_port_src.setName(src_componentInstance + "_port_" + src_port.getName());
instrumented_port_src.setMode(src_port.getMode());
if (src_port.isEvent() != null && src_port.isEvent()) {
instrumented_port_src.setEvent(true);
} else {
instrumented_port_src.setEvent(false);
}
String global_constant_Id = src_componentInstance.getName();
if (instrumented_port_src.getMode() == instrumented_port_dest.getMode()) {
instrumented_port_src.setName(src_port.getName());
if (instrumented_port_src.getMode() == PortMode.IN) {
instrumented_port_src.setMode(PortMode.OUT);
} else {
instrumented_port_dest.setMode(PortMode.IN);
}
} else {
instrumented_port_src.setName(src_port.getName());
}
if (dest_port.getMode() == PortMode.OUT) {
global_constant_Id += "_port_" + dest_port.getName() + "_instrumented";
} else {
global_constant_Id += "_port_" + src_port.getName() + "_instrumented";
}
instrumented_port_src.setType(dest_port.getType());
instrumented_cmp.getPort().add(instrumented_port_src);
vdm_model.getComponentType().add(instrumented_cmp);
// Modify connection.
ConnectionEnd con_end_inst = new ConnectionEnd();
// instrumentd_port.setPort(value);
ComponentInstance instrumented_compInstance = new ComponentInstance();
instrumented_compInstance.setId(connection.getName());
instrumented_compInstance.setName(connection.getName());
instrumented_compInstance.setSpecification(instrumented_cmp);
// -----------------------------------------
// Adding Auxiliary Node.
NodeCall nodeCall = new NodeCall();
nodeCall.setNodeId(instrumented_cmp.getId());
Expression callExpr = new Expression();
callExpr.setCall(nodeCall);
ContractItem true_guarantee_item = new ContractItem();
// true_guarantee_item.setName("true");
Expression true_expr = new Expression();
Boolean true_lit = Boolean.TRUE;
true_expr.setBoolLiteral(true_lit);
true_guarantee_item.setExpression(true_expr);
ContractSpec contractSpec = new ContractSpec();
contractSpec.getGuarantee().add(true_guarantee_item);
// ---------------------------------------------
ComponentImpl instrument_compImpl = new ComponentImpl();
instrument_compImpl.setId(instrumented_cmp.getId() + "_dot_impl");
instrument_compImpl.setName(instrumented_cmp.getName() + "_dot_Impl");
instrument_compImpl.setType(instrumented_cmp);
IfThenElse ifelse = new IfThenElse();
// Condition
Expression cond_expr = new Expression();
global_constant_Id = global_constant_Id.replace(".", "_dot_");
cond_expr.setIdentifier(global_constant_Id);
ifelse.setCondition(cond_expr);
// Then
Expression then_arg = new Expression();
then_arg.setIdentifier(dest_port.getName());
ifelse.setThenBranch(callExpr);
// Else
Expression else_arg = new Expression();
else_arg.setIdentifier(dest_port.getName());
nodeCall.getArgument().add(else_arg);
ifelse.setElseBranch(then_arg);
Expression instrumented_expr = new Expression();
instrumented_expr.setConditionalExpression(ifelse);
NodeEquation n_eq = new NodeEquation();
NodeEquationLHS neq_lhs = new NodeEquationLHS();
neq_lhs.getIdentifier().add(src_port.getName() + "_instrumented");
n_eq.setLhs(neq_lhs);
n_eq.setRhs(instrumented_expr);
NodeBody nodeBody = new NodeBody();
// VariableDeclaration cond_var = new VariableDeclaration();
// cond_var.setName(gloabal_constant_Id);
// DataType dataType = new DataType();
// dataType.setPlainType(PlainType.BOOL);
// cond_var.setDataType(dataType);
// nodeBody.getVariableDeclaration().add(cond_var);
nodeBody.setIsMain(false);
nodeBody.getEquation().add(n_eq);
instrument_compImpl.setDataflowImpl(nodeBody);
instrumented_compInstance.setImplementation(instrument_compImpl);
vdm_model.getComponentImpl().add(instrument_compImpl);
vdm_model.getComponentType().add(instrumented_cmp);
// -----------------------------------------
CompInstancePort compInstance_inst_port = new CompInstancePort();
compInstance_inst_port.setPort(dest_port);
compInstance_inst_port.setSubcomponent(instrumented_compInstance);
con_end_inst.setSubcomponentPort(compInstance_inst_port);
blockImpl.getSubcomponent().add(instrumented_compInstance);
connection.setDestination(con_end_inst);
Connection new_con = new Connection();
// Copying connection related artifacts
new_con.setName(connection.getName() + "_instrumented_channel");
// new_con.setConnType(connection.getConnType());
// new_con.setFlowType(connection.getFlowType());
//
// new_con.setDataEncrypted(connection.isEncryptedTransmission());
// new_con.setAuthenticated(connection.isAuthenticated());
new_con.setSource(con_end_inst);
compInstance_inst_port.setPort(src_port);
new_con.setDestination(dest);
blockImpl.getConnection().add(new_con);
return global_constant_Id;
}
use of verdict.vdm.vdm_lustre.IfThenElse 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;
}
use of verdict.vdm.vdm_lustre.IfThenElse 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());
}
}
}
}
}
}
}
}
}
use of verdict.vdm.vdm_lustre.IfThenElse 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;
}
use of verdict.vdm.vdm_lustre.IfThenElse in project VERDICT by ge-high-assurance.
the class VerdictLustreListener method exitIfThenElseExpr.
/**
* Extract a conditional expression.
*/
@Override
public void exitIfThenElseExpr(LustreParser.IfThenElseExprContext ctx) {
IfThenElse conditional = new IfThenElse();
conditional.setCondition(ctx.expr(0).expression);
conditional.setThenBranch(ctx.expr(1).expression);
conditional.setElseBranch(ctx.expr(2).expression);
ctx.expression = new Expression();
ctx.expression.setConditionalExpression(conditional);
}
Aggregations