Skip to content

Commit

Permalink
expand log_transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
claasga committed Feb 6, 2025
1 parent 4440807 commit 79dc0c5
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 22 deletions.
6 changes: 6 additions & 0 deletions backend/dps_training_k/game/channel_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ def create_trainer_log(cls, applied_action, changes, is_updated):
for material, amount in applied_action.template.produced_resources().items()
}
content["produced"] = str(named_produced_resources)
if applied_action.template.category == template.Action.Category.EXAMINATION:
content["examinitaion_result"] = applied_action.result
elif (
applied_action.state_name == models.ActionInstanceStateNames.CANCELED
and applied_action.states.filter(
Expand Down Expand Up @@ -506,6 +508,10 @@ def create_trainer_log(cls, patient_instance, changes, is_updated):
# get_triage_display gets the long version of a ChoiceField
content["level"] = str(patient_instance.get_triage_display())
type = models.LogEntry.TYPES.TRIAGED
elif changes and "patient_state" in changes:
type = models.LogEntry.TYPES.UPDATED
content["state"] = f"{patient_instance.patient_state.vital_signs}"
content["dead"] = f"{patient_instance.patient_state.is_dead}"
elif changes and changes_set & cls.location_changes:
type = models.LogEntry.TYPES.MOVED
current_location = patient_instance.attached_instance()
Expand Down
5 changes: 5 additions & 0 deletions backend/dps_training_k/game/models/log_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TYPES(models.TextChoices):
EXPIRED = "EX", "expired"
MOVED = "MO", "moved"
TRIAGED = "TR", "triaged"
UPDATED = "UP", "updated"

class Meta:
constraints = [
Expand Down Expand Up @@ -115,6 +116,7 @@ def _verbosify_content(self, content):
self.TYPES.EXPIRED: "wirkt nicht mehr",
self.TYPES.MOVED: "wurde verlegt",
self.TYPES.TRIAGED: "wurde triagiert",
self.TYPES.UPDATED: "wurde aktualisiert",
}
if self.category == self.CATEGORIES.ACTION:
message += f"{(content['name'])} {type_to_submessage[self.type]}"
Expand All @@ -131,6 +133,7 @@ def _verbosify_content(self, content):
message += f" zu {content['location_type']} {content['location_name']}"
elif self.category == self.CATEGORIES.PATIENT:
type_to_submessage[self.TYPES.ARRIVED] = "wurde eingeliefert"
type_to_submessage[self.TYPES.UPDATED] = "hat seinen Zustand gewechselt"
message += f"Patient*in {content['name']}({content['code']}) {type_to_submessage[self.type]}"
if "injuries" in content:
message += f" mit den folgenden Verletzungen: {content['injuries']}"
Expand All @@ -140,6 +143,8 @@ def _verbosify_content(self, content):
message += (
f" nach {content['location_type']} {content['location_name']}"
)
elif "state" in content:
message += f" zu {content['state']}"
elif self.category == self.CATEGORIES.PERSONNEL:
type_to_submessage[self.TYPES.ARRIVED] = "ist eingetroffen"
message += f"{content['name']} {type_to_submessage[self.type]}"
Expand Down
58 changes: 48 additions & 10 deletions backend/dps_training_k/game/templmon/log_transformer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import game.models.log_entry as le
from .signature_mapping import *

max_posix_timestamp = pow(2, 31) - 1

Expand All @@ -8,22 +9,40 @@ class MonpolyLogEntry:
ASSIGNED_PERSONNEL = "assigned_personnel"
UNASSIGNED_PERSONNEL = "unassigned_personnel"
PATIENT_ARRIVED = "patient_arrived"
CHANGED_STATE = "changed_state"
UNKNOW_LOG_TYPE = "unknown_log_type"
EXAMINATION_RESULT = "examination_result"
ACTION_STARTED = "action_started"
ACTION_CANCELED = "action_canceled"


class LogTransformer:
@classmethod
def determine_log_type(cls, log_entry: le.LogEntry):
if log_entry.category == le.LogEntry.CATEGORIES.PERSONNEL:
if log_entry.type == le.LogEntry.TYPES.ARRIVED:
l_types = le.LogEntry.TYPES
l_categories = le.LogEntry.CATEGORIES
if log_entry.category == l_categories.PERSONNEL:
if log_entry.type == l_types.ARRIVED:
return MonpolyLogEntry.PERSONNEL_ARRIVED
elif log_entry.type == le.LogEntry.TYPES.ASSIGNED:
elif log_entry.type == l_types.ASSIGNED:
return MonpolyLogEntry.ASSIGNED_PERSONNEL
elif log_entry.type == le.LogEntry.TYPES.UNASSIGNED:
elif log_entry.type == l_types.UNASSIGNED:
return MonpolyLogEntry.UNASSIGNED_PERSONNEL
elif log_entry.category == le.LogEntry.CATEGORIES.PATIENT:
if log_entry.type == le.LogEntry.TYPES.ARRIVED:
elif log_entry.category == l_categories.PATIENT:
if log_entry.type == l_types.ARRIVED:
return MonpolyLogEntry.PATIENT_ARRIVED
if log_entry.type == l_types.UPDATED:
return MonpolyLogEntry.CHANGED_STATE
elif log_entry.category == l_categories.ACTION:
if (
log_entry.type == l_types.FINISHED
and "examination_result" in log_entry.content
):
return MonpolyLogEntry.EXAMINATION_RESULT
elif log_entry.type == l_types.STARTED:
return MonpolyLogEntry.ACTION_STARTED
elif log_entry.type == l_types.CANCELED:
return MonpolyLogEntry.ACTION_CANCELED
else:
return MonpolyLogEntry.UNKNOW_LOG_TYPE

Expand All @@ -44,18 +63,37 @@ def transform(cls, log_entry: le.LogEntry):
if log_type == MonpolyLogEntry.ASSIGNED_PERSONNEL:
personnel_id = log_entry.personnel.all().first().pk
patient_id = log_entry.patient_instance.pk
log_str += f"assigned_personnel({personnel_id}, {patient_id})"
log_str += AssignedPersonnel.log(personnel_id, patient_id)

elif log_type == MonpolyLogEntry.UNASSIGNED_PERSONNEL:
personnel_id = log_entry.personnel.all().first().pk
log_str += f"unassigned_personnel({personnel_id})"
log_str += UnassignedPersonnel.log(personnel_id)

elif log_type == MonpolyLogEntry.PATIENT_ARRIVED:
patient_id = log_entry.patient_instance.pk
area_id = log_entry.area.pk
triage_display = log_entry.patient_instance.get_triage_display()
injuries = f'"{log_entry.content.get("injuries", "")}"'
log_str += f"patient_arrived({patient_id}, {area_id}, {triage_display}, {injuries})"
injuries = log_entry.content.get("injuries", "")
log_str += PatientArrived.log(patient_id, area_id, triage_display, injuries)
elif log_type == MonpolyLogEntry.CHANGED_STATE:
patient_id = log_entry.patient_instance.pk
airway = log_entry.content.get("state").get("Airway", "")
circulation = log_entry.content.get("state").get("Circulation", "")
dead = log_entry.content.get("dead")
log_str += ChangedState.log(patient_id, airway, circulation, dead)
elif log_type == MonpolyLogEntry.ACTION_STARTED:
patient_id = log_entry.patient_instance.pk
action = log_entry.content.get("name")
log_str += ActionStarted.log(patient_id, action)
elif log_type == MonpolyLogEntry.ACTION_CANCELED:
patient_id = log_entry.patient_instance.pk
action = log_entry.content.get("name")
log_str += ActionCanceled.log(patient_id, action)
elif log_type == MonpolyLogEntry.EXAMINATION_RESULT:
patient_id = log_entry.patient_instance.pk
action = log_entry.content.get("name")
result = log_entry.content.get("examination_result")
log_str += ExaminationResult.log(patient_id, action, result)
else:
log_str += f"unknown_log_type({log_entry.pk})"

Expand Down
81 changes: 69 additions & 12 deletions backend/dps_training_k/game/templmon/signature_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ def __iter__(self):
class LogType:

_BASE_VARIABLES = []
_MONPOLY_NAME = None # Expecting list with unique items
MONPOLY_NAME = None # Expecting list with unique items

def __init__(self):
self._variables = {var.name: var.name for var in self._BASE_VARIABLES}

def mfotl(self):
return f'{self._MONPOLY_NAME}({",".join(self._variables.values())})'
return f'{self.MONPOLY_NAME}({",".join(self._variables.values())})'

def bind(self, keys=[], include=True):
if not include:
Expand Down Expand Up @@ -81,7 +81,11 @@ def bulk_create(cls, amount, matching_variables={}, start_id=1):

@classmethod
def monpoly_representation(cls):
return f"{cls._MONPOLY_NAME}({', '.join([var.type for var in cls._BASE_VARIABLES])})"
return f"{cls.MONPOLY_NAME}({', '.join([var.type for var in cls._BASE_VARIABLES])})"

@classmethod
def log(cls, *args):
raise NotImplementedError

def match(self, others, matching_variables):
for other in others:
Expand All @@ -99,15 +103,33 @@ def set_variable(self, key: str, value):

self._variables[key] = value

@classmethod
def _monpolify_args(cls, args):
for arg, var in zip(args, cls._BASE_VARIABLES):
if var.type == RuleProperty.MPT.string:
arg = f'"{arg}"'
elif isinstance(arg, bool):
arg = str(arg).upper()


class AssignedPersonnel(LogType):
_BASE_VARIABLES = [RuleProperty.PERSONNEL, RuleProperty.PATIENT]
_MONPOLY_NAME = "assigned_personnel"
MONPOLY_NAME = "assigned_personnel"

@classmethod
def log(cls, personnel, patient):
cls._monpolify_args([personnel, patient])
return f"{cls.MONPOLY_NAME}({personnel}, {patient})"


class UnassignedPersonnel(LogType):
_BASE_VARIABLES = [RuleProperty.PERSONNEL]
_MONPOLY_NAME = "unassigned_personnel"
MONPOLY_NAME = "unassigned_personnel"

@classmethod
def log(cls, personnel):
cls._monpolify_args([personnel])
return f"{cls.MONPOLY_NAME}({personnel})"


class ChangedState(LogType):
Expand All @@ -117,7 +139,12 @@ class ChangedState(LogType):
RuleProperty.CIRCULATION,
RuleProperty.DEAD,
]
_MONPOLY_NAME = "changed_state"
MONPOLY_NAME = "changed_state"

@classmethod
def log(cls, patient, airway, circulation, dead: bool):
cls._monpolify_args([patient, airway, circulation, dead])
return f"{cls.MONPOLY_NAME}({patient}, {airway}, {circulation}, {dead})"


class PatientArrived(LogType):
Expand All @@ -127,7 +154,12 @@ class PatientArrived(LogType):
RuleProperty.TRIAGE,
RuleProperty.INJURY_LEVEL,
]
_MONPOLY_NAME = "patient_arrived"
MONPOLY_NAME = "patient_arrived"

@classmethod
def log(cls, patient, location, triage, injury_level):
cls._monpolify_args([patient, location, triage, injury_level])
return f"{cls.MONPOLY_NAME}({patient}, {location}, {triage}, {injury_level})"


class PatientRelocated(LogType):
Expand All @@ -136,7 +168,12 @@ class PatientRelocated(LogType):
RuleProperty.OLD_LOCATION,
RuleProperty.NEW_LOCATION,
]
_MONPOLY_NAME = "patient_relocated"
MONPOLY_NAME = "patient_relocated"

@classmethod
def log(cls, patient, old_location, new_location):
cls._monpolify_args([patient, old_location, new_location])
return f"{cls.MONPOLY_NAME}({patient}, {old_location}, {new_location})"


class ExaminationResult(LogType):
Expand All @@ -145,31 +182,51 @@ class ExaminationResult(LogType):
RuleProperty.EXAMINATION,
RuleProperty.RESULT,
]
_MONPOLY_NAME = "examination_result"
MONPOLY_NAME = "examination_result"

@classmethod
def log(cls, patient, examination, result):
cls._monpolify_args([patient, examination, result])
return f"{cls.MONPOLY_NAME}({patient}, {examination}, {result})"


class ActionStarted(LogType):
_BASE_VARIABLES = [
RuleProperty.PATIENT,
RuleProperty.ACTION,
]
_MONPOLY_NAME = "action_started"
MONPOLY_NAME = "action_started"

@classmethod
def log(cls, patient, action):
cls._monpolify_args([patient, action])
return f"{cls.MONPOLY_NAME}({patient}, {action})"


class ActionCanceled(LogType):
_BASE_VARIABLES = [
RuleProperty.PATIENT,
RuleProperty.ACTION,
]
_MONPOLY_NAME = "action_canceled"
MONPOLY_NAME = "action_canceled"

@classmethod
def log(cls, patient, action):
cls._monpolify_args([patient, action])
return f"{cls.MONPOLY_NAME}({patient}, {action})"


class ActionFinished(LogType):
_BASE_VARIABLES = [
RuleProperty.PATIENT,
RuleProperty.ACTION,
]
_MONPOLY_NAME = "action_finished"
MONPOLY_NAME = "action_finished"

@classmethod
def log(cls, patient, action):
cls._monpolify_args([patient, action])
return f"{cls.MONPOLY_NAME}({patient}, {action})"


def generate_monpoly_signature(file_path):
Expand Down

0 comments on commit 79dc0c5

Please sign in to comment.