HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. M3 C8 q. W# h, U1 c
& Z6 ^: `) k$ k/ m public Object buildActions () {
$ { v" n8 Q$ t. @ super.buildActions();- ]% a% u0 Z8 i0 E" N
1 ~ a" j; v D! A2 m$ I5 p // Create the list of simulation actions. We put these in# H: j5 b y) S+ v. h6 ^+ Y
// an action group, because we want these actions to be" F6 A1 g- X; p' p" z
// executed in a specific order, but these steps should& Z! }$ V6 q3 o" ]/ W3 W! m5 Y
// take no (simulated) time. The M(foo) means "The message& d+ j- T, u" @/ T3 W
// called <foo>". You can send a message To a particular
0 ?3 y# N5 E t' Z* A+ A1 y // object, or ForEach object in a collection.; P' i3 w) H5 W! ?0 V& Z$ C4 y7 z
/ @6 h$ H4 O" ?) H( N: F! H2 |5 i // Note we update the heatspace in two phases: first run
( B& `* u% e( p0 u! S, F- @0 M' H // diffusion, then run "updateWorld" to actually enact the
& C0 j. v/ L1 \$ i4 D. N, T // changes the heatbugs have made. The ordering here is
+ P2 I5 O1 h$ y% W7 ? // significant!
4 k: ]% J, J% O8 D7 `
3 a% _5 V7 l+ e# |$ y+ x* d // Note also, that with the additional
2 s/ g6 A/ S4 J, |$ V# o8 w // `randomizeHeatbugUpdateOrder' Boolean flag we can9 @9 g: h# S$ r( y
// randomize the order in which the bugs actually run
: d% d! J+ k: _% K! ~# J2 M* c // their step rule. This has the effect of removing any* G9 q/ [ P! n' E B+ F5 |
// systematic bias in the iteration throught the heatbug( _3 R2 c5 G) r0 r6 ^/ j3 _7 L
// list from timestep to timestep' S7 y' ]0 f0 u0 s6 a
% m7 R5 m' L5 w2 A4 D# i$ u7 }* s
// By default, all `createActionForEach' modelActions have8 s2 U( D' ~- r- h6 H
// a default order of `Sequential', which means that the6 E" R# `: a5 B3 O# I2 \
// order of iteration through the `heatbugList' will be$ z" R4 F) b$ \) O! S
// identical (assuming the list order is not changed
) s; \/ \5 S& O( N' u1 V // indirectly by some other process).; Y) R; J0 z" w- F0 L) b6 W
2 m$ S2 y9 H% |4 @. b! z8 X: P
modelActions = new ActionGroupImpl (getZone ());
8 K* l: B" o$ ~+ P0 f/ C" t1 H P5 o+ @0 s% X
try {
! a* a: b/ m' P0 T6 D0 g modelActions.createActionTo$message
& _- X, P2 E* O8 } (heat, new Selector (heat.getClass (), "stepRule", false));
C. F+ t4 w! @/ u- m* `% c } catch (Exception e) {
! B7 ]& q$ |8 n% @" N5 g& r, n$ v System.err.println ("Exception stepRule: " + e.getMessage ());
+ J6 D8 K9 L; N& S }
8 H1 x- A' Z" d8 y8 s9 t
6 j$ s; s* b" U; z* v try {' w6 a2 D) @$ ?4 p0 G
Heatbug proto = (Heatbug) heatbugList.get (0);
- k& G+ U7 Q8 A7 I; f# y Selector sel =
+ f" z6 B+ g) f. P% P& a; k& t& t new Selector (proto.getClass (), "heatbugStep", false);
9 K. v% s! e" | actionForEach =
+ p6 R7 o. z/ f4 U modelActions.createFActionForEachHomogeneous$call
2 F1 c; u3 R" A4 {% E0 ~# K (heatbugList,
( H6 k3 R' F; Z; O, N6 v% x, p+ D0 C, _ new FCallImpl (this, proto, sel,0 v- ]8 _, X+ S/ \
new FArgumentsImpl (this, sel)));& p3 ]0 ?0 k& e) ~: N. G( X% I
} catch (Exception e) {$ B; ]$ d; a& z N& [ R
e.printStackTrace (System.err);
! N9 e" E9 I5 k B) h, @ }6 h- Z# R Y; f
7 B% n6 l+ {5 G3 W syncUpdateOrder ();
* {: a- c# Q. U0 d
# B. ?4 ]/ S2 L* T- {/ B8 r try {
2 s4 @( V# b2 o1 l2 Y modelActions.createActionTo$message 2 B: v% J$ b. M# ]8 V
(heat, new Selector (heat.getClass (), "updateLattice", false));7 r( ? d- P, f' v
} catch (Exception e) {
+ u# U& w% ~4 j3 Y System.err.println("Exception updateLattice: " + e.getMessage ());/ v. ^& r4 s6 N+ |
}
/ E% n' _7 d' e8 Y8 D4 c' _ ; F6 n ?0 D2 H5 i
// Then we create a schedule that executes the
# _2 U5 J: Y( z( @) K3 W // modelActions. modelActions is an ActionGroup, by itself it
& C ]2 c* n0 H- Y# R // has no notion of time. In order to have it executed in' M+ x2 k; G( r4 b4 j- Y
// time, we create a Schedule that says to use the
! C6 ~. s6 m; L9 F9 r) U, D // modelActions ActionGroup at particular times. This
6 ^# @3 F, i: `" b- B8 G3 m // schedule has a repeat interval of 1, it will loop every; ^$ y% h% A4 b; a$ U4 W
// time step. The action is executed at time 0 relative to
! X" l/ c6 ?# t1 O2 G // the beginning of the loop.) `! J' }0 V* l0 Y7 J
3 A& V- M: J1 f( P8 }% V/ F2 l // This is a simple schedule, with only one action that is ]# {9 Y$ g- \# _/ D
// just repeated every time. See jmousetrap for more
6 g7 s# `) B% j- ~ // complicated schedules.
2 _5 i z7 v/ q" [, e [9 m
9 L. x ]* I1 z' J! C J d modelSchedule = new ScheduleImpl (getZone (), 1);
* m5 [9 ^, l+ C: u4 _2 l* L w1 _ modelSchedule.at$createAction (0, modelActions);( s: [: `6 r l, k
2 J( u1 ~6 k4 ?( k' y; {# r return this;
+ [/ H& F* p/ [: I; W2 e, D, d } |