/*
 * 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);
    }

}

/*
 * 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<?> planningValueIterator;

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

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

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

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

    public void reset() {
        planningValueIterator = planningValues.iterator();
        Object value = planningValueIterator.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);
    }

}

rename variable