HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& J" j0 ^1 @+ s. j3 I" v) U$ {+ k3 b# I' b; m" i
public Object buildActions () {
+ v# e% i: r9 x' N super.buildActions();
' j, @$ R0 i5 x, `7 ? 5 N! y; n9 _) L2 I7 t
// Create the list of simulation actions. We put these in; c5 t: Z. p. T" }; s; @& |
// an action group, because we want these actions to be4 f0 ]* P% k F* w+ {* z- U
// executed in a specific order, but these steps should* M' c7 f e) Q4 Y& W0 i0 V
// take no (simulated) time. The M(foo) means "The message
1 Z }8 ]) X1 C1 o // called <foo>". You can send a message To a particular
) N& o9 o' [( R+ J0 X" z- C // object, or ForEach object in a collection.8 N" t1 J9 a# S* [# l
0 }- y: H S& h' C+ M: a# g0 t1 O
// Note we update the heatspace in two phases: first run
3 r e E9 _+ @( l; V+ R // diffusion, then run "updateWorld" to actually enact the
, I5 h2 _4 v; d* }7 B // changes the heatbugs have made. The ordering here is: I8 t- _( b: d U" {) a5 b4 Z
// significant!
$ k! v5 x! f: \6 A( R. O7 K
! b2 S4 ^" r+ e; b // Note also, that with the additional
) k3 s! I b2 F% g' @ L7 E! C // `randomizeHeatbugUpdateOrder' Boolean flag we can
9 n9 o! e) l- C2 \0 U // randomize the order in which the bugs actually run
t! C J$ z. a' E* t' {% d8 \. [% k [, a // their step rule. This has the effect of removing any9 w7 M/ S* u2 m% |3 y
// systematic bias in the iteration throught the heatbug
, F+ q: i* b0 d. B1 x // list from timestep to timestep
9 M& R5 f% K' i
3 n4 \2 u: E0 S# c% x // By default, all `createActionForEach' modelActions have8 x' y( A; C% ]# U: m
// a default order of `Sequential', which means that the
6 K6 U! q6 [$ y# M6 _$ e // order of iteration through the `heatbugList' will be8 y% U k0 B" @4 X% k
// identical (assuming the list order is not changed) S7 b- T8 l9 h" }& W
// indirectly by some other process).4 {2 \) [7 E' `' S* u! j
' O7 a9 l" u+ a
modelActions = new ActionGroupImpl (getZone ());
7 w3 t& l# E( t+ O9 L1 m k% w6 k' A
# k" G+ n+ Z7 A5 S" d. L+ J7 \ try {" q5 B: B; V+ t; f# \+ @4 R
modelActions.createActionTo$message
+ z4 N; Z0 h$ q4 W (heat, new Selector (heat.getClass (), "stepRule", false));
. {- w1 N2 I5 z s } catch (Exception e) {
) f2 [; }; i& ^5 E: c- C System.err.println ("Exception stepRule: " + e.getMessage ());
( ]0 j' u& X7 F" R }
$ T& `* \! q# f( N) ^3 ]
4 m9 M9 U( S6 R1 e try {
, ]8 w0 _; T6 w O% m Heatbug proto = (Heatbug) heatbugList.get (0);( Y, w% ]9 r9 g& i
Selector sel =
% G, f/ `/ Y# C6 Y8 o new Selector (proto.getClass (), "heatbugStep", false);, ^/ q2 g/ J9 c" l5 s# x( W
actionForEach =
( I% @2 G: f" a modelActions.createFActionForEachHomogeneous$call
5 d% x" b2 F. O: I: k (heatbugList,
6 ~. A+ X) x4 N8 T new FCallImpl (this, proto, sel,
4 K- D; p" x8 \+ P9 _: d8 ~ new FArgumentsImpl (this, sel)));/ @# g# u0 n% P/ Z
} catch (Exception e) {
( i' g/ f/ _" Q/ P: ? e.printStackTrace (System.err);
) ~5 W8 U4 W1 e6 w$ V }
1 Z8 c5 q* x( k 9 Z, f, @ U" B
syncUpdateOrder ();# b6 b7 m2 F: [1 z6 L
" p& J# c$ V( y4 J% k, m8 B9 Y: z
try {
9 e3 U$ n: m: z. M3 P( M modelActions.createActionTo$message
1 _6 ?/ q8 ^( E) M9 n9 ~4 U (heat, new Selector (heat.getClass (), "updateLattice", false));
: N* h+ { u. |6 G } catch (Exception e) {+ `2 f0 K7 a2 p% R
System.err.println("Exception updateLattice: " + e.getMessage ());( n9 q2 c: }3 D/ I
}) b1 }/ u( H1 ]- Q+ f
! l' Z x; Y; ^3 _+ P) m // Then we create a schedule that executes the0 Z" ?2 }8 X) Q, k
// modelActions. modelActions is an ActionGroup, by itself it
- k. p6 ^% d+ f' N, o // has no notion of time. In order to have it executed in
; q9 A k9 S/ P // time, we create a Schedule that says to use the3 I' b* o h% i9 x: |
// modelActions ActionGroup at particular times. This
. \$ i+ H$ t8 t/ I/ D( j2 g // schedule has a repeat interval of 1, it will loop every
6 s9 |2 i9 _, `( n3 ` }* \ // time step. The action is executed at time 0 relative to
; a# j. \& X; q/ f7 L* W0 C4 X, } // the beginning of the loop.' {: i5 T6 z7 x) d9 ^
! p4 G! _6 R1 E- L9 a // This is a simple schedule, with only one action that is
/ o0 `% _9 Y) p0 U# W // just repeated every time. See jmousetrap for more
/ z: Y: M4 t( | // complicated schedules.5 L! I+ j' x G. |. g; m4 @% W
% D4 ]! h9 S7 P6 d Z5 b+ x
modelSchedule = new ScheduleImpl (getZone (), 1);5 I# m, S* v$ a& p8 ?- R
modelSchedule.at$createAction (0, modelActions);; B# V' G5 J" [0 Y' `, l/ P {# }: e$ {
4 ~8 I4 b% ` Z3 D0 Z return this;9 M5 z6 Q3 g) W6 X& N. R; K
} |