# -*- generated by 1.0.12 -*-
import da
PatternExpr_192 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern196_')])
PatternExpr_199 = da.pat.BoundPattern('_BoundPattern200_')
PatternExpr_224 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('abort'), da.pat.BoundPattern('_BoundPattern229_')])
PatternExpr_232 = da.pat.BoundPattern('_BoundPattern233_')
PatternExpr_254 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern259_')])
PatternExpr_262 = da.pat.FreePattern('c')
PatternExpr_299 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern304_')])
PatternExpr_307 = da.pat.BoundPattern('_BoundPattern308_')
PatternExpr_336 = da.pat.TuplePattern([da.pat.ConstantPattern('done'), da.pat.BoundPattern('_BoundPattern339_')])
PatternExpr_342 = da.pat.BoundPattern('_BoundPattern343_')
PatternExpr_234 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern240_')]), da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('abort'), da.pat.BoundPattern('_BoundPattern246_')])])
PatternExpr_309 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern315_')]), da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern321_')])])
PatternExpr_344 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern350_')]), da.pat.TuplePattern([da.pat.ConstantPattern('done'), da.pat.BoundPattern('_BoundPattern354_')])])
PatternExpr_406 = da.pat.TuplePattern([da.pat.ConstantPattern('prepare'), da.pat.FreePattern('tid')])
PatternExpr_413 = da.pat.FreePattern('coord')
PatternExpr_444 = da.pat.TuplePattern([da.pat.ConstantPattern('abort'), da.pat.FreePattern('tid')])
PatternExpr_457 = da.pat.TuplePattern([da.pat.ConstantPattern('commit'), da.pat.FreePattern('tid')])
PatternExpr_464 = da.pat.FreePattern('coord')
PatternExpr_519 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_524 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.FreePattern(None)]), da.pat.TuplePattern([da.pat.ConstantPattern('done')])])
_config_object = {}
import sys
from random import randint

class Coordinator(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._CoordinatorReceivedEvent_0 = []
        self._CoordinatorReceivedEvent_1 = []
        self._CoordinatorReceivedEvent_2 = []
        self._CoordinatorReceivedEvent_3 = []
        self._CoordinatorReceivedEvent_4 = []
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_0', PatternExpr_192, sources=[PatternExpr_199], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_1', PatternExpr_224, sources=[PatternExpr_232], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_2', PatternExpr_254, sources=[PatternExpr_262], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_3', PatternExpr_299, sources=[PatternExpr_307], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_4', PatternExpr_336, sources=[PatternExpr_342], destinations=None, timestamps=None, record_history=True, handlers=[])])

    def setup(self, tid, cohorts, **rest_593):
        super().setup(tid=tid, cohorts=cohorts, **rest_593)
        self._state.tid = tid
        self._state.cohorts = cohorts
        pass

    def run(self):
        self.to_commit()
        self.send(('done',), to=self._state.cohorts)
        self.output('terminating')

    def to_commit(self):
        self.send(('prepare', self._state.tid), to=self._state.cohorts)
        super()._label('_st_label_183', block=False)
        c = None

        def UniversalOpExpr_184():
            nonlocal c
            for c in self._state.cohorts:

                def ExistentialOpExpr_190(c):
                    for (_, (_, _, _BoundPattern207_), (_ConstantPattern209_, _, _BoundPattern212_)) in self._CoordinatorReceivedEvent_0:
                        if (_BoundPattern207_ == c):
                            if (_ConstantPattern209_ == 'vote'):
                                if (_BoundPattern212_ == self._state.tid):
                                    if True:
                                        return True
                    return False
                if (not ExistentialOpExpr_190(c=c)):
                    return False
            return True
        _st_label_183 = 0
        while (_st_label_183 == 0):
            _st_label_183 += 1
            if UniversalOpExpr_184():
                _st_label_183 += 1
            else:
                super()._label('_st_label_183', block=True)
                _st_label_183 -= 1
        c = None

        def ExistentialOpExpr_217():
            nonlocal c
            for c in self._state.cohorts:
                if PatternExpr_234.match_iter(self._CoordinatorReceivedEvent_1, _BoundPattern240_=c, _BoundPattern246_=self._state.tid, SELF_ID=self._id):
                    return True
            return False
        if ExistentialOpExpr_217():
            s = {c for c in self._state.cohorts for (_, (_, _, _FreePattern270_), (_ConstantPattern272_, _ConstantPattern274_, _BoundPattern276_)) in self._CoordinatorReceivedEvent_2 if (_FreePattern270_ == c) if (_ConstantPattern272_ == 'vote') if (_ConstantPattern274_ == 'ready') if (_BoundPattern276_ == self._state.tid)}
            self.send(('abort', self._state.tid), to=s)
            self.abort(self._state.tid)
        c = None

        def UniversalOpExpr_292():
            nonlocal c
            for c in self._state.cohorts:
                if (not PatternExpr_309.match_iter(self._CoordinatorReceivedEvent_3, _BoundPattern315_=c, _BoundPattern321_=self._state.tid, SELF_ID=self._id)):
                    return False
            return True
        if UniversalOpExpr_292():
            self.send(('commit', self._state.tid), to=self._state.cohorts)
            super()._label('_st_label_328', block=False)
            c = None

            def UniversalOpExpr_329():
                nonlocal c
                for c in self._state.cohorts:
                    if (not PatternExpr_344.match_iter(self._CoordinatorReceivedEvent_4, _BoundPattern350_=c, _BoundPattern354_=self._state.tid, SELF_ID=self._id)):
                        return False
                return True
            _st_label_328 = 0
            while (_st_label_328 == 0):
                _st_label_328 += 1
                if UniversalOpExpr_329():
                    _st_label_328 += 1
                else:
                    super()._label('_st_label_328', block=True)
                    _st_label_328 -= 1
            self.commit(self._state.tid)

    def abort(self, tid):
        self.output(('abort:' + str(tid)))

    def commit(self, tid):
        self.output(('commit:' + str(tid)))

class Cohort(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._CohortReceivedEvent_3 = []
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_0', PatternExpr_406, sources=[PatternExpr_413], destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_405]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_1', PatternExpr_444, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_443]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_2', PatternExpr_457, sources=[PatternExpr_464], destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_456]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_3', PatternExpr_519, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

    def setup(self, failure_rate, **rest_593):
        super().setup(failure_rate=failure_rate, **rest_593)
        self._state.failure_rate = failure_rate
        pass

    def run(self):
        super()._label('_st_label_516', block=False)
        _st_label_516 = 0
        while (_st_label_516 == 0):
            _st_label_516 += 1
            if PatternExpr_524.match_iter(self._CohortReceivedEvent_3, SELF_ID=self._id):
                _st_label_516 += 1
            else:
                super()._label('_st_label_516', block=True)
                _st_label_516 -= 1

    def prepared(self, tid):
        return (randint(0, 100) > self._state.failure_rate)

    def ready(self, tid):
        self.output(('ready:' + str(tid)))

    def abort(self, tid):
        self.output(('abort:' + str(tid)))

    def commit(self, tid):
        self.output(('commit:' + str(tid)))

    def _Cohort_handler_405(self, tid, coord):
        if self.prepared(tid):
            self.send(('vote', 'ready', tid), to=coord)
            self.ready(tid)
        else:
            self.send(('vote', 'abort', tid), to=coord)
            self.abort(tid)
    _Cohort_handler_405._labels = None
    _Cohort_handler_405._notlabels = None

    def _Cohort_handler_443(self, tid):
        self.abort(tid)
    _Cohort_handler_443._labels = None
    _Cohort_handler_443._notlabels = None

    def _Cohort_handler_456(self, tid, coord):
        self.send(('done', tid), to=coord)
        self.commit(tid)
    _Cohort_handler_456._labels = None
    _Cohort_handler_456._notlabels = None

class Node_(da.NodeProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._events.extend([])

    def run(self):
        ncohorts = (int(sys.argv[1]) if (len(sys.argv) > 1) else 10)
        fail_rate = (int(sys.argv[2]) if (len(sys.argv) > 2) else 10)
        cohorts = self.new(Cohort, (fail_rate,), num=ncohorts)
        coordinators = self.new(Coordinator, ('001', cohorts), num=1)
        self._start((cohorts | coordinators))
