### Get Kripke states and transitions Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_models.rst Retrieves the states and transitions of the Kripke structure K using K.states() and K.transitions(). The states are returned as a list and transitions as a list of tuples. ```Python >>> K.states() [0, 1, 2, 3] >>> K.transitions() [(0, 1), (0, 2), (1, 0), (2, 2), (3, 2)] ``` -------------------------------- ### Model check a CTL* formula on a Kripke structure Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Create a Kripke structure and run the CTL* model checking algorithm on a formula (psi). The example shows the result set of states satisfying the formula, and the result when fair model checking is applied with a fairness constraint F=[6]. ```Python >>> from pyModelChecking import Kripke >>> K = Kripke(R=[(0, 1), (0, 2), (1, 4), (4, 1), (4, 2), (2, 0), ... (3, 2), (3, 0), (3, 3), (6, 3), (2, 5), (5, 6)], ... L={0: set(), 1: set(['Start', 'Error']), 2: set(['Close']), ... 3: set(['Close', 'Heat']), ... 4: set(['Start', 'Close', 'Error']), ... 5: set(['Start', 'Close']), ... 6: set(['Start', 'Close', 'Heat'])}) >>> modelcheck(K, psi) set([0, 1, 2, 3, 4, 5, 6]) >>> modelcheck(K, psi, F=[6]) set([]) ``` -------------------------------- ### Create a Kripke structure Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Creates a Kripke structure with given transition relation R and labeling function L. The result is stored in variable K for use in subsequent model checking examples. ```python >>> K=Kripke(R=[(0,0),(0,1),(1,2),(2,2),(3,3)], ... L={0: set(['p']), 1:set(['p','q']),3:set(['p'])}) ``` -------------------------------- ### Get Kripke labels Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_models.rst Retrieves the labels of the Kripke structure K. K.labels() returns the set of all labels used, while K.labels(3) returns the labels of state 3. ```Python >>> K.labels() set(['q', 'p']) >>> K.labels(3) set(['q']) ``` -------------------------------- ### Define a CTL* formula with A, G, Imply, And, Not, Or, F Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Construct a CTL* formula using path quantifiers and temporal operators. The example shows a formula with nested operators and atomic propositions. The output displays the formula in a readable syntax. ```Python >>> phi = A(G( ... Imply(And(Not('Close'), ... 'Start'), ... A(Or(G(Not('Heat')), ... F(Not('Error'))))) ... )) >>> phi A(G(((not Close and Start) --> A((G(not Heat) or F(not Error)))))) ``` -------------------------------- ### Get successors of a Kripke state Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_models.rst Gets the successors of state 0 in the Kripke structure K using K.next(0), returning a set of states {1, 2}. ```Python >>> K.next(0) set([1, 2]) ``` -------------------------------- ### Create and print a Kripke structure Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_models.rst Creates a Kripke structure with states [0,1,3], transitions R, and labeling L, then prints it. The printed representation shows that states are normalized to [0,1,2,3], initial states S0 are empty, and labels are stored as sets. ```Python >>> from pyModelChecking import * >>> K = Kripke(S=[0, 1, 3], ... R=[(0, 2), (2, 2), (0, 1), (1, 0), (3, 2)], ... L={1: ['p', 'q'], 2: ['p', 'q'], 3: ['q']}) >>> print(K) (S=[0, 1, 2, 3],S0=set([]),R=[(0, 1), (0, 2), (1, 0), (2, 2), (3, 2)],L={0: set([]), 1: set(['q', 'p']), 2: set(['q', 'p']), 3: set(['q'])}) ``` -------------------------------- ### Build and model check CTL* formulas with CTLS Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Imports the CTLS module, builds several CTL* formulas (eta, rho, gamma) and prints them with their classes, then model checks them against K. eta is satisfied by all states, psi by state 3, gamma by none. ```python >>> from pyModelChecking.CTLS import * ``` ```python >>> eta=A(F(E(U(True,Imply('p',Not('q')))))) >>> print(eta,eta.__class__) (A(F(E((True U (p --> not q))))), ) >>> rho=A(G(And(X('p'),'p'))) >>> print(rho,rho.__class__) (A(G((X(p) and p))), ) >>> gamma=And(phi, psi) >>> print(gamma,gamma.__class__) (A(True U (q or not (EX p))) and A(G(F(p)))), ) ``` ```python >>> modelcheck(K,eta) set([0, 1, 2, 3]) >>> modelcheck(K, psi) set([3]) >>> modelcheck(K, gamma) set([]) ``` -------------------------------- ### Use operator shortcuts &, |, ~ Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Shows how to use the &, |, and ~ operators as shortcuts for And, Or, and Not constructors. At least one operand must be a Formula object; otherwise a TypeError is raised. ```Python >>> AtomicProposition('p') & True (p and true) >>> True & AtomicProposition('p') (true and p) >>> f = 'p' & Bool(True) >>> f (p and true) >>> True & 'p' & Bool(True) Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for &: 'bool' and 'str' >>> 'p' & Bool(True) & 'p' ((p and true) and p) >>> ~('p' & Bool(True)) | And(~f,'b') (not (p and true) or (not (p and true) and b)) ``` -------------------------------- ### Parse propositional formulas with Parser Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Shows how to create a Parser object and use it to parse strings into Formula objects. The parser accepts the grammar described in the Parser.grammar attribute. ```Python >>> p = Parser() >>> p('p and true') (p and true) >>> p('(~p and q) --> ((q | p))') ((not p and q) --> (q or p)) ``` -------------------------------- ### Use logic operator classes And, Imply, Or, Not Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Demonstrates constructing formulas with And, Imply, Or, and Not classes. Shows that And accepts multiple arguments and that Imply requires exactly two arguments, raising a TypeError otherwise. ```Python >>> And('p', true) (p and true) >>> And('p', true, 'p') (p and true and p) >>> f = Imply('q','p') >>> And('p', f, Imply(Not(f), Or('q','s', f))) (p and (q --> p) and (not (q --> p) --> (q or s or (q --> p)))) >>> Imply('p', 'q', 'p') Traceback (most recent call last): File "", line 1, in TypeError: __init__() takes exactly 3 arguments (4 given) ``` -------------------------------- ### Create and inspect a DiGraph Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_models.rst Creates a DiGraph with vertices 'a', 3 and edges ('a','a'), ('a','b'), then prints it, retrieves nodes and edges, adds edge ('c','b'), and re-prints nodes and edges. Shows that nodes are stored as a list and edges as a list of tuples. ```Python >>> from pyModelChecking import * >>> G = DiGraph(V=['a',3], ... E=[('a','a'), ('a','b')]) >>> print(G) (V=['a', 3, 'b'], E=[('a', 'a'), ('a', 'b')]) >>> G.nodes() ['a', 3, 'b'] >>> G.edges() [('a','a'), ('a','b')] >>> G.add_edge('c','b') >>> G.nodes() ['a', 'c', 3, 'b'] >>> G.edges() [('a', 'a'), ('a', 'b'), ('c', 'b')] ``` -------------------------------- ### Build and model check an LTL formula with LTL module Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Imports the LTL module severeal? imports all LTL functions, builds the LTL formula A(F(G('p'))), prints it (note: output shows A(G(F(p)) though it is A(F(G(p)))), and model checks it against K, returning state set {3}. ```python >>> from pyModelChecking.LTL import * ``` ```python >>> psi=A(F(G('p'))) >>> print(psi) A(G(F(p)) ``` ```python >>> modelcheck(K,psi) set([3]) ``` -------------------------------- ### Build and print a CTL formula with AU, Or, Not, and EX Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Builds CTL formula phi using CTL operators and prints it, outputting A(True U (q or not (EX p))). ```python >>> phi=AU(True,Or('q',Not(EX('p')))) >>> print(phi) A(True U (q or not (EX p))) ``` -------------------------------- ### Create atomic propositions and Boolean values Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Shows how to create AtomicProposition and Bool objects using the pyModelChecking.PL sub-module. The output displays the string representation of the created formulas. ```Python >>> from pyModelChecking.PL import * >>> AtomicProposition('p') p >>> Bool(True) true ``` -------------------------------- ### Use LNot to minimize negation Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Demonstrates the LNot function which returns a logically equivalent formula with minimized outermost negations. Shows that LNot(LNot(f)) is not further simplified. ```Python >>> f = Not(Not(Not(And('p',Not('q'))))) >>> f not not not (p and not q) >>> LNot(f) (p and not q) >>> LNot(Not(f)) not (p and not q) >>> LNot(LNot(f)) not (p and not q) ``` -------------------------------- ### Import CTLS sub-module Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Import the pyModelChecking.CTLS sub-module to access CTL* formula classes and functions. ```Python >>> from pyModelChecking.CTLS import * ``` -------------------------------- ### Parse and combine CTL* formulas with Parser and | Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Use the CTLS Parser to parse a formula string and combine it with another formula using the | operator. The output shows the resulting formula. ```Python >>> p=Parser() >>> p('G(not Heat)') | p('A(F(not Error))') (G(not Heat) or A(F(not Error))) ``` -------------------------------- ### Model check a CTL formula with modelcheck Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Applies the CTL modelcheck function to Kripke structure K and the CTL formula phi, returning the set of states satisfying phi (states 1 and 2). ```python >>> modelcheck(K,phi) set([1, 2]) ``` -------------------------------- ### Compute reversed graph, subgraph, and reachable set Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_models.rst Demonstrates DiGraph methods: get_reversed_graph() returns a new DiGraph with reversed edges, get_subgraph(['a','b',3]) returns a subgraph with specified nodes, and get_reachable_set_from(['a',3]) returns a set of reachable nodes. Calling get_reachable_set_from with a non-node raises RuntimeError: src = 'd' is not a node of this DiGraph. ```Python >>> print(G.get_reversed_graph()) (V=['a', 'c', 3, 'b'], E=[('a', 'a'), ('b', 'a'), ('b', 'c')]) >>> print(G.get_subgraph(['a','b',3])) (V=['a', 3, 'b'], E=[('a', 'a'), ('a', 'b')]) >>> print(G.get_reachable_set_from(['a', 3])) set(['a', 3, 'b']) >>> print(G.get_reachable_set_from(['d'])) Traceback (most recent call last): File "", line 1, in File "pyModelChecking/graph.py", line 203, in get_reachable_set_from for d in self.next(s): File "pyModelChecking/graph.py", line 120, in next 'of this DiGraph') RuntimeError: src = 'd' is not a node of this DiGraph ``` -------------------------------- ### Model check with an LTL formula string Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Model checking accepts a string representing an LTL formula, here 'A G F p', returning the set of states satisfying it (state 3). ```python >>> modelcheck(K,'A G F p') set([3]) ``` -------------------------------- ### Use CTL and LTL modelcheck on CTL* formulas Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Shows that when a CTL* formula is also a CTL formula, CTL model checking can be applied; eta is checked successfully with CTL.modelcheck, while rho is not a CTL formula and raises RuntimeError. Then LTL.modelcheck can be used for rho, returning state 3. ```python >>> import pyModelChecking.CTL as CTL >>> CTL.modelcheck(K,eta) set([0, 1, 2, 3]) ``` ```python >>> CTL.modelcheck(K,rho) Traceback (most recent call last): File "", line 1, in File "pyModelChecking/CTL/model_checking.py", line 183, in modelcheck raise RuntimeError('%s is not a CTL formula' % (formula)) RuntimeError: A(G((X(p) and p))) is not a CTL formula ``` ```python >>> import pyModelChecking.LTL as LTL >>> LTL.modelcheck(K,rho) set([3]) ``` -------------------------------- ### Parse a CTL formula string with Parser Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Parses a string representation of a CTL formula using the Parser class. The parsed formula psi prints as A(True U (q or not EX p)) and has class pyModelChecking.CTL.language.A. ```python >>> parser = Parser() >>> psi = parser("A(true U (q or not E X p))") >>> print(psi) A(True U (q or not EX p)) >>> print(psi.__class__) ``` -------------------------------- ### Print Parser grammar Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Displays the grammar definition used by the Parser class, as stored in the class attribute Parser.grammar. This grammar defines the syntax for propositional formulas. ```Python >>> print(p.grammar) s_formula: "true" -> true | "false" -> false | a_prop | "(" s_formula ")" u_formula: ("not"|"~") u_formula -> not_formula | "(" b_formula ")" | s_formula b_formula: u_formula | u_formula ( ("or"|"|") u_formula )+ -> or_formula | u_formula ( ("and"|"&") u_formula )+ -> and_formula | u_formula ("-->") u_formula -> imply_formula a_prop: /[a-zA-Z_][a-zA-Z_0-9]*/ -> string | ESCAPED_STRING -> e_string formula: b_formula %import common.ESCAPED_STRING %import common.WS %import WS ``` -------------------------------- ### Non-CTL formula raises TypeError Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/README.md Attempting to build the CTL formula A(F(G('p'))) throws a TypeError because F(G('p')) is a CTL path formula, not a state formula. ```python >>> psi=A(F(G('p'))) Traceback (most recent call last): File "", line 1, in File "pyModelChecking/CTL/language.py", line 42, in __init__ self.wrap_subformulas(phi,StateFormula) File "pyModelChecking/CTLS/language.py", line 59, in wrap_subformulas phi.__desc__,phi)) TypeError: expected a CTL state formula, got the CTL path formula G p ``` -------------------------------- ### Model check a string formula with and without explicit parser Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_logics.rst Model check a string representation of a CTL* formula. The first call lets the function create a parser internally; the second passes an explicit parser object. Both return the same set of states. ```Python >>> modelcheck(K, psi_str) set([0, 1, 2, 3, 4, 5, 6]) >>> modelcheck(K, psi_str, parser=parser) set([0, 1, 2, 3, 4, 5, 6]) ``` -------------------------------- ### Compute strongly connected components with compute_SCCs Source: https://github.com/albertocasagrande/pymodelchecking/blob/master/doc/source/using_models.rst Adds edge ('b','a') to the graph G and then computes its strongly connected components using compute_SCCs(G), returning a list of lists. The output shows SCCs as [['a', 'b'], ['c'], [3]]. ```Python >>> G.add_edge('b','a') >>> print(list(compute_SCCs(G))) [['a', 'b'], ['c'], [3]] ```