99import aima .search .map .DynAttributeNames ;
1010import aima .search .map .Scenario ;
1111
12-
1312/**
14- * Provides a useful base class for agent application controller
15- * implementations in the context of route planning agent application
16- * development.
17- * To get it ready to work, all you need to do is, to provide implementations
18- * for the four abstract methods.
19- * See {@link RoutePlanningAgentAppDemo} for an example.
13+ * Provides a useful base class for agent application controller implementations
14+ * in the context of route planning agent application development. To get it
15+ * ready to work, all you need to do is, to provide implementations for the four
16+ * abstract methods. See {@link RoutePlanningAgentAppDemo} for an example.
17+ *
2018 * @author R. Lunde
2119 */
2220public abstract class AbstractMapAgentController extends AgentAppController {
2321 /** A scenario. */
2422 protected Scenario scenario ;
2523 /**
26- * Some location names. For routing problems, only one location should
27- * be specified.
24+ * Some location names. For routing problems, only one location should be
25+ * specified.
2826 */
2927 protected List <String > destinations ;
3028 /** Search method to be used. */
3129 protected aima .search .framework .Search search ;
3230 /** Heuristic function to be used when performing informed search. */
3331 protected AdaptableHeuristicFunction heuristic ;
34-
32+
3533 /** Clears the model's tour history. */
34+ @ Override
3635 public void clearAgent () {
3736 ((AbstractMapAgentModel ) model ).clearTourHistory ();
3837 frame .modelChanged ();
3938 }
40-
41- /**
42- * Performs necessary preparations for running the agent. The behavior is
43- * strongly influenced by the abstract methods {@link #selectScenarioAndDest(int, int)},
44- * {@link #prepareModel()} and {@link #createHeuristic(int)}.
39+
40+ /**
41+ * Template method, which performs necessary preparations for running the
42+ * agent. The behavior is strongly influenced by the primitive operations
43+ * {@link #selectScenarioAndDest(int, int)}, {@link #prepareModel()} and
44+ * {@link #createHeuristic(int)}.
4545 */
46+ @ Override
4647 public void prepareAgent () {
4748 MapAgentFrame .SelectionState state = frame .getSelection ();
48- selectScenarioAndDest (
49- state .getValue (MapAgentFrame .SCENARIO_SEL ),
50- state .getValue (MapAgentFrame .DESTINATION_SEL ));
49+ selectScenarioAndDest (state .getValue (MapAgentFrame .SCENARIO_SEL ), state
50+ .getValue (MapAgentFrame .DESTINATION_SEL ));
5151 prepareModel ();
5252 search = SearchFactory .getInstance ().createSearch (
5353 state .getValue (MapAgentFrame .SEARCH_SEL ),
5454 state .getValue (MapAgentFrame .SEARCH_MODE_SEL ));
55- heuristic = createHeuristic (
56- state .getValue (MapAgentFrame .HEURISTIC_SEL ));
55+ heuristic = createHeuristic (state .getValue (MapAgentFrame .HEURISTIC_SEL ));
5756 scenario .getEnv ().registerView (model );
5857 }
59-
60- /** Starts the agent and updates the status bar of the frame. */
58+
59+ /**
60+ * Template method, which calls {@link #startAgent()} and then updates the
61+ * status bar of the frame.
62+ */
63+ @ Override
6164 public void runAgent () {
6265 startAgent ();
6366 List agents = scenario .getEnv ().getAgents ();
6467 if (agents .size () == 1 ) {
6568 Agent agent = (Agent ) agents .get (0 );
66- String status =
67- (String ) agent .getAttribute (DynAttributeNames .AGENT_STATUS );
68- Integer travelDistance =
69- (Integer ) agent .getAttribute (DynAttributeNames .AGENT_TRAVEL_DISTANCE );
70- if (status != null && travelDistance != null )
71- frame .setStatus ("Task " + status + "; travel distance " + travelDistance );
69+ String status = (String ) agent
70+ .getAttribute (DynAttributeNames .AGENT_STATUS );
71+ Integer travelDistance = (Integer ) agent
72+ .getAttribute (DynAttributeNames .AGENT_TRAVEL_DISTANCE );
73+ StringBuffer statusMsg = new StringBuffer ("Task " );
74+ if (status != null )
75+ statusMsg .append (status );
7276 else
73- frame .setStatus ("Task completed." );
77+ statusMsg .append ("completed" );
78+ if (travelDistance != null )
79+ statusMsg .append ("; travel distance " + travelDistance );
80+ statusMsg .append ("." );
81+ frame .setStatus (statusMsg .toString ());
7482 }
7583 }
76-
77-
78- /////////////////////////////////////////////////////////////////
84+
85+ // ///////////////////////////////////////////////////////////////
7986 // abstract methods
80-
87+
8188 /**
82- * Template method , responsible for assigning values
83- * to attributes {@link #scenario} and {@link #destinations}.
89+ * Primitive operation , responsible for assigning values to attributes
90+ * {@link #scenario} and {@link #destinations}.
8491 */
8592 abstract protected void selectScenarioAndDest (int scenarioIdx , int destIdx );
86-
93+
8794 /**
88- * Template method, responsible for preparing the model.
89- * Scenario and destinations are already selected when
90- * this method is called.
95+ * Primitive operation, responsible for preparing the model. Scenario and
96+ * destinations are already selected when this method is called.
9197 */
9298 abstract protected void prepareModel ();
93-
99+
94100 /**
95101 * Factory method, responsible for creating a heuristic function.
96102 */
97103 abstract protected AdaptableHeuristicFunction createHeuristic (int heuIdx );
98104
99105 /**
100- * Template method , responsible for creating and starting the agent.
101- * Scenario, destinations are selected before as well
102- * as search method and search heuristic.
106+ * Primitive operation , responsible for creating and starting the agent.
107+ * Scenario, destinations are selected before as well as search method and
108+ * search heuristic.
103109 */
104110 protected abstract void startAgent ();
105-
111+
106112}
0 commit comments