HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& n; Y# ~( e1 o4 l$ ]
) i0 \; o9 _& N: B3 T. M% _+ [3 r5 ?" _ public Object buildActions () {
7 u& B2 O% O' e+ d5 F- _" a. ? super.buildActions();& W+ I8 D( d4 y& K2 t/ O! j0 H
) a' }* f9 M5 g1 p/ [; P! o8 z // Create the list of simulation actions. We put these in2 P6 B$ q8 a' \ ^ p! j( Z) g5 Y
// an action group, because we want these actions to be# L8 x8 U$ K% I9 X: X, Y" E
// executed in a specific order, but these steps should
5 g$ V/ y! ?8 ~" a! p // take no (simulated) time. The M(foo) means "The message
. `! s7 \, p7 x* C- ~5 {* W& C // called <foo>". You can send a message To a particular
3 T7 {" q5 l9 j // object, or ForEach object in a collection.) b. K9 R/ V% L9 P. V
* d$ |# h3 @- P$ p* Z( P2 m
// Note we update the heatspace in two phases: first run4 }5 P; e+ C& M/ ~1 o
// diffusion, then run "updateWorld" to actually enact the
5 ^+ r- g: C7 `. e. u( T // changes the heatbugs have made. The ordering here is
+ A o8 ^3 W% {# `4 W // significant!
; I- d: w0 H/ `4 |
5 h& X" Z9 T1 H1 a# w // Note also, that with the additional
( f& @+ D7 ]# P1 f' I // `randomizeHeatbugUpdateOrder' Boolean flag we can. _+ L8 S# o1 o+ S
// randomize the order in which the bugs actually run
1 m2 X; D1 w& o1 w // their step rule. This has the effect of removing any- k5 j6 H, a- N$ C4 C7 j
// systematic bias in the iteration throught the heatbug1 A" H; R; k$ ~& F8 X
// list from timestep to timestep
% l/ C6 C% c5 i4 L
6 X" E! F4 T( X2 `3 [. X // By default, all `createActionForEach' modelActions have
9 j, Y) w$ q6 b3 e+ N# Z // a default order of `Sequential', which means that the
' J- I; g+ Z( A. V$ o$ R& g7 _9 d // order of iteration through the `heatbugList' will be
# r) {2 K! } k0 D# F: L // identical (assuming the list order is not changed
% U7 G a ?7 I: m2 R. _ // indirectly by some other process).
/ y4 a' p: s2 q( d
4 x9 E- v2 }8 T$ a+ `7 L; ^% s modelActions = new ActionGroupImpl (getZone ());
" {: @* |" w( V6 D C! i7 P. s% q6 T {$ D/ y# E
try {: B2 `( p4 [- e0 a+ i V
modelActions.createActionTo$message0 P' Z2 }$ J$ J L& U
(heat, new Selector (heat.getClass (), "stepRule", false));
* b j6 t5 M2 E! a* E( m } catch (Exception e) {
! k& T: d, `9 a, d/ v System.err.println ("Exception stepRule: " + e.getMessage ());
+ {6 o' `6 I7 i } K- w1 Z$ J1 K
7 C1 b! I8 y5 A) C$ `2 M3 f try {
5 P9 P2 A- C, U Heatbug proto = (Heatbug) heatbugList.get (0);6 G1 J* { a; `
Selector sel =
t$ P [ m$ f* A/ G new Selector (proto.getClass (), "heatbugStep", false);
" T0 T6 V2 ] X [& o& t% H' S actionForEach =
. q1 x9 e( Y1 X, f- N modelActions.createFActionForEachHomogeneous$call
) ^* V3 ^* k& A1 l7 S$ [$ J (heatbugList, F7 Z. L9 F" e1 q9 \! v# y
new FCallImpl (this, proto, sel,1 x; g6 A! ]% K/ m6 a
new FArgumentsImpl (this, sel)));
( b. `& R9 d/ O9 v; Y$ X } catch (Exception e) {% F( k7 k; m3 l" R2 H
e.printStackTrace (System.err);2 Y6 S& e6 R( P5 b9 D! [
}! E3 \4 D0 B, ?
; `% X% V) f2 R! U& I; g
syncUpdateOrder ();
* Q$ u* o s, ?9 ]9 T; ]* x" r. r) Y3 ~+ H/ l5 W7 A
try {# r/ R# C9 L0 A1 C8 O
modelActions.createActionTo$message ( r9 n4 Y* P& D! h s% U
(heat, new Selector (heat.getClass (), "updateLattice", false));8 T! r- D' y2 O( Z) O$ [, K
} catch (Exception e) {" z4 p8 |) }9 T( s7 B
System.err.println("Exception updateLattice: " + e.getMessage ());
7 t! T h& F: V4 z$ a$ _) X( G }
, W/ J, u. D' O& { 7 K1 \9 K' l+ o0 Z: x6 y
// Then we create a schedule that executes the+ c |# p0 Q9 m d O
// modelActions. modelActions is an ActionGroup, by itself it
; M t7 E1 a- q: k- A" n" F // has no notion of time. In order to have it executed in
0 l/ Q! ~- z* n8 z+ \& _ // time, we create a Schedule that says to use the% z, v5 L2 y' M, O# _0 y% B
// modelActions ActionGroup at particular times. This1 u) ]: f9 P: e2 K/ F1 O
// schedule has a repeat interval of 1, it will loop every
1 Z: f: s9 e4 W2 L* }6 G9 R // time step. The action is executed at time 0 relative to
+ L0 L' v/ v [2 f- S2 n# [7 G // the beginning of the loop.2 O( f3 |- z8 U9 s6 z. |& G
6 v- p- D [( S' } // This is a simple schedule, with only one action that is1 a$ H4 M. M7 P! k9 g7 |7 Q7 J
// just repeated every time. See jmousetrap for more: o. x' q4 g2 h% o3 {7 a7 h5 ~% G
// complicated schedules. ]# G0 }: ]$ _+ H; ]2 E r0 z
3 ~; [5 D( t" E& p# j: Z modelSchedule = new ScheduleImpl (getZone (), 1);) Z# N9 ?" d6 k0 X" I
modelSchedule.at$createAction (0, modelActions);
' M3 D4 E1 X/ ~/ k' M+ n A, k5 a; u
# U+ E g7 h8 }! j6 D return this;
; k: q1 T, @" z; Z. [ } |