    001    package ifs.example.tt;
    002
    003    import ifs.model.*;
    004    import ifs.util.*;
    005    import java.util.*;
    006
    007    /**
    008     * Activity (variable).
    009     * It encodes a name, length
    010     *
    011     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
    012     * @version 1.0
    013     */
    014    public class Activity extends Variable {
    015        private static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(Activity.class);
    016        private int iLength = 1;
    017        private String iActivityId = null;
    018        private String iName = null;
    019        private Vector iResorces = new FastVector();
    020        private Set iProhibitedSlots = new HashSet();
    021        private Set iDiscouragedSlots = new HashSet();
    022
    023        public Activity(int length, String id, String name) {
    024            super(null);
    025            iLength = length;
    026            iActivityId = id;
    027            iName = name;
    028        }
    029
    030        public String getName() { return iName; }
    031        public String getActivityId() { return iActivityId; }
    032
    033        public int getLength() { return iLength; }
    034
    035        public void addResourceGroup(Vector resources) {
    036            for (Enumeration e=resources.elements();e.hasMoreElements();) {
    037                ((Resource)e.nextElement()).addVariable(this);
    038            }
    039            iResorces.add(resources);
    040        }
    041        public void addResourceGroup(Resource[] resources) {
    042            Vector rg = new FastVector(resources.length);
    043            for (int i=0;i<resources.length;i++) {
    044                rg.addElement(resources[i]);
    045                resources[i].addVariable(this);
    046            }
    047            iResorces.add(rg);
    048        }
    049        public void addResourceGroup(Resource resource) {
    050            Vector rg = new FastVector(1);
    051            rg.addElement(resource);
    052            iResorces.add(rg);
    053            resource.addVariable(this);
    054        }
    055        public Vector getResourceGroup(int idx) {
    056            return (Vector)iResorces.elementAt(idx);
    057        }
    058        public Vector getResourceGroups() {
    059            return iResorces;
    060        }
    061        public Set getProhibitedSlots() { return iProhibitedSlots; }
    062        public Set getDiscouragedSlots() { return iDiscouragedSlots; }
    063        public void addProhibitedSlot(int day, int hour) {
    064            iProhibitedSlots.add(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour));
    065        }
    066        public void addDiscouragedSlot(int day, int hour) {
    067            iDiscouragedSlots.add(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour));
    068        }
    069        public boolean isProhibitedSlot(int day, int hour) {
    070            return iProhibitedSlots.contains(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour));
    071        }
    072        public boolean isDiscouragedSlot(int day, int hour) {
    073            return iDiscouragedSlots.contains(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour));
    074        }
    075        public void addProhibitedSlot(int slot) {
    076            iProhibitedSlots.add(new Integer(slot));
    077        }
    078        public void addDiscouragedSlot(int slot) {
    079            iDiscouragedSlots.add(new Integer(slot));
    080        }
    081        public boolean isProhibitedSlot(int slot) {
    082            return iProhibitedSlots.contains(new Integer(slot));
    083        }
    084        public boolean isDiscouragedSlot(int slot) {
    085            return iDiscouragedSlots.contains(new Integer(slot));
    086        }
    087        public boolean isProhibited(int day, int hour, int length) {
    088            int slot = ((TimetableModel)getModel()).getNrHours()*day+hour;
    089            for (int i=0;i<length;i++)
    090                if (iProhibitedSlots.contains(new Integer(slot+i))) return true;
    091            return false;
    092        }
    093        public void init() {
    094            setValues(computeValues());
    095        }
    096
    097        private void addValues(Vector values, int day, int hour, int level, Resource[] resources) {
    098            if (level==getResourceGroups().size()) {
    099                values.addElement(new Location(this, day, hour, (Resource[])resources.clone()));
    100                return;
    101            }
    102            Vector rg = (Vector)getResourceGroups().elementAt(level);
    103            for (Enumeration f=rg.elements();f.hasMoreElements();) {
    104                Resource r = (Resource)f.nextElement();
    105                if (r.isProhibited(day, hour, getLength())) continue;
    106                resources[level]=r;
    107                addValues(values, day, hour, level+1, resources);
    108            }
    109        }
    110
    111        public Vector computeValues() {
    112            TimetableModel m = (TimetableModel)getModel();
    113            Vector values = new FastVector();
    114            Resource[] res = new Resource[getResourceGroups().size()];
    115            for (int day=0;day<m.getNrDays();day++)
    116                for (int hour=0;hour<=m.getNrHours()-getLength();hour++) {
    117                    if (isProhibited(day,hour,getLength())) continue;
    118                    addValues(values, day, hour, 0, res);
    119                }
    120            return values;
    121        }
    122    }