HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: z& q) ?! r% T
: @( Y" g( r: {% g) A2 W/ w$ v public Object buildActions () {
% q' [6 J9 J% T$ a t9 t super.buildActions();
( H Z. K9 A& F: Y" Q6 w( o
7 Y: Q5 g. d/ E4 n2 k/ G, Q: J // Create the list of simulation actions. We put these in9 P8 K2 n& d9 _
// an action group, because we want these actions to be& w3 |. n8 O `$ N( ]
// executed in a specific order, but these steps should. F: E5 g' [# @2 s S7 U4 ^1 B
// take no (simulated) time. The M(foo) means "The message
" s$ ^3 K7 {9 z& W# d* d: |; g // called <foo>". You can send a message To a particular
6 W# m+ G1 x. O1 ?# H // object, or ForEach object in a collection., b- _1 z3 |5 K \. ?8 g d! Z
7 y0 U# p% A. a8 O# S
// Note we update the heatspace in two phases: first run
$ X; \0 W" p5 ~; Z; N // diffusion, then run "updateWorld" to actually enact the! \ v9 E4 g7 [3 A( H3 {5 Y
// changes the heatbugs have made. The ordering here is
( g- X* f' N: j1 v6 S0 {3 M // significant!: x) F. A& Y1 q6 y2 [/ N; Y
& o! o: h3 V/ [' U. A( D8 K$ Y+ D
// Note also, that with the additional# D( N0 a* i1 p3 d( [, ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ a8 ]2 o$ H" ? // randomize the order in which the bugs actually run$ O. _ Q4 V# z F3 w, C' y( D
// their step rule. This has the effect of removing any
/ c' n: T6 L" t. k // systematic bias in the iteration throught the heatbug
: c6 h. ` G/ c // list from timestep to timestep1 t5 k4 d" f: w7 q1 z
9 ?5 ^* s3 y0 q$ S
// By default, all `createActionForEach' modelActions have# s1 h* u0 v$ |0 H, B0 r7 e! i5 H
// a default order of `Sequential', which means that the
. _( \" U4 q9 t( n0 [* y0 @ // order of iteration through the `heatbugList' will be7 p& Z, l' }/ o7 g$ @% |6 r
// identical (assuming the list order is not changed
0 n3 ~, [' b, T5 _. u // indirectly by some other process).; X/ O$ O0 T7 y" G( t! o0 m
: J! z3 ]* ]; W( r4 [( }. l. y6 { modelActions = new ActionGroupImpl (getZone ());% M% @! h5 c" F7 u1 I8 ?7 b8 e7 d
$ _. w# p E$ Q( `$ z1 O try {
1 @8 d8 Q0 `; p) B- V* { modelActions.createActionTo$message
0 [) C9 k$ \# ^7 G9 ^ (heat, new Selector (heat.getClass (), "stepRule", false));# u+ I$ f; ?+ F) F3 |3 O" \) W, Y( v
} catch (Exception e) {
" V* G$ [+ `; S) ^ System.err.println ("Exception stepRule: " + e.getMessage ());
0 l/ O: q: y% F# H. [- v# p' \ }& a; C* @3 k+ L" o
. `5 ^9 _( z# T n1 x# z: C
try {
. A; z2 n5 e' `; k { Heatbug proto = (Heatbug) heatbugList.get (0);% I3 ~2 ^1 x2 E; j+ G
Selector sel = . U5 A1 u; s b# j
new Selector (proto.getClass (), "heatbugStep", false);
+ S* i6 t9 c/ R {1 {! X actionForEach =
7 x& X& n6 f) a modelActions.createFActionForEachHomogeneous$call
5 R; W1 c$ l+ L4 b (heatbugList,
/ ~+ F2 y( V; B. M4 J) @ @) z new FCallImpl (this, proto, sel,6 e/ l$ C+ L( [6 a5 w0 \0 J$ L
new FArgumentsImpl (this, sel)));4 {, x& O3 x; d- \1 \" D
} catch (Exception e) {' K8 `! @) `2 v% C% z
e.printStackTrace (System.err);
/ d9 p' V- {& x) g$ z# h6 D5 Q( e5 M }# i1 _) e+ i( ] W/ X
5 ~* q# o7 c( B3 n2 O H
syncUpdateOrder ();6 g4 H( J( p7 k$ O, J% l9 }5 o7 v, v
6 i+ o/ }5 x. L) J. Q: q
try {- @( S6 B% d, I8 Q- E, O
modelActions.createActionTo$message 6 u9 K0 I7 F, P; e {! r
(heat, new Selector (heat.getClass (), "updateLattice", false));* P) e2 T3 a& p4 a8 h+ `
} catch (Exception e) { b' m5 o8 q3 V4 g" ?* {
System.err.println("Exception updateLattice: " + e.getMessage ());
A/ ?% G R- [, t. _: B }! h% W1 ^% S8 e* r: p
$ [9 n+ Y/ L3 g/ O0 `* c* p, Z
// Then we create a schedule that executes the/ \3 c2 \: E! ]) t2 d) `
// modelActions. modelActions is an ActionGroup, by itself it5 b# G$ h3 z: N7 k' \6 V
// has no notion of time. In order to have it executed in
# A7 ?6 Q2 A, O0 n1 O3 d k$ R // time, we create a Schedule that says to use the+ g" [$ y2 H3 ?3 D* A
// modelActions ActionGroup at particular times. This3 x- Z" j* y. @
// schedule has a repeat interval of 1, it will loop every
/ G( s6 O& R j" v // time step. The action is executed at time 0 relative to, W7 t: ^+ q) b) d! D9 J
// the beginning of the loop.( Z' Z( `* |5 O6 J3 T) P, R- \
2 H) T3 y2 M' A // This is a simple schedule, with only one action that is; C$ X9 X& c3 K. B! ^: ~: a
// just repeated every time. See jmousetrap for more" h2 p+ q$ s: T$ v
// complicated schedules., P7 a# G3 G0 G) z5 b
' B" K- \3 [+ X& J1 A
modelSchedule = new ScheduleImpl (getZone (), 1);+ H! M8 `% u( @* p" ?3 I+ Q
modelSchedule.at$createAction (0, modelActions);
9 U7 M% ~/ |! L3 m$ I ) I$ S8 }, a+ u9 C
return this;
7 I/ E3 e. I+ \) g } |