def test_run3(self):
     """Doctest case with propage recovered singles"""
     d = ContextAggregator(
         config={
             "propagation_mode": ContextAggregator.AGGREGATION_MODE,
             "max_tau": 1,
             "propagate_recovered_singles": True,
         }
     )
     # d.initialize() # Always execute initialize before newly receive data
     # Emulating receive data from neighbors
     d.receive(1, set([Context(value=1.0, cohorts=[0, 1, 2])]))
     d.receive(2, set([Context(value=2.0, cohorts=[0])]))
     d.receive(3, set([Context(value=3.0, cohorts=[1])]))
     d.receive(4, set([Context(value=7.0, cohorts=[9], hopcount=Context.SPECIAL_CONTEXT)]))
     # Emulating accumulated contexts
     context_db = set(
         [
             Context(value=1.0, cohorts=[2, 4, 5, 3]),
             Context(value=1.0, cohorts=[5, 6]),
             Context(value=7.0, cohorts=[7, 8]),
         ]
     )
     d.set_database(singles=set([]), aggregates=context_db, timestamp=10)
     d.run_dataflow(timestamp=10)
     # Emulating newly found singles and aggregates from database
     self.assertTrue(same(d.get_database_singles(timestamp=10), [[0, 1, 2, 9], []]))
     self.assertTrue(same(d.get_database_aggregates(timestamp=10), [[7, 8], [3, 4, 5], [6, 5]]))
     # Emulating the disaggregation process
     self.assertTrue(same(d.get_singles(timestamp=10), [[0, 1, 2, 9], []]))
     self.assertTrue(same(d.get_primes(timestamp=10), [[7, 8]]))
     self.assertTrue(same(d.get_non_primes(timestamp=10), [[3, 4, 5], [5, 6]]))
     self.assertTrue(same(d.get_selected_non_primes(timestamp=10), [[3, 4, 5]]))
     self.assertTrue(d.get_new_aggregate().get_cohorts_as_set() == set([0, 1, 2, 3, 4, 5, 7, 8, 9]))
     self.assertTrue(same(d.get_filtered_singles(), [[0, 1, 2, 9], []]))
 def test_run1(self):
     """Only [4,5] is prime
     All the others are singles
     """
     d = ContextAggregator()
     d.receive(1, set([Context(value=1.0, cohorts=[0, 1, 2])]))
     d.receive(2, set([Context(value=2.0, cohorts=[0])]))
     d.set_database(
         set([Context(value=1.0, cohorts=[0, 1, 2, 3, 4, 5]), Context(value=2.0, cohorts=[1])]),
         set([Context(value=1.0, cohorts=[2, 3])]),
     )
     d.run_dataflow()
     self.assertTrue(same(d.get_singles(), [[0, 1, 2, 3], []]))
     self.assertTrue(same(d.get_primes(), [[], [4, 5]]))
    def test_run_dataflow_single_only(self):
        # at timestamp = 0
        config = {ContextAggregator.PM:ContextAggregator.SINGLE_ONLY_MODE}
        c0 = ContextAggregator(id=0, config=config)
        c1 = ContextAggregator(id=1, config=config)
        c2 = ContextAggregator(id=2, config=config)

        # First round
        ## compute
        c0.process_to_set_output(neighbors=[1], timestamp = 0)
        c1.process_to_set_output(neighbors=[0,2], timestamp = 0)
        c2.process_to_set_output(neighbors=[1], timestamp = 0)

        ## communication
        r0_1 = c0.send(neighbor=1, timestamp=0)
        r1_0 = c1.send(neighbor=0, timestamp=0)
        r1_2 = c1.send(neighbor=2, timestamp=0)
        r2_1 = c2.send(neighbor=1, timestamp=0)

        c1.receive(from_node=0, contexts=r0_1[1], timestamp=0)
        c0.receive(from_node=1, contexts=r1_0[0], timestamp=0)
        c2.receive(from_node=1, contexts=r1_2[2], timestamp=0)
        c1.receive(from_node=2, contexts=r2_1[1], timestamp=0)

        r1_0 = c1.get_received_data(from_node=0)
        r0_1 = c0.get_received_data(from_node=1)
        r1_2 = c1.get_received_data(from_node=2)
        r2_1 = c2.get_received_data(from_node=1)

        self.assertTrue(contexts_to_standard(r1_0), [[0],[]])
        self.assertTrue(contexts_to_standard(r0_1), [[1],[]])
        self.assertTrue(contexts_to_standard(r1_2), [[2],[]])
        self.assertTrue(contexts_to_standard(r2_1), [[1],[]])

        # Second round
        ## compute
        o0 = c0.process_to_set_output(neighbors=[1], timestamp = 0)
        o1 = c1.process_to_set_output(neighbors=[0,2], timestamp = 0)
        o2 = c2.process_to_set_output(neighbors=[1], timestamp = 0)

        self.assertTrue(contexts_to_standard(c0.get_singles(timestamp=0)) == [[0,1],[]])
        self.assertTrue(contexts_to_standard(c1.get_singles(timestamp=0)) == [[0,1,2],[]])
        self.assertTrue(contexts_to_standard(c2.get_singles(timestamp=0)) == [[1,2],[]])

        self.assertTrue(o0 == {1: [[], []]})
        self.assertTrue(o1 == {0: [[2], []], 2:[[0], []]})
        self.assertTrue(o2 == {1: [[], []]})

        ## communication
        self.assertTrue(c0.is_nothing_to_send())
        self.assertTrue(c2.is_nothing_to_send())

        r1_0 = c1.send(neighbor=0, timestamp=0)
        r1_2 = c1.send(neighbor=2, timestamp=0)
        self.assertTrue(contexts_to_standard(r1_0[0]) == [[2],[]])
        self.assertTrue(contexts_to_standard(r1_2[2]) == [[0],[]])

        c0.receive(from_node=1, contexts=r1_0[0], timestamp=0)
        c2.receive(from_node=1, contexts=r1_2[2], timestamp=0)

        r0_1 = c0.get_received_data(from_node=1)
        r2_1 = c2.get_received_data(from_node=1)

        self.assertTrue(contexts_to_standard(r0_1) == [[2],[]])
        self.assertTrue(contexts_to_standard(r2_1) == [[0],[]])

        # Third iteration
        o0 = c0.process_to_set_output(neighbors=[1], timestamp = 0)
        o1 = c1.process_to_set_output(neighbors=[0,2], timestamp = 0)
        o2 = c2.process_to_set_output(neighbors=[1], timestamp = 0)

        self.assertTrue(contexts_to_standard(c0.get_singles(timestamp=0)) == [[0,1,2],[]])
        self.assertTrue(contexts_to_standard(c1.get_singles(timestamp=0)) == [[0,1,2],[]])
        self.assertTrue(contexts_to_standard(c2.get_singles(timestamp=0)) == [[0,1,2],[]])

        self.assertTrue(o0 == {1: [[], []]})
        #self.assertTrue(o1 == {0: [[], []], 2:[[], []]})
        self.assertTrue(o1 == {}) # {0: [[], []], 2:[[], []]})
        self.assertTrue(o2 == {1: [[], []]})

        self.assertTrue(c0.is_nothing_to_send())
        self.assertTrue(c1.is_nothing_to_send())
        self.assertTrue(c2.is_nothing_to_send())