HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- ]8 g* q* H B: z. O
- E& Y2 B3 U) K! g5 L/ p public Object buildActions () {
; V# x% f3 z3 D/ }5 ?/ ] super.buildActions();6 l' }5 J* |& Z; H
2 P2 n; t0 i8 l( ]9 a
// Create the list of simulation actions. We put these in
1 @; _/ d3 U S: b6 t# U$ O4 e // an action group, because we want these actions to be: z* ~! M7 {4 v- X
// executed in a specific order, but these steps should5 Y- C/ z" O6 P1 b# m
// take no (simulated) time. The M(foo) means "The message
+ {" l+ R' P! ]( U" v! u7 W5 p // called <foo>". You can send a message To a particular
1 ^" a4 u# R0 p, N8 d8 D // object, or ForEach object in a collection.
4 \0 _2 j2 C1 p# N8 l$ S. i7 W ( g; H& [; y4 B
// Note we update the heatspace in two phases: first run" Y$ l' D! ^1 J$ m$ U
// diffusion, then run "updateWorld" to actually enact the. _! ?( Z+ h4 N) n9 p# P
// changes the heatbugs have made. The ordering here is
- f1 d7 C3 U$ F c" a // significant!) L1 B, \" y& P. t! K) f
8 u' c+ H3 G6 f9 X7 w% D/ p& C
// Note also, that with the additional
# Y$ q0 }6 U& Q/ I) L% h" @ // `randomizeHeatbugUpdateOrder' Boolean flag we can& i7 v1 o) ?& a, V* p( r5 U+ s. @
// randomize the order in which the bugs actually run
2 X1 G6 @7 J* A( B // their step rule. This has the effect of removing any
9 Q: q: }9 u% r9 { // systematic bias in the iteration throught the heatbug7 I; G# Q4 P7 O6 y; z
// list from timestep to timestep
0 N) `$ P% J3 J& m& ^ 2 N# G- i4 k" u
// By default, all `createActionForEach' modelActions have8 N3 u1 r) U) Z+ t9 S
// a default order of `Sequential', which means that the
; K" R! t/ w- D8 M // order of iteration through the `heatbugList' will be3 m. Y- A4 Q9 x( ?! K
// identical (assuming the list order is not changed8 p# I' _9 t' ^$ n, s$ e# j6 @
// indirectly by some other process).# O9 }: a2 t; g/ {% ~
: C7 q( X6 o& Z$ ]3 z& r6 \
modelActions = new ActionGroupImpl (getZone ());. K# K" Q/ r: c! |0 ~
9 b6 n% b* r7 ]1 a
try {
$ [$ } a5 G5 \# D1 T9 L- I H0 I modelActions.createActionTo$message
0 }8 m$ H) Z5 T' _& Z9 B; X (heat, new Selector (heat.getClass (), "stepRule", false));
7 \+ C: `# L9 t+ o6 [+ `$ L% D } catch (Exception e) {, t% w( T/ s2 D' u
System.err.println ("Exception stepRule: " + e.getMessage ());5 [0 c+ P6 L4 n6 `* i" |
}
( K1 h7 T6 j; P- T4 `; r" D# E2 a6 v- U
try {! u( K( }( V* j
Heatbug proto = (Heatbug) heatbugList.get (0);4 B: r7 ?+ F9 H9 _
Selector sel =
7 h6 v$ H7 \3 `. z) V/ p* f# @7 c new Selector (proto.getClass (), "heatbugStep", false);0 D, ^9 ~# I: _1 v) w/ H& b
actionForEach =
# g: R6 c8 Y2 {/ i5 g" d modelActions.createFActionForEachHomogeneous$call
7 P. u$ z( |8 j. U& n( V) Y( [) z7 H (heatbugList,
" C# v ~: z& l) M5 i' W new FCallImpl (this, proto, sel,
/ ? P: k B0 I7 C7 h5 h& g new FArgumentsImpl (this, sel)));- o/ f* ~8 e. f+ ~$ ] k; j
} catch (Exception e) {8 i% ~! l1 R6 a$ L1 @, N1 C7 Z" c. @
e.printStackTrace (System.err);
& Z# [8 r1 ?! Y+ \ }
# \/ ~5 U6 _9 _3 z8 U6 M 7 a' T* y8 E1 G! X/ W
syncUpdateOrder ();: y0 z& `: X8 K$ H' T
# _& z, U: s( }) \ try {4 E! N; F W" J; C3 l Q
modelActions.createActionTo$message
4 e( M) `" X+ C7 i7 g (heat, new Selector (heat.getClass (), "updateLattice", false));
J e* w; B$ ~) q" | } catch (Exception e) {$ d; R/ P/ M: v! V0 D
System.err.println("Exception updateLattice: " + e.getMessage ());6 B5 p, t- K* B7 I
}% h* a W0 F: h$ w; k7 i
9 Y' S& d) K0 ]* h
// Then we create a schedule that executes the/ W$ |4 R" d+ L4 ~
// modelActions. modelActions is an ActionGroup, by itself it" b) j. C) z7 G
// has no notion of time. In order to have it executed in7 m$ O( p3 Y% Z3 [ ]: `
// time, we create a Schedule that says to use the+ j; L& o6 `: ^; [: B
// modelActions ActionGroup at particular times. This
" e, j7 `" R. u2 b$ T p* a // schedule has a repeat interval of 1, it will loop every
2 W6 N# G! L0 q2 n1 i // time step. The action is executed at time 0 relative to
! j3 X3 E% G1 B7 P; D0 z- |' K( D // the beginning of the loop.
" k+ ^$ y+ X0 p0 X/ h
; H, r8 I! U! Y+ k7 ` // This is a simple schedule, with only one action that is3 i1 R7 F# S6 k p* ~7 N
// just repeated every time. See jmousetrap for more6 G5 @& l9 t5 x! G0 ]( i0 X" f
// complicated schedules.* P: j" B- I1 |# L4 P1 I
" `) x8 Y1 C: f; c
modelSchedule = new ScheduleImpl (getZone (), 1);
. ~8 g" e2 P: [3 b) I' d modelSchedule.at$createAction (0, modelActions);
4 t( M: q s' }8 s4 N3 j
1 Z$ ~, @- H1 I- v8 \ return this;' {/ n8 ]( o9 H5 O7 P
} |