    001package net.sf.cpsolver.ifs.example.tt;
    002
    003import java.util.ArrayList;
    004import java.util.Collection;
    005import java.util.HashSet;
    006import java.util.List;
    007import java.util.Set;
    008
    009import net.sf.cpsolver.ifs.model.Variable;
    010
    011/**
    012 * Activity (variable). It encodes a name, length
    013 *
    014 * @version IFS 1.2 (Iterative Forward Search)<br>
    015 *          Copyright (C) 2006 - 2010 Tomáš Müller<br>
    016 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    017 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    018 * <br>
    019 *          This library is free software; you can redistribute it and/or modify
    020 *          it under the terms of the GNU Lesser General Public License as
    021 *          published by the Free Software Foundation; either version 3 of the
    022 *          License, or (at your option) any later version. <br>
    023 * <br>
    024 *          This library is distributed in the hope that it will be useful, but
    025 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    026 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    027 *          Lesser General Public License for more details. <br>
    028 * <br>
    029 *          You should have received a copy of the GNU Lesser General Public
    030 *          License along with this library; if not see
    031 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    032 */
    033public class Activity extends Variable<Activity, Location> {
    034    private int iLength = 1;
    035    private String iActivityId = null;
    036    private String iName = null;
    037    private ArrayList<List<Resource>> iResorces = new ArrayList<List<Resource>>();
    038    private Set<Integer> iProhibitedSlots = new HashSet<Integer>();
    039    private Set<Integer> iDiscouragedSlots = new HashSet<Integer>();
    040
    041    public Activity(int length, String id, String name) {
    042        super(null);
    043        iLength = length;
    044        iActivityId = id;
    045        iName = name;
    046    }
    047
    048    @Override
    049    public String getName() {
    050        return iName;
    051    }
    052
    053    public String getActivityId() {
    054        return iActivityId;
    055    }
    056
    057    public int getLength() {
    058        return iLength;
    059    }
    060
    061    public void addResourceGroup(List<Resource> resources) {
    062        for (Resource r : resources)
    063            r.addVariable(this);
    064        iResorces.add(resources);
    065    }
    066
    067    public void addResourceGroup(Resource[] resources) {
    068        ArrayList<Resource> rg = new ArrayList<Resource>(resources.length);
    069        for (int i = 0; i < resources.length; i++) {
    070            rg.add(resources[i]);
    071            resources[i].addVariable(this);
    072        }
    073        iResorces.add(rg);
    074    }
    075
    076    public void addResourceGroup(Resource resource) {
    077        ArrayList<Resource> rg = new ArrayList<Resource>(1);
    078        rg.add(resource);
    079        iResorces.add(rg);
    080        resource.addVariable(this);
    081    }
    082
    083    public List<Resource> getResourceGroup(int idx) {
    084        return iResorces.get(idx);
    085    }
    086
    087    public List<List<Resource>> getResourceGroups() {
    088        return iResorces;
    089    }
    090
    091    public Set<Integer> getProhibitedSlots() {
    092        return iProhibitedSlots;
    093    }
    094
    095    public Set<Integer> getDiscouragedSlots() {
    096        return iDiscouragedSlots;
    097    }
    098
    099    public void addProhibitedSlot(int day, int hour) {
    100        iProhibitedSlots.add(((TimetableModel) getModel()).getNrHours() * day + hour);
    101    }
    102
    103    public void addDiscouragedSlot(int day, int hour) {
    104        iDiscouragedSlots.add(((TimetableModel) getModel()).getNrHours() * day + hour);
    105    }
    106
    107    public boolean isProhibitedSlot(int day, int hour) {
    108        return iProhibitedSlots.contains(((TimetableModel) getModel()).getNrHours() * day + hour);
    109    }
    110
    111    public boolean isDiscouragedSlot(int day, int hour) {
    112        return iDiscouragedSlots.contains(((TimetableModel) getModel()).getNrHours() * day + hour);
    113    }
    114
    115    public void addProhibitedSlot(int slot) {
    116        iProhibitedSlots.add(slot);
    117    }
    118
    119    public void addDiscouragedSlot(int slot) {
    120        iDiscouragedSlots.add(slot);
    121    }
    122
    123    public boolean isProhibitedSlot(int slot) {
    124        return iProhibitedSlots.contains(slot);
    125    }
    126
    127    public boolean isDiscouragedSlot(int slot) {
    128        return iDiscouragedSlots.contains(slot);
    129    }
    130
    131    public boolean isProhibited(int day, int hour, int length) {
    132        int slot = ((TimetableModel) getModel()).getNrHours() * day + hour;
    133        for (int i = 0; i < length; i++)
    134            if (iProhibitedSlots.contains(slot + i))
    135                return true;
    136        return false;
    137    }
    138
    139    public void init() {
    140        setValues(computeValues());
    141    }
    142
    143    private void addValues(Collection<Location> values, int day, int hour, int level, Resource[] resources) {
    144        if (level == getResourceGroups().size()) {
    145            values.add(new Location(this, day, hour, resources.clone()));
    146            return;
    147        }
    148        Collection<Resource> rg = getResourceGroups().get(level);
    149        for (Resource r : rg) {
    150            if (r.isProhibited(day, hour, getLength()))
    151                continue;
    152            resources[level] = r;
    153            addValues(values, day, hour, level + 1, resources);
    154        }
    155    }
    156
    157    public List<Location> computeValues() {
    158        List<Location> values = new ArrayList<Location>();
    159        Resource[] res = new Resource[getResourceGroups().size()];
    160        for (int day = 0; day < ((TimetableModel) getModel()).getNrDays(); day++)
    161            for (int hour = 0; hour <= ((TimetableModel) getModel()).getNrHours() - getLength(); hour++) {
    162                if (isProhibited(day, hour, getLength()))
    163                    continue;
    164                addValues(values, day, hour, 0, res);
    165            }
    166        return values;
    167    }
    168}