package com.example.solver;
import com.example.domain.TimeTable;
import com.example.persistence.TimeTableRepository;
import org.optaplanner.core.api.score.ScoreManager;
import org.optaplanner.core.api.solver.SolverManager;
import org.optaplanner.core.api.solver.SolverStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/timeTable")
public class TimeTableController {
@Autowired
private TimeTableRepository timeTableRepository;
@Autowired
private SolverManager<TimeTable, Long> solverManager;
@Autowired
private ScoreManager<TimeTable> scoreManager;
// To try, GET http://localhost:8080/timeTable
@GetMapping()
public TimeTable getTimeTable() {
// Get the solver status before loading the solution
// to avoid the race condition that the solver terminates between them
SolverStatus solverStatus = getSolverStatus();
TimeTable solution = timeTableRepository.findById(TimeTableRepository.SINGLETON_TIME_TABLE_ID);
scoreManager.updateScore(solution); // Sets the score
solution.setSolverStatus(solverStatus);
return solution;
}
@PostMapping("/solve")
public void solve() {
solverManager.solveAndListen(TimeTableRepository.SINGLETON_TIME_TABLE_ID,
timeTableRepository::findById,
timeTableRepository::save);
}
public SolverStatus getSolverStatus() {
return solverManager.getSolverStatus(TimeTableRepository.SINGLETON_TIME_TABLE_ID);
}
@PostMapping("/stopSolving")
public void stopSolving() {
solverManager.terminateEarly(TimeTableRepository.SINGLETON_TIME_TABLE_ID);
}
}
package com.example.solver;
import com.example.domain.TimeTable;
import com.example.persistence.TimeTableRepository;
import org.optaplanner.core.api.score.ScoreManager;
import org.optaplanner.core.api.solver.SolverManager;
import org.optaplanner.core.api.solver.SolverStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/timeTable")
public class TimeTableController {
@Autowired
private TimeTableRepository timeTableRepository;
@Autowired
private SolverManager<TimeTable, Long> solverManager;
@Autowired
private ScoreManager<TimeTable> scoreManager;
// To try, GET http://localhost:8080/timeTable
@GetMapping()
public TimeTable getTimeTable() {
// Get the solver status before loading the solution
// to avoid the race condition that the solver terminates between them
SolverStatus solverStatus = getSolverStatus();
TimeTable solution = timeTableRepository.findById(TimeTableRepository.SINGLETON_TIME_TABLE_ID);
scoreManager.updateScore(solution); // Sets the score
solution.setSolverStatus(solverStatus);
return solution;
}
@PostMapping("/solve")
public void solve() {
solverManager.solveAndListen(TimeTableRepository.SINGLETON_TIME_TABLE_ID,
timeTableRepository::findById,
timeTableRepository::save);
}
public SolverStatus getSolverStatus() {
return solverManager.getSolverStatus(TimeTableRepository.SINGLETON_TIME_TABLE_ID);
}
@PostMapping("/stopSolving")
public void stopSolving() {
solverManager.terminateEarly(TimeTableRepository.SINGLETON_TIME_TABLE_ID);
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow