HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 _. X) l, a( a2 ^; Y0 g
: r o3 C! Z) ]1 S public Object buildActions () {! H$ n7 g2 @& L3 l5 w( v
super.buildActions();( H/ b% L' v6 H) h; x
2 h: a& i6 W7 a/ k4 B. r
// Create the list of simulation actions. We put these in2 z- R5 z' j- r5 @) Y w' t
// an action group, because we want these actions to be- G z- h* ]- Z+ D0 m( `
// executed in a specific order, but these steps should& J% v/ Y$ F2 l. Y3 ?
// take no (simulated) time. The M(foo) means "The message
6 }1 l: r0 C$ s+ E! S) ` // called <foo>". You can send a message To a particular! S: c' U6 t# S! x4 l4 _
// object, or ForEach object in a collection.
. {' r$ I }. y0 E
7 d# P# ]; ^1 ] a- A9 b I // Note we update the heatspace in two phases: first run N j; O2 T! T' H, w5 I
// diffusion, then run "updateWorld" to actually enact the
8 @, n4 m: v# j3 Q // changes the heatbugs have made. The ordering here is
" u0 s4 l% H5 z. N // significant!
4 g+ B0 L. E) t
' o% e N$ G0 e* ` // Note also, that with the additional' U$ i, K3 a! B _
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 \& q" W6 H# X$ X
// randomize the order in which the bugs actually run
! y# l5 \& O- \" {) @$ e& M // their step rule. This has the effect of removing any
9 f4 t/ k" H$ d' O# K // systematic bias in the iteration throught the heatbug
! h9 t7 I2 t; l- J+ J // list from timestep to timestep) H% U7 y/ N: w
5 u) Y& t. k( i
// By default, all `createActionForEach' modelActions have
# @0 M2 s5 H3 T5 O // a default order of `Sequential', which means that the; _9 {# q2 I) K: k# [
// order of iteration through the `heatbugList' will be
5 [: S* L6 F1 c$ u& G: t- w: ]5 \ // identical (assuming the list order is not changed
0 ~: u$ _( f8 A: z: m2 c- ` // indirectly by some other process).2 i2 w W" Y; \, h+ k0 ~( S
5 V. F# o4 F% P/ l" S modelActions = new ActionGroupImpl (getZone ());" z' ^% n% c% q. {
/ p6 n, D, k( g7 t% }
try {3 m( E7 D! N. C( A, K% h- O
modelActions.createActionTo$message
! t7 [* G4 R9 f" } (heat, new Selector (heat.getClass (), "stepRule", false));8 G7 c9 M8 y9 S) R; c
} catch (Exception e) {
. z$ D( A' x& f$ L System.err.println ("Exception stepRule: " + e.getMessage ());# z7 P0 r$ e: U2 L9 p' @3 F
}
& X: ~; e, W# [* D; C
: q8 |" V1 @7 P# T3 d: j try {$ q `; N" K7 o7 W8 X* e
Heatbug proto = (Heatbug) heatbugList.get (0);
- x5 Y, O7 i" y( q& |1 V Selector sel =
8 Z* \9 a3 Q( g new Selector (proto.getClass (), "heatbugStep", false);' }: i+ Q2 a, C2 y
actionForEach =# ]3 X( `9 G' H& n8 U# x
modelActions.createFActionForEachHomogeneous$call8 N- b0 d( X* u p/ K9 T
(heatbugList,6 J6 `8 P" ]) c( F7 l% v
new FCallImpl (this, proto, sel,8 Q( w c6 j7 ^/ T
new FArgumentsImpl (this, sel)));
; i# N) @6 O4 F. s9 d8 V: H } catch (Exception e) {
* [9 N: l# i! Y- M e.printStackTrace (System.err);$ X1 S2 V$ C/ ^7 m9 m2 P
}9 D& c, X \! E
! x& ?7 I" G- |1 b( u# H% l4 E syncUpdateOrder ();- D3 T L' w3 ?/ B. p
8 e3 t! T! z; `! k+ [
try {
2 s; t! l$ w0 B: U' w5 _ modelActions.createActionTo$message
0 D% f. m: A/ z. } (heat, new Selector (heat.getClass (), "updateLattice", false));: j* @! m& _. ]$ a4 f+ g
} catch (Exception e) {% }0 g" Y; ~7 Y2 Q3 s5 ]2 |7 X# p
System.err.println("Exception updateLattice: " + e.getMessage ());
1 d' f) e1 x& y' X5 A }3 E, h* O$ X- ]6 j v& x2 W( ]
; A1 K2 G6 v) s# C; e3 n2 w9 X% ? // Then we create a schedule that executes the
- F! |5 W! P. ]4 m // modelActions. modelActions is an ActionGroup, by itself it
0 T1 ?6 C# W; e+ o // has no notion of time. In order to have it executed in
4 G& X0 F% j# ^) T8 ] // time, we create a Schedule that says to use the: K( D( f, k: Y8 z1 y; e( V( {
// modelActions ActionGroup at particular times. This B- R3 _, [* I! D
// schedule has a repeat interval of 1, it will loop every
& g/ s- Y( F- Z/ k // time step. The action is executed at time 0 relative to
- r7 \/ V4 f: @9 w4 O# E5 P7 X // the beginning of the loop.
7 J( f/ d3 e; x+ l( u; p! V* D
: t0 t0 ?2 I; Z4 w // This is a simple schedule, with only one action that is4 {; d+ a# m1 X& A0 Z; T/ t% x
// just repeated every time. See jmousetrap for more
. M E4 J' B. h2 h // complicated schedules./ i' ^' Y* F; U# U( t* p# u9 c+ X
5 Z6 s0 k" K; i( i) D7 g, S
modelSchedule = new ScheduleImpl (getZone (), 1);, ^1 N/ M" k3 d+ P
modelSchedule.at$createAction (0, modelActions);, ^; Y2 \% p: ~( f
. N7 j- @( y P' I, b4 G. f5 B# _ return this;, `8 e; |! }" T4 m
} |