/// <summary> /// Visits the <paramref name="formula." /> /// </summary> public override void VisitBinaryFormula(BinaryFormula formula) { _builder.Append("("); Visit(formula.LeftOperand); switch (formula.Operator) { case BinaryOperator.And: _builder.Append(" && "); break; case BinaryOperator.Or: _builder.Append(" || "); break; case BinaryOperator.Implication: _builder.Append(" -> "); break; case BinaryOperator.Equivalence: _builder.Append(" <-> "); break; case BinaryOperator.Until: _builder.Append(" U "); break; default: Assert.NotReached($"Unknown or unsupported binary operator '{formula.Operator}'."); break; } Visit(formula.RightOperand); _builder.Append(")"); }
/// <summary> /// Visits the <paramref name="formula." /> /// </summary> public override void VisitBinaryFormula(BinaryFormula formula) { Visit(formula.LeftOperand); var left = _expression; Visit(formula.RightOperand); var right = _expression; switch (formula.Operator) { case BinaryOperator.And: _expression = Expression.AndAlso(left, right); break; case BinaryOperator.Or: _expression = Expression.OrElse(left, right); break; case BinaryOperator.Implication: _expression = Expression.OrElse(Expression.Not(left), right); break; case BinaryOperator.Equivalence: var leftLocal = Expression.Parameter(typeof(bool), "left"); var rightLocal = Expression.Parameter(typeof(bool), "right"); var bothHold = Expression.AndAlso(leftLocal, rightLocal); var neitherHolds = Expression.AndAlso(Expression.Not(leftLocal), Expression.Not(rightLocal)); _expression = Expression.Block( new[] { leftLocal, rightLocal }, Expression.Assign(leftLocal, left), Expression.Assign(rightLocal, right), Expression.OrElse(bothHold, neitherHolds)); break; case BinaryOperator.Until: throw new InvalidOperationException("Only state formulas can be evaluated."); } }
/// <summary> /// Visits the <paramref name="formula" />. /// </summary> public override void VisitBinaryFormula(BinaryFormula formula) { _builder.Append("("); Visit(formula.LeftOperand); switch (formula.Operator) { case BinaryOperator.And: _builder.Append(" && "); break; case BinaryOperator.Or: _builder.Append(" || "); break; case BinaryOperator.Implication: _builder.Append(" -> "); break; case BinaryOperator.Equivalence: _builder.Append(" <-> "); break; case BinaryOperator.Until: _builder.Append(" U "); break; default: Assert.NotReached($"Unknown or unsupported binary operator '{formula.Operator}'."); break; } Visit(formula.RightOperand); _builder.Append(")"); }
/// <summary> /// Calculate the probability distribution of the true values of all variables. /// If just one variable was given, it also sets the probability distribution in the variable /// </summary> private Probability CalculateProbability(IList <RandomVariable> variables) { Console.Out.WriteLine($"Modelchecking joint probability of: {string.Join(",", variables)}."); var tc = SafetySharpModelChecker.TraversalConfiguration; tc.EnableStaticPruningOptimization = false; tc.DefaultTraceOutput = TextWriter.Null; SafetySharpModelChecker.TraversalConfiguration = tc; var last = variables.Last().ToOnceFormula(); for (var i = variables.Count - 2; i >= 0; i--) { last = new BinaryFormula( variables[i].ToOnceFormula(), BinaryOperator.And, last ); } var probability = SafetySharpModelChecker.CalculateProbabilityToReachStateBounded(_model, last, _stepBounds); GC.Collect(); if (variables.Count == 1) { variables[0].Probability = new[] { probability, probability.Complement() }; } Console.Out.WriteLine($"Finished modelchecking. Result is: {probability}"); return(probability); }
public void CheckWithFaultActivationInFormula() { var m = new Model(); Probability probabilityOfFinal1; var state1WithFault1 = new BinaryFormula(new SimpleStateInRangeFormula(1), BinaryOperator.And, new FaultFormula(m.F1)); var finallyState1WithFault1 = new BoundedUnaryFormula(state1WithFault1, UnaryOperator.Finally, 1); var markovChainGenerator = new SimpleMarkovChainFromExecutableModelGenerator(m); markovChainGenerator.Configuration.ModelCapacity = ModelCapacityByMemorySize.Small; markovChainGenerator.Configuration.WriteGraphvizModels = true; markovChainGenerator.Configuration.DefaultTraceOutput = Output.TextWriterAdapter(); markovChainGenerator.AddFormulaToCheck(finallyState1WithFault1); var dtmc = markovChainGenerator.GenerateMarkovChain(); var typeOfModelChecker = typeof(BuiltinDtmcModelChecker); var modelChecker = (DtmcModelChecker)Activator.CreateInstance(typeOfModelChecker, dtmc, Output.TextWriterAdapter()); using (modelChecker) { probabilityOfFinal1 = modelChecker.CalculateProbability(finallyState1WithFault1); } probabilityOfFinal1.Is(0.275, 0.000001).ShouldBe(true); }
public void CheckOnceWithNonAtomarOperand() { var m = new Model(); Probability probability; var once7MoreComplex = new BinaryFormula(Model.IsInState7, BinaryOperator.And, Model.IsInState7); var once7Formula = new UnaryFormula(once7MoreComplex, UnaryOperator.Once); var is9Once7Formula = new BinaryFormula(Model.IsInState9, BinaryOperator.And, once7Formula); var final9Once7Formula = new UnaryFormula(is9Once7Formula, UnaryOperator.Finally); var markovChainGenerator = new SimpleMarkovChainFromExecutableModelGenerator(m); markovChainGenerator.Configuration.ModelCapacity = ModelCapacityByMemorySize.Small; markovChainGenerator.Configuration.UseAtomarPropositionsAsStateLabels = true; markovChainGenerator.Configuration.DefaultTraceOutput = Output.TextWriterAdapter(); markovChainGenerator.Configuration.WriteGraphvizModels = true; markovChainGenerator.Configuration.UseCompactStateStorage = true; markovChainGenerator.Configuration.EnableEarlyTermination = true; markovChainGenerator.AddFormulaToCheck(final9Once7Formula); var dtmc = markovChainGenerator.GenerateMarkovChain(); var typeOfModelChecker = typeof(BuiltinDtmcModelChecker); var modelChecker = (DtmcModelChecker)Activator.CreateInstance(typeOfModelChecker, dtmc, Output.TextWriterAdapter()); using (modelChecker) { probability = modelChecker.CalculateProbability(final9Once7Formula); } probability.Is(0.4 * 0.4 + 0.6, 0.00000001).ShouldBe(true); }
public void Example4() { var baseWorld = LogicSolver.WorldService.GetNewWorldConstant(); var baseIndex = new WorldIndex(baseWorld); var relation = new AccessibilityRelation(new List <IRelationProperty>() { new SerialProperty(), new TransitiveProperty() }); //x var variable = LogicSolver.TermNamer.GetNewVariable(); var func = LogicSolver.TermNamer.GetNewFunction(new List <Terminal>() { variable }); var baseFormula = new AtomicFormula(func, baseIndex); var formula = new BinaryFormula( new QuantifierFormula(new UnaryFormula(baseFormula, UnaryConnective.Necessity, baseIndex), variable, QuantifierConnective.ForAll, baseIndex), new UnaryFormula(new QuantifierFormula(baseFormula, variable, QuantifierConnective.ForAll, baseIndex), UnaryConnective.Necessity, baseIndex), BinaryConnective.Implication, baseIndex); var solver = new LogicSolver(relation); Assert.IsTrue(solver.Solve(formula)); }
/// <summary> /// Visits the <paramref name="formula." /> /// </summary> public override void VisitBinaryFormula(BinaryFormula formula) { Visit(formula.LeftOperand); if (IsLtlFormula) Visit(formula.RightOperand); }
public static Formula CreateBinaryFormula() { var ap1 = new AtomarPropositionFormula(); var ap2 = new AtomarPropositionFormula(); var bexp = new BinaryFormula(ap1, BinaryOperator.And, ap2); return(bexp); }
public static Formula CreateConnectedNestedOnceFormula() { var once1 = CreateNestedOnceFormula(); var once2 = CreateNestedOnceFormula(); var conOnce = new BinaryFormula(once1, BinaryOperator.Or, once2); return(conOnce); }
/// <summary> /// Visits the <paramref name="formula." /> /// </summary> public override void VisitBinaryFormula(BinaryFormula formula) { Visit(formula.LeftOperand); if (IsLtlFormula) { Visit(formula.RightOperand); } }
public override Formula ToOnceFormula() { var faultList = FaultVariables.ToList(); var last = faultList.Last().ToOnceFormula(); for (var i = faultList.Count - 2; i >= 0; i--) { last = new BinaryFormula(faultList[i].ToOnceFormula(), BinaryOperator.And, last); } return(last); }
public Formula ToFormula() { var faultList = Faults.ToList(); Formula last = new FaultFormula(faultList.Last()); for (var i = faultList.Count - 2; i >= 0; i--) { var faultFormula = new FaultFormula(faultList[i]); last = new BinaryFormula(faultFormula, BinaryOperator.And, last); } return(last); }
/// <summary> /// Visits the <paramref name="formula." /> /// </summary> public override void VisitBinaryFormula(BinaryFormula formula) { if (formula.Operator == BinaryOperator.Until) _containsInvalidOperators = true; else { Visit(formula.LeftOperand); if (IsStateFormula) Visit(formula.RightOperand); } }
public Example9() { Mdp = Create(); States = 4; StateDistributions = 5; InitialDistributions = 1; ExampleFormula1 = new BinaryFormula(Label1Formula, BinaryOperator.And, Label2Formula); ExampleFormula2 = new BinaryFormula(Label1Formula, BinaryOperator.Or, Label2Formula); StatesSatisfyDirectlyLabel1Formula = new Dictionary <int, bool>() { { 2, true } }; StatesSatisfyDirectlyLabel2Formula = new Dictionary <int, bool>() { { 3, true } }; StatesSatisfyDirectlyExampleFormula1 = new Dictionary <int, bool>() { }; StatesSatisfyDirectlyExampleFormula2 = new Dictionary <int, bool>() { { 2, true }, { 3, true } }; AncestorsOfStatesWithLabel1 = new Dictionary <int, bool>() { { 0, true }, { 1, true }, { 2, true } }; AncestorsOfStatesWithLabel2 = new Dictionary <int, bool>() { { 0, true }, { 1, true }, { 3, true } }; StatesProb0ALabel1 = new Dictionary <int, bool>() { { 3, true } }; StatesProb1ELabel1 = new Dictionary <int, bool>() { { 2, true } }; StatesProb0ELabel1 = new Dictionary <int, bool>() { { 0, true }, { 1, true }, { 3, true } }; MinimalProbabilityFinallyLabel1 = 0.0; MaximalProbabilityFinallyLabel1 = 0.6; }
public Example4() { Mdp = Create(); States = 5; StateDistributions = 7; InitialDistributions = 1; ExampleFormula1 = new BinaryFormula(Label1Formula, BinaryOperator.And, Label2Formula); ExampleFormula2 = new BinaryFormula(Label1Formula, BinaryOperator.Or, Label2Formula); StatesSatisfyDirectlyLabel1Formula = new Dictionary <long, bool>() { { 2, true } }; StatesSatisfyDirectlyLabel2Formula = new Dictionary <long, bool>() { }; StatesSatisfyDirectlyExampleFormula1 = new Dictionary <long, bool>() { }; StatesSatisfyDirectlyExampleFormula2 = new Dictionary <long, bool>() { { 2, true } }; AncestorsOfStatesWithLabel1 = new Dictionary <long, bool>() { { 0, true }, { 2, true }, { 3, true }, { 4, true } }; AncestorsOfStatesWithLabel2 = new Dictionary <long, bool>() { }; StatesProb0ALabel1 = new Dictionary <long, bool>() { { 1, true } }; StatesProb1ELabel1 = new Dictionary <long, bool>() { { 0, true }, { 2, true }, { 3, true }, { 4, true } }; //Explanation: 1st iteration removes 1. StatesProb0ELabel1 = new Dictionary <long, bool>() { { 0, true }, { 1, true }, { 3, true }, { 4, true } }; MinimalProbabilityFinallyLabel1 = 0.0; MaximalProbabilityFinallyLabel1 = 1.0; }
public Example1() { Ltmdp = Create(); States = 2; StateDistributions = 2; InitialDistributions = 1; ExampleFormula1 = new BinaryFormula(Label1Formula, BinaryOperator.And, Label2Formula); ExampleFormula2 = new BinaryFormula(Label1Formula, BinaryOperator.Or, Label2Formula); MinimalProbabilityFinallyLabel1 = 1.0; MaximalProbabilityFinallyLabel1 = 1.0; }
public void Check() { var m = new Model(); Probability probabilityOfStep11FrozenValue0AndInvariantViolated; Probability probabilityOfStep11FrozenValue1AndInvariantViolated; Probability probabilityOfStep11FrozenValue0AndInvariantNotViolated; Probability probabilityOfStep11FrozenValue1AndInvariantNotViolated; Formula formulaProbabilityOfStep11FrozenValue0AndInvariantViolated = new BinaryFormula(new SimpleStateInRangeFormula(10), BinaryOperator.And, new SimpleLocalVarIsTrue(0)); Formula formulaProbabilityOfStep11FrozenValue1AndInvariantViolated = new BinaryFormula(new SimpleStateInRangeFormula(10 + 128), BinaryOperator.And, new SimpleLocalVarIsTrue(0)); Formula formulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated = new BinaryFormula(new SimpleStateInRangeFormula(10), BinaryOperator.And, new UnaryFormula(new SimpleLocalVarIsTrue(0), UnaryOperator.Not)); Formula formulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated = new BinaryFormula(new SimpleStateInRangeFormula(10 + 128), BinaryOperator.And, new UnaryFormula(new SimpleLocalVarIsTrue(0), UnaryOperator.Not)); var finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantViolated = new UnaryFormula(formulaProbabilityOfStep11FrozenValue0AndInvariantViolated, UnaryOperator.Finally); var finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantViolated = new UnaryFormula(formulaProbabilityOfStep11FrozenValue1AndInvariantViolated, UnaryOperator.Finally); var finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated = new UnaryFormula(formulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated, UnaryOperator.Finally); var finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated = new UnaryFormula(formulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated, UnaryOperator.Finally); var markovChainGenerator = new SimpleDtmcFromExecutableModelGenerator(m); markovChainGenerator.Configuration.ModelCapacity = ModelCapacityByMemorySize.Small; markovChainGenerator.AddFormulaToCheck(finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantViolated); markovChainGenerator.AddFormulaToCheck(finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantViolated); markovChainGenerator.AddFormulaToCheck(finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated); markovChainGenerator.AddFormulaToCheck(finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated); var dtmc = markovChainGenerator.GenerateMarkovChain(); dtmc.ExportToGv(Output.TextWriterAdapter()); var typeOfModelChecker = typeof(BuiltinDtmcModelChecker); var modelChecker = (DtmcModelChecker)Activator.CreateInstance(typeOfModelChecker, dtmc, Output.TextWriterAdapter()); using (modelChecker) { probabilityOfStep11FrozenValue0AndInvariantViolated = modelChecker.CalculateProbability(finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantViolated); probabilityOfStep11FrozenValue1AndInvariantViolated = modelChecker.CalculateProbability(finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantViolated); probabilityOfStep11FrozenValue0AndInvariantNotViolated = modelChecker.CalculateProbability(finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated); probabilityOfStep11FrozenValue1AndInvariantNotViolated = modelChecker.CalculateProbability(finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated); } var probabilitiesSummedUp = probabilityOfStep11FrozenValue0AndInvariantViolated + probabilityOfStep11FrozenValue1AndInvariantViolated + probabilityOfStep11FrozenValue0AndInvariantNotViolated + probabilityOfStep11FrozenValue1AndInvariantNotViolated; probabilitiesSummedUp.Is(1.0, 0.000001).ShouldBe(true); }
protected override void Check() { var intValue = 7; { var actual = ((Formula)false).EquivalentTo(intValue < 7); var expected = new BinaryFormula( new ExecutableStateFormula(() => false), BinaryOperator.Equivalence, new ExecutableStateFormula(() => intValue < 7)); Check(actual, expected); } { var actual = ((Formula)false).EquivalentTo(F(intValue < 7)); var expected = new BinaryFormula( new ExecutableStateFormula(() => false), BinaryOperator.Equivalence, new UnaryFormula(new ExecutableStateFormula(() => intValue < 7), UnaryOperator.Finally)); Check(actual, expected); } { Formula actual = false.EquivalentTo(intValue < 7); var expected = new ExecutableStateFormula(() => (false && intValue < 7) || (true && intValue >= 7)); Check(actual, expected); } { var actual = false.EquivalentTo(F(intValue < 7)); var expected = new BinaryFormula( new ExecutableStateFormula(() => false), BinaryOperator.Equivalence, new UnaryFormula(new ExecutableStateFormula(() => intValue < 7), UnaryOperator.Finally)); Check(actual, expected); } true.EquivalentTo(true).ShouldBe(true); false.EquivalentTo(true).ShouldBe(false); true.EquivalentTo(false).ShouldBe(false); false.EquivalentTo(false).ShouldBe(true); }
protected override void Check() { var intValue = 7; { var actual = ((Formula)false).Implies(intValue < 7); var expected = new BinaryFormula( new ExecutableStateFormula(() => false), BinaryOperator.Implication, new ExecutableStateFormula(() => intValue < 7)); Check(actual, expected); } { var actual = ((Formula)false).Implies(F(intValue < 7)); var expected = new BinaryFormula( new ExecutableStateFormula(() => false), BinaryOperator.Implication, new UnaryFormula(new ExecutableStateFormula(() => intValue < 7), UnaryOperator.Finally)); Check(actual, expected); } { var actual = false.Implies(intValue < 7); var expected = new ExecutableStateFormula(() => !false || intValue < 7); Check(actual, expected); } { var actual = false.Implies(F(intValue < 7)); var expected = new BinaryFormula( new ExecutableStateFormula(() => false), BinaryOperator.Implication, new UnaryFormula(new ExecutableStateFormula(() => intValue < 7), UnaryOperator.Finally)); Check(actual, expected); } true.Implies(true).ShouldBe(true); false.Implies(true).ShouldBe(true); true.Implies(false).ShouldBe(false); false.Implies(false).ShouldBe(true); }
private void Check(AnalysisConfiguration configuration) { var m = new Model(); Probability probabilityOfStep11FrozenValue0AndInvariantViolated; Probability probabilityOfStep11FrozenValue1AndInvariantViolated; Probability probabilityOfStep11FrozenValue0AndInvariantNotViolated; Probability probabilityOfStep11FrozenValue1AndInvariantNotViolated; Formula formulaProbabilityOfStep11FrozenValue0AndInvariantViolated = new BinaryFormula(new SimpleStateInRangeFormula(10), BinaryOperator.And, new SimpleLocalVarIsTrue(0)); Formula formulaProbabilityOfStep11FrozenValue1AndInvariantViolated = new BinaryFormula(new SimpleStateInRangeFormula(10 + 128), BinaryOperator.And, new SimpleLocalVarIsTrue(0)); Formula formulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated = new BinaryFormula(new SimpleStateInRangeFormula(10), BinaryOperator.And, new UnaryFormula(new SimpleLocalVarIsTrue(0), UnaryOperator.Not)); Formula formulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated = new BinaryFormula(new SimpleStateInRangeFormula(10 + 128), BinaryOperator.And, new UnaryFormula(new SimpleLocalVarIsTrue(0), UnaryOperator.Not)); var finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantViolated = new UnaryFormula(formulaProbabilityOfStep11FrozenValue0AndInvariantViolated, UnaryOperator.Finally); var finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantViolated = new UnaryFormula(formulaProbabilityOfStep11FrozenValue1AndInvariantViolated, UnaryOperator.Finally); var finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated = new UnaryFormula(formulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated, UnaryOperator.Finally); var finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated = new UnaryFormula(formulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated, UnaryOperator.Finally); var markovChainGenerator = new SimpleMarkovChainFromExecutableModelGenerator(m); markovChainGenerator.Configuration = configuration; markovChainGenerator.AddFormulaToCheck(finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantViolated); markovChainGenerator.AddFormulaToCheck(finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantViolated); markovChainGenerator.AddFormulaToCheck(finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated); markovChainGenerator.AddFormulaToCheck(finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated); var ltmc = markovChainGenerator.GenerateLabeledMarkovChain(); var modelChecker = new ConfigurationDependentLtmcModelChecker(configuration, ltmc, Output.TextWriterAdapter()); { probabilityOfStep11FrozenValue0AndInvariantViolated = modelChecker.CalculateProbability(finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantViolated); probabilityOfStep11FrozenValue1AndInvariantViolated = modelChecker.CalculateProbability(finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantViolated); probabilityOfStep11FrozenValue0AndInvariantNotViolated = modelChecker.CalculateProbability(finallyFormulaProbabilityOfStep11FrozenValue0AndInvariantNotViolated); probabilityOfStep11FrozenValue1AndInvariantNotViolated = modelChecker.CalculateProbability(finallyFormulaProbabilityOfStep11FrozenValue1AndInvariantNotViolated); } var probabilitiesSummedUp = probabilityOfStep11FrozenValue0AndInvariantViolated + probabilityOfStep11FrozenValue1AndInvariantViolated + probabilityOfStep11FrozenValue0AndInvariantNotViolated + probabilityOfStep11FrozenValue1AndInvariantNotViolated; probabilitiesSummedUp.Is(1.0, 0.000001).ShouldBe(true); }
public void Check() { Probability probabilityOfFinal1; Probability probabilityOfFinal2; Probability probabilityOfFinal3; Probability probabilityOfFinal4; var m = new Model(); var formula1 = new BinaryFormula(Model.FIs2, BinaryOperator.Or, Model.FIs3); var formula2 = Model.FIs1; var formula3 = new BinaryFormula(new BinaryFormula(Model.GIs0, BinaryOperator.Or, Model.GIs7), BinaryOperator.Or, Model.GIs8); var formula4 = Model.GIs7; var final1 = new UnaryFormula(formula1, UnaryOperator.Finally); var final2 = new UnaryFormula(formula2, UnaryOperator.Finally); var final3 = new UnaryFormula(formula3, UnaryOperator.Finally); var final4 = new UnaryFormula(formula4, UnaryOperator.Finally); var markovChainGenerator = new SimpleDtmcFromExecutableModelGenerator(m); markovChainGenerator.Configuration.ModelCapacity = ModelCapacityByMemorySize.Small; markovChainGenerator.AddFormulaToCheck(final1); markovChainGenerator.AddFormulaToCheck(final2); markovChainGenerator.AddFormulaToCheck(final3); markovChainGenerator.AddFormulaToCheck(final4); var dtmc = markovChainGenerator.GenerateMarkovChain(); var typeOfModelChecker = typeof(BuiltinDtmcModelChecker); var modelChecker = (DtmcModelChecker)Activator.CreateInstance(typeOfModelChecker, dtmc, Output.TextWriterAdapter()); using (modelChecker) { probabilityOfFinal1 = modelChecker.CalculateProbability(final1); probabilityOfFinal2 = modelChecker.CalculateProbability(final2); probabilityOfFinal3 = modelChecker.CalculateProbability(final3); probabilityOfFinal4 = modelChecker.CalculateProbability(final4); } probabilityOfFinal1.Is(0.0, tolerance: 0.0001).ShouldBe(true); probabilityOfFinal2.Is(1.0, tolerance: 0.0001).ShouldBe(true); probabilityOfFinal3.Is(1.0, tolerance: 0.0001).ShouldBe(true); probabilityOfFinal4.Is(0.8333333333, tolerance: 0.0001).ShouldBe(true); }
public Example4() { Ltmc = Create(); ExampleFormula1 = new BinaryFormula(Label1Formula, BinaryOperator.And, Label2Formula); ExampleFormula2 = new BinaryFormula(Label1Formula, BinaryOperator.Or, Label2Formula); //StatesSatisfyDirectlyLabel1Formula = new Dictionary<int, bool>() { { 0, true }, { 2, true } }; //StatesSatisfyDirectlyLabel2Formula = new Dictionary<int, bool>() { { 3, true } }; //StatesSatisfyDirectlyExampleFormula1 = new Dictionary<int, bool>() { }; //StatesSatisfyDirectlyExampleFormula2 = new Dictionary<int, bool>() { { 0, true }, { 2, true }, { 3, true } }; // //AncestorsOfStatesWithLabel1 = new Dictionary<int, bool>() { { 0, true }, { 2, true } }; //AncestorsOfStatesWithLabel2 = new Dictionary<int, bool>() { { 0, true } , { 2, true } , { 3, true }, { 4, true } }; // //ProbabilityFinallyLabel1 = 1.0; //ProbabilityFinally10Label1 = 1.0; //ProbabilityLabel1UntilLabel2 = 0.2; }
private void Check(AnalysisConfiguration configuration) { Probability probabilityOfFinal1; Probability probabilityOfFinal2; Probability probabilityOfFinal3; Probability probabilityOfFinal4; var m = new Model(); var formula1 = new BinaryFormula(Model.FIs2, BinaryOperator.Or, Model.FIs3); var formula2 = Model.FIs1; var formula3 = new BinaryFormula(new BinaryFormula(Model.GIs0, BinaryOperator.Or, Model.GIs7), BinaryOperator.Or, Model.GIs8); var formula4 = Model.GIs7; var final1 = new UnaryFormula(formula1, UnaryOperator.Finally); var final2 = new UnaryFormula(formula2, UnaryOperator.Finally); var final3 = new UnaryFormula(formula3, UnaryOperator.Finally); var final4 = new UnaryFormula(formula4, UnaryOperator.Finally); var markovChainGenerator = new SimpleMarkovChainFromExecutableModelGenerator(m); markovChainGenerator.Configuration = configuration; markovChainGenerator.AddFormulaToCheck(final1); markovChainGenerator.AddFormulaToCheck(final2); markovChainGenerator.AddFormulaToCheck(final3); markovChainGenerator.AddFormulaToCheck(final4); var ltmc = markovChainGenerator.GenerateLabeledMarkovChain(); var modelChecker = new ConfigurationDependentLtmcModelChecker(configuration, ltmc, Output.TextWriterAdapter()); using (modelChecker) { probabilityOfFinal1 = modelChecker.CalculateProbability(final1); probabilityOfFinal2 = modelChecker.CalculateProbability(final2); probabilityOfFinal3 = modelChecker.CalculateProbability(final3); probabilityOfFinal4 = modelChecker.CalculateProbability(final4); } probabilityOfFinal1.Is(0.0, tolerance: 0.0001).ShouldBe(true); probabilityOfFinal2.Is(1.0, tolerance: 0.0001).ShouldBe(true); probabilityOfFinal3.Is(1.0, tolerance: 0.0001).ShouldBe(true); probabilityOfFinal4.Is(0.8333333333, tolerance: 0.0001).ShouldBe(true); }
public Sequent Apply(Sequent sequent) { BinaryFormula implicationFormula = (BinaryFormula)sequent.RightHandSide.Formulas.Where( x => x is BinaryFormula formula && formula.Connective == BinaryConnective.Implication ).FirstOrDefault(); if (implicationFormula == null) { return(null); } sequent.RightHandSide.Formulas.Remove(implicationFormula); var formula = implicationFormula.Clone() as BinaryFormula; sequent.RightHandSide.Formulas.Add(formula.RHSFormula); sequent.Justification = "R3 (" + sequent.Name + ")"; return(sequent); }
protected override void Check() { var x = 0; var f = ((Formula)(x == 2) && x == 3) || x == 4; var expected = new BinaryFormula( new BinaryFormula(new ExecutableStateFormula(() => x == 2), BinaryOperator.And, new ExecutableStateFormula(() => x == 3)), BinaryOperator.Or, new ExecutableStateFormula(() => x == 4)); Check(f, expected); x = 2; Check(f, expected); x = 3; Check(f, expected); x = 4; Check(f, expected); }
private void Check(AnalysisConfiguration configuration) { var m = new Model(); Probability probabilityOfFalse; Formula falseFormula = new BinaryFormula(new SimpleStateInRangeFormula(1), BinaryOperator.And, new SimpleStateInRangeFormula(2)); var finallyFalseFormula = new UnaryFormula(falseFormula, UnaryOperator.Finally); var markovChainGenerator = new SimpleMarkovChainFromExecutableModelGenerator(m); markovChainGenerator.Configuration = configuration; markovChainGenerator.AddFormulaToCheck(finallyFalseFormula); var ltmc = markovChainGenerator.GenerateLabeledMarkovChain(); var modelChecker = new ConfigurationDependentLtmcModelChecker(configuration, ltmc, Output.TextWriterAdapter()); using (modelChecker) { probabilityOfFalse = modelChecker.CalculateProbability(finallyFalseFormula); } probabilityOfFalse.Is(0.0, 0.001).ShouldBe(true); }
public void CalculateProbability() { var model = new ExampleModelBase(); var tc = SafetySharpModelChecker.TraversalConfiguration; tc.WriteGraphvizModels = true; tc.AllowFaultsOnInitialTransitions = false; tc.UseAtomarPropositionsAsStateLabels = true; tc.UseCompactStateStorage = true; SafetySharpModelChecker.TraversalConfiguration = tc; var result = SafetySharpModelChecker.CalculateProbabilityToReachStateBounded(model, model.ModelComponent.Value == 3, 50); Console.Write($"Probability of hazard: {result}"); var is3Formula = new ExecutableStateFormula(() => model.ModelComponent.Value == 3); var loopRequestBug = new ExecutableStateFormula(() => model.ModelComponent.LoopRequestBug); var formula2OnceLoopRequestBug = new BinaryFormula(is3Formula, BinaryOperator.And, new UnaryFormula(loopRequestBug, UnaryOperator.Once)); var result2 = SafetySharpModelChecker.CalculateProbabilityToReachStateBounded(model, formula2OnceLoopRequestBug, 50); Console.WriteLine($"Probability of {formula2OnceLoopRequestBug}: {result2}"); }
protected override void Check() { var intValue = 7; var boolValue = false; var actual = AF(intValue > 7 && boolValue && X(!boolValue) | intValue < 4) && AF(boolValue); var expected = new BinaryFormula( new UnaryFormula( new UnaryFormula( new BinaryFormula( new ExecutableStateFormula(() => intValue > 7 && boolValue), BinaryOperator.And, new BinaryFormula( new UnaryFormula(new ExecutableStateFormula(() => !boolValue), UnaryOperator.Next), BinaryOperator.Or, new ExecutableStateFormula(() => intValue < 4))), UnaryOperator.Finally), UnaryOperator.All), BinaryOperator.And, new UnaryFormula(new UnaryFormula(new ExecutableStateFormula(() => boolValue), UnaryOperator.Finally), UnaryOperator.All)); Check(actual, expected); }
protected override void Check() { var intValue = 7; { var actual = ((Formula)false).EquivalentTo(intValue < 7); var expected = new BinaryFormula( new StateFormula(() => false), BinaryOperator.Equivalence, new StateFormula(() => intValue < 7)); Check(actual, expected); } { var actual = ((Formula)false).EquivalentTo(F(intValue < 7)); var expected = new BinaryFormula( new StateFormula(() => false), BinaryOperator.Equivalence, new UnaryFormula(new StateFormula(() => intValue < 7), UnaryOperator.Finally)); Check(actual, expected); } }
public Example4() { MarkovChain = Create(); ExampleFormula1 = new BinaryFormula(Label1Formula, BinaryOperator.And, Label2Formula); ExampleFormula2 = new BinaryFormula(Label1Formula, BinaryOperator.Or, Label2Formula); StatesSatisfyDirectlyLabel1Formula = new Dictionary <long, bool>() { { 2, true } }; StatesSatisfyDirectlyLabel2Formula = new Dictionary <long, bool>() { { 4, true } }; StatesSatisfyDirectlyExampleFormula1 = new Dictionary <long, bool>() { }; StatesSatisfyDirectlyExampleFormula2 = new Dictionary <long, bool>() { { 2, true }, { 4, true } }; AncestorsOfStatesWithLabel1 = new Dictionary <long, bool>() { { 0, true }, { 2, true } }; AncestorsOfStatesWithLabel2 = new Dictionary <long, bool>() { { 0, true }, { 1, true }, { 2, true }, { 4, true } }; ProbabilityFinallyLabel1 = 0.3; ProbabilityFinally10Label1 = 0.3; ProbabilityLabel1UntilLabel2 = 0.0; }
/// <summary> /// Visits the <paramref name="formula"/>. This expects a CTL State Formula. /// </summary> public override void VisitBinaryFormula(BinaryFormula formula) { // We assume that the topMost operator is either A or G _builder.Append("("); Visit(formula.LeftOperand); switch (formula.Operator) { case BinaryOperator.And: _builder.Append(" && "); break; case BinaryOperator.Or: _builder.Append(" || "); break; case BinaryOperator.Implication: _builder.Append(" -> "); break; case BinaryOperator.Equivalence: _builder.Append(" <-> "); break; case BinaryOperator.Until: throw new InvalidOperationException($"Until only allowed in Path Formulas."); break; default: Assert.NotReached($"Unknown or unsupported binary operator '{formula.Operator}'."); break; } Visit(formula.RightOperand); _builder.Append(")"); }
/// <summary> /// Visits an element of type <see cref="BinaryFormula" />. /// </summary> /// <param name="formula">The <see cref="BinaryFormula" /> instance that should be visited.</param> protected internal virtual void VisitBinaryFormula(BinaryFormula formula) { DefaultVisit(formula); }
/// <summary> /// Visits the <paramref name="formula." /> /// </summary> public abstract void VisitBinaryFormula(BinaryFormula formula);
/// <summary> /// Visits an element of type <see cref="BinaryFormula" />. /// </summary> /// <param name="formula">The <see cref="BinaryFormula" /> instance that should be visited.</param> protected internal override void VisitBinaryFormula(BinaryFormula formula) { Visit(formula.LeftOperand); Visit(formula.RightOperand); }