HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 N; E, I6 e6 u3 K1 g' V" Z; ~( g2 s6 p8 e
public Object buildActions () {8 H1 G, b C+ I& u! o) ]0 ^# T1 K
super.buildActions(); g" A* Q' T; z1 S
5 D( q A9 @) Q // Create the list of simulation actions. We put these in E9 D; Y1 c5 c7 g: D) j' u
// an action group, because we want these actions to be
6 a' h- y3 m8 s) G // executed in a specific order, but these steps should8 Q" {8 N3 q" J
// take no (simulated) time. The M(foo) means "The message/ _7 ]5 L [! P4 d2 M: r
// called <foo>". You can send a message To a particular
5 t3 v$ j. \8 ^/ l3 }( R // object, or ForEach object in a collection.* P# c6 P0 T! \$ N2 }1 ~
& i1 ?! {( j7 @5 U, y8 s# P' `
// Note we update the heatspace in two phases: first run
' @' r' m- j. a // diffusion, then run "updateWorld" to actually enact the
4 N, @6 N& x# Q( P" G( J8 Q/ ^ // changes the heatbugs have made. The ordering here is
9 f9 Y( {2 S3 {' q5 I // significant!
+ w- k( ]/ I5 v
# Z3 Y* ]$ m. v$ L& n8 { // Note also, that with the additional
8 N$ @4 P7 `; Y, E, @, X/ Y- ?" } // `randomizeHeatbugUpdateOrder' Boolean flag we can7 t$ t( O; d- w4 v# a3 R9 \
// randomize the order in which the bugs actually run& L9 S. N! Z4 s' S9 l& B" p, z ^. E
// their step rule. This has the effect of removing any
6 r' V, V( p$ V4 I // systematic bias in the iteration throught the heatbug' J' k6 ]( ^# S4 y1 B- a5 E
// list from timestep to timestep# A8 M* \# L4 N0 {; R# F- I! H
- H) {/ R O* ]4 J // By default, all `createActionForEach' modelActions have J) w3 s! }0 F0 C, T
// a default order of `Sequential', which means that the5 k: q$ `0 B, Q3 u& G8 p z
// order of iteration through the `heatbugList' will be
8 \9 z3 y* `$ i: Y! G) j5 \4 r // identical (assuming the list order is not changed* ^ e- c* d+ k
// indirectly by some other process).' y. H2 j+ F; [( Z
9 R& R$ V5 ?$ M8 b' |: E4 [ modelActions = new ActionGroupImpl (getZone ());7 \1 r- `- \ Z( a8 B9 R" k8 k6 `, U
9 D1 s( u: U+ ?! J9 Q/ ~) {. a try {- E8 N9 k8 D6 Z( P5 n4 R0 S
modelActions.createActionTo$message
/ ~9 C; l8 C8 V' P. i (heat, new Selector (heat.getClass (), "stepRule", false));
5 d; l0 m& j D, i. I; @) _ } catch (Exception e) {
" ~6 U7 a/ P6 d: n. z6 @4 L System.err.println ("Exception stepRule: " + e.getMessage ());9 z% j7 K/ q( T: v/ W. Q
}
3 I( S* x. L& J! N; ^2 [: d$ `5 H" q; F5 X: E4 Y
try {) M& v: I5 D2 n
Heatbug proto = (Heatbug) heatbugList.get (0);
5 z% O: s1 p- ?" h; d* L' B Selector sel = , ?. x9 J) }3 f% L& O. g/ n
new Selector (proto.getClass (), "heatbugStep", false);3 o# s4 K9 z% a& Y2 I3 M( W% d" @
actionForEach =) B5 H( T: k& V- } o$ j. j
modelActions.createFActionForEachHomogeneous$call
- f, C, }& P) z5 X (heatbugList,
2 ]4 l: ?- o8 `$ W new FCallImpl (this, proto, sel,7 A, y! V; N! ~# ?: |
new FArgumentsImpl (this, sel)));0 ^3 s1 P5 } J4 L! A
} catch (Exception e) {3 m) X4 S: J( M# v: _/ J
e.printStackTrace (System.err);
F' f0 Y( ^+ R- r, t8 G- K! z }5 ?7 L) @* K$ V
. j) X- g! `$ K3 W7 `
syncUpdateOrder ();7 O# T+ i e8 }% O) f0 v4 v8 z
' W6 O# }5 `0 e( {! H# G try {$ P" F+ d# b) @3 {0 t5 z& z* m: i
modelActions.createActionTo$message . c' [. a/ D$ u% P, o
(heat, new Selector (heat.getClass (), "updateLattice", false));) h3 \* o; |4 u
} catch (Exception e) {5 Q# l/ I; Y& d$ l6 i/ y% @
System.err.println("Exception updateLattice: " + e.getMessage ());
) h9 X" q( T% b7 b9 V }
5 s. N" S: y% ~1 x9 X1 p* N7 O; c# J
0 z& M, z/ J' U5 P9 \+ @. { // Then we create a schedule that executes the
/ R; y, H: {& p! d3 K // modelActions. modelActions is an ActionGroup, by itself it
4 L: P! j3 B2 C1 k // has no notion of time. In order to have it executed in0 A! k# l4 V/ N' h2 k0 g# W! ^; ?
// time, we create a Schedule that says to use the
& s( s! b# }% m! m4 m, y8 g // modelActions ActionGroup at particular times. This
+ B( S% C9 r* y7 Q1 _* ^: \ // schedule has a repeat interval of 1, it will loop every: q' {( s. X$ ?( p- u$ V
// time step. The action is executed at time 0 relative to
( v: c" B. d( U8 e" A4 d* M // the beginning of the loop.: p, _" s" s* o/ W
7 Z7 ?9 n, y# s b$ m
// This is a simple schedule, with only one action that is
' o3 g" y2 v0 r4 M9 r // just repeated every time. See jmousetrap for more
" F0 t O' ^& [9 K. J/ M# Q // complicated schedules.; r& `$ B" w& J6 c. E9 r
5 R/ K+ ~, }( e% ?0 J* S
modelSchedule = new ScheduleImpl (getZone (), 1);4 T; u! z U( V4 B$ D+ o9 ~ ]
modelSchedule.at$createAction (0, modelActions);
$ E4 f! x3 X, y' j9 Y ; i& H# S) G; d, z4 K
return this;* y: ~5 f, _& R. U: P0 z
} |