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 se.liu.ida.critiquer.activities.Activity;
027    import se.liu.ida.critiquer.activities.ActivityUtils;
028    import se.liu.ida.critiquer.activities.TransportationActivity;
029    import se.liu.ida.critiquer.activities.parameters.ActivityParameter;
030    import se.liu.ida.critiquer.activities.parameters.Location;
031    import se.liu.ida.critiquer.activities.parameters.LocationParameter;
032    import se.liu.ida.critiquer.mics.Comparer;
033    import se.liu.ida.critiquer.mics.Utils;
034    import se.liu.ida.critiquer.resources.Agent;
035    
036    public class DispatchActivity extends Activity implements TransportationActivity {
037    
038            /**
039         * 
040         */
041            private static final String DESTINATION    = "destination";
042    
043            /**
044         * 
045         */
046            private static final String STARTING_LOCATION = "starting location";
047    
048            /**
049         * 
050         */
051            private static final long   serialVersionUID  = 1L;
052    
053            private static int                numActivities  = 0;
054    
055            /**
056         * @param parent
057         */
058            public DispatchActivity(Activity parent) {
059                    super("Dispatch activity", parent);
060            }
061    
062            /*
063         * (non-Javadoc)
064         * 
065         * @see se.liu.ida.critiquer.activities.Activity#addDefaultParams()
066         */
067            @Override
068            public void addDefaultParams() {
069                    super.addDefaultParams();
070    
071                    addParam(new LocationParameter(LocationParameter.Type.ORIG_LOC, STARTING_LOCATION, this));
072                    addParam(new LocationParameter(LocationParameter.Type.DEST_LOC, DESTINATION, this));
073    
074            }
075    
076            /*
077         * (non-Javadoc)
078         * 
079         * @see se.liu.ida.critiquer.activities.Activity#calculateTimeToComplete(java.util.HashSet)
080         */
081            @Override
082            public long calculateTimeToComplete() {
083    
084                    long timeToComplete = 0;
085                    // time in minutes
086                    timeToComplete = 0;
087                    for (Agent agent : ActivityUtils.getAgentsInActivity(this)) {
088                            try {
089                                    timeToComplete = Math.max(timeToComplete, agent.perform(DispatchActivity.class, this, null));
090                            } catch (Exception e) {
091                                    System.err.println(e.getMessage());
092                                    e.printStackTrace();
093                            }
094                    }
095                    return timeToComplete;
096    
097            }
098    
099            /**
100         * 
101         * Add location parameter dependency
102         * 
103         * @see se.liu.ida.critiquer.activities.Activity#order(se.liu.ida.critiquer.activities.Activity)
104         */
105            @Override
106            public void order(Activity laterActivity) {
107                    super.order(laterActivity);
108                    LocationParameter stationaryLocationParam = (LocationParameter) Utils.findFirst(laterActivity.getParams(),
109                                    new Comparer<ActivityParameter>() {
110    
111                                            public boolean check(ActivityParameter param) {
112                                                    boolean check = false;
113                                                    if (param instanceof LocationParameter) {
114                                                            LocationParameter locParam = (LocationParameter) param;
115                                                            if (locParam.getType() == LocationParameter.Type.STATIC_LOC) {
116                                                                    check = true;
117                                                            }
118                                                    }
119                                                    return check;
120                                            }
121    
122                                    });
123                    LocationParameter destinationParam = ActivityUtils.getParamByClassAndName(this,
124                                    LocationParameter.class,
125                                    DESTINATION);
126                    destinationParam.setValue(stationaryLocationParam.getValue());
127            
128            }
129    
130            /*
131         * (non-Javadoc)
132         * 
133         * @see se.liu.ida.critiquer.activities.Activity#removeOrder(se.liu.ida.critiquer.activities.Activity)
134         */
135            @Override
136            public void removeOrder(Activity laterActivity) {
137                    // TODO Auto-generated method stub
138                    super.removeOrder(laterActivity);
139            }
140    
141            @Override
142            public int getNumActivities() {
143                    return DispatchActivity.numActivities++;
144            }
145    
146            /**
147         * Return value of the starting location parameter
148         * 
149         * @see se.liu.ida.critiquer.activities.TransportationActivity#getStart()
150         */
151            public Location getStart() {
152                    return ActivityUtils.getParamByClassAndName(this, LocationParameter.class, STARTING_LOCATION).getValue();
153            }
154    
155            /**
156         * 
157         * Return the value of the destination location parameter
158         * 
159         * @see se.liu.ida.critiquer.activities.TransportationActivity#getEnd()
160         */
161            public Location getEnd() {
162                    return ActivityUtils.getParamByClassAndName(this, LocationParameter.class, DESTINATION).getValue();
163            }
164    
165    }