# -*- generated by 1.0.9b3 -*-
import da
PatternExpr_182 = da.pat.TuplePattern([da.pat.ConstantPattern('Election'), da.pat.FreePattern('p')])
PatternExpr_207 = da.pat.TuplePattern([da.pat.ConstantPattern('Election'), da.pat.SelfPattern()])
PatternExpr_244 = da.pat.TuplePattern([da.pat.ConstantPattern('Leader'), da.pat.FreePattern('leader')])
PatternExpr_273 = da.pat.TuplePattern([da.pat.ConstantPattern('Leader'), da.pat.FreePattern(None)])
PatternExpr_214 = 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('Election'), da.pat.SelfPattern()])])
_config_object = {}
import sys

class P(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._PSentEvent_1 = []
        self._PReceivedEvent_3 = []
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_0', PatternExpr_182, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._P_handler_181]), da.pat.EventPattern(da.pat.SentEvent, '_PSentEvent_1', PatternExpr_207, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_2', PatternExpr_244, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._P_handler_243]), da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_3', PatternExpr_273, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

    def setup(self, left, **rest_356):
        super().setup(left=left, **rest_356)
        self._state.left = left
        self._state.leaderid = None

    def run(self):
        self.initiate()
        super()._label('_st_label_270', block=False)

        def ExistentialOpExpr_271():
            for (_, _, (_ConstantPattern287_, _)) in self._PReceivedEvent_3:
                if (_ConstantPattern287_ == 'Leader'):
                    if True:
                        return True
            return False
        _st_label_270 = 0
        while (_st_label_270 == 0):
            _st_label_270 += 1
            if ExistentialOpExpr_271():
                _st_label_270 += 1
            else:
                super()._label('_st_label_270', block=True)
                _st_label_270 -= 1
        self.output('Leader is', self._state.leaderid)

    def initiate(self):
        self.send(('Election', self._id), to=self._state.left)

    def _P_handler_181(self, p):
        if (p > self._id):
            self.send(('Election', p), to=self._state.left)
        if (p < self._id):
            if (not PatternExpr_214.match_iter(self._PSentEvent_1, SELF_ID=self._id)):
                self.send(('Election', self._id), to=self._state.left)
        if (p == self._id):
            self.send(('Leader', self._id), to=self._state.left)
    _P_handler_181._labels = None
    _P_handler_181._notlabels = None

    def _P_handler_243(self, leader):
        self._state.leaderid = leader
        if (not (leader == self._id)):
            self.send(('Leader', leader), to=self._state.left)
    _P_handler_243._labels = None
    _P_handler_243._notlabels = None

class Node_(da.NodeProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._events.extend([])
    _config_object = {'channel': 'fifo'}

    def run(self):
        n = (int(sys.argv[1]) if (len(sys.argv) > 1) else 10)
        ps = list(self.new(P, num=n))
        for (i, p) in enumerate(ps):
            self._setup({p}, (ps[((i + 1) if (i < (len(ps) - 1)) else 0)],))
        self._start(ps)
