Skip to content

Commit

Permalink
New exercise for self assessment
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsch420 committed Apr 23, 2024
1 parent 2bd5fdc commit af46335
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/random_events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,12 @@ def get_variables_where_assignment_is_different(self, other: Self) -> List[Varia
"""
return [variable for variable in self.keys() if self[variable] != other[variable]]

def to_typst(self) -> str:
"""
Convert the event to a typst string.
"""
return " times ".join(f"{variable.name}_({variable.assignment_to_typst(value)})" for variable, value in
self.items())


class EncodedEvent(Event):
Expand Down Expand Up @@ -770,5 +776,11 @@ def _from_json(cls, data: Dict[str, Any]) -> Self:
events = [Event.from_json(event) for event in data["events"]]
return cls(events)

def to_typst(self) -> str:
"""
Convert the event to a typst string.
"""
return " union ".join(f"({event.to_typst()})" for event in self.events)


EventType = Union[Event, EncodedEvent, ComplexEvent]
12 changes: 12 additions & 0 deletions src/random_events/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ def assignment_from_json(self, data: Any) -> AssignmentType:
def encoded_domain(self):
return self.encode_many(self.domain)

def assignment_to_typst(self, assignment: AssignmentType) -> str:
"""
Convert an assignment to typst string.
"""
raise NotImplementedError


class Continuous(Variable):
"""
Expand Down Expand Up @@ -195,6 +201,9 @@ def assignment_to_json(self, assignment: portion.Interval) -> Any:
def assignment_from_json(self, data: Any) -> portion.Interval:
return portion.from_data(data)

def assignment_to_typst(self, assignment: AssignmentType) -> str:
return " union ".join([interval.__str__() for interval in assignment])


class Discrete(Variable):
"""
Expand Down Expand Up @@ -266,6 +275,9 @@ def assignment_to_json(self, assignment: Tuple) -> Tuple:
def assignment_from_json(self, data: Any) -> AssignmentType:
return tuple(data)

def assignment_to_typst(self, assignment: AssignmentType) -> str:
return "{" + ", ".join([str(element) for element in assignment]) + "}"


class Symbolic(Discrete):
"""
Expand Down

0 comments on commit af46335

Please sign in to comment.