# -*- generated by 1.0.2 -*-
import da
PatternExpr_181 = da.pat.TuplePattern([da.pat.ConstantPattern('Election'), da.pat.FreePattern('p')])
PatternExpr_205 = da.pat.TuplePattern([da.pat.ConstantPattern('Election'), da.pat.SelfPattern()])
PatternExpr_240 = da.pat.TuplePattern([da.pat.ConstantPattern('Leader'), da.pat.FreePattern('leader')])
PatternExpr_268 = da.pat.TuplePattern([da.pat.ConstantPattern('Leader'), da.pat.FreePattern(None)])
PatternExpr_212 = 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_181, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._P_handler_180]), da.pat.EventPattern(da.pat.SentEvent, '_PSentEvent_1', PatternExpr_205, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_2', PatternExpr_240, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._P_handler_239]), da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_3', PatternExpr_268, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

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

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

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

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

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

    def _P_handler_239(self, leader):
        self._state.leaderid = leader
        if (not (leader == self._id)):
            self.send(('Leader', leader), self._state.left)
    _P_handler_239._labels = None
    _P_handler_239._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)
