diff options
| author | yctct <yctct@yctct.com> | 2026-11-25 17:56:21 +0100 |
|---|---|---|
| committer | yctct <yctct@yctct.com> | 2026-11-25 17:56:21 +0100 |
| commit | 489b223db74f468032c1eeb6ca18ee6a4da3bc33 (patch) | |
| tree | a360664813443859ebfc0dcce79dec7fddf93c9f /src/models.py | |
Add files, first commit
Diffstat (limited to 'src/models.py')
| -rw-r--r-- | src/models.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/models.py b/src/models.py new file mode 100644 index 0000000..6af4d65 --- /dev/null +++ b/src/models.py @@ -0,0 +1,90 @@ +"""Classes for Zone, Connections and the map""" +from enum import Enum +from typing_extensions import Self + +from pydantic import BaseModel, Field, model_validator + + +class ZoneType(str, Enum): + """Valid zone types.""" + + NORMAL = "normal" + BLOCKED = "blocked" + RESTRICTED = "restricted" + PRIORITY = "priority" + + +class Zone(BaseModel): + """Represents a zone in the drone network.""" + + name: str = Field(min_length=1) + x: int + y: int + zone_type: ZoneType = Field(default=ZoneType.NORMAL) + color: str | None = None + max_drones: int = Field(default=1, ge=1) + is_start: bool = Field(default=False) + is_end: bool = Field(default=False) + connections: list[str] = Field(default_factory=list) + occupancy: int = Field(default=0, ge=0) + congested: bool = Field(default=False) + reservation: int = Field(default=0, ge=0) + + @model_validator(mode="after") + def validate_name(self) -> Self: + + # checking for space here is not so useful because parsing fails anyway + if " " in self.name or "-" in self.name: + raise ValueError("zone names must not contain spaces or dashes") + + if self.zone_type != ZoneType.BLOCKED and self.max_drones == 0: + raise ValueError("A non-blocked zone type must have capacity of " + "at least one drone.") + + return self + + +class Connection(BaseModel): + """Represents bidirectional link between two connection.""" + + src: Zone + dest: Zone + link_capacity: int = Field(default=1, ge=0) + link_occupancy: int = Field(default=0, ge=0) + congested: bool = Field(default=False) + + @model_validator(mode="after") + def validate_connection(self) -> Self: + + if self.src == self.dest: + raise ValueError("a connection cannot connect a zone to itself") + + if self.src.zone_type != ZoneType.BLOCKED \ + and self.dest.zone_type != ZoneType.BLOCKED \ + and self.link_capacity == 0: + raise ValueError("A connection to a non-blocked zone" + "must have a capacity of at least one drone.") + + return self + + +class MapData(BaseModel): + """All data needed to run a simulation.""" + + nb_drones: int = Field(gt=0) + start: Zone + end: Zone + zones: list[Zone] + connections: list[Connection] + + @model_validator(mode="after") + def post_validate(self) -> Self: + + same_name = self.start.name == self.end.name + same_position = (self.start.x == self.end.x + and self.start.y == self.end.y) + + if self.start == self.end or same_name or same_position: + raise ValueError("Start and end zones cannot overlap.") + + return self |
