Search in sources :

Example 21 with SafetyException

use of edu.umn.cs.crisys.safety.analysis.SafetyException in project AMASE by loonwerks.

the class AddFaultsToNodeVisitor method buildNonFaultCombinationAssertions.

/**
 * Method builds combinations of faults that cannot occur together based on
 * probability values.
 *
 * Uses macros to shrink the size of the entire formula in case the number of
 * fault combinations is too large for the Lustre parser to handle.
 *
 * @param topNode                         AgreeNode, top of program
 * @param builder                         Node builder will have assertions
 *                                        added.
 * @param elementProbabilities            Prob of elements
 * @param faultCombinationsAboveThreshold Which FaultSetProbabilities are above
 *                                        threshold given in top level annex.
 */
private void buildNonFaultCombinationAssertions(AgreeNode topNode, AgreeNodeBuilder builder, ArrayList<FaultProbability> elementProbabilities, ArrayList<FaultSetProbability> faultCombinationsAboveThreshold) {
    // With the valid fault combinations including dependent faults, and
    // noFaultExpr has the default (no-fault) case. Let's construct a proposition.
    Set<FaultProbability> elementProbabilitySet = new HashSet<>(elementProbabilities);
    // the default (no-fault) case
    Expr faultHypothesis = getNoFaultProposition(elementProbabilitySet);
    // Vars for macros
    List<BinaryExpr> macroList = new ArrayList<BinaryExpr>();
    List<String> macroNames = new ArrayList<String>();
    int noGoodEls = 0;
    int unique = 0;
    for (FaultSetProbability fsp : faultCombinationsAboveThreshold) {
        Set<FaultProbability> goodElements = new HashSet<>(elementProbabilities);
        goodElements.removeAll(fsp.elements);
        // add the assertion that the rest of the faults are not to happen
        if (!goodElements.isEmpty()) {
            noGoodEls = noGoodEls + goodElements.size();
            Expr local = getNoFaultProposition(goodElements);
            faultHypothesis = new BinaryExpr(local, BinaryOp.OR, faultHypothesis);
            // Macros
            if (noGoodEls > SAFE_NUM_ELEMENTS) {
                IdExpr macro = new IdExpr("GOODELS_" + noGoodEls + unique);
                macroNames.add("GOODELS_" + noGoodEls + unique);
                noGoodEls = 0;
                unique++;
                BinaryExpr binMacro = new BinaryExpr(macro, BinaryOp.EQUAL, faultHypothesis);
                macroList.add(binMacro);
                faultHypothesis = macro;
            }
        } else // if there are all faults in the current combination
        // add the assertion that all faults are allowed to happen
        // which will be ORed with the default no fault case
        {
            Expr local = getAllFaultProposition(fsp.elements);
            faultHypothesis = new BinaryExpr(local, BinaryOp.OR, faultHypothesis);
        }
    }
    // Add this fault hypothesis as an assertion if not null.
    if (faultHypothesis == null) {
        new SafetyException("There is a problem with fault hypothesis for component: " + topNode.id + ". A possible problem is that single layer analysis" + " is being run with no faults defined in lower layer." + " Check hypothesis statements and fault defs in this analysis.");
    }
    for (String s : macroNames) {
        builder.addLocal(new AgreeVar(s, NamedType.BOOL, topNode.reference));
    }
    for (BinaryExpr b : macroList) {
        builder.addLocalEquation(new AgreeEquation((IdExpr) b.left, b.right, topNode.reference));
        builder.addAssertion(new AgreeStatement("", b, topNode.reference));
    // builder.addGuarantee(new AgreeStatement("", b, topNode.reference));
    }
    builder.addAssertion(new AgreeStatement("", faultHypothesis, topNode.reference));
}
Also used : AgreeStatement(com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement) IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr) ArrayList(java.util.ArrayList) SafetyException(edu.umn.cs.crisys.safety.analysis.SafetyException) AgreeEquation(com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation) TransientConstraint(edu.umn.cs.crisys.safety.safety.TransientConstraint) PermanentConstraint(edu.umn.cs.crisys.safety.safety.PermanentConstraint) TemporalConstraint(edu.umn.cs.crisys.safety.safety.TemporalConstraint) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar) RecordAccessExpr(jkind.lustre.RecordAccessExpr) UnaryExpr(jkind.lustre.UnaryExpr) Expr(jkind.lustre.Expr) IntExpr(jkind.lustre.IntExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) IdExpr(jkind.lustre.IdExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) HashSet(java.util.HashSet)

Example 22 with SafetyException

use of edu.umn.cs.crisys.safety.analysis.SafetyException in project AMASE by loonwerks.

the class GenMCSHandler method execute.

@Override
public Object execute(ExecutionEvent event) {
    AddFaultsToAgree.resetStaticVars();
    Event selEvent = (Event) event.getTrigger();
    MenuItem item = (MenuItem) selEvent.widget;
    AddFaultsToAgree.setTransformFlag(item);
    // clear static variables before each run
    AddFaultsToNodeVisitor.init();
    if (!SafetyUtil.containsSafetyAnnex(getClassifiers())) {
        new SafetyException("A safety annex in the implementation is required to run the fault analysis.");
        return Status.CANCEL_STATUS;
    }
    // Else, return null.
    if (AddFaultsToAgree.getIsGenMCS()) {
        return super.execute(event);
    } else {
        return null;
    }
}
Also used : ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) Event(org.eclipse.swt.widgets.Event) MenuItem(org.eclipse.swt.widgets.MenuItem) SafetyException(edu.umn.cs.crisys.safety.analysis.SafetyException)

Example 23 with SafetyException

use of edu.umn.cs.crisys.safety.analysis.SafetyException in project AMASE by loonwerks.

the class GenMCSHandler method doAnalysis.

// The following method is copied and modified from AGREE VerifyHandler
private IStatus doAnalysis(final Element root, final IProgressMonitor globalMonitor, AnalysisResult result, AgreeResultsLinker linker) {
    Thread analysisThread = new Thread() {

        @Override
        public void run() {
            activateTerminateHandlers(globalMonitor);
            KindApi api = PreferencesUtil.getKindApi();
            KindApi consistApi = PreferencesUtil.getConsistencyApi();
            JRealizabilityApi realApi = PreferencesUtil.getJRealizabilityApi();
            while (!queue.isEmpty() && !globalMonitor.isCanceled()) {
                JKindResult result = queue.peek();
                NullProgressMonitor subMonitor = new NullProgressMonitor();
                monitorRef.set(subMonitor);
                Program program = linker.getProgram(result);
                if (api instanceof JKindApi) {
                    result.getName();
                }
                try {
                    if (result instanceof ConsistencyResult) {
                        consistApi.execute(program, result, subMonitor);
                    } else if (result instanceof JRealizabilityResult) {
                        realApi.execute(program, (JRealizabilityResult) result, subMonitor);
                    } else {
                        api.execute(program, result, subMonitor);
                    }
                } catch (JKindException e) {
                    new SafetyException("JKind exception: " + e.getMessage());
                    System.out.println("******** JKindException Text ********");
                    e.printStackTrace(System.out);
                    System.out.println("******** JKind Output ********");
                    System.out.println(result.getText());
                    System.out.println("******** Agree Lustre ********");
                    System.out.println(program);
                    break;
                }
                queue.remove();
            }
            while (!queue.isEmpty()) {
                queue.remove().cancel();
            }
            // then print empty min cut set fault tree
            if ((!AddFaultsToNodeVisitor.maxFaultHypothesis && !AddFaultsToNodeVisitor.probabilisticHypothesis) || (AddFaultsToNodeVisitor.maxFaultHypothesis && (AddFaultsToNodeVisitor.maxFaultCount == 0)) || (AddFaultsToNodeVisitor.probabilisticHypothesis && AddFaultsToNodeVisitor.faultCombinationsAboveThreshold.isEmpty())) {
                PrintUtils printUtils = new PrintUtils();
                printUtils.printEmptyTree();
                try {
                    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
                    File file = File.createTempFile("ResolvedFT_" + timeStamp + "_", ".ml");
                    BufferedWriter bw = new BufferedWriter(new FileWriter(file));
                    bw.write(printUtils.toString());
                    bw.close();
                    org.eclipse.swt.program.Program.launch(file.toString());
                } catch (IOException e) {
                    Dialog.showError("Unable to open file", e.getMessage());
                    e.printStackTrace();
                }
            } else {
                // open progress bar
                // shell.open();
                IvcToFTGenerator ftGenerator = new IvcToFTGenerator();
                FTResolveVisitor resolveVisitor = new FTResolveVisitor();
                FaultTree faultTree = ftGenerator.generateFT(result, linker);
                resolveVisitor.visit(faultTree);
                LinkedHashMap<String, Set<List<String>>> mapForHFT = ftGenerator.getMapPropertyToMCSs();
                try {
                    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
                    File hierarchyFTFile = File.createTempFile("HierarchicalCausalFactors_" + timeStamp + "_", ".txt");
                    BufferedWriter bw = new BufferedWriter(new FileWriter(hierarchyFTFile));
                    PrintUtils printUtils = new PrintUtils();
                    bw.write(printUtils.printHierarchicalText(mapForHFT));
                    bw.close();
                    // display.dispose();
                    org.eclipse.swt.program.Program.launch(hierarchyFTFile.toString());
                } catch (IOException e) {
                    // close progress bar
                    // display.dispose();
                    Dialog.showError("Unable to open file", e.getMessage());
                    e.printStackTrace();
                }
                try {
                    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
                    File minCutSetFile = File.createTempFile("MinCutSet_" + timeStamp + "_", ".txt");
                    BufferedWriter bw = new BufferedWriter(new FileWriter(minCutSetFile));
                    bw.write(faultTree.printMinCutSetTxt());
                    bw.close();
                    // display.dispose();
                    org.eclipse.swt.program.Program.launch(minCutSetFile.toString());
                } catch (IOException e) {
                    // close progress bar
                    // display.dispose();
                    Dialog.showError("Unable to open file", e.getMessage());
                    e.printStackTrace();
                }
                try {
                    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
                    File minCutSetTallyFile = File.createTempFile("MinCutSetTally_" + timeStamp + "_", ".txt");
                    BufferedWriter bw = new BufferedWriter(new FileWriter(minCutSetTallyFile));
                    bw.write(faultTree.printMinCutSetTally());
                    bw.close();
                    // display.dispose();
                    org.eclipse.swt.program.Program.launch(minCutSetTallyFile.toString());
                } catch (IOException e) {
                    // close progress bar
                    // display.dispose();
                    Dialog.showError("Unable to open file", e.getMessage());
                    e.printStackTrace();
                }
            }
            AddFaultsToAgree.resetStaticVars();
            deactivateTerminateHandlers();
            enableRerunHandler(root);
        }
    };
    analysisThread.start();
    return Status.OK_STATUS;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JRealizabilityResult(jkind.api.results.JRealizabilityResult) Program(jkind.lustre.Program) AgreeProgram(com.rockwellcollins.atc.agree.analysis.ast.AgreeProgram) JKindException(jkind.JKindException) Set(java.util.Set) FileWriter(java.io.FileWriter) IvcToFTGenerator(edu.umn.cs.crisys.safety.analysis.generators.IvcToFTGenerator) IOException(java.io.IOException) SafetyException(edu.umn.cs.crisys.safety.analysis.SafetyException) FTResolveVisitor(edu.umn.cs.crisys.safety.analysis.ast.visitors.FTResolveVisitor) Date(java.util.Date) JKindResult(jkind.api.results.JKindResult) JKindApi(jkind.api.JKindApi) PrintUtils(edu.umn.cs.crisys.safety.analysis.ast.visitors.PrintUtils) BufferedWriter(java.io.BufferedWriter) JRealizabilityApi(jkind.api.JRealizabilityApi) ConsistencyResult(com.rockwellcollins.atc.agree.analysis.ConsistencyResult) FaultTree(edu.umn.cs.crisys.safety.analysis.faultTree.FaultTree) KindApi(jkind.api.KindApi) JKindApi(jkind.api.JKindApi) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Example 24 with SafetyException

use of edu.umn.cs.crisys.safety.analysis.SafetyException in project AMASE by loonwerks.

the class FaultASTBuilder method renameEqId.

/**
 * Make new fault with updated eq stmt ids
 * @param f fault to update
 * @param idMap map from old id to new
 * @return new updated fault
 */
private Fault renameEqId(Fault f, Map<String, String> idMap) {
    Fault newFault = new Fault(f);
    newFault.safetyEqVars.clear();
    newFault.safetyEqAsserts.clear();
    newFault.faultOutputMap.clear();
    newFault.faultInputMap.clear();
    if (!f.triggers.isEmpty()) {
        throw new SafetyException("User-defined triggers are currently unsupported.");
    }
    // update the variable declarations
    for (AgreeVar eq : f.safetyEqVars) {
        if (idMap.containsKey(eq.id)) {
            eq = new AgreeVar(idMap.get(eq.id), eq.type, eq.reference);
        }
        newFault.safetyEqVars.add(eq);
    }
    ReplaceIdVisitor visitor = new ReplaceIdVisitor(idMap);
    for (AgreeStatement s : f.safetyEqAsserts) {
        newFault.safetyEqAsserts.add(visitor.visit(s));
    }
    for (Map.Entry<Expr, String> element : f.faultOutputMap.entrySet()) {
        newFault.faultOutputMap.put(element.getKey().accept(visitor), element.getValue());
    }
    for (Map.Entry<String, Expr> element : f.faultInputMap.entrySet()) {
        newFault.faultInputMap.put(element.getKey(), element.getValue().accept(visitor));
    }
    return newFault;
}
Also used : AgreeStatement(com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement) RecordAccessExpr(jkind.lustre.RecordAccessExpr) TupleExpr(jkind.lustre.TupleExpr) Expr(jkind.lustre.Expr) NodeCallExpr(jkind.lustre.NodeCallExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) IdExpr(jkind.lustre.IdExpr) ReplaceIdVisitor(edu.umn.cs.crisys.safety.analysis.ast.visitors.ReplaceIdVisitor) SafetyException(edu.umn.cs.crisys.safety.analysis.SafetyException) Map(java.util.Map) HashMap(java.util.HashMap) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)

Example 25 with SafetyException

use of edu.umn.cs.crisys.safety.analysis.SafetyException in project AMASE by loonwerks.

the class FaultASTBuilder method setOutput.

/**
 * Populate the fault output map
 *
 * @param the fault in question
 * @param the output to go into the map
 */
private void setOutput(Fault fault, OutputStatement output) {
    for (int i = 0; i < output.getFault_out().size(); i++) {
        String param = output.getFault_out().get(i);
        NamedElement compOut = output.getNom_conn().get(i);
        if (compOut == null) {
            throw new SafetyException("The component " + agreeNode.id + " with fault " + fault.id + " has undefined output.");
        } else {
            IdExpr result = new IdExpr(compOut.getName());
            fault.faultOutputMap.put(result, param);
        }
    }
}
Also used : IdExpr(jkind.lustre.IdExpr) SafetyException(edu.umn.cs.crisys.safety.analysis.SafetyException) NamedElement(org.osate.aadl2.NamedElement)

Aggregations

SafetyException (edu.umn.cs.crisys.safety.analysis.SafetyException)34 ArrayList (java.util.ArrayList)14 IdExpr (jkind.lustre.IdExpr)12 AgreeVar (com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)9 BinaryExpr (jkind.lustre.BinaryExpr)9 BoolExpr (jkind.lustre.BoolExpr)9 Expr (jkind.lustre.Expr)9 NodeCallExpr (jkind.lustre.NodeCallExpr)9 RecordAccessExpr (jkind.lustre.RecordAccessExpr)9 AgreeStatement (com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement)7 IfThenElseExpr (jkind.lustre.IfThenElseExpr)7 ArrayAccessExpr (jkind.lustre.ArrayAccessExpr)6 IntExpr (jkind.lustre.IntExpr)6 UnaryExpr (jkind.lustre.UnaryExpr)6 BaseFault (edu.umn.cs.crisys.safety.analysis.transform.BaseFault)5 PermanentConstraint (edu.umn.cs.crisys.safety.safety.PermanentConstraint)5 TemporalConstraint (edu.umn.cs.crisys.safety.safety.TemporalConstraint)5 TransientConstraint (edu.umn.cs.crisys.safety.safety.TransientConstraint)5 FTAndNode (edu.umn.cs.crisys.safety.analysis.faultTree.FTAndNode)3 FTNode (edu.umn.cs.crisys.safety.analysis.faultTree.FTNode)3