Search in sources :

Example 1 with AstMapVisitor

use of jkind.lustre.visitors.AstMapVisitor in project AGREE by loonwerks.

the class RemoveCondacts method clockNodeCalls.

private Node clockNodeCalls(Node node, final IdExpr clock) {
    return (Node) node.accept(new AstMapVisitor() {

        @Override
        public Expr visit(NodeCallExpr e) {
            List<Expr> args = new ArrayList<>();
            args.add(clock);
            args.addAll(visitExprs(e.args));
            Node clocked = createClockedNode(e.node);
            return new NodeCallExpr(clocked.id, args);
        }

        @Override
        public Expr visit(CondactExpr e) {
            NodeCallExpr call = (NodeCallExpr) super.visit(e.call);
            List<Expr> args = new ArrayList<>();
            args.add(new BinaryExpr(e.clock.accept(this), BinaryOp.AND, clock));
            args.addAll(e.call.args);
            args.addAll(visitExprs(e.args));
            Node condact = createCondactNode(call.node);
            return new NodeCallExpr(condact.id, args);
        }
    });
}
Also used : AstMapVisitor(jkind.lustre.visitors.AstMapVisitor) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) CondactExpr(jkind.lustre.CondactExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) CondactExpr(jkind.lustre.CondactExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) Node(jkind.lustre.Node) BinaryExpr(jkind.lustre.BinaryExpr) ArrayList(java.util.ArrayList)

Example 2 with AstMapVisitor

use of jkind.lustre.visitors.AstMapVisitor in project AGREE by loonwerks.

the class RemoveCondacts method removeCondacts.

private Node removeCondacts(Node node) {
    return (Node) node.accept(new AstMapVisitor() {

        @Override
        public Expr visit(CondactExpr e) {
            NodeCallExpr call = (NodeCallExpr) e.call.accept(this);
            List<Expr> args = new ArrayList<>();
            args.add(e.clock.accept(this));
            args.addAll(e.call.args);
            args.addAll(visitExprs(e.args));
            Node condact = createCondactNode(call.node);
            return new NodeCallExpr(condact.id, args);
        }
    });
}
Also used : AstMapVisitor(jkind.lustre.visitors.AstMapVisitor) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) CondactExpr(jkind.lustre.CondactExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) CondactExpr(jkind.lustre.CondactExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) Node(jkind.lustre.Node) ArrayList(java.util.ArrayList)

Example 3 with AstMapVisitor

use of jkind.lustre.visitors.AstMapVisitor in project AGREE by loonwerks.

the class RemoveCondacts method clockArrowsAndPres.

private Node clockArrowsAndPres(Node node, final IdExpr clock) {
    final VarDecl init = new VarDecl(namePrefix + "init", NamedType.BOOL);
    final List<Equation> preEquations = new ArrayList<>();
    final List<VarDecl> preLocals = new ArrayList<>();
    node = (Node) node.accept(new AstMapVisitor() {

        private int counter = 0;

        @Override
        public Expr visit(BinaryExpr e) {
            if (e.op == BinaryOp.ARROW) {
                return new IfThenElseExpr(new IdExpr(init.id), e.left.accept(this), e.right.accept(this));
            } else {
                return super.visit(e);
            }
        }

        @Override
        public Expr visit(UnaryExpr e) {
            if (e.op == UnaryOp.PRE) {
                String state = namePrefix + "state" + counter++;
                Type type = e.expr.accept(typeReconstructor);
                preLocals.add(new VarDecl(state, type));
                // state = if clock then expr else pre state
                preEquations.add(new Equation(new IdExpr(state), new IfThenElseExpr(clock, e.expr.accept(this), new UnaryExpr(UnaryOp.PRE, new IdExpr(state)))));
                return new UnaryExpr(UnaryOp.PRE, new IdExpr(state));
            } else {
                return super.visit(e);
            }
        }
    });
    NodeBuilder builder = new NodeBuilder(node);
    builder.addLocals(preLocals);
    builder.addLocal(init);
    builder.addEquations(preEquations);
    // init = true -> if pre clock then false else pre init
    builder.addEquation(new Equation(new IdExpr(init.id), new BinaryExpr(new BoolExpr(true), BinaryOp.ARROW, new IfThenElseExpr(new UnaryExpr(UnaryOp.PRE, clock), new BoolExpr(false), new UnaryExpr(UnaryOp.PRE, new IdExpr(init.id))))));
    return builder.build();
}
Also used : BoolExpr(jkind.lustre.BoolExpr) IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder) UnaryExpr(jkind.lustre.UnaryExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) AstMapVisitor(jkind.lustre.visitors.AstMapVisitor) Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) CondactExpr(jkind.lustre.CondactExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) VarDecl(jkind.lustre.VarDecl)

Example 4 with AstMapVisitor

use of jkind.lustre.visitors.AstMapVisitor in project AGREE by loonwerks.

the class RemovePropertySatisficationRequirements method transform.

// private final static String assumeBaseId = "__ASSUME";
public static SimulationProgram transform(final SimulationProgram program) {
    final SimulationProgramBuilder simulationProgramBuilder = new SimulationProgramBuilder(program);
    final ProgramBuilder lustreProgramBuilder = new ProgramBuilder(program.getLustreProgram());
    lustreProgramBuilder.clearNodes();
    for (final Node node : program.getLustreProgram().nodes) {
        final NodeBuilder nodeBuilder = new NodeBuilder(new AstMapVisitor() {

            @Override
            public Node visit(final Node n) {
                return super.visit(n);
            }

            @Override
            public Equation visit(final Equation e) {
                // Force the assumption conjunction to be true
                if (e.lhs.size() == 1 && e.lhs.get(0).id.equals(assumptionConjunctionId)) {
                    return new Equation(new IdExpr(assumptionConjunctionId), new BoolExpr(true));
                }
                final Equation result = super.visit(e);
                return result;
            }
        }.visit(node));
        lustreProgramBuilder.addNode(nodeBuilder.build());
    }
    simulationProgramBuilder.setLustreProgram(lustreProgramBuilder.build());
    return simulationProgramBuilder.build();
}
Also used : BoolExpr(jkind.lustre.BoolExpr) AstMapVisitor(jkind.lustre.visitors.AstMapVisitor) IdExpr(jkind.lustre.IdExpr) ProgramBuilder(jkind.lustre.builders.ProgramBuilder) SimulationProgramBuilder(edu.uah.rsesc.aadlsimulator.agree.SimulationProgramBuilder) Node(jkind.lustre.Node) SimulationProgramBuilder(edu.uah.rsesc.aadlsimulator.agree.SimulationProgramBuilder) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder)

Example 5 with AstMapVisitor

use of jkind.lustre.visitors.AstMapVisitor 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();
}
Also used : HashMap(java.util.HashMap) INode(org.eclipse.xtext.nodemodel.INode) Node(jkind.lustre.Node) NodeBuilder(jkind.lustre.builders.NodeBuilder) LemmaStatement(com.rockwellcollins.atc.agree.agree.LemmaStatement) VarDecl(jkind.lustre.VarDecl) EObject(org.eclipse.emf.ecore.EObject) ComponentInstance(org.osate.aadl2.instance.ComponentInstance) SimulationProperty(edu.uah.rsesc.aadlsimulator.agree.SimulationProperty) GuaranteeStatement(com.rockwellcollins.atc.agree.agree.GuaranteeStatement) Program(jkind.lustre.Program) SimulationProgram(edu.uah.rsesc.aadlsimulator.agree.SimulationProgram) AssumeStatement(com.rockwellcollins.atc.agree.agree.AssumeStatement) IdExpr(jkind.lustre.IdExpr) ProgramBuilder(jkind.lustre.builders.ProgramBuilder) SimulationProgramBuilder(edu.uah.rsesc.aadlsimulator.agree.SimulationProgramBuilder) BinaryExpr(jkind.lustre.BinaryExpr) Equation(jkind.lustre.Equation) UnaryExpr(jkind.lustre.UnaryExpr) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar) AstMapVisitor(jkind.lustre.visitors.AstMapVisitor) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) SimulationProgramBuilder(edu.uah.rsesc.aadlsimulator.agree.SimulationProgramBuilder) AssertStatement(com.rockwellcollins.atc.agree.agree.AssertStatement) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IdExpr (jkind.lustre.IdExpr)5 AstMapVisitor (jkind.lustre.visitors.AstMapVisitor)5 BinaryExpr (jkind.lustre.BinaryExpr)4 BoolExpr (jkind.lustre.BoolExpr)4 Expr (jkind.lustre.Expr)4 Node (jkind.lustre.Node)4 UnaryExpr (jkind.lustre.UnaryExpr)4 ArrayList (java.util.ArrayList)3 CondactExpr (jkind.lustre.CondactExpr)3 Equation (jkind.lustre.Equation)3 IfThenElseExpr (jkind.lustre.IfThenElseExpr)3 NodeCallExpr (jkind.lustre.NodeCallExpr)3 NodeBuilder (jkind.lustre.builders.NodeBuilder)3 SimulationProgramBuilder (edu.uah.rsesc.aadlsimulator.agree.SimulationProgramBuilder)2 VarDecl (jkind.lustre.VarDecl)2 ProgramBuilder (jkind.lustre.builders.ProgramBuilder)2 AssertStatement (com.rockwellcollins.atc.agree.agree.AssertStatement)1 AssumeStatement (com.rockwellcollins.atc.agree.agree.AssumeStatement)1 GuaranteeStatement (com.rockwellcollins.atc.agree.agree.GuaranteeStatement)1 LemmaStatement (com.rockwellcollins.atc.agree.agree.LemmaStatement)1