# -*- generated by 1.0.6 -*-
import da
PatternExpr_191 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern195_')])
PatternExpr_198 = da.pat.BoundPattern('_BoundPattern199_')
PatternExpr_223 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('abort'), da.pat.BoundPattern('_BoundPattern228_')])
PatternExpr_231 = da.pat.BoundPattern('_BoundPattern232_')
PatternExpr_253 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern258_')])
PatternExpr_261 = da.pat.FreePattern('c')
PatternExpr_298 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern303_')])
PatternExpr_306 = da.pat.BoundPattern('_BoundPattern307_')
PatternExpr_335 = da.pat.TuplePattern([da.pat.ConstantPattern('done'), da.pat.BoundPattern('_BoundPattern338_')])
PatternExpr_341 = da.pat.BoundPattern('_BoundPattern342_')
PatternExpr_233 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern239_')]), da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('abort'), da.pat.BoundPattern('_BoundPattern245_')])])
PatternExpr_308 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern314_')]), da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern320_')])])
PatternExpr_343 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern349_')]), da.pat.TuplePattern([da.pat.ConstantPattern('done'), da.pat.BoundPattern('_BoundPattern353_')])])
PatternExpr_405 = da.pat.TuplePattern([da.pat.ConstantPattern('prepare'), da.pat.FreePattern('tid')])
PatternExpr_412 = da.pat.FreePattern('coord')
PatternExpr_443 = da.pat.TuplePattern([da.pat.ConstantPattern('abort'), da.pat.FreePattern('tid')])
PatternExpr_456 = da.pat.TuplePattern([da.pat.ConstantPattern('commit'), da.pat.FreePattern('tid')])
PatternExpr_463 = da.pat.FreePattern('coord')
PatternExpr_518 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_523 = 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_191, sources=[PatternExpr_198], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_1', PatternExpr_223, sources=[PatternExpr_231], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_2', PatternExpr_253, sources=[PatternExpr_261], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_3', PatternExpr_298, sources=[PatternExpr_306], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_4', PatternExpr_335, sources=[PatternExpr_341], destinations=None, timestamps=None, record_history=True, handlers=[])])

    def setup(self, tid, cohorts, **rest_592):
        super().setup(tid=tid, cohorts=cohorts, **rest_592)
        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_182', block=False)
        c = None

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

                def ExistentialOpExpr_189(c):
                    for (_, (_, _, _BoundPattern206_), (_ConstantPattern208_, _, _BoundPattern211_)) in self._CoordinatorReceivedEvent_0:
                        if (_BoundPattern206_ == c):
                            if (_ConstantPattern208_ == 'vote'):
                                if (_BoundPattern211_ == self._state.tid):
                                    if True:
                                        return True
                    return False
                if (not ExistentialOpExpr_189(c=c)):
                    return False
            return True
        _st_label_182 = 0
        while (_st_label_182 == 0):
            _st_label_182 += 1
            if UniversalOpExpr_183():
                _st_label_182 += 1
            else:
                super()._label('_st_label_182', block=True)
                _st_label_182 -= 1
        c = None

        def ExistentialOpExpr_216():
            nonlocal c
            for c in self._state.cohorts:
                if PatternExpr_233.match_iter(self._CoordinatorReceivedEvent_1, _BoundPattern239_=c, _BoundPattern245_=self._state.tid, SELF_ID=self._id):
                    return True
            return False
        if ExistentialOpExpr_216():
            s = {c for c in self._state.cohorts for (_, (_, _, _FreePattern269_), (_ConstantPattern271_, _ConstantPattern273_, _BoundPattern275_)) in self._CoordinatorReceivedEvent_2 if (_FreePattern269_ == c) if (_ConstantPattern271_ == 'vote') if (_ConstantPattern273_ == 'ready') if (_BoundPattern275_ == self._state.tid)}
            self.send(('abort', self._state.tid), to=s)
            self.abort(self._state.tid)
        c = None

        def UniversalOpExpr_291():
            nonlocal c
            for c in self._state.cohorts:
                if (not PatternExpr_308.match_iter(self._CoordinatorReceivedEvent_3, _BoundPattern314_=c, _BoundPattern320_=self._state.tid, SELF_ID=self._id)):
                    return False
            return True
        if UniversalOpExpr_291():
            self.send(('commit', self._state.tid), to=self._state.cohorts)
            super()._label('_st_label_327', block=False)
            c = None

            def UniversalOpExpr_328():
                nonlocal c
                for c in self._state.cohorts:
                    if (not PatternExpr_343.match_iter(self._CoordinatorReceivedEvent_4, _BoundPattern349_=c, _BoundPattern353_=self._state.tid, SELF_ID=self._id)):
                        return False
                return True
            _st_label_327 = 0
            while (_st_label_327 == 0):
                _st_label_327 += 1
                if UniversalOpExpr_328():
                    _st_label_327 += 1
                else:
                    super()._label('_st_label_327', block=True)
                    _st_label_327 -= 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_405, sources=[PatternExpr_412], destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_404]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_1', PatternExpr_443, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_442]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_2', PatternExpr_456, sources=[PatternExpr_463], destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_455]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_3', PatternExpr_518, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

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

    def run(self):
        super()._label('_st_label_515', block=False)
        _st_label_515 = 0
        while (_st_label_515 == 0):
            _st_label_515 += 1
            if PatternExpr_523.match_iter(self._CohortReceivedEvent_3, SELF_ID=self._id):
                _st_label_515 += 1
            else:
                super()._label('_st_label_515', block=True)
                _st_label_515 -= 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_404(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_404._labels = None
    _Cohort_handler_404._notlabels = None

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

    def _Cohort_handler_455(self, tid, coord):
        self.send(('done', tid), to=coord)
        self.commit(tid)
    _Cohort_handler_455._labels = None
    _Cohort_handler_455._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))
