001 package ifs.example.tt;
002
003 import ifs.model.*;
004 import ifs.util.*;
005 import java.util.*;
006
007 /**
008 * Resource constraint
009 *
010 * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
011 * @version 1.0
012 */
013 public class Resource extends Constraint {
014 private String iName = null;
015 private String iResourceId = null;
016 private Activity[] iResource;
017 private Set iProhibitedSlots = new HashSet();
018 private Set iDiscouragedSlots = new HashSet();
019 private int iType = TYPE_OTHER;
020
021 public static final int TYPE_ROOM = 0;
022 public static final int TYPE_INSTRUCTOR = 1;
023 public static final int TYPE_CLASS = 2;
024 public static final int TYPE_OTHER = 3;
025
026 public Resource(String id, int type, String name) {
027 super();
028 iResourceId = id;
029 iName = name;
030 iType = type;
031 }
032
033 public void setModel(Model model) {
034 super.setModel(model);
035 iResource = new Activity[((TimetableModel)model).getNrDays()*((TimetableModel)model).getNrHours()];
036 for (int i=0;i<iResource.length;i++)
037 iResource[i] = null;
038 }
039
040 public String getResourceId() { return iResourceId; }
041 public String getName() { return iName; }
042 public int getType() { return iType; }
043 public Set getProhibitedSlots() { return iProhibitedSlots; }
044 public Set getDiscouragedSlots() { return iDiscouragedSlots; }
045 public void addProhibitedSlot(int day, int hour) {
046 iProhibitedSlots.add(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour));
047 }
048 public void addDiscouragedSlot(int day, int hour) {
049 iDiscouragedSlots.add(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour));
050 }
051 public boolean isProhibitedSlot(int day, int hour) {
052 return iProhibitedSlots.contains(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour));
053 }
054 public boolean isDiscouragedSlot(int day, int hour) {
055 return iDiscouragedSlots.contains(new Integer(((TimetableModel)getModel()).getNrHours()*day+hour));
056 }
057 public void addProhibitedSlot(int slot) {
058 iProhibitedSlots.add(new Integer(slot));
059 }
060 public void addDiscouragedSlot(int slot) {
061 iDiscouragedSlots.add(new Integer(slot));
062 }
063 public boolean isProhibitedSlot(int slot) {
064 return iProhibitedSlots.contains(new Integer(slot));
065 }
066 public boolean isDiscouragedSlot(int slot) {
067 return iDiscouragedSlots.contains(new Integer(slot));
068 }
069 public boolean isProhibited(int day, int hour, int length) {
070 int slot = ((TimetableModel)getModel()).getNrHours()*day+hour;
071 for (int i=0;i<length;i++)
072 if (iProhibitedSlots.contains(new Integer(slot+i))) return true;
073 return false;
074 }
075
076 public void computeConflicts(Value value, Set conflicts) {
077 Activity activity = (Activity) value.variable();
078 Location location = (Location) value;
079 if (!location.containResource(this)) return;
080 for (int i=location.getSlot(); i<location.getSlot()+activity.getLength(); i++) {
081 Activity conf = iResource[i];
082 if (conf!=null && !activity.equals(conf))
083 conflicts.add(conf.getAssignment());
084 }
085 }
086
087 public boolean inConflict(Value value) {
088 Activity activity = (Activity) value.variable();
089 Location location = (Location) value;
090 if (!location.containResource(this)) return false;
091 for (int i=location.getSlot(); i<location.getSlot()+activity.getLength(); i++) {
092 if (iResource[i]!=null) return true;
093 }
094 return false;
095 }
096
097 public boolean isConsistent(Value value1, Value value2) {
098 Location l1 = (Location) value1;
099 Location l2 = (Location) value2;
100 return !l1.containResource(this) || !l2.containResource(this) || !l1.hasIntersection(l2);
101 }
102
103 public void assigned(long iteration, Value value) {
104 super.assigned(iteration, value);
105 Activity activity = (Activity) value.variable();
106 Location location = (Location) value;
107 if (!location.containResource(this)) return;
108 for (int i=location.getSlot(); i<location.getSlot()+activity.getLength(); i++) {
109 iResource[i] = activity;
110 }
111 }
112 public void unassigned(long iteration, Value value) {
113 super.unassigned(iteration, value);
114 Activity activity = (Activity) value.variable();
115 Location location = (Location) value;
116 if (!location.containResource(this)) return;
117 for (int i=location.getSlot(); i<location.getSlot()+activity.getLength(); i++) {
118 iResource[i] = null;
119 }
120 }
121 }