/*
 * Copyright 2012 Red Hat, Inc. and/or its affiliates.
 *
 * 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.optaweb.vehiclerouting.solver;
    dialect "java"

import org.optaplanner.core.api.score.buildin.hardsoftlong.HardSoftLongScoreHolder;

import org.optaweb.vehiclerouting.plugin.planner.domain.PlanningDepot;
import org.optaweb.vehiclerouting.plugin.planner.domain.PlanningLocation;
import org.optaweb.vehiclerouting.plugin.planner.domain.PlanningVehicle;
import org.optaweb.vehiclerouting.plugin.planner.domain.VehicleRoutingSolution;
import org.optaweb.vehiclerouting.plugin.planner.domain.PlanningVisit;

global HardSoftLongScoreHolder scoreHolder;

// ############################################################################
// Hard constraints
// ############################################################################


rule "vehicleCapacity"
    when
        $vehicle : PlanningVehicle($capacity : capacity)
        accumulate(
                    PlanningVisit(
                        vehicle == $vehicle,
                        $demand : demand);
                    $demandTotal : sum($demand);
                    $demandTotal > $capacity
                )
    then
        scoreHolder.addHardConstraintMatch(kcontext, $capacity - $demandTotal);
end



// ############################################################################
// Soft constraints
// ############################################################################

rule "distanceToPreviousStandstill"
    when
        $visit : PlanningVisit(previousStandstill != null, $distanceFromPreviousStandstill : distanceFromPreviousStandstill)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, - $distanceFromPreviousStandstill);
end

rule "distanceFromLastCustomerToDepot"
    when
        $visit : PlanningVisit(previousStandstill != null)
        not PlanningVisit(previousStandstill == $visit)
    then
        PlanningVehicle vehicle = $visit.getVehicle();
        scoreHolder.addSoftConstraintMatch(kcontext, - $visit.getDistanceTo(vehicle));
end

