/*
* Copyright 2012 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.heuristic.selector.entity.decorator;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.drools.planner.core.heuristic.selector.SelectorTestUtils;
import org.drools.planner.core.heuristic.selector.common.SelectionCacheType;
import org.drools.planner.core.heuristic.selector.common.decorator.SelectionFilter;
import org.drools.planner.core.heuristic.selector.entity.EntitySelector;
import org.drools.planner.core.phase.AbstractSolverPhaseScope;
import org.drools.planner.core.phase.step.AbstractStepScope;
import org.drools.planner.core.score.director.ScoreDirector;
import org.drools.planner.core.solver.scope.DefaultSolverScope;
import org.drools.planner.core.testdata.domain.TestdataEntity;
import org.junit.Test;
import static org.drools.planner.core.testdata.util.PlannerAssert.*;
import static org.mockito.Mockito.*;
public class FilteringEntitySelectorTest {
@Test
public void cacheTypeSolver() {
runCacheType(SelectionCacheType.SOLVER, 1);
}
@Test
public void cacheTypePhase() {
runCacheType(SelectionCacheType.PHASE, 2);
}
@Test
public void cacheTypeStep() {
runCacheType(SelectionCacheType.STEP, 5);
}
@Test
public void cacheTypeJustInTime() {
runCacheType(SelectionCacheType.JUST_IN_TIME, 5);
}
public void runCacheType(SelectionCacheType cacheType, int timesCalled) {
EntitySelector childEntitySelector = SelectorTestUtils.mockEntitySelector(TestdataEntity.class,
new TestdataEntity("e1"), new TestdataEntity("e2"), new TestdataEntity("e3"), new TestdataEntity("e4"));
SelectionFilter<TestdataEntity> entityFilter = new SelectionFilter<TestdataEntity>() {
public boolean accept(ScoreDirector scoreDirector, TestdataEntity entity) {
return !entity.getCode().equals("e3");
}
};
List<SelectionFilter> entityFilterList = Arrays.<SelectionFilter>asList(entityFilter);
EntitySelector entitySelector = new FilteringEntitySelector(childEntitySelector, entityFilterList);
if (cacheType.isCached()) {
entitySelector = new CachingEntitySelector(entitySelector, cacheType, false);
}
DefaultSolverScope solverScope = mock(DefaultSolverScope.class);
entitySelector.solvingStarted(solverScope);
AbstractSolverPhaseScope phaseScopeA = mock(AbstractSolverPhaseScope.class);
when(phaseScopeA.getSolverScope()).thenReturn(solverScope);
entitySelector.phaseStarted(phaseScopeA);
AbstractStepScope stepScopeA1 = mock(AbstractStepScope.class);
when(stepScopeA1.getPhaseScope()).thenReturn(phaseScopeA);
entitySelector.stepStarted(stepScopeA1);
runAsserts(entitySelector, cacheType);
entitySelector.stepEnded(stepScopeA1);
AbstractStepScope stepScopeA2 = mock(AbstractStepScope.class);
when(stepScopeA2.getPhaseScope()).thenReturn(phaseScopeA);
entitySelector.stepStarted(stepScopeA2);
runAsserts(entitySelector, cacheType);
entitySelector.stepEnded(stepScopeA2);
entitySelector.phaseEnded(phaseScopeA);
AbstractSolverPhaseScope phaseScopeB = mock(AbstractSolverPhaseScope.class);
when(phaseScopeB.getSolverScope()).thenReturn(solverScope);
entitySelector.phaseStarted(phaseScopeB);
AbstractStepScope stepScopeB1 = mock(AbstractStepScope.class);
when(stepScopeB1.getPhaseScope()).thenReturn(phaseScopeB);
entitySelector.stepStarted(stepScopeB1);
runAsserts(entitySelector, cacheType);
entitySelector.stepEnded(stepScopeB1);
AbstractStepScope stepScopeB2 = mock(AbstractStepScope.class);
when(stepScopeB2.getPhaseScope()).thenReturn(phaseScopeB);
entitySelector.stepStarted(stepScopeB2);
runAsserts(entitySelector, cacheType);
entitySelector.stepEnded(stepScopeB2);
AbstractStepScope stepScopeB3 = mock(AbstractStepScope.class);
when(stepScopeB3.getPhaseScope()).thenReturn(phaseScopeB);
entitySelector.stepStarted(stepScopeB3);
runAsserts(entitySelector, cacheType);
entitySelector.stepEnded(stepScopeB3);
entitySelector.phaseEnded(phaseScopeB);
entitySelector.solvingEnded(solverScope);
verifySolverPhaseLifecycle(childEntitySelector, 1, 2, 5);
verify(childEntitySelector, times(timesCalled)).iterator();
verify(childEntitySelector, times(timesCalled)).getSize();
}
private void runAsserts(EntitySelector entitySelector, SelectionCacheType cacheType) {
Iterator<Object> iterator = entitySelector.iterator();
assertNotNull(iterator);
assertTrue(iterator.hasNext());
assertCode("e1", iterator.next());
assertTrue(iterator.hasNext());
assertCode("e2", iterator.next());
assertTrue(iterator.hasNext());
assertCode("e4", iterator.next());
assertFalse(iterator.hasNext());
assertEquals(false, entitySelector.isContinuous());
assertEquals(false, entitySelector.isNeverEnding());
assertEquals((cacheType.isNotCached() ? 4L : 3L), entitySelector.getSize());
}
}