Skip to content

Commit

Permalink
refactor(database): ♻️ refactor Unit constructors, clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingPufferFish committed Sep 30, 2024
1 parent b41d9a8 commit 2d4a8c0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 51 deletions.
14 changes: 8 additions & 6 deletions backend/src/main/java/org/acme/TimetableResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions backend/src/main/java/org/acme/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +21,7 @@
*/
@Entity
public class Room extends PanacheEntity {

@PlanningId
public String roomCode;

Expand All @@ -45,7 +44,6 @@ public class Room extends PanacheEntity {
*/
@JsonIgnoreProperties("rooms")
@ManyToMany(mappedBy = "rooms", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
// @JsonManagedReference
@JsonIgnore
public List<Timetable> timetables = new ArrayList<Timetable>();

Expand Down
4 changes: 0 additions & 4 deletions backend/src/main/java/org/acme/domain/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,8 +23,6 @@
@Entity
public class Student extends PanacheEntity{

// String studentID;

public String name;

@JsonIgnoreProperties("students")
Expand All @@ -35,7 +32,6 @@ public class Student extends PanacheEntity{
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "unit_id")
)
// @JsonManagedReference
@JsonIgnore
public List<Unit> units = new ArrayList<Unit>();

Expand Down
9 changes: 2 additions & 7 deletions backend/src/main/java/org/acme/domain/Timetable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<Room> rooms;

Expand All @@ -76,7 +72,6 @@ public class Timetable extends PanacheEntity {
joinColumns = @JoinColumn(name = "timetable_id"),
inverseJoinColumns = @JoinColumn(name = "unit_id")
)
// @JsonManagedReference
@PlanningEntityCollectionProperty
public List<Unit> units;

Expand Down Expand Up @@ -191,7 +186,7 @@ public List<ConflictingUnit> calculateSoftUnitConflictList() {
ArrayList<ConflictingUnit> out = new ArrayList<ConflictingUnit>();
for (var first : units) {
for (var second : units) {
if (first.getUnitID() >= second.getUnitID()) {
if (first.getUnitId() >= second.getUnitId()) {
continue;
}
int numStudents = 0;
Expand Down
39 changes: 20 additions & 19 deletions backend/src/main/java/org/acme/domain/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,7 +30,7 @@
public class Unit extends PanacheEntity {

@PlanningId
public int unitID;
public int unitId;

public String name;

Expand All @@ -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<Student> students;

/*
Expand All @@ -73,7 +69,6 @@ public class Unit extends PanacheEntity {
*/
@JsonIgnoreProperties("units")
@ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
// @JsonManagedReference
@JsonIgnore
public List<Timetable> timetables = new ArrayList<Timetable>();

Expand All @@ -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<Student> students) {
this.unitID = unitID;
public Unit(int unitID, String name, String course, Duration duration, List<Student> students) {
this.unitId = unitID;
this.name = name;
this.course = course;
this.duration = duration;
this.students = students;
this.setStudentsUnits();
Expand All @@ -100,14 +97,16 @@ public Unit(int unitID, String name, Duration duration, List<Student> 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<Student> students, boolean wantsLab) {
this.unitID = unitID;
public Unit(int unitID, String name, String course, Duration duration, List<Student> students, boolean wantsLab) {
this.unitId = unitID;
this.name = name;
this.course = course;
this.duration = duration;
this.students = students;
this.wantsLab = wantsLab;
Expand All @@ -118,15 +117,17 @@ public Unit(int unitID, String name, Duration duration, List<Student> 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<Student> students, boolean wantsLab, Room room) {
this.unitID = unitID;
public Unit(int unitID, String name, String course, DayOfWeek dayOfWeek, LocalTime startTime, Duration duration, List<Student> students, boolean wantsLab, Room room) {
this.unitId = unitID;
this.name = name;
this.course = course;
this.dayOfWeek = dayOfWeek;
this.startTime = startTime;
this.duration = duration;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 2d4a8c0

Please sign in to comment.