use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class AgreeNodeToLustreContract method translateNode.
private static Node translateNode(AgreeNode agreeNode) {
List<VarDecl> inputs = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<Equation> eqs = new ArrayList<>();
List<Expr> assertions = new ArrayList<>();
List<String> properties = new ArrayList<>();
inputs.addAll(agreeNode.inputs);
inputs.addAll(agreeNode.outputs);
inputs.addAll(agreeNode.locals);
eqs.addAll(nodeAssertsToEqs(agreeNode));
// right now the AGREE AST just has assertions over input variables
// this step allows us to inline some of these as local variables
// to the node
List<IdExpr> ids = gatherIds(eqs);
List<VarDecl> inlinedVars = new ArrayList<>();
for (VarDecl var : inputs) {
for (IdExpr id : ids) {
if (id.id.equals(var.id)) {
inlinedVars.add(var);
}
}
}
inputs.removeAll(inlinedVars);
locals.addAll(inlinedVars);
for (AgreeStatement statement : agreeNode.assumptions) {
assertions.add(statement.expr);
}
int i = 0;
for (AgreeStatement statement : agreeNode.guarantees) {
String guarName = "__GUARANTEE" + i++;
properties.add(guarName);
locals.add(new VarDecl(guarName, NamedType.BOOL));
eqs.add(new Equation(new IdExpr(guarName), statement.expr));
}
NodeBuilder builder = new NodeBuilder(agreeNode.id);
builder.addInputs(inputs);
builder.addLocals(locals);
builder.addEquations(eqs);
builder.addProperties(properties);
builder.addAssertions(assertions);
return builder.build();
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class LustreAstBuilder method removeProperties.
protected static Node removeProperties(Node node) {
NodeBuilder builder = new NodeBuilder(node.id);
builder.addInputs(node.inputs);
builder.addOutputs(node.outputs);
builder.addLocals(node.locals);
builder.addEquations(node.equations);
return builder.build();
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class LustreAstBuilder method getLustreNode.
protected static Node getLustreNode(AgreeNode agreeNode, String nodePrefix) {
List<VarDecl> inputs = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<Equation> equations = new ArrayList<>();
List<Expr> assertions = new ArrayList<>();
List<String> ivcs = agreeNode.getivcElements();
List<String> properties = new ArrayList<>();
// add assumption history variable
IdExpr assumHist = new IdExpr(assumeHistSufix);
inputs.add(new AgreeVar(assumHist.id, NamedType.BOOL, null, agreeNode.compInst, null));
int i = 0;
for (AgreeStatement statement : agreeNode.assumptions) {
String inputName = assumeSuffix + i++;
inputs.add(new AgreeVar(inputName, NamedType.BOOL, statement.reference, agreeNode.compInst, null));
IdExpr assumeId = new IdExpr(inputName);
assertions.add(new BinaryExpr(assumeId, BinaryOp.EQUAL, statement.expr));
}
int j = 0;
for (AgreeStatement statement : agreeNode.lemmas) {
String inputName = lemmaSuffix + j++;
inputs.add(new AgreeVar(inputName, NamedType.BOOL, statement.reference, agreeNode.compInst, null));
properties.add(inputName);
IdExpr lemmaId = new IdExpr(inputName);
assertions.add(new BinaryExpr(lemmaId, BinaryOp.EQUAL, statement.expr));
}
int k = 0;
Expr guarConjExpr = new BoolExpr(true);
for (AgreeStatement statement : agreeNode.guarantees) {
String inputName = guarSuffix + k++;
locals.add(new AgreeVar(inputName, NamedType.BOOL, statement.reference, agreeNode.compInst, null));
IdExpr guarId = new IdExpr(inputName);
equations.add(new Equation(guarId, statement.expr));
if (agreeNode.getFaultTreeFlag() == false) {
ivcs.add(guarId.id);
} else {
// check if it's leaf node
if (!agreeNode.compInst.getComponentInstances().isEmpty()) {
ivcs.add(guarId.id);
}
}
guarConjExpr = LustreExprFactory.makeANDExpr(guarId, guarConjExpr);
}
// assert that if the assumptions have held historically, then the
// gurantees hold
assertions.add(new BinaryExpr(assumHist, BinaryOp.IMPLIES, guarConjExpr));
for (AgreeStatement statement : agreeNode.assertions) {
assertions.add(statement.expr);
}
// create properties for the patterns
int l = 0;
for (AgreeStatement patternPropState : agreeNode.patternProps) {
String patternVarName = patternPropSuffix + l++;
inputs.add(new AgreeVar(patternVarName, NamedType.BOOL, patternPropState, agreeNode.compInst, null));
assertions.add(new BinaryExpr(new IdExpr(patternVarName), BinaryOp.EQUAL, patternPropState.expr));
}
Expr assertExpr = new BoolExpr(true);
for (Expr expr : assertions) {
assertExpr = LustreExprFactory.makeANDExpr(expr, assertExpr);
}
String outputName = "__ASSERT";
List<VarDecl> outputs = new ArrayList<>();
outputs.add(new VarDecl(outputName, NamedType.BOOL));
equations.add(new Equation(new IdExpr(outputName), assertExpr));
// gather the remaining inputs
for (AgreeVar var : agreeNode.inputs) {
inputs.add(var);
}
for (AgreeVar var : agreeNode.outputs) {
inputs.add(var);
}
for (AgreeVar var : agreeNode.locals) {
locals.add(var);
}
for (AgreeEquation equation : agreeNode.localEquations) {
equations.add(equation);
}
NodeBuilder builder = new NodeBuilder(nodePrefix + agreeNode.id);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(locals);
builder.addEquations(equations);
builder.addProperties(properties);
builder.addIvcs(ivcs);
return builder.build();
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class LustreAstBuilder method getRealizabilityLustreProgram.
// private static AgreeProgram translate(AgreeProgram program){
// return AgreeInlineLatchedConnections.translate(program);
// }
public static Program getRealizabilityLustreProgram(AgreeProgram agreeProgram) {
List<TypeDef> types = AgreeUtils.getLustreTypes(agreeProgram);
List<Expr> assertions = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<VarDecl> inputs = new ArrayList<>();
List<Equation> equations = new ArrayList<>();
List<String> properties = new ArrayList<>();
AgreeNode topNode = agreeProgram.topNode;
for (AgreeStatement assumption : topNode.assumptions) {
assertions.add(assumption.expr);
}
int i = 0;
for (AgreeStatement guarantee : topNode.guarantees) {
String guarName = guarSuffix + i++;
locals.add(new AgreeVar(guarName, NamedType.BOOL, guarantee.reference, topNode.compInst, null));
equations.add(new Equation(new IdExpr(guarName), guarantee.expr));
properties.add(guarName);
}
List<String> inputStrs = new ArrayList<>();
for (AgreeVar var : topNode.inputs) {
inputs.add(var);
inputStrs.add(var.id);
}
for (AgreeVar var : topNode.outputs) {
inputs.add(var);
}
// and type equations. This would clear this up
for (AgreeStatement statement : topNode.assertions) {
if (AgreeUtils.referenceIsInContract(statement, topNode.compInst)) {
// this is a strange hack we have to do. we have to make
// equation and property
// statements not assertions. They should all be binary
// expressions with an
// equals operator. We will need to removing their corresponding
// variable
// from the inputs and add them to the local variables
BinaryExpr binExpr;
IdExpr varId;
try {
binExpr = (BinaryExpr) statement.expr;
varId = (IdExpr) binExpr.left;
} catch (ClassCastException e) {
// some equation variables are assertions for
// subrange types. do not translate these to
// local equations. Just add them to assertions
assertions.add(statement.expr);
continue;
}
boolean found = false;
int index;
for (index = 0; index < inputs.size(); index++) {
VarDecl var = inputs.get(index);
if (var.id.equals(varId.id)) {
found = true;
break;
}
}
if (!found || binExpr.op != BinaryOp.EQUAL) {
throw new AgreeException("Something went very wrong with the lustre generation in the realizability analysis");
}
locals.add(inputs.remove(index));
equations.add(new Equation(varId, binExpr.right));
}
}
NodeBuilder builder = new NodeBuilder("main");
builder.addInputs(inputs);
builder.addLocals(locals);
builder.addEquations(equations);
builder.addProperties(properties);
builder.addAssertions(assertions);
builder.setRealizabilityInputs(inputStrs);
Node main = builder.build();
List<Node> nodes = new ArrayList<>();
nodes.add(main);
nodes.addAll(agreeProgram.globalLustreNodes);
List<Function> uFunctions = new ArrayList<>();
uFunctions.addAll(agreeProgram.uninterpretedFunctions);
Program program = new ProgramBuilder().addTypes(types).addFunctions(uFunctions).addNodes(nodes).setMain(main.id).build();
return program;
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class LustreContractAstBuilder method getContractLustreProgram.
public static Program getContractLustreProgram(AgreeProgram agreeProgram) {
nodes = new ArrayList<>();
List<TypeDef> types = AgreeUtils.getLustreTypes(agreeProgram);
AgreeNode flatNode = flattenAgreeNodeKindContract(agreeProgram.topNode, "_TOP__");
List<Expr> assertions = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<VarDecl> inputs = new ArrayList<>();
List<VarDecl> outputs = new ArrayList<>();
List<Equation> equations = new ArrayList<>();
List<String> properties = new ArrayList<>();
List<Expr> requires = new ArrayList<>();
List<Expr> ensures = new ArrayList<>();
for (AgreeStatement assertion : flatNode.assertions) {
assertions.add(assertion.expr);
}
for (AgreeStatement assumption : flatNode.assumptions) {
requires.add(assumption.expr);
}
for (AgreeStatement guarantee : flatNode.lemmas) {
ensures.add(guarantee.expr);
}
for (AgreeStatement guarantee : flatNode.guarantees) {
ensures.add(guarantee.expr);
}
for (AgreeVar var : flatNode.inputs) {
inputs.add(var);
}
for (AgreeVar var : flatNode.outputs) {
outputs.add(var);
}
for (AgreeVar var : flatNode.outputs) {
if (var.reference instanceof AssumeStatement || var.reference instanceof LemmaStatement) {
throw new AgreeException("This shouldn't happen");
}
}
Contract contract = new Contract(requires, ensures);
NodeBuilder builder = new NodeBuilder("_TOP");
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(locals);
builder.addEquations(equations);
builder.addProperties(properties);
builder.addAssertions(assertions);
builder.setContract(contract);
Node main = builder.build();
nodes.addAll(agreeProgram.globalLustreNodes);
nodes.add(main);
Program program = new ProgramBuilder().addTypes(types).addNodes(nodes).setMain(main.id).build();
return program;
}
Aggregations