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