summaryrefslogtreecommitdiff
path: root/src/models.py
blob: 6af4d65568956582b546af432957e4bb91fe2a26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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