Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.drools.planner.core.solver;
 
 import java.util.List;
 
 
 public class DefaultSolver implements Solver {
 
     protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
     protected SolverEventSupport solverEventSupport = new SolverEventSupport(this);
 
     protected Long randomSeed;
 
     // Note that the basicPlumbingTermination is a component of this termination
     protected Termination termination;
     protected List<SolverPhasesolverPhaseList;
 
     protected AtomicBoolean solving = new AtomicBoolean(false);
 
     protected DefaultSolverScope solverScope = new DefaultSolverScope();
 
     public void setRandomSeed(long randomSeed) {
         this. = randomSeed;
     }
 
     public void setSolutionDirector(DefaultSolutionDirector solutionDirector) {
         .setSolutionDirector(solutionDirector);
     }
 
     public ScoreDefinition getScoreDefinition() {
         return .getScoreDefinition();
     }
 
     public void setBasicPlumbingTermination(BasicPlumbingTermination basicPlumbingTermination) {
         this. = basicPlumbingTermination;
     }
 
     public void setTermination(Termination termination) {
         this. = termination;
     }
 
     public void setBestSolutionRecaller(BestSolutionRecaller bestSolutionRecaller) {
         this. = bestSolutionRecaller;
     }
 
     public List<SolverPhasegetSolverPhaseList() {
         return ;
     }
 
     public void setSolverPhaseList(List<SolverPhasesolverPhaseList) {
         this. = solverPhaseList;
     }
 
     public void setStartingSolution(Solution startingSolution) {
         .getSolutionDirector().setWorkingSolution(startingSolution);
     }
 
     public Solution getBestSolution() {
         return .getBestSolution();
    }
    public long getTimeMillisSpend() {
        return .calculateTimeMillisSpend();
    }
        return ;
    }
    // ************************************************************************
    // Worker methods
    // ************************************************************************
    public boolean isSolving() {
        return .get();
    }
    public boolean terminateEarly() {
        return .terminateEarly();
    }
    public boolean isTerminateEarly() {
    }
    public boolean addPlanningFactChange(PlanningFactChange planningFactChange) {
        return .addPlanningFactChange(planningFactChange);
    }
    public final void solve() {
        .set(true);
        .setRestartSolver(true);
        while (.isRestartSolver()) {
            .setRestartSolver(false);
            solvingStarted();
            runSolverPhases();
            solvingEnded();
            checkPlanningFactChanges();
        }
        .set(false);
    }
    public void solvingStarted(DefaultSolverScope solverScope) {
        if (solverScope.getWorkingSolution() == null) {
            throw new IllegalStateException("The startingSolution must not be null." +
                    " Use Solver.setStartingSolution(Solution).");
        }
        solverScope.reset();
        if ( != null) {
            .info("Solving with random seed ({}).");
            solverScope.setWorkingRandom(new Random());
        } else {
            .info("Solving with a non-fixed random seed.");
            solverScope.setWorkingRandom(new Random());
        }
        .solvingStarted(solverScope);
        .info("Starting with time spend ({}), score ({}), new best score ({}).",
                new Object[]{solverScope.calculateTimeMillisSpend(), solverScope.getStartingInitializedScore(),
                        solverScope.getBestScore()});
    }
    protected void runSolverPhases() {
        Iterator<SolverPhaseit = .iterator();
        while (!.isSolverTerminated() && it.hasNext()) {
            SolverPhase solverPhase = it.next();
            solverPhase.solve();
        }
        // TODO support doing round-robin of phases (only non-construction heuristics)
    }
    public void solvingEnded(DefaultSolverScope solverScope) {
        .solvingEnded(solverScope);;
        long timeMillisSpend = solverScope.calculateTimeMillisSpend();
        if (timeMillisSpend == 0L) {
            // Avoid divide by zero exception on a fast CPU
            timeMillisSpend = 1L;
        }
        long averageCalculateCountPerSecond = solverScope.getCalculateCount() * 1000L / timeMillisSpend;
        .info("Solved with time spend ({}) for best score ({})"
                + " with average calculate count per second ({})."new Object[]{
                timeMillisSpend,
                solverScope.getBestScore(),
                averageCalculateCountPerSecond
        });
    }
    private void checkPlanningFactChanges() {
        BlockingQueue<PlanningFactChangeplanningFactChangeQueue
                = .getPlanningFactChangeQueue();
        if (!planningFactChangeQueue.isEmpty()) {
            .setRestartSolver(true);
            Score score = null;
            int count = 0;
            PlanningFactChange planningFactChange = planningFactChangeQueue.poll();
            while (planningFactChange != null) {
                score = doPlanningFactChange(planningFactChange);
                count++;
                planningFactChange = planningFactChangeQueue.poll();
            }
            .info("Done {} PlanningFactChange(s), score ({}), possibly uninitialized. Restarting solver."countscore);
        }
    }
    private Score doPlanningFactChange(PlanningFactChange planningFactChange) {
        planningFactChange.doChange(.getSolutionDirector());
        .debug("PlanningFactChange done with new score ({})."score);
        return score;
    }
    public void addEventListener(SolverEventListener eventListener) {
        .addEventListener(eventListener);
    }
    public void removeEventListener(SolverEventListener eventListener) {
        .removeEventListener(eventListener);
    }
    public void addSolverPhaseLifecycleListener(SolverPhaseLifecycleListener solverPhaseLifecycleListener) {
        for (SolverPhase solverPhase : ) {
            solverPhase.addSolverPhaseLifecycleListener(solverPhaseLifecycleListener);
        }
    }
    public void removeSolverPhaseLifecycleListener(SolverPhaseLifecycleListener solverPhaseLifecycleListener) {
        for (SolverPhase solverPhase : ) {
            solverPhase.addSolverPhaseLifecycleListener(solverPhaseLifecycleListener);
        }
    }
New to GrepCode? Check out our FAQ X