001 /** 002 * planningtool - A Planning Tool with Critiquing Support. 003 * 004 * Copyright (C) 2006 olale 005 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License 008 * as published by the Free Software Foundation; either version 2 009 * of the License, or (at your option) any later version. 010 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 020 * Contact information: 021 * E-mail: olale@ida.liu.se 022 * olale@lysator.liu.se 023 */ 024 package se.liu.ida.critiquer.activities; 025 026 import java.io.Serializable; 027 import java.util.ArrayList; 028 import java.util.Calendar; 029 import java.util.HashSet; 030 import java.util.List; 031 import java.util.Vector; 032 033 import se.liu.ida.critiquer.activities.parameters.ActivityParameter; 034 import se.liu.ida.critiquer.activities.parameters.AgentParameter; 035 import se.liu.ida.critiquer.activities.parameters.CommanderParameter; 036 import se.liu.ida.critiquer.activities.parameters.NameParameter; 037 import se.liu.ida.critiquer.activities.parameters.NumberSelectionParameter; 038 import se.liu.ida.critiquer.activities.parameters.Parameter; 039 import se.liu.ida.critiquer.activities.parameters.TimeParameter; 040 import se.liu.ida.critiquer.constraints.TimeParameterOrdering; 041 import se.liu.ida.critiquer.mics.Comparer; 042 import se.liu.ida.critiquer.mics.ReferenceHolder; 043 import se.liu.ida.critiquer.mics.Utils; 044 import se.liu.ida.critiquer.resources.Agent; 045 046 public class ActivityUtils implements Serializable { 047 048 /** 049 * 050 */ 051 private static final String COMMANDER = "commander"; 052 /** 053 * 054 */ 055 private static final String MAX_TIME = "max time"; 056 /** 057 * 058 */ 059 private static final long serialVersionUID = 1L; 060 061 @SuppressWarnings("unchecked") 062 public static <T extends ActivityParameter> ArrayList<T> filterParams(Activity a, final Class<T> c) { 063 ArrayList<T> l = new ArrayList<T>(); 064 for (ActivityParameter p : a.getParams()) { 065 if (c.isInstance(p)) { 066 l.add((T) p); 067 } 068 } 069 return l; 070 } 071 072 public static boolean agentInActivity(Agent agent, Activity activity) { 073 return getAgentsInActivity(activity).contains(agent); 074 } 075 076 077 public static HashSet<Agent> getAgentsInActivity(Activity a) { 078 HashSet<Agent> agents = new HashSet<Agent>(); 079 AgentParameter agentParam = getMainAgentParameter(a); 080 if (agentParam!=null) { 081 agents.addAll(agentParam.getCommander().getAgentsInActivity(a)); 082 } 083 return agents; 084 } 085 086 087 public static NameParameter getNameParameter(Activity a) { 088 ArrayList<NameParameter> l = filterParams(a, NameParameter.class); 089 return l.get(0); 090 } 091 092 /** 093 * Retrieves the first time interval parameter 094 */ 095 096 public static TimeParameter getStartTimeParameter(Activity a) { 097 return Utils.findFirst(filterParams(a, TimeParameter.class), new Comparer<TimeParameter>() { 098 099 public boolean check(TimeParameter obj) { 100 return obj.getType() == TimeParameter.Type.START; 101 } 102 103 }); 104 105 } 106 107 public static TimeParameter getEndTimeParameter(Activity a) { 108 return Utils.findFirst(filterParams(a, TimeParameter.class), new Comparer<TimeParameter>() { 109 110 public boolean check(TimeParameter obj) { 111 return obj.getType() == TimeParameter.Type.END; 112 } 113 114 }); 115 } 116 117 public static AgentParameter getMainAgentParameter(Activity a) { 118 119 if (a.isAgentAssignable()) { 120 /** 121 * There is a commander associated with this activity 122 */ 123 CommanderParameter commander = getCommanderParameter(a); 124 return commander.getValue().getAgentParameter(); 125 126 } else if (a.hasParent()) { 127 return getMainAgentParameter(a.getParent()); 128 } else { 129 return null; 130 } 131 132 } 133 134 public static ActivityParameter getParamByName(Activity a, String name) { 135 ArrayList<ActivityParameter> params = a.getParams(); 136 for (ActivityParameter parameter : params) { 137 if (parameter.getName().equals(name)) { 138 return parameter; 139 } 140 } 141 return null; 142 } 143 144 145 public static <T extends ActivityParameter> T getParamByClassAndName(Activity a, Class<T> c, String name) { 146 ArrayList<T> paramsByClass = ActivityUtils.filterParams(a, c); 147 for (T t2 : paramsByClass) { 148 if (t2.getName().equals(name)) { 149 return (T) t2; 150 } 151 } 152 return null; 153 } 154 155 public static Long getCurrentTimeForActivity(Activity activity) { 156 Long returnValue = new Long(0); 157 TimeParameter end = getEndTimeParameter(activity); 158 TimeParameter start = getStartTimeParameter(activity); 159 if (!(end == null || start == null)) { 160 returnValue = end.getValue().getTimeInMillis() - start.getValue().getTimeInMillis(); 161 162 } 163 assert returnValue > 0; 164 return returnValue / 60000; 165 } 166 167 public static void runActivityTimeNotifier(final Activity a) { 168 new Thread() { 169 170 /* 171 * (non-Javadoc) 172 * 173 * @see java.lang.Thread#run() 174 */ 175 @Override 176 public void run() { 177 a.signalActivityTimeChanged(); 178 } 179 180 }.start(); 181 } 182 183 public static void addMaxTimeParameter(Activity activity) { 184 185 Vector<Integer> numbers = new Vector<Integer>(); 186 for (int i = 0; i < 180; i += 10) { 187 numbers.add(i); 188 } 189 NumberSelectionParameter maxTimeParam = new NumberSelectionParameter(activity, MAX_TIME, numbers, 30); 190 activity.addParam(maxTimeParam); 191 192 } 193 194 public static NumberSelectionParameter getMaxTimeParameter(Activity activity) { 195 return getParamByClassAndName(activity, NumberSelectionParameter.class, MAX_TIME); 196 } 197 198 public static void addCommanderParameter(Activity activity) { 199 activity.addParam(new CommanderParameter(COMMANDER,activity)); 200 } 201 202 public static CommanderParameter getCommanderParameter(Activity a) { 203 return getParamByClassAndName(a, CommanderParameter.class, COMMANDER); 204 } 205 206 /** 207 * Set the missionTimeChanged flag in the activity and signal a new 208 * paramChanged event to all interested listeners whenever parameter 209 * <code>param</code> changes value. 210 * 211 * @param param - 212 * The parameter which can change the mission time 213 */ 214 public static AbstractParamChangedListener createMissionTimeUpdater(final ActivityParameter param) { 215 return new AbstractParamChangedListener() { 216 217 /** 218 * 219 */ 220 private static final long serialVersionUID = 1L; 221 222 @Override 223 public <T> void paramChanged(Activity activity, Parameter<T> p) { 224 if (p == param) { 225 runActivityTimeNotifier(activity); 226 } 227 228 } 229 230 }; 231 } 232 233 234 @SuppressWarnings("unchecked") 235 public static void removeConstraintsForActivity(Activity currentActivity) { 236 ArrayList<TimeParameterOrdering> temporalConstraints = (ArrayList<TimeParameterOrdering>) ReferenceHolder.temporalConstraints.clone(); 237 for (TimeParameterOrdering ordering : temporalConstraints) { 238 if (ordering.getT1().getActivity().equals(currentActivity) || ordering.getT2() 239 .getActivity() 240 .equals(currentActivity)) { 241 ReferenceHolder.temporalConstraints.remove(ordering); 242 } 243 } 244 } 245 246 /** 247 * Checks if two activities overlap in time 248 * @return true iff there is an overlap in time 249 */ 250 251 public static boolean overlap(Activity a1, Activity a2) { 252 Calendar start1 = getStartTimeParameter(a1).getValue(); 253 Calendar start2 = getStartTimeParameter(a2).getValue(); 254 Calendar end1 = getEndTimeParameter(a1).getValue(); 255 Calendar end2 = getEndTimeParameter(a2).getValue(); 256 Calendar lastStart = (start1.after(start2)?start1:start2); 257 Calendar firstEnd = (end1.before(end2)?end1:end2); 258 return lastStart.before(firstEnd); 259 260 } 261 262 public static Activity getActivityByName(final Comparable name) { 263 if (!(name instanceof String)) { 264 return null; 265 } 266 return Utils.findFirst(ReferenceHolder.allActivities, 267 new Comparer<Activity>() { 268 269 public boolean check(Activity activity) { 270 return activity.toString().equals(name); 271 } 272 273 }); 274 } 275 276 277 }