checker.py 8.19 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python
# -*- coding: utf-8 -*-

''' Property parser for SDL systems

Template for a simple model checker

(c) European Space Agency, 2015-2017

Author: Maxime Perrotin
'''
from stringtemplate3 import StringTemplateGroup, StringTemplate
import opengeode

asn1_module = '''with <module>;
use <module>;
'''

provided_interface = '''<interface>_pi'''

required_interface = '''<interface>_ri'''

event = '''when <interface>_pi =>
<if(param)>
    <interface>_Param: aliased asn1Scc<sort>;
<else>
    null;
<endif>'''

Maxime Perrotin's avatar
Maxime Perrotin committed
30
31
32
33
34
35
36
print_event = '''when <interface>_pi =>
<if(param)>
    Put_Line("<interface>(" & <sort>_Pkg.Image(Event.<interface>_Param) & ")");
<else>
    Put_Line("<interface>");
<endif>'''

Maxime Perrotin's avatar
Maxime Perrotin committed
37
38
39
40
41
42
43
44
check_ppty = u'''errno := <num>;
res := <model>_stop_conditions.p\u00dcproperty_<num>;
if res then
    return res;
end if;'''

exhaust_procedure = '''procedure exhaust_<interface> is
    <if(param)><interface>_it    : <sort>_pkg.Instance;<endif>
Maxime Perrotin's avatar
Maxime Perrotin committed
45
46
    event     : Event_ty(<interface>_pi);
    S_Hash    : Hash_Type;
Maxime Perrotin's avatar
Maxime Perrotin committed
47
48
49
begin
<if(param)>
    for each of <interface>_it loop
50
        <sort>_pkg.To_ASN1(each, event.<interface>_param);
Maxime Perrotin's avatar
Maxime Perrotin committed
51
        <model>.<interface>(event.<interface>_param'access);
52
        ES.count := ES.count + 1;
Maxime Perrotin's avatar
Maxime Perrotin committed
53
        S_Hash := Add_to_graph(event => event);
54
        Check_And_Report(S_Hash);
Maxime Perrotin's avatar
Maxime Perrotin committed
55
        Process_Ctxt := backup_ctxt;
Maxime Perrotin's avatar
Maxime Perrotin committed
56
        exit when properties.length >= 10;
Maxime Perrotin's avatar
Maxime Perrotin committed
57
58
59
    end loop;
<else>
    <model>.<interface>;
60
    ES.count := ES.count + 1;
Maxime Perrotin's avatar
Maxime Perrotin committed
61
    S_Hash := Add_to_graph(event => event);
62
    Check_And_Report(S_Hash);
63
    Process_Ctxt := backup_ctxt;
Maxime Perrotin's avatar
Maxime Perrotin committed
64
65
66
67
68
69
70
71
72
73
<endif>

end;'''

exhaust = '''if Process_Ctxt.State in <states;separator= " | "> then
    exhaust_<interface>;
end if;'''

checker_template = '''with <model>;
use <model>;
74
75
76
with <model>_Stop_Conditions;
with ASN1_Ada_Iterators.Iterators;
use ASN1_Ada_Iterators.Iterators;
Maxime Perrotin's avatar
Maxime Perrotin committed
77
78
79

<asn1_modules;separator="\n">

80
81
82
83
84
85
with ASN1_Iterators.Exhaustive_Simulation;
use ASN1_Iterators;

with Ada.Text_IO,
     Ada.Containers,
     Ada.Calendar;
86
87
88
89
90
91

use Ada.Containers,
    Ada.Calendar,
    Ada.Text_IO;

procedure Model_Checker is
Maxime Perrotin's avatar
Maxime Perrotin committed
92
93
94
95
96
    subtype Context_ty is <model>_ctxt_ty;
    Process_Ctxt : Context_ty renames <model>_Ctxt;

    -- Type representing an event (input or output)
    type Interfaces is (start, <interfaces;separator=", ">);
97
    type Event_ty (Option: Interfaces := start) is
Maxime Perrotin's avatar
Maxime Perrotin committed
98
99
100
101
102
103
104
105
        record
            case Option is
                when start =>
                    null;
                <events;separator="\n">
            end case;
    end record;

Maxime Perrotin's avatar
Maxime Perrotin committed
106
107
108
109
110
111
112
113
114
115
    -- Display scenario (in the future: generate MSC)
    procedure Print_Event(Event: Event_ty) is
    begin
         case Event.Option is
            when start =>
                Put_Line("START");
            <print_events;separator="\n">
        end case;
    end;

Maxime Perrotin's avatar
Maxime Perrotin committed
116
117
118
119
120
121
122
123
    -- Check all properties in one go, and if one fails, set errno
    function check_properties(errno: out natural) return boolean is
        res : Boolean := false;
    begin
        <check_properties;separator="\n">
        return res;
    end;

124
125
126
127
128
129
    package ES is new Exhaustive_Simulation(Context_ty       => <model>_Ctxt_Ty,
                                            Process_Ctxt     => Process_Ctxt'access,
                                            Event_Ty         => Event_ty,
                                            Print_Event      => Print_Event,
                                            Check_Properties => Check_Properties);
    use ES;
Maxime Perrotin's avatar
Maxime Perrotin committed
130
131
132
133
134


    -- One function per PI to exhaust the interface parameters
    <exhaust_procedures;separator="\n\n">

135
    procedure Exhaustive_Simulator is
Maxime Perrotin's avatar
Maxime Perrotin committed
136
    begin
137
138
        backup_ctxt := Process_Ctxt;
        backup_hash := State_Hash(Backup_ctxt);
Maxime Perrotin's avatar
Maxime Perrotin committed
139
140
141
        <exhausts;separator="\n">
    end;

Maxime Perrotin's avatar
Maxime Perrotin committed
142

Maxime Perrotin's avatar
Maxime Perrotin committed
143
    event      : Event_ty(start);
Maxime Perrotin's avatar
Maxime Perrotin committed
144
    Start_Time : constant Time := Clock;
Maxime Perrotin's avatar
Maxime Perrotin committed
145
146
begin
    put_line("Exhaustive simulation. Hit Ctrl-C to stop if it is too long...");
147
    <model>.startup;
Maxime Perrotin's avatar
Maxime Perrotin committed
148
    Start_Hash := Add_to_graph(event => event);
149
150
151
152
153
154
155
    Check_And_Report(Start_Hash);
    ES.Queue.append(Start_Hash);
    Visited.Include(Start_Hash);
    while ES.Queue.Length > 0 and Properties.Length \<= 10 loop
        Process_Ctxt := Grafset.Element(Key => ES.Queue.Last_Element).Context;
        Exhaustive_Simulator;
        ES.Queue.delete_last;
Maxime Perrotin's avatar
Maxime Perrotin committed
156
    end loop;
157
    if Properties.Length > 0 then
Maxime Perrotin's avatar
Maxime Perrotin committed
158
159
        Generate_MSC;
    end if;
160
161
    Put_Line("Executed" & ES.count'Img & " functions");
    Put_Line("Visited" & Grafset.Length'img & " states");
Maxime Perrotin's avatar
Maxime Perrotin committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    Put_Line("Execution time:" & Duration'Image(Clock - Start_Time) & "s.");
end;
'''

def generate(ast, stop_conditions):
    ''' Generate the code for the model checker '''
    process = ast.processes[0]

    # Set up a StringTemplate group (needed to configure angle-bracket)
    group = StringTemplateGroup('checker', '/tmp', lexer='angle-bracket')

    # List of ASN.1 modules
    asn1 = [name.replace('-', '_') for name in ast.asn1Modules]

    # Provided interfaces (process.input_signals)
    pis = {pi['name']: pi.get('type', None) for pi in process.input_signals}

    # Required interfaces (process.output_signals)
    ris = {ri['name']: ri.get('type', None) for ri in process.output_signals}

    # Instiantate and fill in the templates with the model data

    asn1_modules = []
    for each in asn1:
        asn1_tpl = StringTemplate(asn1_module, group=group)
        asn1_tpl['module'] = each
        asn1_modules.append(str(asn1_tpl))

    provided_interfaces = []
    for each in pis.keys():
        pi_tpl = StringTemplate(provided_interface, group=group)
        pi_tpl['interface'] = each
        provided_interfaces.append(str(pi_tpl))

    required_interfaces = []
    for each in ris.keys():
        ri_tpl = StringTemplate(required_interface, group=group)
        ri_tpl['interface'] = each
        required_interfaces.append(str(ri_tpl))

    events = []
Maxime Perrotin's avatar
Maxime Perrotin committed
203
    print_events = []
Maxime Perrotin's avatar
Maxime Perrotin committed
204
205
    for name, sort in pis.viewitems():
        event_tpl = StringTemplate(event, group=group)
Maxime Perrotin's avatar
Maxime Perrotin committed
206
207
208
209
210
211
        print_event_tpl = StringTemplate(print_event, group=group)
        event_tpl['interface'] = print_event_tpl['interface'] = name
        event_tpl['param'] = print_event_tpl['param'] = True if sort else False
        event_tpl['sort'] = print_event_tpl['sort'] = \
                (opengeode.AdaGenerator.type_name(sort, use_prefix=False)
                             if sort else "")
Maxime Perrotin's avatar
Maxime Perrotin committed
212
        events.append(str(event_tpl))
Maxime Perrotin's avatar
Maxime Perrotin committed
213
        print_events.append(str(print_event_tpl))
Maxime Perrotin's avatar
Maxime Perrotin committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

    check_ppties = []
    for idx in range(len(stop_conditions)):
        check_ppty_tpl = StringTemplate(check_ppty, group=group)
        check_ppty_tpl['model'] = process.processName
        check_ppty_tpl['num'] = str(idx)
        check_ppties.append(unicode(check_ppty_tpl))

    exhaust_procedures = []
    for name, sort in pis.viewitems():
        exhaust_procedure_tpl = StringTemplate(exhaust_procedure, group=group)
        exhaust_procedure_tpl['interface'] = name
        exhaust_procedure_tpl['param'] = True if sort else False
        exhaust_procedure_tpl['sort'] = (opengeode.AdaGenerator.type_name(sort,
                                                              use_prefix=False)
                                                               if sort else '')
        exhaust_procedure_tpl['model'] = process.processName
        exhaust_procedures.append(str(exhaust_procedure_tpl))

    exhausts = []
    for name, sort in pis.viewitems():
        exhaust_tpl = StringTemplate(exhaust, group=group)
        exhaust_tpl['interface'] = name
        # get a mapping {input: {state : transition}}
        opengeode.Helper.flatten(process, sep=u'\u00dc')
        mapping = opengeode.Helper.map_input_state(process)
        states = [state for state in mapping[name].keys() if "START" not
                  in state]
        exhaust_tpl['states'] = states
        exhausts.append(str(exhaust_tpl))

    complete = StringTemplate(checker_template, group=group)
    complete['model'] = process.processName
    complete['asn1_modules'] = asn1_modules
    complete['interfaces'] = provided_interfaces
    complete['events'] = events
Maxime Perrotin's avatar
Maxime Perrotin committed
250
    complete['print_events'] = print_events
Maxime Perrotin's avatar
Maxime Perrotin committed
251
252
253
254
255
256
    complete['check_properties'] = check_ppties
    complete['exhaust_procedures'] = exhaust_procedures
    complete['exhausts'] = exhausts

    with open('model_checker.adb', 'w') as ada_file:
        ada_file.write(unicode(complete).encode('latin1'))