001 package ifs.example.tt;
002
003 import ifs.model.*;
004 import java.util.*;
005
006 /**
007 * Location (value, i.e., a single placement of the activity).
008 * Location encodes a slot and a selection of resources.
009 *
010 * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
011 * @version 1.0
012 */
013 public class Location extends Value {
014 private int iSlot;
015 private Resource[] iResources;
016 private int iNrOfDiscouragedSlots = -1;
017
018 /**
019 * Constructor.
020 * @param activity parent activity
021 * @param slot starting time
022 * @param resources selection of resources
023 */
024 public Location(Activity activity, int slot, Resource[] resources) {
025 super(activity);
026 iSlot = slot;
027 iResources = resources;
028 iNrOfDiscouragedSlots = computeNrOfDiscouragedSlots();
029 }
030
031 /**
032 * Constructor. slot = nrHours * day + hour
033 * @param activity parent activity
034 * @param day day
035 * @param hour starting hour
036 * @param resources required resources
037 */
038 public Location(Activity activity, int day, int hour, Resource[] resources) {
039 super(activity);
040 iSlot = ((TimetableModel)activity.getModel()).getNrHours()*day + hour;
041 iResources = resources;
042 iNrOfDiscouragedSlots = computeNrOfDiscouragedSlots();
043 }
044
045 /** Gets slot */
046 public int getSlot() { return iSlot; }
047 /** Gets selection of resources */
048 public Resource[] getResources() { return iResources; }
049 /** Gets given resource */
050 public Resource getResource(int idx) { return iResources[idx]; }
051 /** Returns true if the given resource is used by this location */
052 public boolean containResource(Resource resource) {
053 for (int i=0;i<iResources.length;i++)
054 if (iResources[i].equals(resource)) return true;
055 return false;
056 }
057
058 /** Number of slots (over all resources) which are discouraged */
059 public int getNrOfDiscouragedSlots() { return iNrOfDiscouragedSlots; }
060 /** Int value (for optimization) -- getNrOfDiscouragedSlots() is returned */
061 public int toInt() { return iNrOfDiscouragedSlots; }
062 /** Computes number of discouraged slots (over all resources and the activity) */
063 public int computeNrOfDiscouragedSlots() {
064 Activity a = (Activity)variable();
065 int ret = 0;
066 for (int i=getSlot();i<getSlot()+a.getLength();i++) {
067 if (a.isDiscouragedSlot(i)) ret++;
068 for (int j=0;j<getResources().length;j++)
069 if (getResource(j).isDiscouragedSlot(i)) ret++;
070 }
071 return ret;
072 }
073 /**
074 * Returns true if the location intersects with another location.
075 * This means the same resource is used in the same time.
076 */
077 public boolean hasIntersection(Location location) {
078 int s1 = getSlot();
079 int l1 = ((Activity)variable()).getLength();
080 int s2 = location.getSlot();
081 int l2 = ((Activity)location.variable()).getLength();
082 return !(s1+l1<=s2 || s2+l2<=s1);
083 }
084 /**
085 * Returns true if the location is prohibited.
086 * This means that the activity or a required resource has a time slot which is used by this location prohibited.
087 */
088 public boolean isProhibited() {
089 Activity a = (Activity)variable();
090 for (int i=getSlot();i<getSlot()+a.getLength();i++) {
091 if (a.isProhibitedSlot(i)) return true;
092 for (int j=0;j<getResources().length;j++)
093 if (getResource(j).isProhibitedSlot(i)) return true;
094 }
095 return false;
096 }
097
098 public String getName() {
099 StringBuffer sb = new StringBuffer(getSlot()+"/");
100 for (int i=0;i<iResources.length;i++) {
101 if (i>0) sb.append(",");
102 sb.append(iResources[i].getName());
103 }
104 return sb.toString();
105 }
106 }