~verifypn-stub/verifypn/short-circut

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
203
204
205
206
207
208
209
210
211
212
213
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#include "OnTheFlyDG.h"

#include "../../PetriEngine/SuccessorGenerator.h"

#include <algorithm>
#include <string.h>
#include <iostream>

using namespace DependencyGraph;

namespace PetriNets {

OnTheFlyDG::OnTheFlyDG(PetriEngine::PetriNet *t_net) : encoder(t_net->numberOfPlaces(), 0), edge_alloc(1) {
    net = t_net;
    n_places = t_net->numberOfPlaces();
    n_transitions = t_net->numberOfTransitions();
}


OnTheFlyDG::~OnTheFlyDG()
{
    cleanUp();
    //Note: initial marking is in the markings set, therefore it will be deleted by the for loop
    //TODO: Ensure we don't leak memory here, when code moving is done

}

bool OnTheFlyDG::fastEval(CTLQuery &query, size_t marking, Marking* unfolded)
{
    assert(!query.IsTemporal);
    if (query.GetQuantifier() == AND){
        return fastEval(*query.GetFirstChild(), marking, unfolded) && fastEval(*query.GetSecondChild(), marking, unfolded);
    } else if (query.GetQuantifier() == OR){
        return fastEval(*query.GetFirstChild(), marking, unfolded) || fastEval(*query.GetSecondChild(), marking, unfolded);
    } else if (query.GetQuantifier() == NEG){
        bool result = fastEval(*query.GetFirstChild(), marking, unfolded);
        return !result;
    } else {
        return evaluateQuery(query, marking, unfolded);
    }
}


void OnTheFlyDG::successors(Configuration *c)
{
    PetriConfig *v = static_cast<PetriConfig*>(c);
    trie.unpack(v->marking, encoder.scratchpad().raw());
    encoder.decode(query_marking.marking(), encoder.scratchpad().raw());
    //    v->printConfiguration();

    CTLType query_type = v->query->GetQueryType();
    if(query_type == EVAL){
        //assert(false && "Someone told me, this was a bad place to be.");
        if (evaluateQuery(*v->query, v->marking, &query_marking)){
            v->successors.push_back(newEdge(*v));
        }
    }
    else if (query_type == LOPERATOR){
        if(v->query->GetQuantifier() == NEG){
            Configuration* c = createConfiguration(v->marking, *(v->query->GetFirstChild()));
            Edge* e = newEdge(*v);
            e->is_negated = true;
            e->targets.push_back(c);
            v->successors.push_back(e);
        }
        else if(v->query->GetQuantifier() == AND){
            //Check if left is false
            if(!v->query->GetFirstChild()->IsTemporal){
                if(!fastEval(*(v->query->GetFirstChild()), v->marking, &query_marking))
                    //query cannot be satisfied, return empty succ set
                    return;
            }

            //check if right is false
            if(!v->query->GetSecondChild()->IsTemporal){
                if(!fastEval(*(v->query->GetSecondChild()), v->marking, &query_marking))
                    return;
            }

            Edge *e = newEdge(*v);

            //If we get here, then either both propositions are true (shouldn't be possible)
            //Or a temporal operator and a true proposition
            //Or both are temporal
            if(v->query->GetFirstChild()->IsTemporal){
                e->targets.push_back(createConfiguration(v->marking, *(v->query->GetFirstChild())));
            }
            if(v->query->GetSecondChild()->IsTemporal){
                e->targets.push_back(createConfiguration(v->marking, *(v->query->GetSecondChild())));
            }
            v->successors.push_back(e);
        }
        else if(v->query->GetQuantifier() == OR){
            //Check if left is true
            if(!v->query->GetFirstChild()->IsTemporal){
                if(fastEval(*(v->query->GetFirstChild()), v->marking, &query_marking)){
                    //query is satisfied, return
                    v->successors.push_back(newEdge(*v));
                    return;
                }
            }

            if(!v->query->GetSecondChild()->IsTemporal){
                if(fastEval(*(v->query->GetSecondChild()), v->marking, &query_marking)){
                    v->successors.push_back(newEdge(*v));
                    return;
                }
            }

            //If we get here, either both propositions are false
            //Or one is false and one is temporal
            //Or both temporal
            if(v->query->GetFirstChild()->IsTemporal){
                Edge *e = newEdge(*v);
                e->targets.push_back(createConfiguration(v->marking, *(v->query->GetFirstChild())));
                v->successors.push_back(e);
            }
            if(v->query->GetSecondChild()->IsTemporal){
                Edge *e = newEdge(*v);
                e->targets.push_back(createConfiguration(v->marking, *(v->query->GetSecondChild())));
                v->successors.push_back(e);
            }
        }
        else{
            assert(false && "An unknown error occoured in the loperator-part of the successor generator");
        }
    }
    else if (query_type == PATHQEURY){
        if(v->query->GetQuantifier() == A){
            if (v->query->GetPath() == U){
                Edge *right = NULL;
                if (!v->query->GetSecondChild()->IsTemporal){
                    //right side is not temporal, eval it right now!
                    bool valid = fastEval(*(v->query->GetSecondChild()), v->marking, &query_marking);
                    if (valid) {    //satisfied, no need to go through successors
                        v->successors.push_back(newEdge(*v));
                        return;
                    }//else: It's not valid, no need to add any edge, just add successors
                }
                else {
                    //right side is temporal, we need to evaluate it as normal
                    Configuration* c = createConfiguration(v->marking, *(v->query->GetSecondChild()));
                    right = newEdge(*v);
                    right->targets.push_back(c);
                }
                bool valid = false;
                Configuration *left = NULL;
                if (!v->query->GetFirstChild()->IsTemporal) {
                    //left side is not temporal, eval it right now!
                    valid = fastEval(*(v->query->GetFirstChild()), v->marking, &query_marking);
                } else {
                    //left side is temporal, include it in the edge
                    left = createConfiguration(v->marking, *(v->query->GetFirstChild()));
                }
                if (valid || left != NULL) {
                    //if left side is guaranteed to be not satisfied, skip successor generation
                    Edge* leftEdge = NULL;
                    nextStates (query_marking,
                                [&](){ leftEdge = newEdge(*v);},
                                [&](size_t m, Marking&){
                                    Configuration* c = createConfiguration(m, *(v->query));
                                    leftEdge->targets.push_back(c);
                                    return true;
                                },
                                [&]()
                                {
                                    if (left != NULL) {
                                        leftEdge->targets.push_back(left);
                                    }
                                    v->successors.push_back(leftEdge);                                    
                                }
                            );
                } //else: Left side is not temporal and it's false, no way to succeed there...

                if (right != NULL) {
                    v->successors.push_back(right);
                }
            }
            else if(v->query->GetPath() == F){
                Edge *subquery = NULL;
                if (!v->query->GetFirstChild()->IsTemporal) {
                    bool valid = fastEval(*(v->query->GetFirstChild()), v->marking, &query_marking);
                    if (valid) {
                        v->successors.push_back(newEdge(*v));
                        return;
                    }
                } else {
                    subquery = newEdge(*v);
                    Configuration* c = createConfiguration(v->marking, *(v->query->GetFirstChild()));
                    subquery->targets.push_back(c);
                }
                Edge* e1 = NULL;
                nextStates(query_marking,
                        [&](){e1 = newEdge(*v);},
                        [&](size_t m, Marking&)
                        {
                            Configuration* c = createConfiguration(m, *(v->query));
                            e1->targets.push_back(c);
                            return true;
                        },
                        [&]()
                        {
                            v->successors.push_back(e1);
                        }                    
                );

                if (subquery != NULL) {
                    v->successors.push_back(subquery);
                }
            }
            else if(v->query->GetPath() == X){

                if (v->query->GetFirstChild()->IsTemporal) {   //regular check
                    Edge* e = newEdge(*v);
                    nextStates(query_marking, 
                            [](){}, 
                            [&](size_t m, Marking&){
                                Configuration* c = createConfiguration(m, *(v->query->GetFirstChild()));
                                e->targets.push_back(c);
                                return true;
                            }, 
                            [](){}
                        );
                    v->successors.push_back(e);
                } else {
                    bool allValid = true;
                    nextStates(query_marking, 
                                [](){},
                                [&](size_t m, Marking& marking){
                                    bool valid = fastEval(*(v->query->GetFirstChild()), m, &marking);
                                    if (!valid) {
                                        allValid = false;
                                        return false;
                                    }
                                    return true;
                                },
                                [](){}
                            );
                    if (allValid) {
                        v->successors.push_back(newEdge(*v));
                        return;
                    }
                }
            }
            else if(v->query->GetPath() == G ){
                assert(false && "Path operator G had not been translated - Parse error detected in succ()");
            }
            else
                assert(false && "An unknown error occoured in the successor generator");
        }
        else if(v->query->GetQuantifier() == E){
            if (v->query->GetPath() == U){
                Edge *right = NULL;
                if (v->query->GetSecondChild()->IsTemporal) {
                    Configuration* c = createConfiguration(v->marking, *(v->query->GetSecondChild()));
                    right = newEdge(*v);
                    right->targets.push_back(c);
                } else {
                    bool valid = fastEval(*(v->query->GetSecondChild()), v->marking, &query_marking);
                    if (valid) {
                        v->successors.push_back(newEdge(*v));
                        return;
                    }   // else: right condition is not satisfied, no need to add an edge
                }


                Configuration *left = NULL;
                bool valid = false;
                nextStates(query_marking, 
                    [&](){
                        if (v->query->GetFirstChild()->IsTemporal) {
                            left = createConfiguration(v->marking, *(v->query->GetFirstChild()));
                        } else {
                            valid = fastEval(*(v->query->GetFirstChild()), v->marking, &query_marking);
                        }                        
                    },
                    [&](size_t m, Marking&){
                        if(left == NULL && !valid) return false;
                        Edge* e = newEdge(*v);
                        Configuration* c1 = createConfiguration(m, *(v->query));
                        e->targets.push_back(c1);
                        if (left != NULL) {
                            e->targets.push_back(left);
                        }
                        v->successors.push_back(e);
                        return true;
                }, [](){});

                if (right != NULL) {
                    v->successors.push_back(right);
                }
            }
            else if(v->query->GetPath() == F){
                Edge *subquery = NULL;
                if (!v->query->GetFirstChild()->IsTemporal) {
                    bool valid = fastEval(*(v->query->GetFirstChild()), v->marking, &query_marking);
                    if (valid) {
                        v->successors.push_back(newEdge(*v));
                        return;
                    }
                } else {
                    Configuration* c = createConfiguration(v->marking, *(v->query->GetFirstChild()));
                    subquery = newEdge(*v);
                    subquery->targets.push_back(c);
                }

                nextStates(query_marking,
                            [](){},
                            [&](size_t m, Marking&){
                                Edge* e = newEdge(*v);
                                Configuration* c = createConfiguration(m, *(v->query));
                                e->targets.push_back(c);
                                v->successors.push_back(e);
                                return true;
                            },
                            [](){}
                        );

                if (subquery != NULL) {
                    v->successors.push_back(subquery);
                }
            }
            else if(v->query->GetPath() == X){
                CTLQuery* query = v->query->GetFirstChild();
                if (query->IsTemporal) {    //have to check, no way to skip that
                    nextStates(query_marking, 
                            [](){}, 
                            [&](size_t m, Marking& marking) {
                                Edge* e = newEdge(*v);
                                Configuration* c = createConfiguration(m, *query);
                                e->targets.push_back(c);
                                v->successors.push_back(e);
                                return true;
                            },
                            [](){}
                        );
                } else {
                    nextStates(query_marking, 
                            [](){}, 
                            [&](size_t m, Marking& marking) {
                                bool valid = fastEval(*query, m, &marking);
                                if (valid) {
                                    v->successors.push_back(newEdge(*v));
                                    return false;
                                }   //else: It can't hold there, no need to create an edge
                                return true;
                            },
                            [](){}
                        );
                }
            }
            else if(v->query->GetPath() == G ){
                assert(false && "Path operator G had not been translated - Parse error detected in succ()");
            }
            else
                assert(false && "An unknown error occoured in the successor generator");
        }
    }
}

bool OnTheFlyDG::evaluateQuery(CTLQuery &query, size_t marking, Marking* unfolded)
{    
    assert(query.GetQueryType() == EVAL);
    EvaluateableProposition *proposition = query.GetProposition();

    if (proposition->GetPropositionType() == FIREABILITY) {
        for(const auto f : proposition->GetFireset()){
            // We might be able to optimize this out
            // by keeping track of the fired transitions
            // during successor generation
            if(net->fireable(unfolded->marking(), f)){
                return true;
            }
        }
        return false;
    }
    else if (proposition->GetPropositionType() == CARDINALITY) {
        int first_param = GetParamValue(proposition->GetFirstParameter(), *unfolded);
        int second_param = GetParamValue(proposition->GetSecondParameter(), *unfolded);
        return EvalCardianlity(first_param, proposition->GetLoperator(), second_param);
    }
    else if (proposition->GetPropositionType() == DEADLOCK) {
        return net->deadlocked(unfolded->marking());
    }
    else
        assert(false && "Incorrect query proposition type was attempted evaluated");
    exit(EXIT_FAILURE);
}

int OnTheFlyDG::GetParamValue(CardinalityParameter *param, Marking& marking) {
    if(param->isPlace){
        int res = 0;
        for(int place : param->places_i){
            res = res + marking.marking()[place];
        }
        return res;
    }
    else{
        return param->value;
    }
}

bool OnTheFlyDG::EvalCardianlity(int a, LoperatorType lop, int b) {
    if(lop == EQ)
        return a == b;
    else if (lop == LE)
        return a < b;
    else if (lop == LEQ)
        return a <= b;
    else if (lop == GR)
        return a > b;
    else if (lop == GRQ)
        return a >= b;
    else if (lop == NE)
        return a != b;
    std::cerr << "Error: Query unsupported - Attempted to compare " << a << " with " << b << " by an unknown logical operator " << std::endl;
    exit(EXIT_FAILURE);
}


Configuration* OnTheFlyDG::initialConfiguration()
{
    return initial_config;
}

void OnTheFlyDG::nextStates(Marking& t_marking, 
    std::function<void ()> pre, 
    std::function<bool (size_t, Marking&)> foreach, 
    std::function<void ()> post)
{

    bool first = true;
    PetriEngine::SuccessorGenerator PNGen(*net);
    PNGen.prepare(&query_marking);
    memcpy(working_marking.marking(), query_marking.marking(), n_places*sizeof(PetriEngine::MarkVal));    

    
    while(PNGen.next(working_marking)){
        if(first) pre();
        first = false;
        if(!foreach(createMarking(working_marking), working_marking))
        {
            break;
        }
    }

    if(!first) post();
}

void OnTheFlyDG::cleanUp()
{    
    // TODO, implement proper cleanup
}

void OnTheFlyDG::setQuery(CTLQuery *query)
{
    cleanUp();
    this->query = query;
    delete[] working_marking.marking();
    delete[] query_marking.marking();
    working_marking.setMarking  (net->makeInitialMarking());
    query_marking.setMarking    (net->makeInitialMarking());
    initial_config = createConfiguration(createMarking(working_marking), *query);
}

int OnTheFlyDG::configurationCount() const
{
    return _configurationCount;
}

int OnTheFlyDG::markingCount() const
{
    return _markingCount;
}

PetriConfig *OnTheFlyDG::createConfiguration(size_t t_marking, CTLQuery &t_query)
{
    auto& configs = trie.get_data(t_marking);
    for(PetriConfig* c : configs){
        if(c->query == &t_query)
            return c;
    }

    _configurationCount++;
    PetriConfig* newConfig = new PetriConfig();
    newConfig->marking = t_marking;
    newConfig->query = &t_query;
    configs.push_back(newConfig);
    return newConfig;
}



size_t OnTheFlyDG::createMarking(Marking& t_marking){
    size_t sum = 0;
    bool allsame = true;
    uint32_t val = 0;
    uint32_t active = 0;
    uint32_t last = 0;
    markingStats(t_marking.marking(), sum, allsame, val, active, last);
    unsigned char type = encoder.getType(sum, active, allsame, val);
    size_t length = encoder.encode(t_marking.marking(), type);
    binarywrapper_t w = binarywrapper_t(encoder.scratchpad().raw(), length*8);
    auto tit = trie.insert(w);
    if(tit.first){
        _markingCount++;
    }
	return tit.second;
}

Edge* OnTheFlyDG::newEdge(Configuration &t_source)
{
    size_t n = edge_alloc.next(0);
    Edge* e = &edge_alloc[n];
    e->source = &t_source;
    return e;
}

void OnTheFlyDG::markingStats(const uint32_t* marking, size_t& sum, 
        bool& allsame, uint32_t& val, uint32_t& active, uint32_t& last)
{
    uint32_t cnt = 0;

    for (uint32_t i = 0; i < n_places; i++)
    {
        uint32_t old = val;
        if(marking[i] != 0)
        {
            ++cnt;
            last = std::max(last, i);
            val = std::max(marking[i], val);
            if(old != 0 && marking[i] != old) allsame = false;
            ++active;
            sum += marking[i];
        }
    }
}


}//PetriNet