/*
 * Copyright 2011 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.bruteforce;

import java.util.Collection;
import java.util.Iterator;

import org.drools.WorkingMemory;
import org.drools.planner.core.domain.meta.PlanningVariableDescriptor;
import org.drools.runtime.rule.FactHandle;

public class BruteForcePlanningVariableIterator {

    private final BruteForceSolverScope bruteForceSolverScope;
    private final Object planningEntity;
    private final PlanningVariableDescriptor planningVariableDescriptor;

    private Collection<?> planningValues;
    private Iterator<?> planningValuesIterator;

    public BruteForcePlanningVariableIterator(BruteForceSolverScope bruteForceSolverScope, Object planningEntity,
            PlanningVariableDescriptor planningVariableDescriptor) {
        this.bruteForceSolverScope = bruteForceSolverScope;
        this.planningEntity = planningEntity;
        this.planningVariableDescriptor = planningVariableDescriptor;

        planningValues = planningVariableDescriptor.getRangeValues(bruteForceSolverScope.getWorkingSolution());
        planningValuesIterator = planningValues.iterator();
    }

    public boolean hasNext() {
        return planningValuesIterator.hasNext();
    }

    public void next() {
        Object value = planningValuesIterator.next();
        changeValue(value);
    }

    public void reset() {
        planningValuesIterator = planningValues.iterator();
        Object value = planningValuesIterator.next();
        changeValue(value);
    }

    private void changeValue(Object value) {
        WorkingMemory workingMemory = bruteForceSolverScope.getWorkingMemory();
        FactHandle factHandle = workingMemory.getFactHandle(planningEntity);
        planningVariableDescriptor.setValue(planningEntity, value);
        workingMemory.update(factHandle, planningEntity);
    }

}

@@ -1133,33 +1133,32 @@
 ?%3E planningValue
-s
 Iterator;%0A%0A    p
@@ -1637,33 +1637,32 @@
    planningValue
-s
 Iterator = plann
@@ -1741,33 +1741,32 @@
 rn planningValue
-s
 Iterator.hasNext
@@ -1829,33 +1829,32 @@
  = planningValue
-s
 Iterator.next();
@@ -1928,33 +1928,32 @@
    planningValue
-s
 Iterator = plann
@@ -2010,17 +2010,16 @@
 ingValue
-s
 Iterator

rename variable