Skip to content

Commit

Permalink
AbstractSolution: Revert getProblemFactsFromClass to addProblemFactsF…
Browse files Browse the repository at this point in the history
…romClass (Adds to an existing to {@link List} to avoid copying the entire list with {@link List#addAll(Collection)}.)
  • Loading branch information
ge0ffrey committed Mar 29, 2016
1 parent f2e7d9f commit eddc025
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;


import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty; import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
Expand Down Expand Up @@ -50,21 +51,25 @@ public void setScore(S score) {
} }


/** /**
* Convenience method for tests. * @return a list with every problem fact that is in a field of this class
* * (directly or indirectly as an element of a {@link Collection} or {@link Map} field)
* @return All entities from anywhere in this class' hierarchy.
*/ */
@ProblemFactCollectionProperty @ProblemFactCollectionProperty
protected Collection<?> getProblemFacts() { protected List<Object> getProblemFactList() {
Class<? extends AbstractSolution> instanceClass = getClass(); List<Object> factList = new ArrayList<Object>();
return getProblemFactsFromClass(instanceClass); addProblemFactsFromClass(factList, getClass());
return factList;
} }


private Collection<Object> getProblemFactsFromClass(Class<?> instanceClass) { /**
Collection<Object> factList = new ArrayList<>(); * Adds to an existing to {@link List} to avoid copying the entire list with {@link List#addAll(Collection)}.
* @param factList never null
* @param instanceClass never null
*/
private void addProblemFactsFromClass(List<Object> factList, Class<?> instanceClass) {
if (instanceClass.equals(AbstractSolution.class)) { if (instanceClass.equals(AbstractSolution.class)) {
// The field score should not be included // The field score should not be included
return factList; return;
} }
for (Field field : instanceClass.getDeclaredFields()) { for (Field field : instanceClass.getDeclaredFields()) {
field.setAccessible(true); field.setAccessible(true);
Expand All @@ -91,9 +96,8 @@ private Collection<Object> getProblemFactsFromClass(Class<?> instanceClass) {
} }
Class<?> superclass = instanceClass.getSuperclass(); Class<?> superclass = instanceClass.getSuperclass();
if (superclass != null) { if (superclass != null) {
factList.addAll(getProblemFactsFromClass(superclass)); addProblemFactsFromClass(factList, superclass);
} }
return factList;
} }


private boolean isFieldAPlanningEntityPropertyOrPlanningEntityCollectionProperty(Field field, private boolean isFieldAPlanningEntityPropertyOrPlanningEntityCollectionProperty(Field field,
Expand Down
Expand Up @@ -49,7 +49,7 @@ public int countMutations(Solution_ a, Solution_ b) {
for (Iterator aIt = aEntities.iterator(), bIt = bEntities.iterator(); aIt.hasNext() && bIt.hasNext(); ) { for (Iterator aIt = aEntities.iterator(), bIt = bEntities.iterator(); aIt.hasNext() && bIt.hasNext(); ) {
Object aEntity = aIt.next(); Object aEntity = aIt.next();
Object bEntity = bIt.next(); Object bEntity = bIt.next();
for (GenuineVariableDescriptor variableDescriptor : entityDescriptor.getGenuineVariableDescriptors()) { for (GenuineVariableDescriptor<Solution_> variableDescriptor : entityDescriptor.getGenuineVariableDescriptors()) {
// TODO broken if the value is an entity, because then it's never the same // TODO broken if the value is an entity, because then it's never the same
// But we don't want to depend on value/entity equals() => use surrogate entity id's to compare // But we don't want to depend on value/entity equals() => use surrogate entity id's to compare
// https://issues.jboss.org/browse/PLANNER-170 // https://issues.jboss.org/browse/PLANNER-170
Expand All @@ -70,4 +70,5 @@ public int countMutations(Solution_ a, Solution_ b) {
public String toString() { public String toString() {
return "MutationCounter(" + solutionDescriptor + ")"; return "MutationCounter(" + solutionDescriptor + ")";
} }

} }
Expand Up @@ -16,7 +16,6 @@


package org.optaplanner.core.impl.domain.solution; package org.optaplanner.core.impl.domain.solution;


import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
Expand Down Expand Up @@ -63,7 +62,7 @@ public void getProblemFacts() {
solution.setSingleEntity(singleEntity); solution.setSingleEntity(singleEntity);
SimpleScore score = SimpleScore.valueOf(-10); SimpleScore score = SimpleScore.valueOf(-10);
solution.setScore(score); solution.setScore(score);
assertCollectionContainsExactly((Collection<Object>) solution.getProblemFacts(), singleValue, v1, v2, v3); assertCollectionContainsExactly(solution.getProblemFactList(), singleValue, v1, v2, v3);
} }


@Test @Test
Expand All @@ -78,7 +77,7 @@ public void getProblemFactsWithNullField() {
solution.setEntityList(null); solution.setEntityList(null);
solution.setSingleEntity(null); solution.setSingleEntity(null);
solution.setScore(null); solution.setScore(null);
assertCollectionContainsExactly((Collection<Object>) solution.getProblemFacts(), v1, v2, v3); assertCollectionContainsExactly(solution.getProblemFactList(), v1, v2, v3);
} }


public static class TestdataAbstractSolutionBasedSolution extends AbstractSolution<SimpleScore> { public static class TestdataAbstractSolutionBasedSolution extends AbstractSolution<SimpleScore> {
Expand Down
Expand Up @@ -19,11 +19,14 @@
import java.util.List; import java.util.List;


import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty; import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.drools.ProblemFactCollectionProperty;
import org.optaplanner.core.api.domain.solution.drools.ProblemFactProperty; import org.optaplanner.core.api.domain.solution.drools.ProblemFactProperty;
import org.optaplanner.core.api.domain.solution.PlanningSolution; import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.impl.domain.solution.AbstractSolution; import org.optaplanner.core.impl.domain.solution.AbstractSolution;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor; import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.core.impl.testdata.domain.TestdataEntity; import org.optaplanner.core.impl.testdata.domain.TestdataEntity;
import org.optaplanner.core.impl.testdata.domain.TestdataValue;


@PlanningSolution @PlanningSolution
public class TestdataExtendedAbstractSolution extends AbstractSolution { public class TestdataExtendedAbstractSolution extends AbstractSolution {
Expand All @@ -32,6 +35,7 @@ public static SolutionDescriptor buildSolutionDescriptor() {
return SolutionDescriptor.buildSolutionDescriptor(TestdataExtendedAbstractSolution.class, TestdataEntity.class); return SolutionDescriptor.buildSolutionDescriptor(TestdataExtendedAbstractSolution.class, TestdataEntity.class);
} }


private List<TestdataValue> valueList;
private Object extraObject; private Object extraObject;


private List<TestdataEntity> entityList; private List<TestdataEntity> entityList;
Expand All @@ -40,6 +44,16 @@ public TestdataExtendedAbstractSolution(String code) {
super(); super();
} }


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

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

@ProblemFactProperty @ProblemFactProperty
public Object getExtraObject() { public Object getExtraObject() {
return extraObject; return extraObject;
Expand Down

0 comments on commit eddc025

Please sign in to comment.