HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) E' G5 t! c* F& c2 q
3 W, ^6 g' H6 }. ^9 K1 s public Object buildActions () {
' d% q- K5 n7 D( @6 p super.buildActions();6 U, P% @ s) C# T
6 S. k9 l& u' M2 s7 H) P5 x! G, K
// Create the list of simulation actions. We put these in
0 d% Z; p& H3 ~' c // an action group, because we want these actions to be% }" m' r9 ^' q! W; c
// executed in a specific order, but these steps should* J+ \8 w |: f9 d
// take no (simulated) time. The M(foo) means "The message& Q j' Z8 @0 n9 f
// called <foo>". You can send a message To a particular
% p% s$ _ d o! |2 Q: a5 J/ P // object, or ForEach object in a collection.
g9 K/ f" @+ q4 Y6 V$ c 8 F* O `. W, a4 Y! g, \6 b& a& W
// Note we update the heatspace in two phases: first run
I" I. M5 ^) g // diffusion, then run "updateWorld" to actually enact the
; ?) P9 i2 x' I3 G* s. n3 a // changes the heatbugs have made. The ordering here is' l& Y9 u7 {4 J, R
// significant!
( ~3 \# q' {. O+ M7 Q4 d 3 h4 Q0 [1 S+ [$ k) |) R
// Note also, that with the additional' H# O# {! Q$ y% b: a4 t5 O1 O9 @
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 H" T9 I) f! l; V' b
// randomize the order in which the bugs actually run7 r8 P7 T5 A# N3 M5 |
// their step rule. This has the effect of removing any$ S" }* E& P1 e. S9 i
// systematic bias in the iteration throught the heatbug7 C0 B- m6 q$ ], \. Y+ X$ \6 y$ }
// list from timestep to timestep
. Z0 u3 n3 l3 g8 Q9 Y0 T: o Z 7 q: V4 O5 ~! a' C7 N+ Z
// By default, all `createActionForEach' modelActions have
8 z8 C2 y$ m; ^& j4 r8 ~# e" R // a default order of `Sequential', which means that the' c. J; X+ G' m1 q
// order of iteration through the `heatbugList' will be( ?9 d' H) g9 S6 t
// identical (assuming the list order is not changed
, H( B X: K! [7 }3 P, d! _ // indirectly by some other process).
" b+ p- ~ F/ N3 R
{# }7 V9 D1 P( }" j7 c6 j modelActions = new ActionGroupImpl (getZone ());
' H3 q' r* j# {/ F' r E! @+ d
( R/ L4 P( z* r1 |. u try {" z6 N: _2 N7 w3 d' K4 q0 j
modelActions.createActionTo$message
9 N3 { o9 i9 V! p @. Z5 Z x (heat, new Selector (heat.getClass (), "stepRule", false));
0 r N& [ [( `0 k } catch (Exception e) {
& M/ L! u) \9 |3 I1 n System.err.println ("Exception stepRule: " + e.getMessage ());8 V* L, d/ D- W6 j% z3 l. ^
}# _+ X# P2 b0 |+ D& v0 v! L7 }( M
+ V& W" }/ K0 c( D0 M
try {# s+ I& t, m: ~0 M4 O& Q
Heatbug proto = (Heatbug) heatbugList.get (0);; Z3 { \& w: }' P a0 _/ ^
Selector sel = 4 K3 y* n6 w0 d! ^$ o! E5 }# {
new Selector (proto.getClass (), "heatbugStep", false);
. k s: r' \! P. M3 q5 S actionForEach =
4 z$ u/ q, A& b( L% t p6 @; `$ y modelActions.createFActionForEachHomogeneous$call6 J1 K2 C2 S4 ~' W+ j u
(heatbugList,
. W: C, n+ U5 [ new FCallImpl (this, proto, sel,1 l' [, `! Z2 p( t6 o5 S4 b
new FArgumentsImpl (this, sel)));) r% p6 E8 g. v
} catch (Exception e) {
/ ~) I# d5 Z: {' v e.printStackTrace (System.err);) y- B1 @1 V" w
}
' I- x4 Q t. f* E' s: P8 e : A# Y s( ^# c- s+ B! b
syncUpdateOrder ();# u5 q# q- q8 G u
R" D6 C! Y7 k4 Q1 C6 ^) I
try {5 K1 ^, o9 ?& ^% f1 y5 O; ^7 {0 g, z
modelActions.createActionTo$message
. F3 v6 v9 X9 G% C) j0 L3 { (heat, new Selector (heat.getClass (), "updateLattice", false));
) n5 [" m+ b5 K } catch (Exception e) {
( `- v# D! e: Z( ^! A# t W System.err.println("Exception updateLattice: " + e.getMessage ());, ]3 d3 T1 Y% `4 S! h5 s! f. v
}, ^0 _# v# e3 f; B o
& N* S" t0 [) } z) y7 p: t // Then we create a schedule that executes the
4 g% S. N, m3 H X3 e // modelActions. modelActions is an ActionGroup, by itself it- y7 N( {: o! \8 k! [4 E
// has no notion of time. In order to have it executed in
6 D% y5 i9 f6 T; N% L( N // time, we create a Schedule that says to use the
% D2 |. b* h f: k // modelActions ActionGroup at particular times. This
- ?, O+ d! U0 O& V: \: u( p, Y/ Z // schedule has a repeat interval of 1, it will loop every
% { V! `9 p) ^2 P // time step. The action is executed at time 0 relative to% {6 A `( E3 M3 T) F2 `
// the beginning of the loop.
q0 k @* a" ]
8 f1 m5 G+ P5 l- @( `2 w3 L // This is a simple schedule, with only one action that is
/ _# r0 B7 @) L# I // just repeated every time. See jmousetrap for more
! J& t& r. W4 F% H2 X/ j! a // complicated schedules.+ `9 H7 |5 Z$ l2 V7 F* a" S
$ I4 b, W1 \) j$ u3 Z modelSchedule = new ScheduleImpl (getZone (), 1);
" Z0 [( T" H0 j" \% g. F. P0 l modelSchedule.at$createAction (0, modelActions);
! ]- e1 z6 I; w* Y# N+ D
+ j9 W) x, H3 S) X: ` return this;) }4 c7 ^; ^" P7 S6 p5 W
} |