use of verdict.vdm.vdm_model.ComponentType in project VERDICT by ge-high-assurance.
the class VDMInstrumentor method retrieve_block_impl.
protected ComponentImpl retrieve_block_impl(ComponentImpl compImpl) {
BlockImpl blockImpl = null;
String cmpID = compImpl.getType().getId();
for (ComponentImpl cImpl : vdm_model.getComponentImpl()) {
if (cImpl.getBlockImpl() != null) {
blockImpl = cImpl.getBlockImpl();
for (ComponentInstance cmpInstance : blockImpl.getSubcomponent()) {
ComponentImpl impl = cmpInstance.getImplementation();
ComponentType enumType = null;
if (impl != null) {
enumType = impl.getType();
} else {
enumType = cmpInstance.getSpecification();
}
if (cmpID.equals(enumType.getId())) {
// compImpl.getId());
return cImpl;
}
}
}
}
return compImpl;
}
use of verdict.vdm.vdm_model.ComponentType 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_model.ComponentType 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_model.ComponentType in project VERDICT by ge-high-assurance.
the class VerdictTestInstrumentor method getTopLevelSystemType.
/**
* Note: presently only supports one top-level system. If there are multiple, prints a warning
* and chooses the first one.
*
* <p>Also note that instrumentation generates additional system types. But these are added
* after the one we want. In this case the warning is erroneous.
*
* @return the top-level system type in model
*/
private ComponentType getTopLevelSystemType() {
Set<ComponentType> subcomps = new HashSet<>();
for (ComponentImpl impl : model.getComponentImpl()) {
if (impl.getBlockImpl() != null) {
for (ComponentInstance comp : impl.getBlockImpl().getSubcomponent()) {
if (comp.getSpecification() != null) {
subcomps.add(comp.getSpecification());
} else if (comp.getImplementation() != null) {
subcomps.add(comp.getImplementation().getType());
} else {
throw new RuntimeException("ComponentInstance has neither specification nor implementation");
}
}
}
}
List<ComponentType> topLevels = new ArrayList<>();
for (ComponentType comp : model.getComponentType()) {
if (!subcomps.contains(comp)) {
topLevels.add(comp);
}
}
if (topLevels.isEmpty()) {
throw new RuntimeException("Verdict ATG error: No top-level component found");
}
if (topLevels.size() > 1) {
System.out.println("Verdict ATG Warning: Multiple top-level systems found, using first one (may be caused by instrumentation)");
}
return topLevels.get(0);
}
use of verdict.vdm.vdm_model.ComponentType 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;
}
Aggregations