use of com.rockwellcollins.atc.agree.agree.LiftContractStatement in project AGREE by loonwerks.
the class AgreeASTBuilder method getAgreeNode.
private AgreeNode getAgreeNode(ComponentInstance compInst, boolean isTop) {
List<AgreeVar> inputs = new ArrayList<>();
List<AgreeVar> outputs = new ArrayList<>();
List<AgreeVar> locals = new ArrayList<>();
List<AgreeAADLConnection> aadlConnections = new ArrayList<>();
List<AgreeOverriddenConnection> userDefinedConections = new ArrayList<>();
List<AgreeConnection> connections = new ArrayList<>();
List<AgreeNode> subNodes = new ArrayList<>();
List<AgreeStatement> assertions = new ArrayList<>();
List<AgreeStatement> assumptions = new ArrayList<>();
List<AgreeStatement> guarantees = new ArrayList<>();
List<AgreeStatement> lemmas = new ArrayList<>();
List<AgreeEquation> localEquations = new ArrayList<>();
List<AgreeStatement> patternProps = Collections.emptyList();
timeOfVarMap = new HashMap<>();
timeRiseVarMap = new HashMap<>();
timeFallVarMap = new HashMap<>();
unspecifiedAadlProperties = new HashMap<>();
Expr clockConstraint = new BoolExpr(true);
Expr initialConstraint = new BoolExpr(true);
String id = compInst.getName();
AgreeVar clockVar = new AgreeVar(id + clockIDSuffix, NamedType.BOOL, compInst.getSubcomponent(), compInst, null);
EObject reference = isTop ? compInst.getComponentClassifier() : compInst.getSubcomponent();
TimingModel timing = null;
boolean foundSubNode = false;
boolean hasDirectAnnex = false;
boolean hasSubcomponents = false;
ComponentClassifier compClass = compInst.getComponentClassifier();
Set<ComponentType> effectiveTypes = new HashSet<>();
Map<String, jkind.lustre.Expr> portRewriteMap = new HashMap<>();
if (compClass instanceof ComponentImplementation) {
boolean latched = false;
ComponentImplementation cc = (ComponentImplementation) compClass;
effectiveTypes.addAll(getEffectiveComponentTypes(cc.getType()));
if (isTop || isMonolithic) {
while (cc != null) {
AgreeContractSubclause annex = getAgreeAnnex(cc);
for (ComponentInstance subInst : compInst.getComponentInstances()) {
hasSubcomponents = true;
curInst = subInst;
AgreeNode subNode = getAgreeNode(subInst, false);
if (subNode != null && subNodes.stream().noneMatch(it -> it.reference.equals(subNode.reference))) {
foundSubNode = true;
subNodes.add(subNode);
}
}
if (annex != null) {
hasDirectAnnex = true;
AgreeContract contract = (AgreeContract) annex.getContract();
curInst = compInst;
assertions.addAll(getAssertionStatements(contract.getSpecs()));
getEquationStatements(contract.getSpecs(), portRewriteMap).addAllTo(locals, assertions, guarantees);
assertions.addAll(getPropertyStatements(contract.getSpecs()));
assertions.addAll(getAssignmentStatements(contract.getSpecs()));
userDefinedConections.addAll(getConnectionStatements(contract.getSpecs()));
lemmas.addAll(getLemmaStatements(contract.getSpecs()));
lemmas.addAll(getReachableStatements(contract.getSpecs(), portRewriteMap));
addLustreNodes(contract.getSpecs());
gatherLustreTypes(contract.getSpecs());
// the clock constraints contain other nodes that we add
clockConstraint = getClockConstraint(contract.getSpecs(), subNodes);
timing = getTimingModel(contract.getSpecs());
outputs.addAll(getEquationVars(contract.getSpecs(), compInst));
for (SpecStatement spec : contract.getSpecs()) {
if (spec instanceof LatchedStatement) {
latched = true;
break;
}
}
}
// Find extended effective types
effectiveTypes.addAll(getEffectiveComponentTypes(cc.getType()));
cc = cc.getExtended();
}
EList<ConnectionInstance> connectionInstances = compInst.getAllEnclosingConnectionInstances();
List<ConnectionInstance> foo = new ArrayList<>();
compInst.getAllComponentInstances().forEach(ci -> ci.allEnclosingConnectionInstances().forEach(foo::add));
aadlConnections.addAll(getConnectionsFromInstances(connectionInstances, compInst, subNodes, latched));
connections.addAll(filterConnections(aadlConnections, userDefinedConections));
}
ComponentType compType = ((ComponentImplementation) compClass).getType();
AgreeContractSubclause compImpAnnex = getAgreeAnnex(compClass);
if (compImpAnnex != null) {
AgreeContract contract = (AgreeContract) compImpAnnex.getContract();
for (SpecStatement spec : contract.getSpecs()) {
if (spec instanceof LiftContractStatement) {
Subcomponent sub = ((ComponentImplementation) compClass).getOwnedSubcomponents().get(0);
ComponentType ct = sub.getComponentType();
for (Connection conn : ((ComponentImplementation) compClass).getAllConnections()) {
NamedElement sourceNe = conn.getSource().getConnectionEnd();
NamedElement destNe = conn.getDestination().getConnectionEnd();
String sourceStr = sourceNe.getName().replace("::", "__");
String destStr = destNe.getName().replace("::", "__");
if (ct == sourceNe.getContainingClassifier()) {
portRewriteMap.put(sourceStr, new IdExpr(destStr));
} else if (ct == destNe.getContainingClassifier()) {
portRewriteMap.put(destStr, new IdExpr(sourceStr));
}
}
effectiveTypes.addAll(getEffectiveComponentTypes(ct));
}
}
}
// make compClass the type so we can get it's other contract elements
compClass = compType;
} else if (compClass instanceof ComponentType) {
effectiveTypes.addAll(getEffectiveComponentTypes((ComponentType) compClass));
} else {
throw new AgreeException("Internal error: attempt to run AGREE analysis on instance " + compInst.getFullName() + " not instance of ComponentImplementation or ComponentType.");
}
curInst = compInst;
if (timing == null) {
timing = TimingModel.SYNC;
}
for (ComponentType compType : effectiveTypes) {
AgreeContractSubclause annex = getAgreeAnnex(compType);
if (annex != null) {
hasDirectAnnex = true;
AgreeContract contract = (AgreeContract) annex.getContract();
// this makes files for monolithic verification a bit smaller
if (!isMonolithic || isTop || !hasSubcomponents) {
assumptions.addAll(getAssumptionStatements(contract.getSpecs(), portRewriteMap));
guarantees.addAll(getGuaranteeStatements(contract.getSpecs(), portRewriteMap));
lemmas.addAll(getReachableStatements(contract.getSpecs(), portRewriteMap));
}
// Count eq statements with expressions as assertions
getEquationStatements(contract.getSpecs(), portRewriteMap).addAllTo(locals, assertions, guarantees);
assertions.addAll(getPropertyStatements(contract.getSpecs()));
outputs.addAll(getEquationVars(contract.getSpecs(), compInst));
getAgreeInputVars(contract.getSpecs(), compInst).addAllTo(inputs, assumptions, guarantees);
initialConstraint = getInitialConstraint(contract.getSpecs());
addLustreNodes(contract.getSpecs());
gatherLustreTypes(contract.getSpecs());
}
}
gatherUnspecifiedAadlProperties(unspecifiedAadlProperties, inputs, assumptions, guarantees);
if (!(foundSubNode || hasDirectAnnex)) {
return null;
}
gatherOutputsInputsAndTypes(outputs, inputs, compInst.getFeatureInstances(), assumptions, guarantees);
AgreeNodeBuilder builder = new AgreeNodeBuilder(id);
builder.addInput(inputs);
builder.addOutput(outputs);
builder.addLocal(locals);
builder.addLocalEquation(localEquations);
builder.addConnection(connections);
builder.addSubNode(subNodes);
// Clean up any vacuous true predicates
Predicate<AgreeStatement> isBoolExprAndisTrue = st -> (st.expr instanceof BoolExpr) && ((BoolExpr) st.expr).value;
assertions.removeIf(isBoolExprAndisTrue);
assumptions.removeIf(isBoolExprAndisTrue);
guarantees.removeIf(isBoolExprAndisTrue);
builder.addAssertion(assertions);
builder.addAssumption(assumptions);
builder.addGuarantee(guarantees);
builder.addLemma(lemmas);
builder.addPatternProp(patternProps);
builder.setClockConstraint(clockConstraint);
builder.setInitialConstraint(initialConstraint);
builder.setClockVar(clockVar);
builder.setReference(reference);
builder.setTiming(timing);
builder.setCompInst(compInst);
builder.addTimeOf(timeOfVarMap);
builder.addTimeRise(timeRiseVarMap);
builder.addTimeFall(timeFallVarMap);
AgreeNode result = builder.build();
renamings.put(id, compInst.getName());
refMap.put(id, compInst);
return linearizationRewriter.visit(result);
}
Aggregations