use of com.rockwellcollins.atc.agree.agree.NodeEq in project VERDICT by ge-high-assurance.
the class Agree2Vdm method addNodeDefToTypeDeclarations.
private void addNodeDefToTypeDeclarations(NodeDef nodeDef, HashSet<String> dataTypeDecl, HashSet<String> nodeDecl, Model model) {
Node vdmNode = new Node();
// String agreeNodeName = nodeDef.getName();
String agreeNodeName = nodeDef.getQualifiedName();
vdmNode.setName(agreeNodeName);
// SETTING NODE INPUT PARAMETERS
// get agree node's args and set them as input parameter
EList<Arg> nodeInpArgs = nodeDef.getArgs();
for (Arg arg : nodeInpArgs) {
NodeParameter nodeParameter = new NodeParameter();
nodeParameter.setName(arg.getName());
// get types of each arg and define those types if needed -- will be done in getVdmTypeFromAgreeType()
nodeParameter.setDataType(getVdmTypeFromAgreeType(arg.getType(), dataTypeDecl, model));
vdmNode.getInputParameter().add(nodeParameter);
}
// SETTING NODE OUTPUT PARAMETERS
EList<Arg> nodeReturnArgs = nodeDef.getRets();
// get agree node's rets and set them as output parameter
for (Arg arg : nodeReturnArgs) {
NodeParameter nodeParameter = new NodeParameter();
nodeParameter.setName(arg.getName());
// get types of each arg and define those types if needed -- will be done in getVdmTypeFromAgreeType()
nodeParameter.setDataType(getVdmTypeFromAgreeType(arg.getType(), dataTypeDecl, model));
vdmNode.getOutputParameter().add(nodeParameter);
}
// SETTING NODE BODY
NodeBody vdmNodeBody = new NodeBody();
// get agree node's body
NodeBodyExpr agreeNodeBody = nodeDef.getNodeBody();
EList<NodeStmt> agreeNodeStmts = agreeNodeBody.getStmts();
for (NodeStmt agreeNodeStmt : agreeNodeStmts) {
if (agreeNodeStmt instanceof NodeEqImpl) {
NodeEq agreeNodeEq = (NodeEq) agreeNodeStmt;
// get all LHS identifiers in the statement and add it to the vdm node equation LHS
NodeEquation vdmNodeEquation = new NodeEquation();
EList<Arg> agreeLHSArgs = agreeNodeEq.getLhs();
// this type is just a list of strings
NodeEquationLHS vdmNodeEquationLHS = new NodeEquationLHS();
for (Arg agreeLHSArg : agreeLHSArgs) {
vdmNodeEquationLHS.getIdentifier().add(agreeLHSArg.getName());
}
vdmNodeEquation.setLhs(vdmNodeEquationLHS);
// get the RHS i.e.expr of the agree NodeEq and set it as the vdm node equation's RHS
vdmNodeEquation.setRhs(getVdmExpressionFromAgreeExpression(agreeNodeEq.getExpr(), dataTypeDecl, nodeDecl, model));
vdmNodeBody.getEquation().add(vdmNodeEquation);
} else {
System.out.println("Node contains non-eq type statements");
}
}
vdmNode.setBody(vdmNodeBody);
vdmNode.setIsFunction(false);
vdmNode.setIsImported(false);
if (!nodeDecl.contains(agreeNodeName)) {
nodeDecl.add(agreeNodeName);
LustreProgram lustreProgram = model.getDataflowCode();
lustreProgram.getNodeDeclaration().add(vdmNode);
model.setDataflowCode(lustreProgram);
}
}
use of com.rockwellcollins.atc.agree.agree.NodeEq in project AGREE by loonwerks.
the class AgreeASTBuilder method caseNodeDef.
@Override
public Expr caseNodeDef(NodeDef expr) {
String nodeName = AgreeUtils.getNodeName(expr);
for (Node node : globalNodes) {
if (node.id.equals(nodeName)) {
return null;
}
}
List<VarDecl> inputs = agreeVarsFromArgs(expr.getArgs(), null);
List<VarDecl> outputs = agreeVarsFromArgs(expr.getRets(), null);
NodeBodyExpr body = expr.getNodeBody();
List<VarDecl> internals = agreeVarsFromArgs(body.getLocs(), null);
List<Equation> eqs = new ArrayList<>();
List<String> props = new ArrayList<>();
// TODO are node lemmas deprecated?
String lemmaName = "__nodeLemma";
int lemmaIndex = 0;
for (NodeStmt stmt : body.getStmts()) {
if (stmt instanceof NodeLemma) {
NodeLemma nodeLemma = (NodeLemma) stmt;
String propName = lemmaName + lemmaIndex++;
IdExpr eqId = new IdExpr(propName);
internals.add(new VarDecl(eqId.id, NamedType.BOOL));
Expr eqExpr = doSwitch(nodeLemma.getExpr());
Equation eq = new Equation(eqId, eqExpr);
eqs.add(eq);
props.add(eqId.id);
} else if (stmt instanceof NodeEq) {
eqs.add(nodeEqToEq((NodeEq) stmt));
}
}
NodeBuilder builder = new NodeBuilder(nodeName);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(internals);
builder.addEquations(eqs);
builder.addProperties(props);
Node n = builder.build();
addToNodeList(n);
return null;
}
use of com.rockwellcollins.atc.agree.agree.NodeEq in project AGREE by loonwerks.
the class LinearizationRewriter method generateAgreeConstraintForm.
private static NodeDef generateAgreeConstraintForm(LinearizationDef linDef, BoundingSegments segs) {
NodeDef result = af.createNodeDef();
result.setName(getConstraintFormName(AgreeUtils.getNodeName(linDef)));
Arg inputArg = af.createArg();
PrimType inputArgType = af.createPrimType();
inputArgType.setName("real");
inputArg.setType(inputArgType);
inputArg.setName("inp");
result.getArgs().add(inputArg);
Arg outputArg = af.createArg();
PrimType outputArgType = af.createPrimType();
outputArgType.setName("real");
outputArg.setType(outputArgType);
outputArg.setName("result");
result.getArgs().add(outputArg);
Arg constraintArg = af.createArg();
PrimType constraintArgType = af.createPrimType();
constraintArgType.setName("bool");
constraintArg.setType(constraintArgType);
constraintArg.setName("constraint");
result.getRets().add(constraintArg);
NamedElmExpr inputId = af.createNamedElmExpr();
inputId.setElm(EcoreUtil.copy(inputArg));
RealLitExpr domainCheckLowerLit = af.createRealLitExpr();
domainCheckLowerLit.setVal(Double.toString(segs.lower.getFirst().startX));
BinaryExpr domainCheckLowerExpr = af.createBinaryExpr();
domainCheckLowerExpr.setOp("<=");
domainCheckLowerExpr.setLeft(domainCheckLowerLit);
domainCheckLowerExpr.setRight(EcoreUtil.copy(inputId));
RealLitExpr domainCheckUpperLit = af.createRealLitExpr();
domainCheckUpperLit.setVal(Double.toString(segs.lower.getLast().stopX));
BinaryExpr domainCheckUpperExpr = af.createBinaryExpr();
domainCheckUpperExpr.setOp("<=");
domainCheckUpperExpr.setLeft(EcoreUtil.copy(inputId));
domainCheckUpperExpr.setRight(domainCheckUpperLit);
BinaryExpr domainCheckExpr = af.createBinaryExpr();
domainCheckExpr.setOp("and");
domainCheckExpr.setLeft(domainCheckLowerExpr);
domainCheckExpr.setRight(domainCheckUpperExpr);
BoolLitExpr trueLitExpr = af.createBoolLitExpr();
BooleanLiteral trueLitValue = aadlF.createBooleanLiteral();
trueLitValue.setValue(true);
trueLitExpr.setVal(trueLitValue);
Expr upperBoundExpr = EcoreUtil.copy(trueLitExpr);
for (Segment seg : segs.upper) {
BinaryExpr andExpr = af.createBinaryExpr();
andExpr.setOp("and");
andExpr.setLeft(upperBoundExpr);
andExpr.setRight(generateAgreeLinearBoundImplicationExpr(inputArg, outputArg, "<=", seg));
upperBoundExpr = andExpr;
}
Expr lowerBoundExpr = EcoreUtil.copy(trueLitExpr);
for (Segment seg : segs.lower) {
BinaryExpr andExpr = af.createBinaryExpr();
andExpr.setOp("and");
andExpr.setLeft(lowerBoundExpr);
andExpr.setRight(generateAgreeLinearBoundImplicationExpr(inputArg, outputArg, ">=", seg));
lowerBoundExpr = andExpr;
}
BinaryExpr boundsCheckExpr = af.createBinaryExpr();
boundsCheckExpr.setOp("and");
boundsCheckExpr.setLeft(upperBoundExpr);
boundsCheckExpr.setRight(lowerBoundExpr);
BinaryExpr constraintExpr = af.createBinaryExpr();
constraintExpr.setOp("and");
constraintExpr.setLeft(domainCheckExpr);
constraintExpr.setRight(boundsCheckExpr);
NodeEq constraintEq = af.createNodeEq();
constraintEq.getLhs().add(constraintArg);
constraintEq.setExpr(constraintExpr);
NodeBodyExpr nodeBody = af.createNodeBodyExpr();
nodeBody.getStmts().add(constraintEq);
result.setNodeBody(nodeBody);
NodeLemma domainCheckLemma = af.createNodeLemma();
domainCheckLemma.setStr(result.getName() + " domain check");
domainCheckLemma.setExpr(EcoreUtil.copy(domainCheckExpr));
nodeBody.getStmts().add(domainCheckLemma);
return result;
}
Aggregations