Skip to content

Commit

Permalink
drop cache_rsult from container.get_arrival_time and container.get_de…
Browse files Browse the repository at this point in the history
…parture_time, add typehints for fields
  • Loading branch information
1kastner committed Sep 9, 2023
1 parent 0f84b13 commit d57b96a
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions conflowgen/domain_models/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
from peewee import ForeignKeyField
from peewee import IntegerField

from conflowgen.data_summaries.data_summaries_cache import DataSummariesCache
from .arrival_information import TruckArrivalInformationForDelivery, TruckArrivalInformationForPickup
from .base_model import BaseModel
from .data_types.container_length import CONTAINER_LENGTH_TO_OCCUPIED_TEU
from .data_types.container_length import CONTAINER_LENGTH_TO_OCCUPIED_TEU, ContainerLength
from .field_types.container_length import ContainerLengthField
from .field_types.mode_of_transport import ModeOfTransportField
from .field_types.storage_requirement import StorageRequirementField
from .large_vehicle_schedule import Destination
from .vehicle import LargeScheduledVehicle
from .vehicle import Truck
from .data_types.storage_requirement import StorageRequirement
from ..domain_models.data_types.mode_of_transport import ModeOfTransport


Expand All @@ -35,78 +35,78 @@ def __init__(self, container, vehicle_type):
class Container(BaseModel):
"""A representation of the physical container that is moved through the yard."""
id = AutoField()
weight = IntegerField(
weight: int = IntegerField(
null=False,
help_text="The weight of the container (approximated). This value should suit to the container weight "
"distribution."
)
length = ContainerLengthField(
length: ContainerLength = ContainerLengthField(
null=False,
help_text="The length of the container in feet, typically 20' or 40' are used in international trade."
)
storage_requirement = StorageRequirementField(
storage_requirement: StorageRequirement = StorageRequirementField(
null=False,
help_text="Some containers must be stored separately, e.g. if they are reefers or dangerous goods containers."
)
delivered_by = ModeOfTransportField(
delivered_by: ModeOfTransport = ModeOfTransportField(
null=False,
help_text="This vehicle type delivers this container to the terminal. This helps to quickly pick the correct "
"foreign key in the next step and is thus just additional information."
)
picked_up_by_initial = ModeOfTransportField(
picked_up_by_initial: ModeOfTransport = ModeOfTransportField(
null=False,
help_text="This vehicle type is first drawn randomly for picking up the container. It might be overwritten "
"later because no vehicle satisfies the constraints, e.g. because all vehicles of that type arrive "
"too early or too late or they are already full. Large deviations between `picked_up_by_initial` "
"and `picked_up_by` might indicate calibration issues with the random distributions or schedules."
)
picked_up_by = ModeOfTransportField(
picked_up_by: ModeOfTransport = ModeOfTransportField(
null=False,
help_text="This vehicle type is later actually used for picking up the container. This helps to quickly pick "
"the correct foreign key in the next step and is thus just additional information."
)
delivered_by_large_scheduled_vehicle = ForeignKeyField(
delivered_by_large_scheduled_vehicle: LargeScheduledVehicle = ForeignKeyField(
LargeScheduledVehicle,
null=True,
help_text="Points at the large scheduled vehicle it is delivered by (null if truck). "
"Any arrival information of the container is attached to that vehicle."
)
delivered_by_truck = ForeignKeyField(
delivered_by_truck: Truck = ForeignKeyField(
Truck,
null=True,
help_text="Points at the truck it is delivered by (null if large scheduled vehicle). "
"Any arrival information of the container is attached to that vehicle)."
)
picked_up_by_large_scheduled_vehicle = ForeignKeyField(
picked_up_by_large_scheduled_vehicle: LargeScheduledVehicle = ForeignKeyField(
LargeScheduledVehicle,
null=True,
help_text="Points at the large scheduled vehicle it is picked up by (null if truck). "
"Any departure information of the container is attached to that vehicle."
)
picked_up_by_truck = ForeignKeyField(
picked_up_by_truck: Truck = ForeignKeyField(
Truck,
null=True,
help_text="Points at the truck it is picked up by (null if large scheduled vehicle). "
"Any departure information of the container is attached to that vehicle."
)
destination = ForeignKeyField(
destination: Destination = ForeignKeyField(
Destination,
null=True,
help_text="Points at the next destination of the container. Only applicable if picked up by a large scheduled "
"vehicle. This information is sometimes used for better container stacking in the yard. For vessels, "
"this can be regarded as a simplified stowage plan, likewise for trains and barges."
)
emergency_pickup = BooleanField(
emergency_pickup: bool = BooleanField(
default=False,
help_text="This indicates that no regular means of transport was available so that a vehicle had to be called "
"explicitly to pick up the container so that the maximum dwell time is not exceeded."
)
cached_arrival_time = DateTimeField(
cached_arrival_time: datetime.datetime = DateTimeField(
default=None,
null=True,
help_text="This field is used to cache the arrival time for faster evaluation of analyses."
)
cached_departure_time = DateTimeField(
cached_departure_time: datetime.datetime = DateTimeField(
default=None,
null=True,
help_text="This field is used to cache the departure time for faster evaluation of analyses."
Expand All @@ -116,9 +116,11 @@ class Container(BaseModel):
def occupied_teu(self) -> float:
return CONTAINER_LENGTH_TO_OCCUPIED_TEU[self.length]

@DataSummariesCache.cache_result
def get_arrival_time(self) -> datetime.datetime:

if self.cached_arrival_time is not None:
return self.cached_arrival_time

container_arrival_time: datetime.datetime
if self.delivered_by == ModeOfTransport.truck:
# noinspection PyTypeChecker
Expand All @@ -136,9 +138,11 @@ def get_arrival_time(self) -> datetime.datetime:
self.save()
return container_arrival_time

@DataSummariesCache.cache_result
def get_departure_time(self) -> datetime.datetime:

if self.cached_departure_time is not None:
return self.cached_departure_time

container_departure_time: datetime.datetime
if self.picked_up_by_truck is not None:
# noinspection PyTypeChecker
Expand Down

0 comments on commit d57b96a

Please sign in to comment.