use of com.rockwellcollins.atc.agree.analysis.ast.AgreeVar in project AGREE by loonwerks.
the class LustreContractAstBuilder method getLustreNode.
protected static Node getLustreNode(AgreeNode agreeNode, String nodePrefix) {
List<VarDecl> inputs = new ArrayList<>();
List<VarDecl> outputs = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<Equation> equations = new ArrayList<>();
List<Expr> assertions = new ArrayList<>();
List<Expr> requires = new ArrayList<>();
List<Expr> ensures = new ArrayList<>();
for (AgreeStatement statement : agreeNode.assumptions) {
requires.add(statement.expr);
}
for (AgreeStatement statement : agreeNode.lemmas) {
ensures.add(statement.expr);
}
for (AgreeStatement statement : agreeNode.guarantees) {
ensures.add(statement.expr);
}
for (AgreeStatement statement : agreeNode.assertions) {
assertions.add(statement.expr);
if (AgreeUtils.referenceIsInContract(statement.reference, agreeNode.compInst)) {
ensures.add(statement.expr);
}
}
// gather the remaining inputs
for (AgreeVar var : agreeNode.inputs) {
inputs.add(var);
}
for (AgreeVar var : agreeNode.outputs) {
outputs.add(var);
}
// Contract contract = new Contract(nodePrefix + agreeNode.id, requires, ensures);
Contract contract = new Contract(requires, ensures);
NodeBuilder builder = new NodeBuilder(nodePrefix + agreeNode.id);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(locals);
builder.addEquations(equations);
builder.addAssertions(assertions);
builder.setContract(contract);
return builder.build();
}
use of com.rockwellcollins.atc.agree.analysis.ast.AgreeVar in project AGREE by loonwerks.
the class LustreToMATLABTranslator method translate.
public static MATLABPrimaryFunction translate(Node lustreNode, AgreeProgram agreeProgram) {
IPreferenceStore prefs = Activator.getDefault().getPreferenceStore();
intTypeStr = prefs.getString(PreferenceConstants.PREF_INT);
realTypeStr = prefs.getString(PreferenceConstants.PREF_REAL);
List<MATLABIdExpr> inputs = new ArrayList<>();
List<MATLABStatement> statements = new ArrayList<>();
List<MATLABFunction> functions = new ArrayList<>();
List<MATLABPersistentVarDecl> persistentVarDecl = new ArrayList<>();
List<MATLABPort> ports = new ArrayList<>();
LustreToMATLABExprVisitor exprVisitor = new LustreToMATLABExprVisitor();
LustreToMATLABTypeVisitor typeVisitor = new LustreToMATLABTypeVisitor();
// get function name
String functionName = "check_" + lustreNode.id;
// add record types
for (Type type : agreeProgram.globalTypes) {
if (type instanceof RecordType) {
RecordType recordType = (RecordType) type;
SortedMap<String, MATLABType> fields = new TreeMap<>(new StringNaturalOrdering());
Iterator<Entry<String, Type>> iterator = recordType.fields.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Type> entry = iterator.next();
fields.put(exprVisitor.updateName(entry.getKey(), recordType.id), entry.getValue().accept(typeVisitor));
}
exprVisitor.recordTypeMap.put(recordType.id, fields);
}
}
// add input and output ports for subsystem to verify
for (VarDecl inputVar : lustreNode.inputs) {
String varName = null;
if (!inputVar.id.equals("time")) {
// update name
varName = exprVisitor.updateName(inputVar.id, "");
// add inputs
inputs.add(new MATLABIdExpr(varName));
// translate the Lustre expression to MATLAB expression
// add input Ids to inputList of the exprVisitor
// to help identify local variables
exprVisitor.inputSet.add(inputVar.id);
// get inputVar and type
AgreeVar agreeVar = (AgreeVar) inputVar;
Type type = agreeVar.type;
if (type instanceof RecordType || type instanceof NamedType) {
MATLABType portType = (agreeVar.type).accept(typeVisitor);
SortedMap<String, MATLABType> fields = null;
// get the fields if it is a RecordType
if (type instanceof RecordType) {
fields = exprVisitor.recordTypeMap.get(((RecordType) type).id);
}
// if it is from AADL feature, get the feature instance
if (agreeVar.featInst != null) {
FeatureInstance featInst = agreeVar.featInst;
ports.add(new MATLABPort(featInst.getName(), featInst.getDirection().getName(), portType, fields));
} else // if it is not from AADL feature, but an eq variable from
// AGREE
// set them as output variables from the subsystem
{
ports.add(new MATLABPort(inputVar.id, "out", portType, fields));
}
}
}
}
// add local variable and their types
for (VarDecl localVar : lustreNode.locals) {
// get local var Name and Type
exprVisitor.localVarTypeMap.put(exprVisitor.updateName(localVar.id, ""), localVar.type.accept(typeVisitor));
}
// translate equations to assignments
if (!lustreNode.equations.isEmpty()) {
for (Equation equation : lustreNode.equations) {
// get the variable to assign
String varId = exprVisitor.updateName(equation.lhs.get(0).id, "");
MATLABIdExpr varToAssign = new MATLABIdExpr(varId);
// get the type for the local variable
// MATLABType type = exprVisitor.localVarTypeMap.get(varId);
// translate expressions
MATLABExpr expr = exprVisitor.visit(equation.expr);
// conduct explicit type cast if it's a constant of double type or int type
// no need to type cast for assignment from an input variable
// or operations (including functions) involving known types
MATLABTypeCastExprVisitor typeCastVisitor = new MATLABTypeCastExprVisitor();
expr = typeCastVisitor.visit(expr);
// add any new preVar init from exprVisitor
Iterator<MATLABPersistentVarInit> persistentVarInitIterator = exprVisitor.persistentVarInits.iterator();
while (persistentVarInitIterator.hasNext()) {
MATLABPersistentVarInit persistentVarInit = persistentVarInitIterator.next();
// add new preVar init to the statements before the assignment
statements.add(persistentVarInit);
// remove the new preVar init from exprVisitor
persistentVarInitIterator.remove();
}
// add any new local Bus var init from exprVisitor
Iterator<MATLABLocalBusVarInit> busVarInitIterator = exprVisitor.localBusVarInits.iterator();
while (busVarInitIterator.hasNext()) {
MATLABLocalBusVarInit busVarInit = busVarInitIterator.next();
// add new local Bus var init to the statements before the assignment
statements.add(busVarInit);
// remove the new local Bus var init from exprVisitor
busVarInitIterator.remove();
}
// add assignment
MATLABAssignment assignment = new MATLABAssignment(varToAssign, expr);
statements.add(assignment);
}
}
// add persistentVar decl and assignments
Iterator<Entry<String, MATLABExpr>> it = exprVisitor.persistentVarMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, MATLABExpr> pair = it.next();
String varToAssign = pair.getKey();
MATLABExpr expr = pair.getValue();
statements.add(new MATLABAssignment(new MATLABIdExpr(varToAssign), expr));
persistentVarDecl.add(new MATLABPersistentVarDecl(varToAssign));
}
// translate assertions
for (Expr assertionExpr : lustreNode.assertions) {
MATLABExpr expr = exprVisitor.visit(assertionExpr);
// add assertions
MATLABAssumption assumption = new MATLABAssumption(expr);
statements.add(assumption);
}
// translate properties
for (String propertyStr : lustreNode.properties) {
propertyStr = exprVisitor.updateName(propertyStr, "");
MATLABProperty property = new MATLABProperty(propertyStr);
statements.add(property);
}
// add definitions for the functions that have been called
for (Map.Entry<String, MATLABFunction> functionEntry : exprVisitor.functionMap.entrySet()) {
MATLABFunction function = functionEntry.getValue();
if (function.functionCalled) {
functions.add(function);
}
}
// Create primary function AST
MATLABPrimaryFunction primaryFunction = new MATLABPrimaryFunction(functionName, inputs, persistentVarDecl, statements, functions, ports);
return primaryFunction;
}
use of com.rockwellcollins.atc.agree.analysis.ast.AgreeVar in project AGREE by loonwerks.
the class CreateSimulationProperties method transform.
public static SimulationProgram transform(final SimulationProgram program) {
final Program lustreProgram = program.getLustreProgram();
if (lustreProgram.nodes.size() != 1) {
throw new IllegalArgumentException("Only lustre programs with exactly one node are supported");
}
final SimulationProgramBuilder builder = new SimulationProgramBuilder(program);
final Node mainNode = lustreProgram.getMainNode();
// Build a new main node which includes support statements
final NodeBuilder lustreNodeBuilder = new NodeBuilder(mainNode);
// Create simulation properties for each local variable that references a lemma, assume, guarantee, or assert statement
lustreNodeBuilder.clearIvc();
final Map<String, SimulationProperty> idToSimulationPropertyMap = new HashMap<>();
// Create a mapping from the component instance/variable reference to a collection of Lustre Id's that will be used to create the simulation properties
final Map<ComponentInstance, Map<EObject, Collection<String>>> componentInstanceToReferenceToVarIdMap = new HashMap<>();
for (final VarDecl local : mainNode.locals) {
if (local instanceof AgreeVar) {
final AgreeVar var = (AgreeVar) local;
if (var.reference instanceof LemmaStatement || var.reference instanceof AssumeStatement || var.reference instanceof GuaranteeStatement || var.reference instanceof AssertStatement) {
final boolean createSimProp = (!(var.reference instanceof AssumeStatement) || var.compInst == var.compInst.getSystemInstance()) || var instanceof SimulationPropertyVar;
if (createSimProp) {
Map<EObject, Collection<String>> referenceToVarIdMap = componentInstanceToReferenceToVarIdMap.get(var.compInst);
if (referenceToVarIdMap == null) {
referenceToVarIdMap = new HashMap<>();
componentInstanceToReferenceToVarIdMap.put(var.compInst, referenceToVarIdMap);
}
Collection<String> varIds = referenceToVarIdMap.get(var.reference);
if (varIds == null) {
varIds = new HashSet<>();
referenceToVarIdMap.put(var.reference, varIds);
}
varIds.add(var.id);
}
}
}
}
// Create the simulation properties.
// A single simulation property will be created for each component instance, reference combination.
int propertyIndex = 0;
for (final Entry<ComponentInstance, Map<EObject, Collection<String>>> componentInstanceToReferenceToVarIdMapEntry : componentInstanceToReferenceToVarIdMap.entrySet()) {
final ComponentInstance propComponentInstance = componentInstanceToReferenceToVarIdMapEntry.getKey();
for (Entry<EObject, Collection<String>> referenceToVarIdsEntry : componentInstanceToReferenceToVarIdMapEntry.getValue().entrySet()) {
final EObject propReference = referenceToVarIdsEntry.getKey();
final Collection<String> propLustreIds = referenceToVarIdsEntry.getValue();
final String propertyDesc = getDescription(propComponentInstance, propReference);
final String enablementVariableId;
// Only properties which cause the simulation to be halted may be disabled.
// That is: only top-level assumptions and non-top-level guarantees may be disabled
final boolean disableable = (propReference instanceof GuaranteeStatement && propComponentInstance != propComponentInstance.getSystemInstance()) || (propReference instanceof AssumeStatement && propComponentInstance == propComponentInstance.getSystemInstance());
if (disableable) {
enablementVariableId = propertyEnablementPrefix + propertyIndex;
lustreNodeBuilder.addLocal(new VarDecl(enablementVariableId, NamedType.BOOL));
} else {
enablementVariableId = null;
}
final SimulationProperty simProp = new SimulationProperty(propLustreIds, propertyDesc, propReference, enablementVariableId);
builder.addSimulationProperty(simProp);
propertyIndex++;
for (final String propLustreId : propLustreIds) {
idToSimulationPropertyMap.put(propLustreId, simProp);
lustreNodeBuilder.addIvc(propLustreId);
}
}
}
// Edit main node to make simulation properties inside of assertions to be disableable
final AstMapVisitor propertyVarDisableTransformation = new AstMapVisitor() {
@Override
public Expr visit(final IdExpr e) {
final SimulationProperty simProp = idToSimulationPropertyMap.get(e.id);
if (simProp != null && simProp.getEnablementVariableId() != null) {
return new BinaryExpr(new UnaryExpr(UnaryOp.NOT, new IdExpr(simProp.getEnablementVariableId())), BinaryOp.OR, e);
}
return super.visit(e);
}
@Override
public Equation visit(final Equation e) {
// Don't transform the equations for simulation properties
if (e.lhs.size() == 1 && idToSimulationPropertyMap.containsKey(e.lhs.get(0))) {
return e;
}
return super.visit(e);
}
};
// Transform assertions and equations
lustreNodeBuilder.clearAssertions();
for (final Expr assertion : mainNode.assertions) {
lustreNodeBuilder.addAssertion(assertion.accept(propertyVarDisableTransformation));
}
lustreNodeBuilder.clearEquations();
for (final Equation eq : mainNode.equations) {
lustreNodeBuilder.addEquation((Equation) eq.accept(propertyVarDisableTransformation));
}
// Build a new program with the new main node
final ProgramBuilder lustreProgramBuilder = new ProgramBuilder(lustreProgram);
lustreProgramBuilder.clearNodes();
lustreProgramBuilder.addNode(lustreNodeBuilder.build());
builder.setLustreProgram(lustreProgramBuilder.build());
return builder.build();
}
use of com.rockwellcollins.atc.agree.analysis.ast.AgreeVar in project AGREE by loonwerks.
the class InlineNodeCalls method getTranslation.
private Map<String, IdExpr> getTranslation(String prefix, Node node) {
Map<String, IdExpr> translation = new HashMap<>();
for (VarDecl decl : Util.getVarDecls(node)) {
String id = prefix + decl.id;
if (decl instanceof SimulationPropertyVar) {
final SimulationPropertyVar var = (SimulationPropertyVar) decl;
newLocals.add(new SimulationPropertyVar(id, decl.type, var.reference, var.compInst, var.featInst));
} else if (decl instanceof AgreeVar) {
final AgreeVar var = (AgreeVar) decl;
newLocals.add(new AgreeVar(id, decl.type, var.reference, var.compInst, var.featInst));
} else {
newLocals.add(new VarDecl(id, decl.type));
}
translation.put(decl.id, new IdExpr(id));
}
return translation;
}
Aggregations