def test_isub(self):
        q = MathSet(self.small2_listnum)
        q -= self.small2_listnum
        assert q == MathSet()

        q = MathSet(self.small2_listnum)
        q -= MathSet()
        assert q == q
class MathSet_operations_test(object):
    def setUp(self):
        self.x = MathSet(['a', 1, [1, 2], {'a', 'b', '8'}])
        self.y = MathSet(['b', -2, [3.5, 2.25], {'/', '_'}])
        self.small2_listnum = MathSet([[1, 2], 0.2])
        self.small1_set = MathSet([{-1, 1}])

    def tearDown(self):
        self.x = None
        self.y = None

    def test_mul(self):
        assert self.small2_listnum * self.small1_set == \
            MathSet([([1,2], {-1, 1}), (0.2, {-1, 1})])
        assert self.small2_listnum * self.small2_listnum == \
            MathSet([([1,2], [1,2]), ([1,2], 0.2), (0.2, [1,2]), (0.2, 0.2)])

    def test_sub(self):
        assert self.small2_listnum - self.small2_listnum == MathSet()
        assert self.small1_set - self.small2_listnum == self.small1_set
        assert self.x - self.small2_listnum == \
            MathSet(['a', 1, {'a', 'b', '8'} ] )

    def test_isub(self):
        q = MathSet(self.small2_listnum)
        q -= self.small2_listnum
        assert q == MathSet()

        q = MathSet(self.small2_listnum)
        q -= MathSet()
        assert q == q

    def test_add_unhashable(self):
        self.small1_set.add([1, 2])
        assert self.small1_set == MathSet([{-1, 1}, [1, 2]])

    def test_pop(self):
        # These tests could be improved by seeding random number
        # generation to ensure determinism.
        assert self.small1_set.pop() == {-1, 1}
        assert self.small1_set == MathSet()

        original = MathSet(self.small2_listnum)
        q = self.small2_listnum.pop()
        assert len(self.small2_listnum) + 1 == len(original)
        self.small2_listnum.add(q)
        assert self.small2_listnum == original

    def test_intersection(self):
        assert self.x.intersection(self.small2_listnum) == MathSet([[1, 2]])
        assert self.x.intersection(MathSet()) == MathSet()

    def test_intersects(self):
        assert self.x.intersects(self.small2_listnum)
        assert not self.small2_listnum.intersects(self.small1_set)
class MathSet_operations_test:
    def setUp(self):
        self.x = MathSet(["a", 1, [1, 2], {"a", "b", "8"}])
        self.y = MathSet(["b", -2, [3.5, 2.25], {"/", "_"}])
        self.small2_listnum = MathSet([[1, 2], 0.2])
        self.small1_set = MathSet([{-1, 1}])

    def tearDown(self):
        self.x = None
        self.y = None

    def test_mul(self):
        assert self.small2_listnum * self.small1_set == MathSet([([1, 2], {-1, 1}), (0.2, {-1, 1})])
        assert self.small2_listnum * self.small2_listnum == MathSet(
            [([1, 2], [1, 2]), ([1, 2], 0.2), (0.2, [1, 2]), (0.2, 0.2)]
        )

    def test_sub(self):
        assert self.small2_listnum - self.small2_listnum == MathSet()
        assert self.small1_set - self.small2_listnum == self.small1_set
        assert self.x - self.small2_listnum == MathSet(["a", 1, {"a", "b", "8"}])

    def test_isub(self):
        q = MathSet(self.small2_listnum)
        q -= self.small2_listnum
        assert q == MathSet()

        q = MathSet(self.small2_listnum)
        q -= MathSet()
        assert q == q

    def test_add_unhashable(self):
        self.small1_set.add([1, 2])
        assert self.small1_set == MathSet([{-1, 1}, [1, 2]])

    def test_pop(self):
        # These tests could be improved by seeding random number
        # generation to ensure determinism.
        assert self.small1_set.pop() == {-1, 1}
        assert self.small1_set == MathSet()

        original = MathSet(self.small2_listnum)
        q = self.small2_listnum.pop()
        assert len(self.small2_listnum) + 1 == len(original)
        self.small2_listnum.add(q)
        assert self.small2_listnum == original

    def test_intersection(self):
        assert self.x.intersection(self.small2_listnum) == MathSet([[1, 2]])
        assert self.x.intersection(MathSet()) == MathSet()

    def test_intersects(self):
        assert self.x.intersects(self.small2_listnum)
        assert not self.small2_listnum.intersects(self.small1_set)
    def test_pop(self):
        # These tests could be improved by seeding random number
        # generation to ensure determinism.
        assert self.small1_set.pop() == {-1, 1}
        assert self.small1_set == MathSet()

        original = MathSet(self.small2_listnum)
        q = self.small2_listnum.pop()
        assert len(self.small2_listnum) + 1 == len(original)
        self.small2_listnum.add(q)
        assert self.small2_listnum == original
Exemple #5
0
def mathset_test():
    s = MathSet([1,2,[1,2] ] )
    q = MathSet()

    q.add(1)
    q |= [1,2]

    q |= ['a', 3, {3}]

    c = s | s

    d = s | q

    assert(c is not s)

    assert(q._set == {'a',1,2,3} )
    assert(q._list == [{3} ] )

    assert(c._set == {1,2} )
    assert(c._list == [[1,2] ] )

    assert(d._set == {1,2,3,'a'} )
    assert(d._list == [[1,2], {3} ] )

    assert(isinstance(s, Iterable) )
    assert(1 in s)
    assert(2 in s)
    assert([1, 2] in s)
    assert(5 not in q)

    assert(len(s) == 3)

    s.remove([1,2] )
    assert([1,2] not in s)
    assert(s._set == {1,2} )
    assert(s._list == [])
    assert(len(s) == 2)

    for item in s:
        print(item)

    """Mutable"""
    a = MathSet()
    b = MathSet([{'a':1} ] )

    a |= b

    assert(a._set == set() )
    assert(a._list == [{'a':1} ] )
def ba_test():
    ba = trs.BA()

    aps = ['p']
    ba.atomic_propositions |= {'p'}
    assert ('p' in ba.atomic_propositions)
    assert (ba.atomic_propositions == MathSet(aps))
    assert (ba.alphabet == PowerSet(aps))

    ba.states.add_from({'q0', 'q1'})
    assert (set(ba.states) == {'q0', 'q1'})

    ba.states.initial.add('q0')
    assert (set(ba.states.initial) == {'q0'})

    ba.states.accepting.add('q1')
    assert (set(ba.states.accepting) == {'q1'})

    ba.transitions.add('q0', 'q1', letter={'p'})
    ba.transitions.add('q1', 'q1', letter={'p'})
    ba.transitions.add('q1', 'q0', letter=set())
    ba.transitions.add('q0', 'q0', letter=set())

    logger.debug(ba)
    ba.save('ba.pdf')
    return ba
def open_fts_multiple_env_actions_test():
    env_modes = MathSet({'up', 'down'})
    env_choice = MathSet({'left', 'right'})

    env_actions = [
        {
            'name': 'env_modes',
            'values': env_modes,
            'setter': True},
        {
            'name': 'env_choices',
            'values': env_choice}]
    ts = FTS(env_actions)
    assert(ts.env_modes is env_modes)
    assert(not hasattr(ts, 'env_choices') )
    assert(ts.sys_actions == MathSet() )
Exemple #8
0
 def __init__(self):
     edge_label_types = [{
         'name': self.action_label,
         'values': MathSet(),
         'setter': True
     }]
     super(MarkovDecisionProcess, self).__init__()
     super(MarkovDecisionProcess,
           self).add_label_types(edge_label_types, True)
     self.actions = self.action
def powerset_test():
    s = [[1, 2], '3', {'a': 1}, 1]

    p = PowerSet(s)

    s2 = ['3', {'a': 1}, [1, 2], 1]
    q = PowerSet()
    q.math_set = MathSet(s2)

    assert (p.math_set == MathSet(s))
    assert (q.math_set == MathSet(s))
    assert (isinstance(q.math_set, MathSet))
    assert (p == q)

    q.math_set.add(6)

    # CAUTION: comparing p() and q() might yield False, due to ordering
    f = p + q
    assert (isinstance(f, PowerSet))
    assert (f.math_set._set == {1, '3', 6})
    assert (compare_lists(f.math_set._list, [[1, 2], {'a': 1}]))

    return s
def mathset_test():
    s = MathSet([1, 2, [1, 2]])
    q = MathSet()

    q.add(1)
    q |= [1, 2]

    q |= ["a", 3, {3}]

    c = s | s

    d = s | q

    assert c is not s

    assert q._set == {"a", 1, 2, 3}
    assert q._list == [{3}]

    assert c._set == {1, 2}
    assert c._list == [[1, 2]]

    assert d._set == {1, 2, 3, "a"}
    assert d._list == [[1, 2], {3}]

    assert isinstance(s, Iterable)
    assert 1 in s
    assert 2 in s
    assert [1, 2] in s
    assert 5 not in q

    assert len(s) == 3

    s.remove([1, 2])
    assert [1, 2] not in s
    assert s._set == {1, 2}
    assert s._list == []
    assert len(s) == 2

    for item in s:
        print (item)

    """Mutable"""
    a = MathSet()
    b = MathSet([{"a": 1}])

    a |= b

    assert a._set == set()
    assert a._list == [{"a": 1}]
    def test_add_from(self):
        self.S_ap.add_from([(0, {'ap':{'p'}}), (1, {'ap':{'q'} })])
        assert len(self.S_ap) == 2
        current_labels = [l["ap"] for (s,l) in self.S_ap(data=True)]

        assert len(current_labels) == 2 and \
            MathSet(current_labels) == MathSet([{'p'}, {'q'}])

        self.S_ap.add_from([(10, {'ap':{'r'} }),
                                      (11, {'ap':{'x'} })],
                                      check=False)
        assert len(self.S_ap) == 4
        self.S_ap.add_from([(10, {'ap':{'a'} }),
                                      (11, {'ap':{'b'} })])
        assert len(self.S_ap) == 4
        current_labels = [l["ap"] for (s,l) in self.S_ap(data=True)]
        assert len(current_labels) == 4 and \
            MathSet(current_labels)== MathSet([{'p'}, {'q'}, {'a'}, {'b'}])
        assert MathSet([l["ap"] for (s,l) in self.S_ap(data=True)]) == MathSet(current_labels)
Exemple #12
0
    def __init__(self, env_actions=None, sys_actions=None):
        """Instantiate finite transition system.

        @param env_actions: environment (uncontrolled) actions,
            defined as C{edge_label_types} in L{LabeledDiGraph.__init__}

        @param sys_actions: system (controlled) actions, defined as
            C{edge_label_types} in L{LabeledDiGraph.__init__}
        """
        self._owner = 'sys'

        if env_actions is None:
            env_actions = [
                {'name': 'env_actions',
                 'values': MathSet(),
                 'setter': True}]
        if sys_actions is None:
            sys_actions = [
                {'name': 'sys_actions',
                 'values': MathSet(),
                 'setter': True}]
        # note: "sys_actions" used to be "actions"
        # in closed systems (old FTS)
        action_types = env_actions + sys_actions
        edge_label_types = action_types
        ap_labels = PowerSet()
        node_label_types = [
            {'name': 'ap',
             'values': ap_labels,
             'setter': ap_labels.math_set,
             'default': set()}]
        super(FiniteTransitionSystem, self).__init__(
            node_label_types, edge_label_types)
        # make them available also via an "actions" dicts
        # name, codomain, *rest = x
        actions = {x['name']: x['values'] for x in edge_label_types}
        if 'actions' in actions:
            msg = '"actions" cannot be used as an action type name,\n'
            msg += 'because if an attribute for this action type'
            msg += 'is requested,\n then it will conflict with '
            msg += 'the dict storing all action types.'
            raise ValueError(msg)
        self.actions = actions
        self.atomic_propositions = self.ap
        self.aps = self.atomic_propositions  # shortcut
        # action constraint used in synth.synthesize
        self.env_actions_must = 'xor'
        self.sys_actions_must = 'xor'
        # dot formatting
        self._state_dot_label_format = {
            'ap': '',
            'type?label': '',
            'separator': r'\\n'}
        self._transition_dot_label_format = {
            'sys_actions': 'sys',  # todo: '' if no env
            'env_actions': 'env',
            'type?label': ':',  # todo: '' if no env
            'separator': r'\\n'}
        self._transition_dot_mask = dict()
        self.dot_node_shape = {'normal': 'box'}  # todo: rectangle if no env
        self.default_export_fname = 'fts'
 def test_add_untyped_keys(self):
     self.S_ap.add(1, foo=MathSet(['p']), check=True)
 def test_add_unhashable(self):
     self.small1_set.add([1, 2])
     assert self.small1_set == MathSet([{-1, 1}, [1, 2]])
 def test_sub(self):
     assert self.small2_listnum - self.small2_listnum == MathSet()
     assert self.small1_set - self.small2_listnum == self.small1_set
     assert self.x - self.small2_listnum == \
         MathSet(['a', 1, {'a', 'b', '8'} ] )
 def test_mul(self):
     assert self.small2_listnum * self.small1_set == \
         MathSet([([1,2], {-1, 1}), (0.2, {-1, 1})])
     assert self.small2_listnum * self.small2_listnum == \
         MathSet([([1,2], [1,2]), ([1,2], 0.2), (0.2, [1,2]), (0.2, 0.2)])
 def setUp(self):
     self.x = MathSet(['a', 1, [1, 2], {'a', 'b', '8'}])
     self.y = MathSet(['b', -2, [3.5, 2.25], {'/', '_'}])
     self.small2_listnum = MathSet([[1, 2], 0.2])
     self.small1_set = MathSet([{-1, 1}])
 def setUp(self):
     self.p = PowerSet({1, 2, 3})
     self.q_unhashable = PowerSet(MathSet([[1, 2], ["a", "b"]]))
     self.singleton = PowerSet(set([1]))
     self.empty = PowerSet()
def test_tuple():
    s = MathSet((1, 2))
    assert (s._set == {1, 2})
    assert (s._list == [])
 def test_intersection(self):
     assert self.x.intersection(self.small2_listnum) == MathSet([[1, 2]])
     assert self.x.intersection(MathSet()) == MathSet()
 def setUp(self):
     self.x = MathSet(["a", 1, [1, 2], {"a", "b", "8"}])
     self.y = MathSet(["b", -2, [3.5, 2.25], {"/", "_"}])
     self.small2_listnum = MathSet([[1, 2], 0.2])
     self.small1_set = MathSet([{-1, 1}])