Example #1
0
def test_add_jobs():
    itinerary = Itinerary(45, "2222")

    move_job = itinerary.move_to(123)
    current_job = itinerary.current_job
    assert move_job == current_job
    assert move_job is not None
    assert current_job is not None
    assert itinerary.next_jobs == []
    assert itinerary.jobs_to_complete == [current_job]
    assert not itinerary.is_completed()

    itinerary.pickup("1212")
    assert len(itinerary.next_jobs) == 1
    assert len(itinerary.jobs_to_complete) == 2
    assert itinerary.current_job == current_job
    assert not itinerary.is_completed()

    itinerary.dropoff("333")
    assert len(itinerary.next_jobs) == 2
    assert len(itinerary.jobs_to_complete) == 3
    assert itinerary.current_job == current_job
    assert not itinerary.is_completed()

    itinerary.wait(444)
    assert len(itinerary.next_jobs) == 3
    assert len(itinerary.jobs_to_complete) == 4
    assert itinerary.current_job == current_job
    assert not itinerary.is_completed()

    with pytest.raises(Exception):
        itinerary.add_job("Not a Base")
Example #2
0
def test_Dispatcher():
    clock = Clock()
    router = LinearRouter(clock)

    fleet = Fleet(clock, router)
    vehicle = Vehicle(clock)
    fleet.infleet(vehicle, Position(13.3764, 52.5461))

    cnt = Dispatcher()

    cnt.step()

    booking = Booking(clock, Position(13.4014, 52.5478),
                      Position(13.3764, 52.5461))

    itinerary = Itinerary(101, vehicle)
    itinerary.move_to(Position(13.4014, 52.5478))
    itinerary.pickup(booking)
    itinerary.dropoff(booking)

    cnt.dispatch(itinerary)
    cnt.step()

    assert vehicle.is_moving
    assert booking.is_waiting_pickup()

    booking.set_pickup()

    cnt.dispatch(itinerary)
    cnt.step()

    assert booking.is_waiting_dropoff()
Example #3
0
def test_update_next_bookings():
    v = create_vehicle()

    clock = Clock()
    b1 = Booking(
        clock,
        GeographicPosition(13.4014, 52.5478),
        GeographicPosition(13.3393, 52.5053),
    )
    b2 = Booking(
        clock,
        GeographicPosition(13.4014, 52.5478),
        GeographicPosition(13.3393, 52.5053),
    )

    itinerary = Itinerary(3434, v)
    itinerary.move_to(111)
    itinerary.pickup(b1)
    itinerary.dropoff(b1)
    itinerary.pickup(b2)
    itinerary.dropoff(b2)

    update_next_bookings(itinerary)

    assert b1.is_waiting_pickup()
    assert b2.is_waiting_pickup()

    b1.set_pickup()
    update_next_bookings(itinerary)
    assert b1.is_waiting_dropoff()
    assert b2.is_waiting_pickup()
Example #4
0
def test_Dispatcher_3():
    clock = Clock()
    router = LinearRouter(clock)

    fleet = Fleet(clock, router)
    vehicle = Vehicle(clock)
    fleet.infleet(vehicle, Position(13.3764, 52.5461))

    cnt = Dispatcher()

    booking = Booking(clock, Position(13.4014, 52.5478),
                      Position(13.3764, 52.5461))

    itinerary = Itinerary(101, vehicle)
    job1 = itinerary.pickup(booking)
    job2 = itinerary.move_to(Position(13.4014, 52.5478))
    job3 = itinerary.dropoff(booking)

    cnt.dispatch(itinerary)
    cnt.step()

    # finish 2 jobs in 1 step
    assert itinerary.current_job is job2

    assert booking.is_waiting_dropoff()
Example #5
0
def test_job_types():
    created_at = 2
    itinerary = Itinerary(created_at, "343433")
    itinerary.created_at = created_at

    job = itinerary.pickup("34")
    assert job.name() == "pickup"
    assert job.is_pickup()
    assert job.itinerary_id == itinerary.id

    job = itinerary.dropoff("33")
    assert job.name() == "dropoff"
    assert job.is_dropoff()
    assert not job.is_pickup()
    assert job.itinerary_id == itinerary.id

    job = itinerary.move_to("33")
    assert job.name() == "move_to"
    assert job.is_move_to()
    assert not job.is_dropoff()
    assert not job.is_pickup()
    assert job.itinerary_id == itinerary.id

    job = itinerary.wait("33")
    assert job.name() == "wait"
    assert job.is_wait()
    assert not job.is_move_to()
    assert not job.is_dropoff()
    assert not job.is_pickup()
    assert job.itinerary_id == itinerary.id
Example #6
0
def test_job_complete():
    itinerary = Itinerary(234, "3434")

    move_job = itinerary.move_to(33)
    itinerary.job_complete(move_job)

    assert itinerary.is_completed()
    assert itinerary.current_job is None
    assert itinerary.completed_jobs == [move_job]

    pickup = itinerary.pickup("aaaa")
    assert itinerary.current_job == pickup
    assert not itinerary.is_completed()
    assert itinerary.jobs_to_complete == [pickup]
    assert itinerary.next_jobs == []

    dropoff = itinerary.dropoff("aaaa")
    move_job = itinerary.move_to(66666)

    assert itinerary.next_jobs == [dropoff, move_job]

    # jobs can be completed only in the order they planned
    with pytest.raises(Exception):
        itinerary.job_complete(dropoff)

    itinerary.job_complete(pickup)
    assert not itinerary.is_completed()
    assert itinerary.next_jobs == [move_job]
    assert itinerary.current_job == dropoff
    assert itinerary.jobs_to_complete == [dropoff, move_job]

    itinerary.job_complete(dropoff)
    itinerary.job_complete(move_job)
    assert itinerary.is_completed()
Example #7
0
def test_Dispatcher_2():
    clock = Clock()
    router = LinearRouter(clock)

    fleet = Fleet(clock, router)
    vehicle = Vehicle(clock)
    fleet.infleet(vehicle, GeographicPosition(13.3764, 52.5461))

    cnt = Dispatcher()

    booking = Booking(
        clock,
        GeographicPosition(13.4014, 52.5478),
        GeographicPosition(13.3764, 52.5461),
    )

    itinerary = Itinerary(101, vehicle)
    job1 = itinerary.pickup(booking)
    job2 = itinerary.dropoff(booking)

    cnt.dispatch(itinerary)
    cnt.step()

    # finish 2 jobs in 1 step
    assert itinerary.current_job is None

    assert booking.is_complete()
def test_do_current_job_3():
    v = create_vehicle()

    itinerary = Itinerary(12, v)
    job = itinerary.move_to(2323)

    with patch("simobility.core.state_transitions.move_vehicle") as fn:
        do_job(itinerary)
        fn.assert_called_once_with(itinerary)

    booking = 34
    itinerary = Itinerary(23, v)
    itinerary.pickup(booking)

    with patch("simobility.core.state_transitions.pickup_booking") as fn:
        do_job(itinerary)
        fn.assert_called_once_with(booking, itinerary)

    itinerary = Itinerary(4545, v)
    itinerary.dropoff(booking)

    with patch("simobility.core.state_transitions.dropoff_booking") as fn:
        do_job(itinerary)
        fn.assert_called_once_with(booking, itinerary)
Example #9
0
    fleet = create_fleet(clock)
    vehicle = fleet.get_online_vehicles()[0]

    booking = create_booking(clock)

    print_estimates(vehicle, booking, clock)

    print("\nObject states before simulation:")
    print(f'Booking state is "{booking.state.value}"')
    print(f'Vehicle state is "{vehicle.state.value}"')

    # explain vehicle what to do
    itinerary = Itinerary(clock.now, vehicle)
    itinerary.move_to(booking.pickup)
    itinerary.pickup(booking)
    itinerary.move_to(booking.dropoff)
    itinerary.dropoff(booking)

    dispatcher = Dispatcher()
    dispatcher.dispatch(itinerary)

    print(
        f"\nStart simulation at {clock.to_datetime()} ({clock.now} clock time)"
    )

    # run simulation - all state changes and movements will happen here
    # the order of steps in important: fleet -> dispatcher -> clock
    while not itinerary.is_completed():
        fleet.step()
        dispatcher.step()