HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% t6 Z, E( l4 q! z( B: y* L$ ]
6 `' T/ W: e9 c' |+ O" M8 G public Object buildActions () {
( z4 @" ~& R/ n super.buildActions();
" Z7 K, w% E) v9 j; k
/ Q: M+ ~+ _- [+ U+ C: Q // Create the list of simulation actions. We put these in, x6 }: L) l+ G. O: b
// an action group, because we want these actions to be& z+ }1 J& e8 K2 F$ ^# J
// executed in a specific order, but these steps should q6 F) U) z$ c0 o/ X8 R' q7 l, x
// take no (simulated) time. The M(foo) means "The message
- A% i' @* t* b1 _, U! A- `6 j // called <foo>". You can send a message To a particular
7 x( `+ |) Q) R/ S! z // object, or ForEach object in a collection.
0 y" F Q% W" S1 S& e/ E
5 F1 Z8 R+ M. k5 W( \ // Note we update the heatspace in two phases: first run
) d# j [( I$ g$ v P! s // diffusion, then run "updateWorld" to actually enact the* G- h2 _4 x. h: {! \. s# {9 R
// changes the heatbugs have made. The ordering here is
" c9 z" `3 V# f4 D& h // significant!8 R- U" s4 k& j7 t& \& x/ I
' a4 o! ~/ B4 k3 ?% O // Note also, that with the additional6 [6 Y/ @: e- X7 V% f$ M% x5 y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 l( C2 v. t: r1 p2 J( c0 x' d // randomize the order in which the bugs actually run
8 G% l0 a7 f. X8 b2 g2 J+ L5 z // their step rule. This has the effect of removing any( U8 R0 E9 y' [/ R1 R/ k: n* ~
// systematic bias in the iteration throught the heatbug
4 r* _2 T: M3 R+ n- }2 f // list from timestep to timestep
8 M* D$ f& K0 U7 C- q
) x1 f# L& _" O // By default, all `createActionForEach' modelActions have
* E' K& u, `7 f // a default order of `Sequential', which means that the, l+ e @: y* {: o
// order of iteration through the `heatbugList' will be
* E- V5 l& b! r // identical (assuming the list order is not changed
" M' g" p) o' w4 U* G) k // indirectly by some other process)./ T8 y$ V1 P9 U2 |- ^
2 f! V, X( P) D; `
modelActions = new ActionGroupImpl (getZone ());
& W4 P$ \0 i; ^3 g( G& A+ R2 a' ~6 J6 d, S
try {
! O2 o3 \+ A/ d$ A. o" J7 v# L modelActions.createActionTo$message+ ^6 h2 ~! F6 G) y/ h$ V5 a$ m* M$ `5 q
(heat, new Selector (heat.getClass (), "stepRule", false));
( }7 M8 K; ?( C# e } catch (Exception e) {3 b- d- [! g1 u* | `) B
System.err.println ("Exception stepRule: " + e.getMessage ());: d! ]# K; U) V' U+ W& ~8 |/ }# H$ B& ~
}( ?" G8 ]# w3 Z/ a5 z @: P p7 u) L. ~
& V5 w6 _/ t5 y try {
0 y, ~. p2 i$ u7 D: Y6 c ^$ ?' f Heatbug proto = (Heatbug) heatbugList.get (0);
; c" B& J; R7 H! P8 O Selector sel =
: y( ]4 k. ]+ ~) O/ o' C new Selector (proto.getClass (), "heatbugStep", false);
! _1 h3 w5 [- ~% p/ W* W$ Q# l1 G8 k$ @ actionForEach =
; X' K; G' v5 k0 y: b modelActions.createFActionForEachHomogeneous$call0 a7 d# u7 t% E) U; E K
(heatbugList,
5 R% B. i9 K3 t) v new FCallImpl (this, proto, sel,. h3 O1 E5 M6 o. M/ n3 a; s% P
new FArgumentsImpl (this, sel)));. D, O4 T5 P* i$ |) T
} catch (Exception e) {
4 e8 R% p) F! g/ ?- M# M e.printStackTrace (System.err);
9 a$ O$ i& |$ {( _5 @, U }- Y2 b* R+ M* g/ t7 E! D
2 i+ {) }8 c# {
syncUpdateOrder ();
! O0 G) K; c8 t0 q+ J
* n; T* w7 H. M3 x( ~9 X. l try {- x' K# O) X1 X8 i
modelActions.createActionTo$message , n2 N; w% u# A4 r3 {, I
(heat, new Selector (heat.getClass (), "updateLattice", false));% s/ l: O8 e7 a5 _3 _4 n& g
} catch (Exception e) {
" _0 z! |9 G! k; j' \6 E8 C( n r System.err.println("Exception updateLattice: " + e.getMessage ());
. }+ O$ Y8 |; }3 B g }) w- n# X, D5 I9 P/ w0 a
) f2 F* g: }0 ?9 } // Then we create a schedule that executes the
# X" W# f G* P: R: B; x- R // modelActions. modelActions is an ActionGroup, by itself it$ K& B; @8 t; D
// has no notion of time. In order to have it executed in
# n4 C' E- o3 e6 i2 `( `$ Y // time, we create a Schedule that says to use the
3 l& O% ~6 l0 J+ J) K // modelActions ActionGroup at particular times. This
: X- o+ E# w' o3 W" y- g // schedule has a repeat interval of 1, it will loop every0 w0 z/ _" m5 N8 K
// time step. The action is executed at time 0 relative to
1 A; d' h2 M5 V7 R5 i8 L4 ~+ B // the beginning of the loop.
. G: Z+ D6 N: I
4 O4 d" J' r, J! M8 |: a- C // This is a simple schedule, with only one action that is- B' ?) a+ W1 r0 p
// just repeated every time. See jmousetrap for more
* I3 d# l, [* I) X4 F2 b; ] // complicated schedules.$ P' O- ]4 g4 l3 ^/ t5 y
2 R3 ^' |3 U4 U8 o modelSchedule = new ScheduleImpl (getZone (), 1);% _7 ~+ O K4 q! V- p3 _
modelSchedule.at$createAction (0, modelActions);
4 J; Q) A e0 ~ 4 z+ {8 c1 U* E9 U
return this;
2 c: Q+ [! `, R } |