Esempio n. 1
0
    def test_json_back_and_forth(self) -> None:
        """ test converting back and forth from and to json """
        # GIVEN
        input_dict = TestSyncStartInputValidation.get_default_inputs()

        # WHEN
        sync_start = SyncStart(**input_dict)

        # THEN converting back and forth should in the end give the same result
        sync_start_dict = sync_start.to_json()
        sync_start_from_json = SyncStart.from_json(sync_start_dict=sync_start_dict)
        self.assertDictEqual(sync_start_dict, sync_start_from_json.to_json())
    def test_no_shift_possible(self) -> None:
        """ Test finding the shifts for a schedule without an unambiguous shift"""
        # GIVEN
        sync_start = SyncStart(from_id="sg1", to_id="sg2")
        fts = FixedTimeSchedule(greenyellow_intervals=dict(
            sg1=[GreenYellowInterval(start_greenyellow=10, end_greenyellow=30),
                 GreenYellowInterval(start_greenyellow=40, end_greenyellow=50),
                 GreenYellowInterval(start_greenyellow=60, end_greenyellow=80)],
            sg2=[GreenYellowInterval(start_greenyellow=10, end_greenyellow=30),
                 GreenYellowInterval(start_greenyellow=40, end_greenyellow=50),
                 GreenYellowInterval(start_greenyellow=60, end_greenyellow=80)]),
            period=100)

        # swap two intervals (we do this after initialization as otherwise we would get a ValueError (not correct order
        #  of greenyellow intervals) when initializing the FixedTimeSchedule
        fts._greenyellow_intervals["sg2"][:2] = reversed(fts._greenyellow_intervals["sg2"][:2])

        # WHEN
        matches = find_other_sg_relation_matches(other_relation=sync_start, fts=fts, index_from=0)
        matches2 = find_other_sg_relation_matches(other_relation=sync_start, fts=fts, index_from=1)
        matches3 = find_other_sg_relation_matches(other_relation=sync_start, fts=fts, index_from=2)

        # THEN
        self.assertListEqual(matches, [0, 1, 0])
        self.assertListEqual(matches2, [1, 0, 0])
        self.assertListEqual(matches3, [0, 0, 1])
Esempio n. 3
0
    def test_successful_validation(self) -> None:
        """ Test initializing SyncStart object with correct input """
        # GIVEN
        input_dict = TestSyncStartInputValidation.get_default_inputs()

        # WHEN
        SyncStart(**input_dict)
Esempio n. 4
0
 def test_non_unique_ids(self) -> None:
     """ Test giving two identical ids to initialize a SyncStart """
     # GIVEN
     input_dict = TestSyncStartInputValidation.get_default_inputs()
     input_dict["from_id"] = "1"
     input_dict["to_id"] = "1"
     with self.assertRaises(ValueError):
         SyncStart(**input_dict)
Esempio n. 5
0
    def test_wrong_datatype_for_ids(self) -> None:
        """ Test giving wrong datatype to SyncStart for ids """
        # GIVEN
        input_dict = TestSyncStartInputValidation.get_default_inputs()
        input_dict["from_id"] = 1
        input_dict["to_id"] = 2

        # WHEN initializing the synchronous start
        sync_start = SyncStart(**input_dict)

        # Should not give an error (datatype is converted to string); note that from_id and to_id might have swapped;
        # this is done to store this SyncStart in an unambiguous manner.
        self.assertSetEqual({sync_start.from_id, sync_start.to_id}, {"1", "2"})
Esempio n. 6
0
 def get_default_inputs() -> Dict:
     """ Function to get default (valid) inputs for Intersection() """
     signalgroups = [SignalGroup(id=f"sg{i+1}", traffic_lights=[TrafficLight(capacity=1800, lost_time=1)],
                                 min_greenyellow=10, max_greenyellow=80, min_red=10, max_red=80) for i in range(6)]
     conflicts = [Conflict(id1="sg1", id2="sg2", setup12=1, setup21=2),
                  Conflict(id1="sg1", id2="sg6", setup12=1, setup21=2),
                  Conflict(id1="sg2", id2="sg6", setup12=1, setup21=2)]
     sync_starts = [SyncStart(from_id="sg1", to_id="sg3")]
     offsets = [Offset(from_id="sg1", to_id="sg4", seconds=10)]
     periodic_orders = [PeriodicOrder(order=["sg1", "sg2", "sg6"])]
     greenyellow_leads = [GreenyellowLead(from_id="sg1", to_id="sg5", min_seconds=1, max_seconds=10)]
     greenyellow_trails = [GreenyellowTrail(from_id="sg5", to_id="sg1", min_seconds=2, max_seconds=8)]
     return dict(signalgroups=signalgroups, conflicts=conflicts, sync_starts=sync_starts,
                 offsets=offsets, greenyellow_leads=greenyellow_leads, greenyellow_trails=greenyellow_trails,
                 periodic_orders=periodic_orders)
    def test_shift_of_one(self) -> None:
        """ Test finding a shift of one """
        # GIVEN
        sync_start = SyncStart(from_id="sg1", to_id="sg2")
        fts = FixedTimeSchedule(greenyellow_intervals=dict(
            sg1=[GreenYellowInterval(start_greenyellow=10, end_greenyellow=40),
                 GreenYellowInterval(start_greenyellow=50, end_greenyellow=70)],
            sg2=[GreenYellowInterval(start_greenyellow=50, end_greenyellow=80),
                 GreenYellowInterval(start_greenyellow=10, end_greenyellow=30)]),
            period=100)

        # WHEN
        shift = get_other_sg_relation_shift(other_relation=sync_start, fts=fts)

        # THEN
        self.assertEqual(shift, 1)
    def test_shift_one(self) -> None:
        """ Test finding a shift of one """
        # GIVEN
        sync_start = SyncStart(from_id="sg1", to_id="sg2")
        fts = FixedTimeSchedule(greenyellow_intervals=dict(
            sg1=[GreenYellowInterval(start_greenyellow=10, end_greenyellow=40),
                 GreenYellowInterval(start_greenyellow=50, end_greenyellow=70)],
            sg2=[GreenYellowInterval(start_greenyellow=50, end_greenyellow=60),
                 GreenYellowInterval(start_greenyellow=10, end_greenyellow=30)]),
            period=100)

        # WHEN
        matches = find_other_sg_relation_matches(other_relation=sync_start, fts=fts, index_from=0)
        matches2 = find_other_sg_relation_matches(other_relation=sync_start, fts=fts, index_from=1)

        # THEN
        self.assertListEqual(matches, [0, 1])
        self.assertListEqual(matches2, [1, 0])
    def test_correct_sync_starts(self) -> None:
        """
        Test that validation of correct synchronous start passes.
        :return:
        """
        # GIVEN
        sync_start = SyncStart(from_id="sg3", to_id="sg4")
        fts = FixedTimeSchedule(greenyellow_intervals=dict(
            sg1=[GreenYellowInterval(start_greenyellow=15, end_greenyellow=35)],
            sg2=[GreenYellowInterval(start_greenyellow=45, end_greenyellow=65)],
            sg3=[GreenYellowInterval(start_greenyellow=10, end_greenyellow=40),
                 GreenYellowInterval(start_greenyellow=50, end_greenyellow=70)],
            sg4=[GreenYellowInterval(start_greenyellow=10, end_greenyellow=30),
                 GreenYellowInterval(start_greenyellow=50, end_greenyellow=60)]),
            period=100)
        signalgroup3 = TestFTSOtherSGRelationValidation.get_default_signalgroup(name="sg3")
        signalgroup4 = TestFTSOtherSGRelationValidation.get_default_signalgroup(name="sg4")
        intersection = TestFTSOtherSGRelationValidation.get_default_intersection(
            additional_signalgroups=[signalgroup3, signalgroup4], sync_starts=[sync_start])

        # WHEN validating
        validate_other_sg_relations(intersection=intersection, fts=fts)
    def test_no_shift_possible(self) -> None:
        """ Test finding no shift is possible for a schedule without an unambiguous shift"""
        # GIVEN
        sync_start = SyncStart(from_id="sg1", to_id="sg2")
        fts = FixedTimeSchedule(greenyellow_intervals=dict(
            sg1=[GreenYellowInterval(start_greenyellow=10, end_greenyellow=30),
                 GreenYellowInterval(start_greenyellow=40, end_greenyellow=50),
                 GreenYellowInterval(start_greenyellow=60, end_greenyellow=80)],
            sg2=[GreenYellowInterval(start_greenyellow=10, end_greenyellow=30),
                 GreenYellowInterval(start_greenyellow=40, end_greenyellow=50),
                 GreenYellowInterval(start_greenyellow=60, end_greenyellow=80)]),
            period=100)

        # Swap two intervals (we do this after initialization as otherwise we would get a ValueError (not correct order
        #  of greenyellow intervals)
        fts._greenyellow_intervals["sg2"][:2] = reversed(fts._greenyellow_intervals["sg2"][:2])

        # WHEN
        shift = get_other_sg_relation_shift(other_relation=sync_start, fts=fts)

        # THEN
        self.assertEqual(shift, None)
Esempio n. 11
0
    def from_json(intersection_dict: Dict) -> Intersection:
        """
        Loading intersection from json (expected same json structure as generated with to_json)
        :param intersection_dict:
        :return: intersection object
        """
        # load signal groups
        signalgroups = [
            SignalGroup.from_json(signalgroup_dict=signalgroup_dict)
            for signalgroup_dict in intersection_dict["signalgroups"]
        ]

        if "periodic_orders" in intersection_dict:
            periodic_orders = [
                PeriodicOrder.from_json(order_dict=order_dict)
                for order_dict in intersection_dict["periodic_orders"]
            ]
        else:
            periodic_orders = []

        # load conflicts
        conflicts = [
            Conflict.from_json(conflict_dict=conflict_dict)
            for conflict_dict in intersection_dict["conflicts"]
        ]

        # load other relations (synchronous starts, offsets and greenyellow_lead)
        sync_starts = []
        offsets = []
        greenyellow_leads = []
        greenyellow_trails = []
        for other_relation_dict in intersection_dict["other_relations"]:
            assert other_relation_dict["from_start_gy"] == other_relation_dict["to_start_gy"], \
                "besides conflicts, at the moment the cloud api can only handle synchronous starts, offsets, " \
                "greenyellow-leads and greenyellow-trails."
            if other_relation_dict[
                    "from_start_gy"] is True and other_relation_dict[
                        "to_start_gy"] is True:
                if other_relation_dict["min_time"] == other_relation_dict[
                        "max_time"]:
                    if other_relation_dict["min_time"] == 0:  # sync start
                        sync_starts.append(
                            SyncStart.from_json(
                                sync_start_dict=other_relation_dict))
                    else:  # offset
                        offsets.append(
                            Offset.from_json(offset_dict=other_relation_dict))
                else:  # greenyellow-leads
                    greenyellow_leads.append(
                        GreenyellowLead.from_json(
                            json_dict=other_relation_dict))
            elif other_relation_dict[
                    "from_start_gy"] is False and other_relation_dict[
                        "to_start_gy"] is False:
                greenyellow_trails.append(
                    GreenyellowTrail.from_json(json_dict=other_relation_dict))

        return Intersection(signalgroups=signalgroups,
                            conflicts=conflicts,
                            sync_starts=sync_starts,
                            offsets=offsets,
                            greenyellow_leads=greenyellow_leads,
                            greenyellow_trails=greenyellow_trails,
                            periodic_orders=periodic_orders)