package org.optaplanner.core.impl.domain.solution.cloner;
import junit.framework.TestCase;
import org.optaplanner.core.api.domain.entity.PlanningEntity;
import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.Solution;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.domain.variable.PlanningVariable;
import org.optaplanner.core.api.domain.variable.PlanningVariableGraphType;
import org.optaplanner.core.api.score.buildin.simple.SimpleScore;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Description: <br>
* <p>
* Date: 07.04.15<br>
* </p>
*/
public class FieldAccessingSolutionClonerTest extends TestCase {
FieldAccessingSolutionCloner cloner;
public static abstract class AbstractTestSolution implements Solution<SimpleScore> {
List<TestVehicle> vehicles;
private SimpleScore score;
List<TestWorkload> workloads;
@Override
public SimpleScore getScore() {
return score;
}
@Override
public void setScore(SimpleScore score) {
this.score = score;
}
@Override
public Collection<?> getProblemFacts() {
return vehicles;
}
@PlanningEntityCollectionProperty
@ValueRangeProvider(id = "workloadsRange")
public List<TestWorkload> getWorkloads() {
return workloads;
}
@ValueRangeProvider(id = "vehiclesRange")
public List<TestVehicle> getVehicles() {
return vehicles;
}
}
public static class TestSolution extends AbstractTestSolution {
}
public static abstract class AbstractTestWorkload implements TestStandstill {
String id;
private TestStandstill previousStandstill;
@PlanningVariable(graphType = PlanningVariableGraphType.CHAINED,
valueRangeProviderRefs = {"vehiclesRange", "workloadsRange"})
public TestStandstill getPreviousStandstill() {
return previousStandstill;
}
public void setPreviousStandstill(
TestStandstill previousStandstill) {
this.previousStandstill = previousStandstill;
}
public String toString() {
return getClass().getSimpleName() + "{id=" + id + "}";
}
}
@PlanningEntity
public static class TestWorkload extends AbstractTestWorkload {
}
public static class TestVehicle implements TestStandstill {
String id;
public String toString() {
return getClass().getSimpleName() + "{id=" + id + "}";
}
}
public interface TestStandstill {
}
public void testCloneSolutionWithInheritedVariables() {
SolutionDescriptor descriptor = new SolutionDescriptor(TestSolution.class);
cloner = new FieldAccessingSolutionCloner(descriptor);
TestSolution solution = createTestSolution();
TestSolution clone = (TestSolution) cloner.cloneSolution(solution);
assertTrue(clone != null);
assertTrue(clone != solution); assertTrue(clone.workloads != solution.workloads); assertTrue(clone.workloads.size() == solution.workloads.size()); assertTrue(clone.workloads.get(0) != solution.workloads.get(0)); assertTrue(
clone.workloads.get(0).getPreviousStandstill() == clone.workloads.get(1)); assertTrue(clone.workloads.get(1).getPreviousStandstill() == clone.vehicles.get(0));
}
private TestSolution createTestSolution() {
TestSolution s = new TestSolution();
s.vehicles = new ArrayList<>();
s.vehicles.add(new TestVehicle());
s.workloads = new ArrayList<>();
s.workloads.add(new TestWorkload());
s.workloads.add(new TestWorkload());
s.workloads.get(0).setPreviousStandstill(s.workloads.get(1));
s.workloads.get(1).setPreviousStandstill(s.vehicles.get(0));
s.workloads.get(0).id = "W1";
s.workloads.get(1).id = "W2";
s.vehicles.get(0).id = "V1";
return s;
}
}