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