HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 m2 k0 ^, h$ x d- ?+ F2 Q2 W
5 d7 t0 T% u; b+ X9 D public Object buildActions () {
' G1 A' f( @* T) v9 u1 @9 A: d! D4 \ super.buildActions();/ N; Q: J/ }. c' w( M
5 B/ h) W4 d' \+ U1 ^ ^' x! X' l
// Create the list of simulation actions. We put these in
* c3 A9 Q- X# |) M6 i) M // an action group, because we want these actions to be4 w/ S5 S7 m% M
// executed in a specific order, but these steps should
0 v! B7 A* ]; v0 A // take no (simulated) time. The M(foo) means "The message
9 \& U+ p- i: w8 P7 ^ // called <foo>". You can send a message To a particular# J# z' U, ` \4 g3 ~& i
// object, or ForEach object in a collection., A! J/ W3 J& b" Y3 D# _9 q8 u
, q+ ?# G2 c$ O8 j' e, d+ _6 W
// Note we update the heatspace in two phases: first run
# Y+ V8 P! Q, [% @- D% _ // diffusion, then run "updateWorld" to actually enact the
2 y# Z) f+ u# \; I. F // changes the heatbugs have made. The ordering here is& v* E! D G1 ]2 R7 f. {# R0 C
// significant!: b: x* l6 k; c5 Z: d" @" v
1 N+ k8 ?3 @9 Q. t
// Note also, that with the additional# {' z. X) q2 |! R4 h9 c ]8 u
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 R$ w5 F5 |1 ]3 M6 I' w
// randomize the order in which the bugs actually run3 H2 c- C9 T4 j q( b; D
// their step rule. This has the effect of removing any
8 U+ d4 u% D) c6 n1 t // systematic bias in the iteration throught the heatbug
- z2 I/ ~" g; P // list from timestep to timestep
& E/ m/ E6 z0 ]9 r6 `* Y ! Z* r! a) Q/ V$ x
// By default, all `createActionForEach' modelActions have
, @; u+ D: K u) n# [ // a default order of `Sequential', which means that the
$ i* F6 H& A8 G& k+ T // order of iteration through the `heatbugList' will be
, ]5 b. g* p- l8 v // identical (assuming the list order is not changed! g: ]1 y6 z8 |" h+ Z
// indirectly by some other process).* L2 t7 I# U) n
' h) U* H7 M8 Q* [8 S& n1 Y modelActions = new ActionGroupImpl (getZone ());/ O& o' b( A" v% g
/ z) Q" `( g( |& r2 z) P5 ~
try {: |4 ]# M! D$ W/ V% c: y1 H, C( S
modelActions.createActionTo$message9 I4 E& B [+ [5 g- @# ^3 u8 c+ Y
(heat, new Selector (heat.getClass (), "stepRule", false));& W% ^/ ^' Q( B& U& f0 O
} catch (Exception e) {9 D8 k! F2 i7 h/ R- C2 x; {2 B
System.err.println ("Exception stepRule: " + e.getMessage ());) ]% W6 j& w* u: X, q/ Z3 y
}
5 F- d, l. L% m* J4 s
- }- ~, v8 H) l try {
6 f3 B( V9 h7 X8 J Heatbug proto = (Heatbug) heatbugList.get (0);% Q* x6 |$ A ~* s) ?% M. X5 a
Selector sel =
# g4 J/ I7 n' ~+ U% m0 B( R new Selector (proto.getClass (), "heatbugStep", false);
( e C: m' m# c6 |, S actionForEach =: W) E+ r: o$ k% l( B' o1 M# V
modelActions.createFActionForEachHomogeneous$call! u5 g( k- ]# s. [
(heatbugList,
5 H O3 D4 l3 a! l5 v new FCallImpl (this, proto, sel,
, o. `) B$ J' g Z5 D% N5 ~1 ~7 z8 C" J new FArgumentsImpl (this, sel)));
: Z9 z$ ]) S7 Z s* \ } catch (Exception e) {
2 _8 _9 u% S O& {& s& j e.printStackTrace (System.err);! j+ o2 B8 c, j0 w& ~0 C. m: a
}0 ~4 \+ X/ ?1 |/ r. _& h6 `
3 I( M; O. F5 b7 S8 x syncUpdateOrder ();* ? O4 p9 I& `0 F
4 u: k8 F- o( b. N! m/ I try {
2 J" n: r* k! Y4 I8 ]0 I modelActions.createActionTo$message 0 y5 r3 l9 ?7 b2 e4 c- T9 ?. ~- E' i$ T
(heat, new Selector (heat.getClass (), "updateLattice", false));
) ]2 O/ J& R9 p9 V. f( x } catch (Exception e) {
3 b5 Q% u/ `* U: R% L4 E" h System.err.println("Exception updateLattice: " + e.getMessage ());
# \/ K6 y, c3 D) x! V }
; b6 J. y. E! g6 B
2 J$ v0 g- P) D6 H' X) v // Then we create a schedule that executes the. S+ |4 Z: R) ]# v
// modelActions. modelActions is an ActionGroup, by itself it* | ^+ H p6 r( s" \1 b6 ^; U
// has no notion of time. In order to have it executed in/ C# @. W" S& Z- s6 s
// time, we create a Schedule that says to use the
; Z0 Y% M( D+ i$ K4 Q) L: S" R // modelActions ActionGroup at particular times. This
7 g( v( |& E" w // schedule has a repeat interval of 1, it will loop every: B0 e( g$ l; |; E$ ^* F
// time step. The action is executed at time 0 relative to
; |1 ]7 G* r& P // the beginning of the loop." \7 Y' b% l9 A( ^6 v
, X9 W( t- J% z9 I0 S; W // This is a simple schedule, with only one action that is- x; R" M6 `. D3 [" |
// just repeated every time. See jmousetrap for more
, l4 X4 g# |' z/ K3 E* v% V$ N+ f U // complicated schedules.
& r2 z: {5 q0 \. ~ : N; Z5 h3 W$ L8 O
modelSchedule = new ScheduleImpl (getZone (), 1);! \- p( `. b* v1 H0 H
modelSchedule.at$createAction (0, modelActions);1 `. s1 \% X) u1 X
% Q' W0 }. M% N9 Q1 Q" R return this;
( h2 B) j/ l; J7 q4 S. h4 Z6 d; | } |