我的optaplanner web應用程序出現問題。這個應用程序是基於車輛演示網站示例。我想在課程時間表的Web示例中實現一個手動doChangeMove函數。我的代碼(courseTimeTablingManager.java)情況如下:爲什麼optaplanner在調用move.doMove(guiScoreDirector)時爲空指針;
public synchronized boolean solve(final String sessionId) {
//final Solver<CourseSchedule> solver = solverFactory.buildSolver();
Solver<CourseSchedule> solver;
if(sessionSolverMap.containsKey(sessionId)){
solver = sessionSolverMap.get(sessionId);
}else{
solver = solverFactory.buildSolver();
}
//solver = solverFactory.buildSolver();
solver.addEventListener(new SolverEventListener<CourseSchedule>() {
@Override
public void bestSolutionChanged(BestSolutionChangedEvent<CourseSchedule> event) {
CourseSchedule bestSolution = event.getNewBestSolution();
synchronized (CourseTimeTablingManager.this) {
sessionSolutionMap.put(sessionId, bestSolution);
}
}
});
if (sessionSolverMap.containsKey(sessionId)) {
return false;
}
sessionSolverMap.put(sessionId, solver);
final CourseSchedule solution = retrieveOrCreateSolution(sessionId, xml);
executor.submit(new Runnable() {
@Override
public void run() {
Solver<CourseSchedule> solver = sessionSolverMap.get(sessionId);
CourseSchedule bestSolution = solver.solve(solution);
synchronized (CourseTimeTablingManager.this) {
sessionSolutionMap.put(sessionId, bestSolution);
//sessionSolverMap.remove(sessionId);
}
}
});
return true;
}
public synchronized boolean terminateEarly(String sessionId) {
//Solver<CourseSchedule> solver = sessionSolverMap.remove(sessionId);
Solver<CourseSchedule> solver = sessionSolverMap.get(sessionId);
if (solver != null) {
solver.terminateEarly();
return true;
} else {
return false;
}
}
public synchronized ChangeMove createChangeMove(Object entity, String variableName, Object toPlanningValue, String sessionId) {
// TODO Solver should support building a ChangeMove
Solver<CourseSchedule> solver = sessionSolverMap.get(sessionId);
ScoreDirectorFactory scoreDirectorFactory = solver.getScoreDirectorFactory();
guiScoreDirector = scoreDirectorFactory.buildScoreDirector();
InnerScoreDirector guiInnerScoreDirector = (InnerScoreDirector) this.guiScoreDirector;
SolutionDescriptor solutionDescriptor = guiInnerScoreDirector.getSolutionDescriptor();
GenuineVariableDescriptor variableDescriptor = solutionDescriptor.findGenuineVariableDescriptorOrFail(
entity, variableName);
if (variableDescriptor.isChained()) {
SupplyManager supplyManager = guiInnerScoreDirector.getSupplyManager();
SingletonInverseVariableSupply inverseVariableSupply = supplyManager.demand(
new SingletonInverseVariableDemand(variableDescriptor));
return new ChainedChangeMove(entity, variableDescriptor, inverseVariableSupply, toPlanningValue);
} else {
return new ChangeMove(entity, variableDescriptor, toPlanningValue);
}
}
public synchronized void doChangeMove(Object entity, String variableName, Object toPlanningValue, String sessionId) {
ChangeMove move = createChangeMove(entity, variableName, toPlanningValue, sessionId);
doMove(move, sessionId);
}
public synchronized void doMove(Move move, String sessionId) {
Solver<CourseSchedule> solver = sessionSolverMap.get(sessionId);
if (solver.isSolving()) {
//logger.error("Not doing user move ({}) because the solver is solving.", move);
System.out.println("!!!!!!!!!!!!!!!!!! Not doing user move ({}) because the solver is solving.");
return;
}
if (!move.isMoveDoable(guiScoreDirector)) {
System.out.println("################## Not doing user move ({}) because it is not doable.");
return;
}
//System.out.println("------------------ doing user move ({}) .");
move.doMove(guiScoreDirector); // <----- Here, crash with NULL pointer!!
System.out.println("------------------ doing user move done ({}), try update score ... .");
guiScoreDirector.calculateScore();
}
每次我運行的應用程序,它會在功能doMove的move.doMove(guiScoreDirector)崩潰。唯一的例外是:
Caused by: java.lang.NullPointerException
at org.optaplanner.core.impl.score.director.drools.DroolsScoreDirector.update(DroolsScoreDirector.java:157)
at org.optaplanner.core.impl.score.director.drools.DroolsScoreDirector.afterVariableChanged(DroolsScoreDirector.java:152)
at org.optaplanner.core.impl.heuristic.selector.move.generic.ChangeMove.doMoveOnGenuineVariables(ChangeMove.java:69)
at org.optaplanner.core.impl.heuristic.move.AbstractMove.doMove(AbstractMove.java:34)
at org.optaplanner.webexamples.tt.rest.cdi.CourseTimeTablingManager.doMove(CourseTimeTablingManager.java:232)
at org.optaplanner.webexamples.tt.rest.cdi.CourseTimeTablingManager.doChangeMove(CourseTimeTablingManager.java:217)
at org.optaplanner.webexamples.tt.rest.cdi.CourseTimeTablingManager$Proxy$_$$_WeldClientProxy.doChangeMove(Unknown Source)
at org.optaplanner.webexamples.tt.rest.service.DefaultCourseTimeTablingRestService.move(DefaultCourseTimeTablingRestService.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
... 32 more
誰能幫助?提前致謝!
忘記把optaplanner版本:optaplanner-distribution-6.5.0.Final –