__init__.py 3.98 KB
Newer Older
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
1
from abc import ABCMeta, abstractmethod
2
from enum import Enum
3
from typing import List, Tuple
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
4

5
from model.fa import Symbol
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
6
from model.ra import Action
7

8
9
10
11
12
__all__ = [
    "get_simulation",
    "get_scalable_sut",
    "scalable_sut_classes"
    ]
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
13

14
15
16
17
18
19
20
21
22
23
class Observation():
    @abstractmethod
    def trace(self):
        """returns the trace to be added to the solver"""
        pass

    @abstractmethod
    def inputs(self):
        """returns all the inputs from an observation"""
        pass
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
24

25
26
27
class TransducerObservation(metaclass=ABCMeta):
    def __init__(self, iotrace:List[Tuple[object,object]]):
        self.iotrace = iotrace
28

29
30
    def trace(self) -> List[Tuple[object,object]]:
        return self.iotrace
31

32
33
34
35
36
37
38
39
    def inputs(self) -> List[object]:
        return [a for (a,_) in self.iotrace]

    def __str__(self):
        return "Obs" + str(self.trace()) + ""

class AcceptorObservation(metaclass=ABCMeta):
    def __init__(self, seq:List[object], acc:bool):
40
41
42
        self.seq = seq
        self.acc = acc

43
    def trace(self) -> Tuple[List[object], bool]:
44
45
        return (self.seq, self.acc)

46
    def inputs(self) -> List[object]:
47
48
        return self.seq

49
50
    def __str__(self):
        return "Obs" + str(self.trace())
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class DFAObservation(AcceptorObservation):
    def __init__(self, seq, acc):
        super().__init__(seq, acc)

    def trace(self) -> Tuple[List[Symbol], bool]:
        return super().trace()

    def inputs(self) -> List[Symbol]:
        return super().inputs()

class MealyObservation(TransducerObservation):
    def __init__(self, iotrace):
        super().__init__(iotrace)

    def trace(self) -> List[Tuple[Symbol, Symbol]]:
        return super().trace()
68
69

    def inputs(self) -> List[Symbol]:
70
        return super().inputs()
71

72
73
74
75
76
77
78
79
80
class RegisterMachineObservation(Observation):

    @abstractmethod
    def values(self):
        """returns all the values in the trace"""
        pass



81
class RAObservation(AcceptorObservation, RegisterMachineObservation):
82
    def __init__(self, seq, acc):
83
        super().__init__(seq, acc)
84

85
86
    def trace(self) -> Tuple[List[Action], bool]:
        return self.trace()
87

88
89
    def inputs(self) -> List[Action]:
        return super().inputs()
90
91

    def values(self):
92
        return set([a.value for a in self.inputs() if a.value is not None])
93

94
class IORAObservation(TransducerObservation, RegisterMachineObservation):
95
    def __init__(self, trace):
96
        super().__init__(trace)
97

98
    def trace(self) -> List[Tuple[Action, Action]]:
99
        return super().trace()
100

101
102
    def inputs(self) -> List[Action]:
        return super().inputs()
103
104

    def values(self):
105
106
        iv = [a.value for (a,_) in self.trace() if a.value is not None]
        ov = [a.value for (_,a) in self.trace() if a.value is not None]
107
108
        return set(iv+ov)

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class SUT(metaclass=ABCMeta):
    OK = "OK"
    NOK = "NOK"
    @abstractmethod
    def run(self, seq:List[object]) -> Observation:
        """Runs a sequence of inputs on the SUT and returns an observation"""
        pass

    @abstractmethod
    def input_interface(self) -> List[object]:
        """Runs the list of inputs or input signatures comprising the input interface"""
        pass

class SUTType(Enum):
    IORA = 1
    RA = 2
    Mealy = 3
    Moore = 4
    DFA = 5

    def is_acceptor(self):
        return  self == SUTType.RA or self.DFA

132
133
134
    def has_registers(self):
        return self == SUTType.RA or self == SUTType.IORA

135
136
137
    def is_transducer(self):
        return  not self.is_acceptor()

138
139
140
141
142
# some useful functions
from sut.scalable import scalable_sut_classes, get_scalable_sut
from sut.simulation import get_simulation


Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class StatsSUT(SUT):
    def __init__(self, sut:SUT):
        self._sut = sut
        self._inputs = 0
        self._resets = 0

    def input_interface(self):
        return self._sut.input_interface()

    def run(self, seq:List[object]):
        self._inputs += len(seq)
        self._resets += 1
        return self._sut.run(seq)

    def inputs(self):
        return self._inputs

    def resets(self):
        return self._resets