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.scenarios.standard; 025 026 import java.util.HashMap; 027 import java.util.HashSet; 028 import java.util.Vector; 029 030 import se.liu.ida.critiquer.activities.Activity; 031 import se.liu.ida.critiquer.activities.ActivityUtils; 032 import se.liu.ida.critiquer.activities.parameters.ActivityParameter; 033 import se.liu.ida.critiquer.activities.parameters.LocationParameter; 034 import se.liu.ida.critiquer.activities.parameters.NumberSelectionParameter; 035 import se.liu.ida.critiquer.mics.Comparer; 036 import se.liu.ida.critiquer.mics.Utils; 037 import se.liu.ida.critiquer.resources.Agent; 038 039 public class FirstAidActivity extends Activity { 040 041 042 /** 043 * 044 */ 045 private static final String ACCIDENT_LOCATION = "Accident location"; 046 /** 047 * 048 */ 049 private static final long serialVersionUID = 1L; 050 private static int numActivities; 051 052 public FirstAidActivity(Activity parent) { 053 super("First aid activity",parent); 054 055 } 056 057 /* (non-Javadoc) 058 * @see se.liu.ida.critiquer.activities.Activity#addDefaultParams() 059 */ 060 @Override 061 public void addDefaultParams() { 062 super.addDefaultParams(); 063 Vector<Integer> numbers = new Vector<Integer>(); 064 for (int i = 0; i < 10; i++) { 065 numbers.add(i); 066 } 067 final NumberSelectionParameter injuredSelector = new NumberSelectionParameter(this,"number injured",numbers,0); 068 addPrivateUpdateListener(ActivityUtils.createMissionTimeUpdater(injuredSelector)); 069 addParam(injuredSelector); 070 addParam(new LocationParameter(LocationParameter.Type.STATIC_LOC,ACCIDENT_LOCATION,this)); 071 } 072 073 074 075 /* (non-Javadoc) 076 * @see se.liu.ida.critiquer.activities.Activity#calculateTimeToComplete(java.util.HashSet) 077 */ 078 @Override 079 public long calculateTimeToComplete() { 080 081 // time in minutes 082 long totalTime=0; 083 long timeForRound=0; 084 HashMap<String, Integer> attributes = new HashMap<String,Integer>(); 085 attributes.put("patients",getInjured()); 086 HashSet<Agent> agents = ActivityUtils.getAgentsInActivity(getRootActivity()); 087 while (attributes.get("patients") > 0) { 088 timeForRound = 0; 089 for (Agent agent : agents) { 090 timeForRound = Math.max(timeForRound, agent.perform(FirstAidActivity.class, this, attributes)); 091 if (attributes.get("patients") > 0) { 092 break; 093 } 094 } 095 totalTime += timeForRound; 096 } 097 098 return totalTime; 099 } 100 101 102 /** 103 * @return Returns the injured. 104 */ 105 public int getInjured() { 106 NumberSelectionParameter numberSelector = ActivityUtils.getParamByClassAndName(this, NumberSelectionParameter.class, "number injured"); 107 int returnValue = 0; 108 returnValue = numberSelector.getValue(); 109 return returnValue; 110 } 111 112 113 114 @Override 115 public int getNumActivities() { 116 return FirstAidActivity.numActivities++; 117 } 118 119 /** 120 * 121 * Add location parameter dependency 122 * 123 * @see se.liu.ida.critiquer.activities.Activity#order(se.liu.ida.critiquer.activities.Activity) 124 */ 125 @Override 126 public void order(Activity laterActivity) { 127 super.order(laterActivity); 128 LocationParameter startLocationParam = (LocationParameter) Utils.findFirst(laterActivity.getParams(), 129 new Comparer<ActivityParameter>() { 130 131 public boolean check(ActivityParameter param) { 132 boolean check = false; 133 if (param instanceof LocationParameter) { 134 LocationParameter locParam = (LocationParameter) param; 135 if (locParam.getType() == LocationParameter.Type.ORIG_LOC) { 136 check = true; 137 } 138 } 139 return check; 140 } 141 142 }); 143 // Just a precaution, should probably be a junit test 144 assert (startLocationParam != null); 145 ActivityUtils.getParamByClassAndName(this, LocationParameter.class, ACCIDENT_LOCATION) 146 .setValue(startLocationParam.getValue()); 147 148 } 149 150 151 }