예제 #1
0
    def test_ag(self):
        fsm = self.cardgame()

        # Compute AG step <= 4
        expr = Nu(Variable("Z"), And(Atom("step <= 3"), Box(Variable("Z"))))

        for state in fsm.pick_all_states(
                expr.eval(fsm) & fsm.reachable_states):
            expl = expr.explain(fsm, state)
            self.assertEqual(expl.initial.state, state)

            dot = expl.dot()
            self.assertIsNotNone(dot)

            # Inspect all nodes
            found = set()
            pending = {expl.initial}
            while len(pending) > 0:
                e = pending.pop()
                found.add(e)

                self.assertEqual(e.fsm, fsm)
                self.assertTrue(e.state <= expr.eval(fsm))
                self.assertEqual(e.context, {})
                self.assertTrue(
                    len(list(edge for edge in expl.graph.edges
                             if edge[0] == e)) >= 1 or
                    len(list(edge
                             for edge in expl.graph.edges if edge[0] == e)) <=
                    0 and e.formula == Atom("step <= 3"))

                pending |= {
                    edge[2]
                    for edge in expl.graph.edges if edge[0] == e
                } - found
예제 #2
0
    def test_af(self):
        fsm = self.cardgame()

        # Compute AF step = 2
        expr = Mu(Variable("Z"), Or(Atom("step = 2"), Box(Variable("Z"))))

        for state in fsm.pick_all_states(
                expr.eval(fsm) & fsm.reachable_states):
            expl = expr.explain(fsm, state)
            self.assertEqual(expl.initial.state, state)

            dot = expl.dot()
            self.assertIsNotNone(dot)

            # Follow one path of expl and check that step = 2 is reached
            e = expl.initial
            while e is not None:
                self.assertEqual(e.fsm, fsm)
                self.assertTrue(e.state <= expr.eval(fsm))
                self.assertEqual(e.context, {})

                self.assertTrue(
                    len(list(edge
                             for edge in expl.graph.edges if edge[0] == e)) > 0
                    or
                    len(list(edge
                             for edge in expl.graph.edges if edge[0] == e)) <=
                    0 and e.formula == Atom("step = 2"))

                if len(list(edge
                            for edge in expl.graph.edges if edge[0] == e)) > 0:
                    e = next(edge for edge in expl.graph.edges
                             if edge[0] == e)[2]
                else:
                    e = None
예제 #3
0
    def test_ef(self):
        fsm = self.cardgame()

        # Compute EF win
        expr = Mu(Variable("Z"), Or(Atom("win"), Diamond(Variable("Z"))))

        for state in fsm.pick_all_states(
                expr.eval(fsm) & fsm.reachable_states):
            expl = expr.explain(fsm, state)
            self.assertEqual(expl.initial.state, state)

            dot = expl.dot()
            self.assertIsNotNone(dot)

            e = expl.initial
            while e is not None:
                self.assertEqual(e.fsm, fsm)
                self.assertTrue(e.state <= expr.eval(fsm))
                self.assertEqual(e.context, {})

                self.assertTrue(
                    len(list(edge for edge in expl.graph.edges
                             if edge[0] == e)) == 1 or
                    len(list(edge
                             for edge in expl.graph.edges if edge[0] == e)) <=
                    0 and e.formula == Atom("win"))

                if len(list(edge
                            for edge in expl.graph.edges if edge[0] == e)) > 0:
                    e = next(edge for edge in expl.graph.edges
                             if edge[0] == e)[2]
                else:
                    e = None
예제 #4
0
    def test_eval_fp(self):
        fsm = self.cardgame()

        s0 = eval_simple_expression(fsm, "step = 0")
        s1 = eval_simple_expression(fsm, "step = 1")
        s2 = eval_simple_expression(fsm, "step = 2")
        pa = eval_simple_expression(fsm, "pcard = Ac")
        pk = eval_simple_expression(fsm, "pcard = K")
        pq = eval_simple_expression(fsm, "pcard = Q")
        da = eval_simple_expression(fsm, "dcard = Ac")
        dk = eval_simple_expression(fsm, "dcard = K")
        dq = eval_simple_expression(fsm, "dcard = Q")
        dda = eval_simple_expression(fsm, "ddcard = Ac")
        ddk = eval_simple_expression(fsm, "ddcard = K")
        ddq = eval_simple_expression(fsm, "ddcard = Q")
        win = eval_simple_expression(fsm, "win")
        lose = eval_simple_expression(fsm, "lose")
        true = eval_simple_expression(fsm, "TRUE")
        false = eval_simple_expression(fsm, "FALSE")

        # mu Z. win | pre(Z)
        R = Mu(Variable("Z"), Or(Atom("win"), Diamond(Variable("Z"))))
        self.assertTrue(s0 <= R.eval(fsm))

        # nu Z. ~win & pre(Z)
        NW = Nu(Variable("Z"), And(Not(Atom("win")), Diamond(Variable("Z"))))
        self.assertTrue(NW.eval(fsm) <= ~win)
예제 #5
0
 def fair(fsm):
     body = EX(Reach(And(Variable("Z"), Variable("fci"))))
     return Nu(
         Variable("Z"),
         BigAnd(body, Variable("fci"), [
             POI(Variable("fc%d" % index))
             for index in range(len(fsm.fairness_constraints))
         ]))
예제 #6
0
    def test_simple_fair_with_alias(self):
        class main(md.Module):
            c = md.Var(md.Boolean())
            INIT = c
            TRANS = (c.next() == ~c)
            FAIRNESS = [c, ~c]

        model.load(main)
        fsm = model.bddModel()
        self.assertIsNotNone(fsm)

        @alias("EX {child}")
        def EX(child):
            return Diamond(child)

        @alias()
        def Reach(target):
            return Mu(Variable("W"), Or(target, EX(Variable("W"))))

        @alias("And{{{variable} in {elements}}} {body}")
        def BigAnd(body, variable, elements):
            if len(elements) <= 0:
                return MTrue()
            else:
                element = next(iter(elements))
                elembody = body._substitute(variable, element)
                elements = elements[1:]
                if len(elements) > 1:
                    return And(elembody, BigAnd(body, variable, elements))
                elif len(elements) == 1:
                    n = next(iter(elements))
                    nbody = body._substitute(variable, n)
                    return And(elembody, nbody)
                else:
                    return elembody

        @alias(form="fair")
        def fair(fsm):
            body = EX(Reach(And(Variable("Z"), Variable("fci"))))
            return Nu(
                Variable("Z"),
                BigAnd(body, Variable("fci"), [
                    POI(Variable("fc%d" % index))
                    for index in range(len(fsm.fairness_constraints))
                ]))

        # Compute fair
        expr = SPOI(fair(fsm))

        context = {
            Variable("fc%d" % index): constraint
            for index, constraint in enumerate(fsm.fairness_constraints)
        }
        sat = expr.eval(fsm, context=context)
        self.assertTrue(fsm.init <= sat)
        state = fsm.pick_one_state(sat & fsm.init)

        expl = expr.explain(fsm, state, context=context)
        self.assertIsNotNone(expl.dot())
예제 #7
0
 def EG(inv):
     return POI(Nu(Variable("Z"), And(POI(inv), EX(Variable("Z")))))
예제 #8
0
 def Reach(target):
     return Mu(Variable("W"), Or(target, EX(Variable("W"))))
예제 #9
0
 def EF(target):
     return Mu(Variable("Z"), Or(target, EX(Variable("Z"))))
예제 #10
0
 def AG(inv):
     return Nu(Variable("Z"), And(inv, AX(Variable("Z"))))
예제 #11
0
 def Reach(target):
     return POI(Mu(Variable("Z"), Or(POI(target), EX(Variable("Z")))))