use of verdict.vdm.vdm_lustre.NodeBody 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.NodeBody in project VERDICT by ge-high-assurance.
the class VDM2Lustre method visit.
// B) Component Implementation Translated into Lustre Node
public void visit(ComponentImpl componentImpl, Node node) {
NodeBody nodeBody = new NodeBody();
// Option 1) Block Implementation
// retrieve_block(componentImpl);
BlockImpl blockImpl = componentImpl.getBlockImpl();
// BlockImpl
if (blockImpl != null) {
ComponentType componentType = componentImpl.getType();
for (ComponentInstance componentInstance : blockImpl.getSubcomponent()) {
// replace to make naming compliant with lustre code
String id = componentInstance.getId();
id = id.replace(".", "_dot_");
id = id.replace("::", "_double_colon_");
componentInstance.setId(id);
String name = componentInstance.getName();
name = name.replace(".", "_dot_");
name = name.replace("::", "_double_colon_");
componentInstance.setName(name);
componentType = componentInstance.getSpecification();
ComponentImpl subcomponentImpl = componentInstance.getImplementation();
if (componentType == null && subcomponentImpl == null) {
System.out.println(componentInstance.getName() + " subcomponent is missing both a specification and an implemention.");
System.out.println("Please provide some specification or an implementation to continue.");
System.exit(-1);
}
// Option 1) Implementation
if (subcomponentImpl != null) {
componentType = subcomponentImpl.getType();
if (!this.marked_types.contains(componentType)) {
visit(componentType, nodeBody, componentInstance.getId(), true);
}
} else // Option 2) Specification
if (componentType != null) {
if (!this.marked_types.contains(componentType)) {
visit(componentType, nodeBody, componentInstance.getId(), false);
}
}
}
for (Connection connection : blockImpl.getConnection()) {
if (!ignoreConnection(connection)) {
visit(connection, nodeBody);
}
}
} else {
// Option 2) DataFlow Implementation / NodeBody
nodeBody = componentImpl.getDataflowImpl();
// node.setBody(nodeBody);
}
node.setBody(nodeBody);
}
use of verdict.vdm.vdm_lustre.NodeBody in project VERDICT by ge-high-assurance.
the class Agree2Vdm method translateAgreeAnnex.
private Model translateAgreeAnnex(List<SystemType> systemTypes, List<SystemImplementation> systemImpls, Model model, HashSet<String> dataTypeDecl, HashSet<String> nodeDecl) {
LustreProgram lustreProgram = new LustreProgram();
// Initializing the lustre program in the VDM
model.setDataflowCode(lustreProgram);
// System.out.println("Processing "+systemTypes.size()+" SystemTypes for agree annexes");
for (SystemType sysType : systemTypes) {
// unpacking sysType
for (AnnexSubclause annex : sysType.getOwnedAnnexSubclauses()) {
if (annex.getName().equalsIgnoreCase("agree")) {
// annex is of type DefaultAnnexSubclause
DefaultAnnexSubclause ddASC = (DefaultAnnexSubclause) annex;
// AnnexSubclause aSC = ddASC.getParsedAnnexSubclause();
AgreeContractSubclause agreeAnnex = (AgreeContractSubclause) ddASC.getParsedAnnexSubclause();
// populating agree contracts in the vdm component type -- SHOULD ADD THIS CODE TO AADL2VDM
verdict.vdm.vdm_lustre.ContractSpec contractSpec = new verdict.vdm.vdm_lustre.ContractSpec();
EList<EObject> annexContents = agreeAnnex.eContents();
if (annexContents.isEmpty()) {
System.out.println("Empty Agree Annex.");
}
for (EObject clause : annexContents) {
// mapping to AgreeContractclause
AgreeContract agreeContract = (AgreeContract) clause;
// getting specStatements
EList<SpecStatement> specStatements = agreeContract.getSpecs();
for (SpecStatement specStatement : specStatements) {
if (specStatement instanceof EqStatement) {
EqStatement eqStmt = (EqStatement) specStatement;
// translate EqStatement in Agree to SymbolDefinition in vdm
SymbolDefinition symbDef = translateEqStatement(eqStmt, model, dataTypeDecl, nodeDecl);
// Add agree variable/symbol definition to the contractSpec in vdm
contractSpec.getSymbol().add(symbDef);
} else if (specStatement instanceof GuaranteeStatement) {
GuaranteeStatement guaranteeStmt = (GuaranteeStatement) specStatement;
ContractItem contractItem = translateGuaranteeStatement(guaranteeStmt, dataTypeDecl, nodeDecl, model);
contractSpec.getGuarantee().add(contractItem);
} else if (specStatement instanceof AssumeStatement) {
AssumeStatement assumeStmt = (AssumeStatement) specStatement;
ContractItem contractItem = translateAssumeStatement(assumeStmt, dataTypeDecl, nodeDecl, model);
contractSpec.getAssume().add(contractItem);
} else {
if (!(specStatement instanceof ConstStatementImpl)) {
System.out.println("Element not recognizable" + clause.eContents().toString());
}
}
}
}
if (contractSpec != null) {
List<ComponentType> vdmComponentTypes = model.getComponentType();
for (ComponentType vdmComponentType : vdmComponentTypes) {
// populating agree contract details in the corresponding componentType instance in vdm
if (vdmComponentType.getName().equalsIgnoreCase(sysType.getName())) {
vdmComponentType.setContract(contractSpec);
}
}
// populating agree contract details in the componentType instance in vdm
// packComponent.setContract(contractSpec);
}
}
}
// End of unpacking sysType
}
for (SystemImplementation sysImpl : systemImpls) {
// unpacking sysType
for (AnnexSubclause annex : sysImpl.getOwnedAnnexSubclauses()) {
if (annex.getName().equalsIgnoreCase("agree")) {
// annex is of type DefaultAnnexSubclause
DefaultAnnexSubclause ddASC = (DefaultAnnexSubclause) annex;
// AnnexSubclause aSC = ddASC.getParsedAnnexSubclause();
AgreeContractSubclause agreeAnnex = (AgreeContractSubclause) ddASC.getParsedAnnexSubclause();
// populating agree contracts in the vdm node body for component implementation type
verdict.vdm.vdm_lustre.NodeBody nodeBody = new verdict.vdm.vdm_lustre.NodeBody();
;
EList<EObject> annexContents = agreeAnnex.eContents();
if (annexContents.isEmpty()) {
System.out.println("Empty Agree Annex.");
}
for (EObject clause : annexContents) {
// mapping to AgreeContractclause
AgreeContract agreeContract = (AgreeContract) clause;
// getting specStatements
EList<SpecStatement> specStatements = agreeContract.getSpecs();
for (SpecStatement specStatement : specStatements) {
if (specStatement instanceof ConstStatementImpl) {
ConstStatementImpl constStmtImpl = (ConstStatementImpl) specStatement;
ConstantDeclaration constDecl = translateConstStatementImpl(constStmtImpl, dataTypeDecl, nodeDecl, model);
nodeBody.getConstantDeclaration().add(constDecl);
} else if (specStatement instanceof EqStatementImpl) {
EqStatementImpl eqStmtImpl = (EqStatementImpl) specStatement;
nodeBody = translateEqStatementImpl(eqStmtImpl, dataTypeDecl, nodeDecl, model, nodeBody);
} else if (specStatement instanceof AssignStatementImpl) {
AssignStatementImpl assignStmtImpl = (AssignStatementImpl) specStatement;
NodeEquation nodeEquation = translateAssignStatementImpl(assignStmtImpl, dataTypeDecl, nodeDecl, model);
nodeBody.getEquation().add(nodeEquation);
} else if (specStatement instanceof AssertStatementImpl) {
AssertStatementImpl assertStmtImpl = (AssertStatementImpl) specStatement;
Expression assertion = translateAssertStatementImpl(assertStmtImpl, dataTypeDecl, nodeDecl, model);
nodeBody.getAssertion().add(assertion);
} else {
System.out.println("Element not recognizable" + clause.eContents().toString());
}
}
}
List<ComponentImpl> vdmComponentImpls = model.getComponentImpl();
for (ComponentImpl vdmComponentImpl : vdmComponentImpls) {
// populating agree contract details in the corresponding componentImplType instance in vdm
if (vdmComponentImpl.getName().equalsIgnoreCase(sysImpl.getName())) {
vdmComponentImpl.setDataflowImpl(nodeBody);
}
}
}
}
}
return model;
}
use of verdict.vdm.vdm_lustre.NodeBody in project VERDICT by ge-high-assurance.
the class VerdictLustreListener method exitNodeBody.
/**
* Extract a node body.
*/
@Override
public void exitNodeBody(LustreParser.NodeBodyContext ctx) {
ctx.body = new NodeBody();
ctx.localDecl().forEach(local -> {
ctx.body.getConstantDeclaration().addAll(local.constantDeclarations);
ctx.body.getVariableDeclaration().addAll(local.variableDeclarations);
});
ctx.definition().forEach(definition -> {
if (definition.equation() != null) {
ctx.body.getEquation().add(definition.equation().equa);
}
if (definition.assertion() != null) {
ctx.body.getAssertion().add(definition.assertion().expr().expression);
}
if (definition.property() != null) {
ctx.body.getProperty().add(definition.property().prop);
}
if (definition.main() != null) {
ctx.body.setIsMain(true);
}
});
}
use of verdict.vdm.vdm_lustre.NodeBody in project VERDICT by ge-high-assurance.
the class PrettyPrinter method visit.
public void visit(Node node) {
sb.append("node ");
// if (node.isIsImported()) {
sb.append("imported ");
// }
sb.append(node.getName());
sb.append("(");
sb.append(NL);
for (NodeParameter param : node.getInputParameter()) {
visit(param);
}
sb.append(")");
sb.append(NL);
sb.append("returns (");
sb.append(NL);
for (NodeParameter param : node.getOutputParameter()) {
visit(param);
}
sb.append(");");
sb.append(NL);
ContractSpec contractSpec = node.getContract();
if (contractSpec != null) {
sb.append("(*@contract ");
visit(contractSpec);
sb.append("*)");
// sb.append(NL);
}
// if (node.isIsFunction()) {
NodeBody nodeBody = node.getBody();
if (nodeBody != null) {
// sb.append("{");
sb.append(NL);
visit(nodeBody);
// sb.append("}");
sb.append(NL);
}
// }
}
Aggregations