From a1b22cce2ff6561be72b6be75679073cf5df8cd0 Mon Sep 17 00:00:00 2001 From: Fulvio Corno Date: Mon, 17 May 2021 10:20:51 +0200 Subject: [PATCH 1/2] risoluzione prima parte (incompleta) --- src/main/java/model/Event.java | 52 +++++- src/main/java/model/Patient.java | 46 ++++++ src/main/java/model/Simulator.java | 211 +++++++++++++++++++++++++ src/main/java/model/TestSimulator.java | 11 +- 4 files changed, 317 insertions(+), 3 deletions(-) create mode 100644 src/main/java/model/Patient.java diff --git a/src/main/java/model/Event.java b/src/main/java/model/Event.java index de4e3ad..7b4c4d3 100644 --- a/src/main/java/model/Event.java +++ b/src/main/java/model/Event.java @@ -1,5 +1,55 @@ package model; -public class Event { +import java.time.LocalTime; +public class Event implements Comparable{ + + @Override + public String toString() { + return "Event [time=" + time + ", type=" + type + ", patient=" + patient + "]"; + } + + enum EventType { + ARRIVAL, // arriva un nuovo paziente, entra in triage + TRIAGE, // è finito il triage, entro in sala d'attesa + TIMEOUT, // passa un certo tempo di attesa + FREE_STUDIO, // si è liberato uno studio, chiamiamo qualcuno + TREATED, // paziente CURATO + } ; + + private LocalTime time ; + private EventType type ; + private Patient patient ; + + public Event(LocalTime time, EventType type, Patient patient) { + super(); + this.time = time; + this.type = type; + this.patient = patient; + } + public LocalTime getTime() { + return time; + } + public void setTime(LocalTime time) { + this.time = time; + } + public EventType getType() { + return type; + } + public void setType(EventType type) { + this.type = type; + } + public Patient getPatient() { + return patient; + } + public void setPatient(Patient patient) { + this.patient = patient; + } + + @Override + public int compareTo(Event other) { + return this.time.compareTo(other.time); + } + + } diff --git a/src/main/java/model/Patient.java b/src/main/java/model/Patient.java new file mode 100644 index 0000000..a922a53 --- /dev/null +++ b/src/main/java/model/Patient.java @@ -0,0 +1,46 @@ +package model; + +import java.time.LocalTime; + +public class Patient { + + @Override + public String toString() { + return "Patient [arrivalTime=" + arrivalTime + ", color=" + color + "]"; + } + + public enum ColorCode { + NEW, // in triage + WHITE, YELLOW, RED, BLACK, // in sala d'attesa + TREATING, // dentro studio medico + OUT // a casa (abbandonato o curato) + } ; + + private LocalTime arrivalTime ; + private ColorCode color ; + + public Patient(LocalTime arrivalTime, ColorCode color) { + super(); + this.arrivalTime = arrivalTime; + this.color = color; + } + + public LocalTime getArrivalTime() { + return arrivalTime; + } + + public void setArrivalTime(LocalTime arrivalTime) { + this.arrivalTime = arrivalTime; + } + + public ColorCode getColor() { + return color; + } + + public void setColor(ColorCode color) { + this.color = color; + } + + + +} diff --git a/src/main/java/model/Simulator.java b/src/main/java/model/Simulator.java index 71e1df8..5a60878 100644 --- a/src/main/java/model/Simulator.java +++ b/src/main/java/model/Simulator.java @@ -1,5 +1,216 @@ package model; +import java.time.Duration; +import java.time.LocalTime; +import java.util.ArrayList; +import java.util.List; +import java.util.PriorityQueue; + +import model.Event.EventType; +import model.Patient.ColorCode; + public class Simulator { + // Coda degli eventi + private PriorityQueue queue; + + // Modello del mondo + private List patients; + private int freeStudios; // numero studi liberi + + private Patient.ColorCode ultimoColore; + + // Parametri di input + private int totStudios = 3; // NS + + private int numPatients = 120; // NP + private Duration T_ARRIVAL = Duration.ofMinutes(5); + + private Duration DURATION_TRIAGE = Duration.ofMinutes(5); + private Duration DURATION_WHITE = Duration.ofMinutes(10); + private Duration DURATION_YELLOW = Duration.ofMinutes(15); + private Duration DURATION_RED = Duration.ofMinutes(30); + + private Duration TIMEOUT_WHITE = Duration.ofMinutes(60); + private Duration TIMEOUT_YELLOW = Duration.ofMinutes(30); + private Duration TIMEOUT_RED = Duration.ofMinutes(30); + + private LocalTime startTime = LocalTime.of(8, 00); + private LocalTime endTime = LocalTime.of(20, 00); + + // Parametri di output + private int patientsTreated; + private int patientsAbandoned; + private int patientsDead; + + // INIZIALIZZA IL SIMULATORE e crea gli eventi iniziali + public void init() { + // inizializza coda eventi + this.queue = new PriorityQueue<>(); + + // inizializza modello del mondo + this.patients = new ArrayList<>(); + this.freeStudios = this.totStudios; + + this.ultimoColore = ColorCode.RED; + + // inizializza i parametri di output + this.patientsAbandoned = 0; + this.patientsDead = 0; + this.patientsTreated = 0; + + // inietta gli eventi di input (ARRIVAL) + + LocalTime ora = this.startTime; + int inseriti = 0; +// Patient.ColorCode colore = ColorCode.WHITE ; + + while (ora.isBefore(this.endTime) && inseriti < this.numPatients) { + Patient p = new Patient(ora, ColorCode.NEW); + + Event e = new Event(ora, EventType.ARRIVAL, p); + + this.queue.add(e); + this.patients.add(p); + + inseriti++; + ora = ora.plus(T_ARRIVAL); + + /* + */ + } + + } + + private Patient.ColorCode prossimoColore() { + if (ultimoColore.equals(ColorCode.WHITE)) + ultimoColore = ColorCode.YELLOW; + else if (ultimoColore.equals(ColorCode.YELLOW)) + ultimoColore = ColorCode.RED; + else + ultimoColore = ColorCode.WHITE; + return ultimoColore; + } + + // ESEGUE LA SIMULAZIONE + public void run() { + while (!this.queue.isEmpty()) { + Event e = this.queue.poll(); + System.out.println(e); + processEvent(e); + } + } + + private void processEvent(Event e) { + + Patient p = e.getPatient(); + LocalTime ora = e.getTime(); + Patient.ColorCode colore = p.getColor(); + + switch (e.getType()) { + case ARRIVAL: + this.queue.add(new Event(ora.plus(DURATION_TRIAGE), EventType.TRIAGE, p)); + break; + + case TRIAGE: + p.setColor(prossimoColore()); + if (p.getColor().equals(Patient.ColorCode.WHITE)) + this.queue.add(new Event(ora.plus(TIMEOUT_WHITE), EventType.TIMEOUT, p)); + else if (p.getColor().equals(Patient.ColorCode.YELLOW)) + this.queue.add(new Event(ora.plus(TIMEOUT_YELLOW), EventType.TIMEOUT, p)); + else if (p.getColor().equals(Patient.ColorCode.RED)) + this.queue.add(new Event(ora.plus(TIMEOUT_RED), EventType.TIMEOUT, p)); + break; + + case FREE_STUDIO: + break; + + case TIMEOUT: + switch (colore) { + case WHITE: + p.setColor(ColorCode.OUT); + this.patientsAbandoned++ ; + break; + + case YELLOW: + p.setColor(ColorCode.RED); + this.queue.add(new Event(ora.plus(TIMEOUT_RED), EventType.TIMEOUT, p)); + break; + + case RED: + p.setColor(ColorCode.BLACK); + this.patientsDead++ ; + break; + + default: + System.out.println("ERRORE: TIMEOUT CON COLORE "+colore) ; + } + break; + + case TREATED: + break; + } + + } + + public void setTotStudios(int totStudios) { + this.totStudios = totStudios; + } + + public void setNumPatients(int numPatients) { + this.numPatients = numPatients; + } + + public void setT_ARRIVAL(Duration t_ARRIVAL) { + T_ARRIVAL = t_ARRIVAL; + } + + public void setDURATION_TRIAGE(Duration dURATION_TRIAGE) { + DURATION_TRIAGE = dURATION_TRIAGE; + } + + public void setDURATION_WHITE(Duration dURATION_WHITE) { + DURATION_WHITE = dURATION_WHITE; + } + + public void setDURATION_YELLOW(Duration dURATION_YELLOW) { + DURATION_YELLOW = dURATION_YELLOW; + } + + public void setDURATION_RED(Duration dURATION_RED) { + DURATION_RED = dURATION_RED; + } + + public void setTIMEOUT_WHITE(Duration tIMEOUT_WHITE) { + TIMEOUT_WHITE = tIMEOUT_WHITE; + } + + public void setTIMEOUT_YELLOW(Duration tIMEOUT_YELLOW) { + TIMEOUT_YELLOW = tIMEOUT_YELLOW; + } + + public void setTIMEOUT_RED(Duration tIMEOUT_RED) { + TIMEOUT_RED = tIMEOUT_RED; + } + + public void setStartTime(LocalTime startTime) { + this.startTime = startTime; + } + + public void setEndTime(LocalTime endTime) { + this.endTime = endTime; + } + + public int getPatientsTreated() { + return patientsTreated; + } + + public int getPatientsAbandoned() { + return patientsAbandoned; + } + + public int getPatientsDead() { + return patientsDead; + } + } diff --git a/src/main/java/model/TestSimulator.java b/src/main/java/model/TestSimulator.java index b9aa009..8f82a9b 100644 --- a/src/main/java/model/TestSimulator.java +++ b/src/main/java/model/TestSimulator.java @@ -3,8 +3,15 @@ public class TestSimulator { public static void main(String[] args) { - // TODO Auto-generated method stub - + Simulator sim = new Simulator() ; + + // impostare i parametri + + // eseguire simulazione + sim.init(); + sim.run(); + + // leggere gli output } } From d10e03f0e4d13efc8a75909bdca0aa56a2cdea2a Mon Sep 17 00:00:00 2001 From: Fulvio Corno Date: Mon, 17 May 2021 11:34:20 +0200 Subject: [PATCH 2/2] Completamento esercizio --- src/main/java/model/Event.java | 1 + src/main/java/model/Patient.java | 52 +++++++++++++++++++---- src/main/java/model/Simulator.java | 67 +++++++++++++++++++++++------- 3 files changed, 97 insertions(+), 23 deletions(-) diff --git a/src/main/java/model/Event.java b/src/main/java/model/Event.java index 7b4c4d3..e1357f6 100644 --- a/src/main/java/model/Event.java +++ b/src/main/java/model/Event.java @@ -15,6 +15,7 @@ enum EventType { TIMEOUT, // passa un certo tempo di attesa FREE_STUDIO, // si è liberato uno studio, chiamiamo qualcuno TREATED, // paziente CURATO + TICK, // timer per controllare se ci sono studi liberi } ; private LocalTime time ; diff --git a/src/main/java/model/Patient.java b/src/main/java/model/Patient.java index a922a53..4f79883 100644 --- a/src/main/java/model/Patient.java +++ b/src/main/java/model/Patient.java @@ -2,13 +2,8 @@ import java.time.LocalTime; -public class Patient { +public class Patient implements Comparable { - @Override - public String toString() { - return "Patient [arrivalTime=" + arrivalTime + ", color=" + color + "]"; - } - public enum ColorCode { NEW, // in triage WHITE, YELLOW, RED, BLACK, // in sala d'attesa @@ -16,11 +11,13 @@ public enum ColorCode { OUT // a casa (abbandonato o curato) } ; + private int num ; private LocalTime arrivalTime ; private ColorCode color ; - public Patient(LocalTime arrivalTime, ColorCode color) { + public Patient(int num, LocalTime arrivalTime, ColorCode color) { super(); + this.num = num ; this.arrivalTime = arrivalTime; this.color = color; } @@ -40,6 +37,47 @@ public ColorCode getColor() { public void setColor(ColorCode color) { this.color = color; } + + @Override + public String toString() { + return "Patient [num=" + num + ", arrivalTime=" + arrivalTime + ", color=" + color + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + num; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Patient other = (Patient) obj; + if (num != other.num) + return false; + return true; + } + + @Override + public int compareTo(Patient other) { + if(this.color.equals(other.color)) + return this.arrivalTime.compareTo(other.arrivalTime) ; + else if(this.color.equals(Patient.ColorCode.RED)) + return -1 ; + else if(other.color.equals(Patient.ColorCode.RED)) + return +1 ; + else if(this.color.equals(Patient.ColorCode.YELLOW)) // Y - W + return -1 ; + else // W - Y + return +1 ; + } diff --git a/src/main/java/model/Simulator.java b/src/main/java/model/Simulator.java index 5a60878..38e8ae7 100644 --- a/src/main/java/model/Simulator.java +++ b/src/main/java/model/Simulator.java @@ -16,6 +16,9 @@ public class Simulator { // Modello del mondo private List patients; + private PriorityQueue waitingRoom; + // contiene SOLO i pazienti in attesa (WHITE/YELLOW/RED) + private int freeStudios; // numero studi liberi private Patient.ColorCode ultimoColore; @@ -50,6 +53,7 @@ public void init() { // inizializza modello del mondo this.patients = new ArrayList<>(); + this.waitingRoom = new PriorityQueue<>() ; this.freeStudios = this.totStudios; this.ultimoColore = ColorCode.RED; @@ -65,8 +69,10 @@ public void init() { int inseriti = 0; // Patient.ColorCode colore = ColorCode.WHITE ; + this.queue.add(new Event(ora, EventType.TICK, null)) ; + while (ora.isBefore(this.endTime) && inseriti < this.numPatients) { - Patient p = new Patient(ora, ColorCode.NEW); + Patient p = new Patient(inseriti, ora, ColorCode.NEW); Event e = new Event(ora, EventType.ARRIVAL, p); @@ -75,11 +81,7 @@ public void init() { inseriti++; ora = ora.plus(T_ARRIVAL); - - /* - */ } - } private Patient.ColorCode prossimoColore() { @@ -105,7 +107,6 @@ private void processEvent(Event e) { Patient p = e.getPatient(); LocalTime ora = e.getTime(); - Patient.ColorCode colore = p.getColor(); switch (e.getType()) { case ARRIVAL: @@ -114,43 +115,77 @@ private void processEvent(Event e) { case TRIAGE: p.setColor(prossimoColore()); - if (p.getColor().equals(Patient.ColorCode.WHITE)) + if (p.getColor().equals(Patient.ColorCode.WHITE)) { this.queue.add(new Event(ora.plus(TIMEOUT_WHITE), EventType.TIMEOUT, p)); - else if (p.getColor().equals(Patient.ColorCode.YELLOW)) + this.waitingRoom.add(p); + } else if (p.getColor().equals(Patient.ColorCode.YELLOW)) { this.queue.add(new Event(ora.plus(TIMEOUT_YELLOW), EventType.TIMEOUT, p)); - else if (p.getColor().equals(Patient.ColorCode.RED)) + this.waitingRoom.add(p); + } else if (p.getColor().equals(Patient.ColorCode.RED)) { this.queue.add(new Event(ora.plus(TIMEOUT_RED), EventType.TIMEOUT, p)); + this.waitingRoom.add(p); + } break; case FREE_STUDIO: + if(this.freeStudios==0) + return ; + // Quale paziente ha diritto di entrare??? + Patient primo = this.waitingRoom.poll() ; + if(primo!=null) { + // ammetti il paziente nello studio + if(primo.getColor().equals(ColorCode.WHITE)) + this.queue.add(new Event(ora.plus(DURATION_WHITE), EventType.TREATED, primo)) ; + if(primo.getColor().equals(ColorCode.YELLOW)) + this.queue.add(new Event(ora.plus(DURATION_YELLOW), EventType.TREATED, primo)) ; + if(primo.getColor().equals(ColorCode.RED)) + this.queue.add(new Event(ora.plus(DURATION_RED), EventType.TREATED, primo)) ; + primo.setColor(ColorCode.TREATING); + this.freeStudios-- ; + } break; case TIMEOUT: + Patient.ColorCode colore = p.getColor(); switch (colore) { case WHITE: + this.waitingRoom.remove(p) ; p.setColor(ColorCode.OUT); - this.patientsAbandoned++ ; + this.patientsAbandoned++; break; - + case YELLOW: + this.waitingRoom.remove(p) ; p.setColor(ColorCode.RED); this.queue.add(new Event(ora.plus(TIMEOUT_RED), EventType.TIMEOUT, p)); + this.waitingRoom.add(p) ; break; - + case RED: + this.waitingRoom.remove(p) ; p.setColor(ColorCode.BLACK); - this.patientsDead++ ; + this.patientsDead++; break; - + default: - System.out.println("ERRORE: TIMEOUT CON COLORE "+colore) ; +// System.out.println("ERRORE: TIMEOUT CON COLORE " + colore); } break; case TREATED: + this.patientsTreated++; + p.setColor(ColorCode.OUT); + this.freeStudios++ ; + this.queue.add(new Event(ora, EventType.FREE_STUDIO, null)) ; + break; + + case TICK: + if(this.freeStudios>0 && !this.waitingRoom.isEmpty()) + this.queue.add(new Event(ora, EventType.FREE_STUDIO, null)) ; + if(ora.isBefore(this.endTime)) + this.queue.add(new Event(ora.plus(Duration.ofMinutes(5)), EventType.TICK, null)); break; } - } public void setTotStudios(int totStudios) {