# -*- generated by 1.0.2 -*-
import da
PatternExpr_197 = da.pat.TuplePattern([da.pat.ConstantPattern('Value'), da.pat.FreePattern('V2'), da.pat.FreePattern(None)])
PatternExpr_229 = da.pat.TuplePattern([da.pat.ConstantPattern('Value'), da.pat.FreePattern(None), da.pat.FreePattern(None)])
PatternExpr_254 = da.pat.TuplePattern([da.pat.ConstantPattern('Value'), da.pat.FreePattern('V2'), da.pat.FreePattern(None)])
_config_object = {}
import sys

class P(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._PSentEvent_0 = []
        self._PReceivedEvent_1 = []
        self._PReceivedEvent_2 = []
        self._events.extend([da.pat.EventPattern(da.pat.SentEvent, '_PSentEvent_0', PatternExpr_197, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_1', PatternExpr_229, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_PReceivedEvent_2', PatternExpr_254, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

    def setup(self, ps, v, maxfail, **rest_353):
        super().setup(ps=ps, v=v, maxfail=maxfail, **rest_353)
        self._state.ps = ps
        self._state.v = v
        self._state.maxfail = maxfail
        self._state.V = {self._state.v}

    def run(self):
        for rnd in range(1, self._state.maxfail):

            def ExistentialOpExpr_195(v):
                for (_, _, (_ConstantPattern213_, V2, _)) in self._PSentEvent_0:
                    if (_ConstantPattern213_ == 'Value'):
                        if (self._state.v in V2):
                            return True
                return False
            self.send(('Value', {self._state.v for self._state.v in self._state.V if (not ExistentialOpExpr_195(v=self._state.v))}, self._id), self._state.ps)
            for attr in dir(self):
                if (attr.find('ReceivedEvent_') != (- 1)):
                    getattr(self, attr).clear()
            super()._label('_st_label_226', block=False)

            def ExistentialOpExpr_227():
                for (_, _, (_ConstantPattern244_, _, _)) in self._PReceivedEvent_1:
                    if (_ConstantPattern244_ == 'Value'):
                        if True:
                            return True
                return False
            _st_label_226 = 0
            while (_st_label_226 == 0):
                _st_label_226 += 1
                if ExistentialOpExpr_227():
                    _st_label_226 += 1
                else:
                    super()._label('_st_label_226', block=True)
                    _st_label_226 -= 1
            else:
                if (_st_label_226 != 2):
                    continue
            if (_st_label_226 != 2):
                break
            self._state.V |= {self._state.v for (_, _, (_ConstantPattern270_, V2, _)) in self._PReceivedEvent_2 if (_ConstantPattern270_ == 'Value') for self._state.v in V2}
        x = min(self._state.V)
        self.output(x)

class Node_(da.NodeProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._events.extend([])

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