# -*- generated by 1.0.12 -*-
import da
PatternExpr_188 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.FreePattern(None)])
PatternExpr_194 = da.pat.BoundPattern('_BoundPattern195_')
PatternExpr_218 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('aborting')])
PatternExpr_225 = da.pat.BoundPattern('_BoundPattern226_')
PatternExpr_246 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready')])
PatternExpr_253 = da.pat.FreePattern('c')
PatternExpr_286 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready')])
PatternExpr_293 = da.pat.BoundPattern('_BoundPattern294_')
PatternExpr_319 = da.pat.ConstantPattern('done')
PatternExpr_323 = da.pat.BoundPattern('_BoundPattern324_')
PatternExpr_227 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern233_')]), da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('aborting')])])
PatternExpr_295 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern301_')]), da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready')])])
PatternExpr_325 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern331_')]), da.pat.ConstantPattern('done')])
PatternExpr_373 = da.pat.ConstantPattern('prepare')
PatternExpr_377 = da.pat.FreePattern('coord')
PatternExpr_403 = da.pat.ConstantPattern('abort')
PatternExpr_412 = da.pat.ConstantPattern('commit')
PatternExpr_416 = da.pat.FreePattern('coord')
_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_188, sources=[PatternExpr_194], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_1', PatternExpr_218, sources=[PatternExpr_225], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_2', PatternExpr_246, sources=[PatternExpr_253], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_3', PatternExpr_286, sources=[PatternExpr_293], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_4', PatternExpr_319, sources=[PatternExpr_323], destinations=None, timestamps=None, record_history=True, handlers=[])])

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

    def run(self):
        self.to_commit()
        self.output('terminating')

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

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

                def ExistentialOpExpr_186(c):
                    for (_, (_, _, _BoundPattern202_), (_ConstantPattern204_, _)) in self._CoordinatorReceivedEvent_0:
                        if (_BoundPattern202_ == c):
                            if (_ConstantPattern204_ == 'vote'):
                                if True:
                                    return True
                    return False
                if (not ExistentialOpExpr_186(c=c)):
                    return False
            return True
        _st_label_179 = 0
        while (_st_label_179 == 0):
            _st_label_179 += 1
            if UniversalOpExpr_180():
                _st_label_179 += 1
            else:
                super()._label('_st_label_179', block=True)
                _st_label_179 -= 1
        c = None

        def ExistentialOpExpr_211():
            nonlocal c
            for c in self._state.cohorts:
                if PatternExpr_227.match_iter(self._CoordinatorReceivedEvent_1, _BoundPattern233_=c, SELF_ID=self._id):
                    return True
            return False
        if ExistentialOpExpr_211():
            s = {c for c in self._state.cohorts for (_, (_, _, _FreePattern261_), (_ConstantPattern263_, _ConstantPattern265_)) in self._CoordinatorReceivedEvent_2 if (_FreePattern261_ == c) if (_ConstantPattern263_ == 'vote') if (_ConstantPattern265_ == 'ready')}
            self.send('abort', to=s)
            self.abort()
        c = None

        def UniversalOpExpr_279():
            nonlocal c
            for c in self._state.cohorts:
                if (not PatternExpr_295.match_iter(self._CoordinatorReceivedEvent_3, _BoundPattern301_=c, SELF_ID=self._id)):
                    return False
            return True
        if UniversalOpExpr_279():
            self.send('commit', to=self._state.cohorts)
            super()._label('_st_label_311', block=False)
            c = None

            def UniversalOpExpr_312():
                nonlocal c
                for c in self._state.cohorts:
                    if (not PatternExpr_325.match_iter(self._CoordinatorReceivedEvent_4, _BoundPattern331_=c, SELF_ID=self._id)):
                        return False
                return True
            _st_label_311 = 0
            while (_st_label_311 == 0):
                _st_label_311 += 1
                if UniversalOpExpr_312():
                    _st_label_311 += 1
                else:
                    super()._label('_st_label_311', block=True)
                    _st_label_311 -= 1
            self.commit()

    def abort(self):
        self.output('abort')

    def commit(self):
        self.output('commit')

class Cohort(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_0', PatternExpr_373, sources=[PatternExpr_377], destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_372]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_1', PatternExpr_403, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_402]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_2', PatternExpr_412, sources=[PatternExpr_416], destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_411])])

    def setup(self, failure_rate, **rest_515):
        super().setup(failure_rate=failure_rate, **rest_515)
        self._state.failure_rate = failure_rate
        self._state.terminate = False

    def run(self):
        super()._label('_st_label_455', block=False)
        _st_label_455 = 0
        while (_st_label_455 == 0):
            _st_label_455 += 1
            if self._state.terminate:
                _st_label_455 += 1
            else:
                super()._label('_st_label_455', block=True)
                _st_label_455 -= 1

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

    def ready(self):
        self.output('ready')

    def abort(self):
        self.output('abort')
        self._state.terminate = True

    def commit(self):
        self.output('commit')
        self._state.terminate = True

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

    def _Cohort_handler_402(self):
        self.abort()
    _Cohort_handler_402._labels = None
    _Cohort_handler_402._notlabels = None

    def _Cohort_handler_411(self, coord):
        self.send('done', to=coord)
        self.commit()
    _Cohort_handler_411._labels = None
    _Cohort_handler_411._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, (cohorts,), num=1)
        self._start((cohorts | coordinators))
