Skip to content

Commit

Permalink
Remove Solver.solve(legacy Solution) because the other solve method c…
Browse files Browse the repository at this point in the history
…an handle it (and we can't overload getBestSolution()) + unit test a legacy solution solving
  • Loading branch information
ge0ffrey committed Apr 5, 2016
1 parent 4fa7b5d commit f7c1560
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 27 deletions.
Expand Up @@ -83,15 +83,6 @@ public interface Solver<Solution_> {
*/ */
Solution_ solve(Solution_ planningProblem); Solution_ solve(Solution_ planningProblem);


/**
* Retained for backwards compatibility with 6.x. This method will be removed in 8.0.
* @param planningProblem never null
* @return never null
* @deprecated In favor of {@link #solve(Object)}
*/
@Deprecated
Solution solve(Solution planningProblem);

/** /**
* This method is thread-safe. * This method is thread-safe.
* @return true if the {@link #solve(Solution_)} method is still running. * @return true if the {@link #solve(Solution_)} method is still running.
Expand Down
Expand Up @@ -197,11 +197,6 @@ public final Solution_ solve(Solution_ planningProblem) {
return solverScope.getBestSolution(); return solverScope.getBestSolution();
} }


@Override
public Solution solve(Solution planningProblem) {
return (Solution) solve((Solution_) planningProblem);
}

public void outerSolvingStarted(DefaultSolverScope<Solution_> solverScope) { public void outerSolvingStarted(DefaultSolverScope<Solution_> solverScope) {
solving.set(true); solving.set(true);
basicPlumbingTermination.resetTerminateEarly(); basicPlumbingTermination.resetTerminateEarly();
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.optaplanner.core.impl.testdata.domain.TestdataEntity; import org.optaplanner.core.impl.testdata.domain.TestdataEntity;
import org.optaplanner.core.impl.testdata.domain.extended.TestdataAnnotatedExtendedSolution; import org.optaplanner.core.impl.testdata.domain.extended.TestdataAnnotatedExtendedSolution;
import org.optaplanner.core.impl.testdata.domain.extended.abstractsolution.TestdataExtendedAbstractSolution; import org.optaplanner.core.impl.testdata.domain.extended.abstractsolution.TestdataExtendedAbstractSolution;
import org.optaplanner.core.impl.testdata.domain.extended.legacysolution.TestdataLegacySolution;
import org.optaplanner.core.impl.testdata.domain.solutionproperties.TestdataNoProblemFactPropertySolution; import org.optaplanner.core.impl.testdata.domain.solutionproperties.TestdataNoProblemFactPropertySolution;
import org.optaplanner.core.impl.testdata.domain.solutionproperties.TestdataProblemFactPropertySolution; import org.optaplanner.core.impl.testdata.domain.solutionproperties.TestdataProblemFactPropertySolution;
import org.optaplanner.core.impl.testdata.domain.solutionproperties.TestdataReadMethodProblemFactCollectionPropertySolution; import org.optaplanner.core.impl.testdata.domain.solutionproperties.TestdataReadMethodProblemFactCollectionPropertySolution;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void problemFactIsPlanningEntityCollectionProperty() {
@Test @Test
public void noProblemFactPropertyWithEasyScoreCalculation() { public void noProblemFactPropertyWithEasyScoreCalculation() {
SolverFactory<TestdataNoProblemFactPropertySolution> solverFactory SolverFactory<TestdataNoProblemFactPropertySolution> solverFactory
= PlannerTestUtils.buildSolverFactoryWithEasyScoreDirector( = PlannerTestUtils.buildSolverFactory(
TestdataNoProblemFactPropertySolution.class, TestdataEntity.class); TestdataNoProblemFactPropertySolution.class, TestdataEntity.class);
solverFactory.buildSolver(); solverFactory.buildSolver();
} }
Expand Down Expand Up @@ -120,4 +121,16 @@ public void extendedAbstractSolution() {
"entityList"); "entityList");
} }


@Test @Deprecated
public void legacySolution() {
SolutionDescriptor<TestdataLegacySolution> solutionDescriptor
= TestdataLegacySolution.buildSolutionDescriptor();
assertMapContainsKeysExactly(solutionDescriptor.getProblemFactMemberAccessorMap());
assertMapContainsKeysExactly(solutionDescriptor.getProblemFactCollectionMemberAccessorMap(),
"problemFacts");
assertMapContainsKeysExactly(solutionDescriptor.getEntityMemberAccessorMap());
assertMapContainsKeysExactly(solutionDescriptor.getEntityCollectionMemberAccessorMap(),
"entityList");
}

} }
Expand Up @@ -31,8 +31,8 @@ public class MutationCounterTest {


@Test @Test
public void countMutationsNone() { public void countMutationsNone() {
SolutionDescriptor solutionDescriptor = TestdataSolution.buildSolutionDescriptor(); SolutionDescriptor<TestdataSolution> solutionDescriptor = TestdataSolution.buildSolutionDescriptor();
MutationCounter mutationCounter = new MutationCounter(solutionDescriptor); MutationCounter<TestdataSolution> mutationCounter = new MutationCounter<>(solutionDescriptor);


TestdataValue val1 = new TestdataValue("1"); TestdataValue val1 = new TestdataValue("1");
TestdataValue val2 = new TestdataValue("2"); TestdataValue val2 = new TestdataValue("2");
Expand Down Expand Up @@ -64,8 +64,8 @@ public void countMutationsNone() {


@Test @Test
public void countMutationsSome() { public void countMutationsSome() {
SolutionDescriptor solutionDescriptor = TestdataSolution.buildSolutionDescriptor(); SolutionDescriptor<TestdataSolution> solutionDescriptor = TestdataSolution.buildSolutionDescriptor();
MutationCounter mutationCounter = new MutationCounter(solutionDescriptor); MutationCounter<TestdataSolution> mutationCounter = new MutationCounter<>(solutionDescriptor);


TestdataValue val1 = new TestdataValue("1"); TestdataValue val1 = new TestdataValue("1");
TestdataValue val2 = new TestdataValue("2"); TestdataValue val2 = new TestdataValue("2");
Expand Down Expand Up @@ -97,8 +97,8 @@ public void countMutationsSome() {


@Test @Test
public void countMutationsAll() { public void countMutationsAll() {
SolutionDescriptor solutionDescriptor = TestdataSolution.buildSolutionDescriptor(); SolutionDescriptor<TestdataSolution> solutionDescriptor = TestdataSolution.buildSolutionDescriptor();
MutationCounter mutationCounter = new MutationCounter(solutionDescriptor); MutationCounter<TestdataSolution> mutationCounter = new MutationCounter<>(solutionDescriptor);


TestdataValue val1 = new TestdataValue("1"); TestdataValue val1 = new TestdataValue("1");
TestdataValue val2 = new TestdataValue("2"); TestdataValue val2 = new TestdataValue("2");
Expand Down
@@ -0,0 +1,64 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* 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.optaplanner.core.impl.solver;

import java.util.Arrays;

import org.junit.Test;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.impl.testdata.domain.TestdataEntity;
import org.optaplanner.core.impl.testdata.domain.TestdataSolution;
import org.optaplanner.core.impl.testdata.domain.TestdataValue;
import org.optaplanner.core.impl.testdata.domain.extended.legacysolution.TestdataLegacySolution;
import org.optaplanner.core.impl.testdata.util.PlannerTestUtils;

import static org.junit.Assert.*;

public class DefaultSolverTest {

@Test
public void solve() {
SolverFactory<TestdataSolution> solverFactory = PlannerTestUtils.buildSolverFactory(
TestdataSolution.class, TestdataEntity.class);
Solver<TestdataSolution> solver = solverFactory.buildSolver();


TestdataSolution solution = new TestdataSolution("s1");
solution.setValueList(Arrays.asList(new TestdataValue("v1"), new TestdataValue("v2")));
solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2")));

solution = solver.solve(solution);
assertNotNull(solution);
}

@Test
public void solveLegacy() {
SolverFactory<TestdataLegacySolution> solverFactory = PlannerTestUtils.buildSolverFactory(
TestdataLegacySolution.class, TestdataEntity.class);
Solver<TestdataLegacySolution> solver = solverFactory.buildSolver();


TestdataLegacySolution solution = new TestdataLegacySolution("s1");
solution.setValueList(Arrays.asList(new TestdataValue("v1"), new TestdataValue("v2")));
solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2")));

solution = solver.solve(solution);
assertNotNull(solution);
}

}
@@ -0,0 +1,89 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates.
*
* 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.optaplanner.core.impl.testdata.domain.extended.legacysolution;

import java.util.Collection;
import java.util.List;

import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.solution.Solution;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.simple.SimpleScore;
import org.optaplanner.core.impl.domain.solution.AbstractSolution;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.core.impl.testdata.domain.TestdataEntity;
import org.optaplanner.core.impl.testdata.domain.TestdataValue;

@PlanningSolution
@Deprecated
public class TestdataLegacySolution implements Solution<SimpleScore> {

public static SolutionDescriptor buildSolutionDescriptor() {
return SolutionDescriptor.buildSolutionDescriptor(TestdataLegacySolution.class, TestdataEntity.class);
}

private List<TestdataValue> valueList;

private List<TestdataEntity> entityList;

private SimpleScore score;

public TestdataLegacySolution() {}

public TestdataLegacySolution(String code) {
super();
}

@ValueRangeProvider(id = "valueRange")
public List<TestdataValue> getValueList() {
return valueList;
}

public void setValueList(List<TestdataValue> valueList) {
this.valueList = valueList;
}

@PlanningEntityCollectionProperty
public List<TestdataEntity> getEntityList() {
return entityList;
}

public void setEntityList(List<TestdataEntity> entityList) {
this.entityList = entityList;
}

@Override
public SimpleScore getScore() {
return score;
}

@Override
public void setScore(SimpleScore score) {
this.score = score;
}

// ************************************************************************
// Complex methods
// ************************************************************************

@Override
public Collection<?> getProblemFacts() {
return valueList;
}

}
Expand Up @@ -17,18 +17,24 @@
package org.optaplanner.core.impl.testdata.util; package org.optaplanner.core.impl.testdata.util;


import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List;


import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.XStream;
import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import org.mockito.AdditionalAnswers; import org.mockito.AdditionalAnswers;
import org.optaplanner.core.api.score.buildin.simple.SimpleScore; import org.optaplanner.core.api.score.buildin.simple.SimpleScore;
import org.optaplanner.core.api.solver.SolverFactory; import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.config.constructionheuristic.ConstructionHeuristicPhaseConfig;
import org.optaplanner.core.config.localsearch.LocalSearchPhaseConfig;
import org.optaplanner.core.config.phase.PhaseConfig;
import org.optaplanner.core.config.score.definition.ScoreDefinitionType; import org.optaplanner.core.config.score.definition.ScoreDefinitionType;
import org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig; import org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig;
import org.optaplanner.core.config.score.trend.InitializingScoreTrendLevel; import org.optaplanner.core.config.score.trend.InitializingScoreTrendLevel;
import org.optaplanner.core.config.solver.SolverConfig; import org.optaplanner.core.config.solver.SolverConfig;
import org.optaplanner.core.config.solver.termination.TerminationConfig;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor; import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.core.impl.score.DummySimpleScoreEasyScoreCalculator; import org.optaplanner.core.impl.score.DummySimpleScoreEasyScoreCalculator;
import org.optaplanner.core.impl.score.buildin.simple.SimpleScoreDefinition; import org.optaplanner.core.impl.score.buildin.simple.SimpleScoreDefinition;
Expand All @@ -45,7 +51,7 @@ public class PlannerTestUtils {
// SolverFactory methods // SolverFactory methods
// ************************************************************************ // ************************************************************************


public static <Solution_> SolverFactory<Solution_> buildSolverFactoryWithEasyScoreDirector( public static <Solution_> SolverFactory<Solution_> buildSolverFactory(
Class<Solution_> solutionClass, Class<?>... entityClasses) { Class<Solution_> solutionClass, Class<?>... entityClasses) {
SolverFactory<Solution_> solverFactory = SolverFactory.createEmpty(); SolverFactory<Solution_> solverFactory = SolverFactory.createEmpty();
SolverConfig solverConfig = solverFactory.getSolverConfig(); SolverConfig solverConfig = solverFactory.getSolverConfig();
Expand All @@ -55,19 +61,25 @@ public static <Solution_> SolverFactory<Solution_> buildSolverFactoryWithEasySco
scoreDirectorFactoryConfig.setScoreDefinitionType(ScoreDefinitionType.SIMPLE); scoreDirectorFactoryConfig.setScoreDefinitionType(ScoreDefinitionType.SIMPLE);
scoreDirectorFactoryConfig.setEasyScoreCalculatorClass(DummySimpleScoreEasyScoreCalculator.class); scoreDirectorFactoryConfig.setEasyScoreCalculatorClass(DummySimpleScoreEasyScoreCalculator.class);
solverConfig.setScoreDirectorFactoryConfig(scoreDirectorFactoryConfig); solverConfig.setScoreDirectorFactoryConfig(scoreDirectorFactoryConfig);
List<PhaseConfig> phaseConfigList = new ArrayList<>(2);
phaseConfigList.add(new ConstructionHeuristicPhaseConfig());
LocalSearchPhaseConfig localSearchPhaseConfig = new LocalSearchPhaseConfig();
TerminationConfig terminationConfig = new TerminationConfig();
terminationConfig.setStepCountLimit(10);
localSearchPhaseConfig.setTerminationConfig(terminationConfig);
phaseConfigList.add(localSearchPhaseConfig);
solverConfig.setPhaseConfigList(phaseConfigList);
return solverFactory; return solverFactory;
} }


public static <Solution_> SolverFactory<Solution_> buildSolverFactoryWithDroolsScoreDirector( public static <Solution_> SolverFactory<Solution_> buildSolverFactoryWithDroolsScoreDirector(
Class<Solution_> solutionClass, Class<?>... entityClasses) { Class<Solution_> solutionClass, Class<?>... entityClasses) {
SolverFactory<Solution_> solverFactory = SolverFactory.createEmpty(); SolverFactory<Solution_> solverFactory = buildSolverFactory(solutionClass, entityClasses);
SolverConfig solverConfig = solverFactory.getSolverConfig(); SolverConfig solverConfig = solverFactory.getSolverConfig();
solverConfig.setSolutionClass(solutionClass); ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = solverConfig.getScoreDirectorFactoryConfig();
solverConfig.setEntityClassList(Arrays.asList(entityClasses)); scoreDirectorFactoryConfig.setEasyScoreCalculatorClass(null);
ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig();
scoreDirectorFactoryConfig.setScoreDrlList(Collections.singletonList( scoreDirectorFactoryConfig.setScoreDrlList(Collections.singletonList(
"org/optaplanner/core/impl/score/dummySimpleScoreDroolsScoreRules.drl")); "org/optaplanner/core/impl/score/dummySimpleScoreDroolsScoreRules.drl"));
solverConfig.setScoreDirectorFactoryConfig(scoreDirectorFactoryConfig);
return solverFactory; return solverFactory;
} }


Expand Down

0 comments on commit f7c1560

Please sign in to comment.