HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ U9 s m- w8 L1 v4 b
* K3 L& z- z6 M% \ public Object buildActions () {% q2 i& j$ _' W/ r8 |0 v
super.buildActions();4 p& H5 g" }1 e0 ? H; a1 }) u1 r7 H$ E
! `; ^2 o) F! k1 f+ z // Create the list of simulation actions. We put these in
. Y r4 h/ L5 \+ a- K1 B3 f; l // an action group, because we want these actions to be$ q/ |/ o6 n9 X- O1 a
// executed in a specific order, but these steps should2 A3 z8 l9 I+ n/ g% m) h0 o
// take no (simulated) time. The M(foo) means "The message
6 @6 `7 d0 K. U; h4 s5 K3 F // called <foo>". You can send a message To a particular
# h, G& D" J) I9 h5 q // object, or ForEach object in a collection. f" O3 r1 h; ~; D1 @
2 L! y& [ g/ d3 k // Note we update the heatspace in two phases: first run
" L3 `2 q5 T! @$ ?5 F0 Z // diffusion, then run "updateWorld" to actually enact the
7 g' e/ K H7 t4 n7 F // changes the heatbugs have made. The ordering here is8 D$ F7 X* j' F# w$ t
// significant!9 Z' \* | w$ @
5 y! b6 c* J" k* L& n4 U
// Note also, that with the additional
) m6 I0 [4 f; |5 Q2 ~7 S% D, y // `randomizeHeatbugUpdateOrder' Boolean flag we can
) B8 p3 K# S Z: j$ C // randomize the order in which the bugs actually run
! a( @0 u' l( s // their step rule. This has the effect of removing any% W5 q0 M8 r" R s
// systematic bias in the iteration throught the heatbug- I9 x8 g) \' l9 }2 C" E1 S
// list from timestep to timestep& [' ?6 j) P+ l" E0 t* I
1 x( A" {. ~ j8 b+ B6 A/ E // By default, all `createActionForEach' modelActions have
# n0 g, Y. G" l& X // a default order of `Sequential', which means that the
4 U* u+ x2 C& b) x: B1 H // order of iteration through the `heatbugList' will be
5 k0 h& Y8 g6 r+ @5 _5 e) o // identical (assuming the list order is not changed
w! D- N" A+ u% X' Y. Z // indirectly by some other process).4 n. t/ K. j4 H
- v: R; K( j8 E+ p" L, E! b
modelActions = new ActionGroupImpl (getZone ());/ l& Q' J* N7 A& ?
- J) |, W0 k% ^4 M9 ]
try {
: n. W2 M- ]* G) a# i4 F4 c modelActions.createActionTo$message
: E, X- z3 m& c% S. l7 L (heat, new Selector (heat.getClass (), "stepRule", false)); B' f6 n+ J; _7 v( W8 q3 ]
} catch (Exception e) {$ b7 U8 e" N: m, U/ T( T
System.err.println ("Exception stepRule: " + e.getMessage ());
8 `3 b2 M% D! b3 D1 |- _9 Y }6 k* ~: i* y/ @# Y. E3 i
1 n! e# \# t7 d
try {& r8 Z, X! c% k1 W; c& M0 r
Heatbug proto = (Heatbug) heatbugList.get (0);
0 x' r; U1 R4 ~# B: D3 h' W6 D Selector sel =
6 f, }5 H6 ~: o* p; m new Selector (proto.getClass (), "heatbugStep", false);4 @# K3 F2 O0 Q
actionForEach = T0 |/ K4 e& \# w% ]- m
modelActions.createFActionForEachHomogeneous$call4 ^6 x, I [" n( S) P
(heatbugList,
( h& {0 C! {* ^7 E: a% d new FCallImpl (this, proto, sel,
$ \8 w/ v* l z3 x9 ]4 \# x new FArgumentsImpl (this, sel)));
5 s" L, F: }' g* Q* w( R* I } catch (Exception e) {" D) m( I# ?0 M0 e- g* Q
e.printStackTrace (System.err);
( P4 {2 h, n* } }
5 n- K$ w" H+ p. L( I: q$ E: O
8 _. j" s: Z$ O: t% W V syncUpdateOrder (); m" T C+ j% o0 N8 O. [/ ]! @
' h$ C# \6 o( H
try {+ N9 u5 ]8 {) U6 `
modelActions.createActionTo$message # W* I; Z* _3 Q3 ^/ ?& R5 c& x
(heat, new Selector (heat.getClass (), "updateLattice", false));
& |8 s) @* m w* q6 f, i5 {! Z0 G } catch (Exception e) {
" F( h; ]! g/ c: t( u, m System.err.println("Exception updateLattice: " + e.getMessage ());
, t/ a% g. K' F/ u0 B }
\8 B9 Y1 P/ j" u
$ P) C- g$ F! D6 B; b& Q3 R; E // Then we create a schedule that executes the
; N6 a9 p$ ^( O/ q: K# d/ o // modelActions. modelActions is an ActionGroup, by itself it
2 a5 Y0 v" \8 q // has no notion of time. In order to have it executed in, q0 B: i! U: j) }& T
// time, we create a Schedule that says to use the+ L/ ]* x% c0 K" d9 Z, t
// modelActions ActionGroup at particular times. This
7 s. J, e/ I3 _! { // schedule has a repeat interval of 1, it will loop every' K: }" U" x7 M- u6 {# s4 x
// time step. The action is executed at time 0 relative to
; P8 h, ?- S6 e8 d! m // the beginning of the loop. B F4 V. J4 A+ \9 F( Y* o
+ Z2 O8 R! q: K' M
// This is a simple schedule, with only one action that is9 X$ d) P/ y' ~- U1 _2 d
// just repeated every time. See jmousetrap for more; K/ \4 Z3 }, b
// complicated schedules.3 u$ F" y* D% `5 A
5 [; b- E) X4 w
modelSchedule = new ScheduleImpl (getZone (), 1);
3 f9 r; K9 |# m$ R- {0 Q( k! O4 p modelSchedule.at$createAction (0, modelActions);& s' w T# n& S2 K+ ?/ z
0 r. @: c* y& a7 C) { return this;
$ X* E, Y/ n2 I) U5 P5 u! I } |