From 2d4a8c0a6608d862aa8c0bc85052945142f157e6 Mon Sep 17 00:00:00 2001 From: Eden Xu <130119691+FlyingPufferFish@users.noreply.github.com> Date: Mon, 30 Sep 2024 18:06:58 +1000 Subject: [PATCH] refactor(database): :recycle: refactor Unit constructors, clean code --- .../main/java/org/acme/TimetableResource.java | 14 ++++--- .../src/main/java/org/acme/domain/Room.java | 4 +- .../main/java/org/acme/domain/Student.java | 4 -- .../main/java/org/acme/domain/Timetable.java | 9 +---- .../src/main/java/org/acme/domain/Unit.java | 39 ++++++++++--------- .../TimetableConstraintProviderTest.java | 24 ++++++------ 6 files changed, 43 insertions(+), 51 deletions(-) diff --git a/backend/src/main/java/org/acme/TimetableResource.java b/backend/src/main/java/org/acme/TimetableResource.java index a7caf6d..5a2c5da 100644 --- a/backend/src/main/java/org/acme/TimetableResource.java +++ b/backend/src/main/java/org/acme/TimetableResource.java @@ -41,10 +41,12 @@ public Timetable handleRequest(Timetable problem) throws ExecutionException, Int jobId += 1; String name = "Job" + Integer.toString(jobId); - // return problem; - + // generate solution timetable with TimeFold Solver Timetable solution = solverManager.solve(name, problem).getFinalBestSolution(); + + // store the solution timetable to the database solution.persist(); + return solution; } @@ -81,10 +83,10 @@ public Timetable solveExample() throws ExecutionException, InterruptedException Room r2 = new Room("Room2", 4, false); Room r3 = new Room("Room3", 4, false); - Unit u1 = new Unit(1, "1", Duration.ofHours(2), List.of(a, b), true); - Unit u2 = new Unit(2, "2", Duration.ofHours(2), List.of(a, c, d, e), true); - Unit u3 = new Unit(3, "3", Duration.ofHours(2), List.of(f, g, h, i), false); - Unit u4 = new Unit(4, "4", Duration.ofHours(2), List.of(a, b), false); + Unit u1 = new Unit(1, "1", "Course A", Duration.ofHours(2), List.of(a, b), true); + Unit u2 = new Unit(2, "2", "Course A", Duration.ofHours(2), List.of(a, c, d, e), true); + Unit u3 = new Unit(3, "3", "Course B", Duration.ofHours(2), List.of(f, g, h, i), false); + Unit u4 = new Unit(4, "4", "Course C", Duration.ofHours(2), List.of(a, b), false); var problem = new Timetable( List.of( diff --git a/backend/src/main/java/org/acme/domain/Room.java b/backend/src/main/java/org/acme/domain/Room.java index 5e5a048..d78aec4 100644 --- a/backend/src/main/java/org/acme/domain/Room.java +++ b/backend/src/main/java/org/acme/domain/Room.java @@ -3,10 +3,8 @@ import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonManagedReference; import ai.timefold.solver.core.api.domain.lookup.PlanningId; import io.quarkus.hibernate.orm.panache.PanacheEntity; @@ -23,6 +21,7 @@ */ @Entity public class Room extends PanacheEntity { + @PlanningId public String roomCode; @@ -45,7 +44,6 @@ public class Room extends PanacheEntity { */ @JsonIgnoreProperties("rooms") @ManyToMany(mappedBy = "rooms", fetch = FetchType.LAZY, cascade = {CascadeType.ALL}) - // @JsonManagedReference @JsonIgnore public List timetables = new ArrayList(); diff --git a/backend/src/main/java/org/acme/domain/Student.java b/backend/src/main/java/org/acme/domain/Student.java index ece4d03..83b20c4 100644 --- a/backend/src/main/java/org/acme/domain/Student.java +++ b/backend/src/main/java/org/acme/domain/Student.java @@ -6,7 +6,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonManagedReference; import io.quarkus.hibernate.orm.panache.PanacheEntity; import jakarta.persistence.CascadeType; @@ -24,8 +23,6 @@ @Entity public class Student extends PanacheEntity{ - // String studentID; - public String name; @JsonIgnoreProperties("students") @@ -35,7 +32,6 @@ public class Student extends PanacheEntity{ joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "unit_id") ) - // @JsonManagedReference @JsonIgnore public List units = new ArrayList(); diff --git a/backend/src/main/java/org/acme/domain/Timetable.java b/backend/src/main/java/org/acme/domain/Timetable.java index b45fb47..43a1cdd 100644 --- a/backend/src/main/java/org/acme/domain/Timetable.java +++ b/backend/src/main/java/org/acme/domain/Timetable.java @@ -20,9 +20,7 @@ import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.JsonProperty; /** @@ -57,10 +55,8 @@ public class Timetable extends PanacheEntity { joinColumns = @JoinColumn(name = "timetable_id"), inverseJoinColumns = @JoinColumn(name = "room_id") ) - // @JsonManagedReference @ProblemFactCollectionProperty - // @JsonIgnore - // @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) @ValueRangeProvider public List rooms; @@ -76,7 +72,6 @@ public class Timetable extends PanacheEntity { joinColumns = @JoinColumn(name = "timetable_id"), inverseJoinColumns = @JoinColumn(name = "unit_id") ) - // @JsonManagedReference @PlanningEntityCollectionProperty public List units; @@ -191,7 +186,7 @@ public List calculateSoftUnitConflictList() { ArrayList out = new ArrayList(); for (var first : units) { for (var second : units) { - if (first.getUnitID() >= second.getUnitID()) { + if (first.getUnitId() >= second.getUnitId()) { continue; } int numStudents = 0; diff --git a/backend/src/main/java/org/acme/domain/Unit.java b/backend/src/main/java/org/acme/domain/Unit.java index 16409e1..3515d21 100644 --- a/backend/src/main/java/org/acme/domain/Unit.java +++ b/backend/src/main/java/org/acme/domain/Unit.java @@ -17,11 +17,8 @@ import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonManagedReference; - /** * Represents a unit. @@ -33,7 +30,7 @@ public class Unit extends PanacheEntity { @PlanningId - public int unitID; + public int unitId; public String name; @@ -50,7 +47,6 @@ public class Unit extends PanacheEntity { // TODO: change unit to be the owner, rather than the student being owner @JsonIgnoreProperties("units") @ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = {CascadeType.ALL}) - // @JsonManagedReference public List students; /* @@ -73,7 +69,6 @@ public class Unit extends PanacheEntity { */ @JsonIgnoreProperties("units") @ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = {CascadeType.ALL}) - // @JsonManagedReference @JsonIgnore public List timetables = new ArrayList(); @@ -84,13 +79,15 @@ public Unit() { * Creates a unit. * * @param unitID The unit’s ID. - * @param name The unit’s ID. + * @param name The unit’s name. + * @param course The course that the unit belongs to. * @param duration The unit’s duration. * @param students The list of students enrolled in the unit. */ - public Unit(int unitID, String name, Duration duration, List students) { - this.unitID = unitID; + public Unit(int unitID, String name, String course, Duration duration, List students) { + this.unitId = unitID; this.name = name; + this.course = course; this.duration = duration; this.students = students; this.setStudentsUnits(); @@ -100,14 +97,16 @@ public Unit(int unitID, String name, Duration duration, List students) * Creates a unit. * * @param unitID The unit’s ID. - * @param name The unit’s ID. + * @param name The unit’s name. + * @param course The course that the unit belongs to. * @param duration The unit’s duration. * @param students The list of students enrolled in the unit. * @param wantsLab Whether the unit wants a laboratory room. */ - public Unit(int unitID, String name, Duration duration, List students, boolean wantsLab) { - this.unitID = unitID; + public Unit(int unitID, String name, String course, Duration duration, List students, boolean wantsLab) { + this.unitId = unitID; this.name = name; + this.course = course; this.duration = duration; this.students = students; this.wantsLab = wantsLab; @@ -118,15 +117,17 @@ public Unit(int unitID, String name, Duration duration, List students, * Creates a unit. * * @param unitID The unit’s ID. - * @param name The unit’s ID. + * @param name The unit’s name. + * @param course The course that the unit belongs to. * @param duration The unit’s duration. * @param students The list of students enrolled in the unit. * @param wantsLab Whether the unit wants a laboratory room. * @param room The unit's room. */ - public Unit(int unitID, String name, DayOfWeek dayOfWeek, LocalTime startTime, Duration duration, List students, boolean wantsLab, Room room) { - this.unitID = unitID; + public Unit(int unitID, String name, String course, DayOfWeek dayOfWeek, LocalTime startTime, Duration duration, List students, boolean wantsLab, Room room) { + this.unitId = unitID; this.name = name; + this.course = course; this.dayOfWeek = dayOfWeek; this.startTime = startTime; this.duration = duration; @@ -135,12 +136,12 @@ public Unit(int unitID, String name, DayOfWeek dayOfWeek, LocalTime startTime, D this.room = room; } - public int getUnitID() { - return unitID; + public int getUnitId() { + return unitId; } - public void setUnitID(int unitID) { - this.unitID = unitID; + public void setUnitId(int unitID) { + this.unitId = unitID; } public String getName() { diff --git a/backend/src/test/java/org/acme/solver/TimetableConstraintProviderTest.java b/backend/src/test/java/org/acme/solver/TimetableConstraintProviderTest.java index 4ab03fc..74a8011 100644 --- a/backend/src/test/java/org/acme/solver/TimetableConstraintProviderTest.java +++ b/backend/src/test/java/org/acme/solver/TimetableConstraintProviderTest.java @@ -35,9 +35,9 @@ public class TimetableConstraintProviderTest { */ @Test void studentConflict() { - Unit firstUnit = new Unit(1, "unit1", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1); - Unit conflictingUnit = new Unit(2, "unit2", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), false, ROOM2); - Unit nonConflictingUnit = new Unit(3, "unit3", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2), true, ROOM3); + Unit firstUnit = new Unit(1, "unit1", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1); + Unit conflictingUnit = new Unit(2, "unit2", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), false, ROOM2); + Unit nonConflictingUnit = new Unit(3, "unit3","Course B", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2), true, ROOM3); ConflictingUnit conflictingUnitPair = new ConflictingUnit(firstUnit, conflictingUnit, 1); constraintVerifier.verifyThat(TimetableConstraintProvider::studentConflict) @@ -50,9 +50,9 @@ void studentConflict() { */ @Test void roomConflict() { - Unit firstUnit = new Unit(1, "unit1", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1); - Unit conflictingUnit = new Unit(2, "unit2", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2), true, ROOM1); - Unit nonConflictingUnit = new Unit(3, "unit3", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT3), false, ROOM2); + Unit firstUnit = new Unit(1, "unit1", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1); + Unit conflictingUnit = new Unit(2, "unit2", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2), true, ROOM1); + Unit nonConflictingUnit = new Unit(3, "unit3", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT3), false, ROOM2); constraintVerifier.verifyThat(TimetableConstraintProvider::roomConflict) .given(firstUnit, conflictingUnit, nonConflictingUnit) .penalizesBy(1); @@ -63,9 +63,9 @@ void roomConflict() { */ @Test void roomCapacityConflict() { - Unit firstUnit = new Unit(1, "unit1", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1); - Unit conflictingUnit = new Unit(2, "unit2", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2, STUDENT3, STUDENT4), false, ROOM2); - Unit nonConflictingUnit = new Unit(3, "unit3", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT5), true, ROOM3); + Unit firstUnit = new Unit(1, "unit1", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1); + Unit conflictingUnit = new Unit(2, "unit2", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2, STUDENT3, STUDENT4), false, ROOM2); + Unit nonConflictingUnit = new Unit(3, "unit3", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT5), true, ROOM3); constraintVerifier.verifyThat(TimetableConstraintProvider::roomCapacity) .given(firstUnit, conflictingUnit, nonConflictingUnit) .penalizesBy(1); @@ -76,9 +76,9 @@ void roomCapacityConflict() { */ @Test void labConflict() { - Unit firstUnit = new Unit(1, "unit1", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1); - Unit conflictingUnit = new Unit(2, "unit2", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2), true, ROOM2); - Unit nonConflictingUnit = new Unit(3, "unit3", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT5), true, ROOM3); + Unit firstUnit = new Unit(1, "unit1", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1); + Unit conflictingUnit = new Unit(2, "unit2", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2), true, ROOM2); + Unit nonConflictingUnit = new Unit(3, "unit3", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT5), true, ROOM3); constraintVerifier.verifyThat(TimetableConstraintProvider::labPreference) .given(firstUnit, conflictingUnit, nonConflictingUnit) .penalizesBy(1);