# -*- generated by 1.0.2 -*-
import da
PatternExpr_190 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern194_')])
PatternExpr_197 = da.pat.BoundPattern('_BoundPattern198_')
PatternExpr_222 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('abort'), da.pat.BoundPattern('_BoundPattern227_')])
PatternExpr_230 = da.pat.BoundPattern('_BoundPattern231_')
PatternExpr_252 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern257_')])
PatternExpr_260 = da.pat.FreePattern('c')
PatternExpr_296 = da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern301_')])
PatternExpr_304 = da.pat.BoundPattern('_BoundPattern305_')
PatternExpr_332 = da.pat.TuplePattern([da.pat.ConstantPattern('done'), da.pat.BoundPattern('_BoundPattern335_')])
PatternExpr_338 = da.pat.BoundPattern('_BoundPattern339_')
PatternExpr_232 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern238_')]), da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('abort'), da.pat.BoundPattern('_BoundPattern244_')])])
PatternExpr_306 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern312_')]), da.pat.TuplePattern([da.pat.ConstantPattern('vote'), da.pat.ConstantPattern('ready'), da.pat.BoundPattern('_BoundPattern318_')])])
PatternExpr_340 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern346_')]), da.pat.TuplePattern([da.pat.ConstantPattern('done'), da.pat.BoundPattern('_BoundPattern350_')])])
PatternExpr_401 = da.pat.TuplePattern([da.pat.ConstantPattern('prepare'), da.pat.FreePattern('tid')])
PatternExpr_408 = da.pat.FreePattern('coord')
PatternExpr_437 = da.pat.TuplePattern([da.pat.ConstantPattern('abort'), da.pat.FreePattern('tid')])
PatternExpr_450 = da.pat.TuplePattern([da.pat.ConstantPattern('commit'), da.pat.FreePattern('tid')])
PatternExpr_457 = da.pat.FreePattern('coord')
PatternExpr_511 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_516 = 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_190, sources=[PatternExpr_197], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_1', PatternExpr_222, sources=[PatternExpr_230], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_2', PatternExpr_252, sources=[PatternExpr_260], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_3', PatternExpr_296, sources=[PatternExpr_304], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CoordinatorReceivedEvent_4', PatternExpr_332, sources=[PatternExpr_338], destinations=None, timestamps=None, record_history=True, handlers=[])])

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

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

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

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

                def ExistentialOpExpr_188(c):
                    for (_, (_, _, _BoundPattern205_), (_ConstantPattern207_, _, _BoundPattern210_)) in self._CoordinatorReceivedEvent_0:
                        if (_BoundPattern205_ == c):
                            if (_ConstantPattern207_ == 'vote'):
                                if (_BoundPattern210_ == self._state.tid):
                                    if True:
                                        return True
                    return False
                if (not ExistentialOpExpr_188(c=c)):
                    return False
            return True
        _st_label_181 = 0
        while (_st_label_181 == 0):
            _st_label_181 += 1
            if UniversalOpExpr_182():
                _st_label_181 += 1
            else:
                super()._label('_st_label_181', block=True)
                _st_label_181 -= 1
        c = None

        def ExistentialOpExpr_215():
            nonlocal c
            for c in self._state.cohorts:
                if PatternExpr_232.match_iter(self._CoordinatorReceivedEvent_1, _BoundPattern238_=c, _BoundPattern244_=self._state.tid, SELF_ID=self._id):
                    return True
            return False
        if ExistentialOpExpr_215():
            s = {c for c in self._state.cohorts for (_, (_, _, _FreePattern268_), (_ConstantPattern270_, _ConstantPattern272_, _BoundPattern274_)) in self._CoordinatorReceivedEvent_2 if (_FreePattern268_ == c) if (_ConstantPattern270_ == 'vote') if (_ConstantPattern272_ == 'ready') if (_BoundPattern274_ == self._state.tid)}
            self.send(('abort', self._state.tid), s)
            self.abort(self._state.tid)
        c = None

        def UniversalOpExpr_289():
            nonlocal c
            for c in self._state.cohorts:
                if (not PatternExpr_306.match_iter(self._CoordinatorReceivedEvent_3, _BoundPattern312_=c, _BoundPattern318_=self._state.tid, SELF_ID=self._id)):
                    return False
            return True
        if UniversalOpExpr_289():
            self.send(('commit', self._state.tid), self._state.cohorts)
            super()._label('_st_label_324', block=False)
            c = None

            def UniversalOpExpr_325():
                nonlocal c
                for c in self._state.cohorts:
                    if (not PatternExpr_340.match_iter(self._CoordinatorReceivedEvent_4, _BoundPattern346_=c, _BoundPattern350_=self._state.tid, SELF_ID=self._id)):
                        return False
                return True
            _st_label_324 = 0
            while (_st_label_324 == 0):
                _st_label_324 += 1
                if UniversalOpExpr_325():
                    _st_label_324 += 1
                else:
                    super()._label('_st_label_324', block=True)
                    _st_label_324 -= 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_401, sources=[PatternExpr_408], destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_400]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_1', PatternExpr_437, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_436]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_2', PatternExpr_450, sources=[PatternExpr_457], destinations=None, timestamps=None, record_history=None, handlers=[self._Cohort_handler_449]), da.pat.EventPattern(da.pat.ReceivedEvent, '_CohortReceivedEvent_3', PatternExpr_511, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

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

    def run(self):
        super()._label('_st_label_508', block=False)
        _st_label_508 = 0
        while (_st_label_508 == 0):
            _st_label_508 += 1
            if PatternExpr_516.match_iter(self._CohortReceivedEvent_3, SELF_ID=self._id):
                _st_label_508 += 1
            else:
                super()._label('_st_label_508', block=True)
                _st_label_508 -= 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_400(self, tid, coord):
        if self.prepared(tid):
            self.send(('vote', 'ready', tid), coord)
            self.ready(tid)
        else:
            self.send(('vote', 'abort', tid), coord)
            self.abort(tid)
    _Cohort_handler_400._labels = None
    _Cohort_handler_400._notlabels = None

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

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