HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 ~0 N" B2 _! M2 |' X
! |4 g' t, R9 Y; u+ O1 s, |1 t public Object buildActions () {
% [. H& [7 M5 f" L0 [6 l4 ^, B super.buildActions();
$ I& n3 F* q- b# W+ M& F
* B6 d) P( L# K8 r+ Q; e! X // Create the list of simulation actions. We put these in
6 _8 d) o, r# _8 Y8 ]# N! W) Q // an action group, because we want these actions to be5 Z. |. g! U) _3 Y# ~, c6 R
// executed in a specific order, but these steps should
% n6 S& J2 f6 m1 B9 _ // take no (simulated) time. The M(foo) means "The message
, |2 \% o2 _: r# J+ K! |: T // called <foo>". You can send a message To a particular4 z& {5 v. {& u# O( n) X/ c7 a# A; O
// object, or ForEach object in a collection.; N8 i) S( Y3 e6 `0 o- R
2 f a! g' H! Z. m
// Note we update the heatspace in two phases: first run) \. |, ^" B( C, b
// diffusion, then run "updateWorld" to actually enact the B/ p8 R: d j- q
// changes the heatbugs have made. The ordering here is
; U' E/ I2 d9 b // significant!* ~! o% B7 ?6 E- }. m! a6 e
5 I. a6 T# `$ \' i3 N5 M: A // Note also, that with the additional- w8 {: ]- i& b& B
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ `' A4 R ~% S/ F // randomize the order in which the bugs actually run( `9 f, Z) n4 p" o( k! X$ M$ O! D
// their step rule. This has the effect of removing any
8 Z1 R8 a( f- T- u+ v9 q5 p // systematic bias in the iteration throught the heatbug
0 O. d' z w, z$ f% p // list from timestep to timestep" l5 a5 [$ X8 I1 A z5 V, ]+ q
* `* ]" b8 Y7 b2 T, f6 p0 k // By default, all `createActionForEach' modelActions have' X$ g3 R. [# }( z& Q- _3 U. f+ E/ y
// a default order of `Sequential', which means that the( C/ u T6 U& n1 a8 w2 R- U+ L
// order of iteration through the `heatbugList' will be
2 q; H+ h1 ?1 j1 L1 D0 w9 g8 I // identical (assuming the list order is not changed
2 d, f# E! R; u' b8 W% m: o // indirectly by some other process).3 Z1 h: y9 M# K) u0 k
* j5 t+ S! c1 D modelActions = new ActionGroupImpl (getZone ());+ u8 _6 B6 [- l- S/ M" r
: ?8 z K) u u6 [9 J1 h try {8 M5 |4 y3 {& p4 C: A* D
modelActions.createActionTo$message
9 h( s$ s1 t& Y& D (heat, new Selector (heat.getClass (), "stepRule", false));
/ i& K+ o5 G1 ^5 q: u- Y) H0 x$ X( o# ] } catch (Exception e) {
3 }4 j# i% ~7 v9 V# n! y System.err.println ("Exception stepRule: " + e.getMessage ());
8 Z" _7 [ U: L) @ }
2 O: Y$ P& Q8 e# A9 z, C+ Y9 k/ Z1 Q0 m. |2 V- S7 H. A
try {- m0 U9 j3 @8 a! y; |0 N6 _+ m
Heatbug proto = (Heatbug) heatbugList.get (0);
: ~& s( }# @& s; Z5 x+ T0 I Selector sel = % Z* u9 M" J8 \! W8 J
new Selector (proto.getClass (), "heatbugStep", false);
& i3 w3 j8 x. u! L actionForEach =; V$ V$ H- t+ _; s5 F
modelActions.createFActionForEachHomogeneous$call
" |1 _2 N9 |6 r% a (heatbugList,
0 }, D' U$ o/ H# ]3 R3 p7 S5 h new FCallImpl (this, proto, sel,! H5 G8 ~7 Q# s8 \) M4 N7 P
new FArgumentsImpl (this, sel)));
7 x/ w5 }% U4 E } catch (Exception e) {
5 y' A) `$ R8 u: G; S( | e.printStackTrace (System.err);% [; [8 ^+ i) |
}$ f# v, Z$ p# q' c& p
5 `1 d" s5 ^* b7 X" _
syncUpdateOrder ();
) X( B& R! K) i1 r- _1 N( V1 ^3 N% }3 _. r
try {8 M/ U# {' q, F/ J
modelActions.createActionTo$message 8 i# e/ R/ b7 ~9 r3 e$ f3 l
(heat, new Selector (heat.getClass (), "updateLattice", false));* [; S" D0 S& C
} catch (Exception e) {' B& b8 t/ Q& A
System.err.println("Exception updateLattice: " + e.getMessage ());# V9 v# G$ R2 C, z
}
% J& T) {* P7 q8 G5 j1 E! `( ]$ T+ W ! H$ S1 m8 Y$ J+ r" D
// Then we create a schedule that executes the
6 ~( y) @: e5 d+ j: B' [/ T4 P // modelActions. modelActions is an ActionGroup, by itself it
) K7 u0 a9 v$ U/ ?; M // has no notion of time. In order to have it executed in( O) r& X. I( ?8 Y# V6 P0 d8 W
// time, we create a Schedule that says to use the
5 l& t H4 S* P0 t7 i // modelActions ActionGroup at particular times. This
# O7 I0 |. K5 ]5 P // schedule has a repeat interval of 1, it will loop every3 V( ]; N/ R! h: I8 A
// time step. The action is executed at time 0 relative to" `1 v8 h' _9 i) t( c0 h' a
// the beginning of the loop.* s4 k7 G% j# V4 ]( ?
; W1 r( B4 f* g7 y: C/ o
// This is a simple schedule, with only one action that is- D8 C' F2 u- ?' w7 v8 N& b
// just repeated every time. See jmousetrap for more
$ Q% u+ b/ x/ E/ I) e // complicated schedules.
% J, @; n) T0 G, w) @! p: X4 @# h4 y / A6 [2 w' q+ l1 W: T" f% V7 Y
modelSchedule = new ScheduleImpl (getZone (), 1);( d5 }8 y8 Z/ h7 y
modelSchedule.at$createAction (0, modelActions);! v2 z2 @; e3 [- c$ a* Z( l1 a/ U
M u: O$ Q$ v f( k return this; Y6 H( T' @- x/ n
} |