HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. l, [+ k+ f1 H4 {. j
! [8 j8 H( @+ [0 Z6 _9 \0 Q# z
public Object buildActions () {
+ r a( Q1 H1 n$ S" R super.buildActions();
6 k& v9 C8 v+ M ; T: e% u: ^/ H/ C/ _& j
// Create the list of simulation actions. We put these in
. K+ P' e P6 O6 [ g! W // an action group, because we want these actions to be8 H- k* v; Y8 e* |9 V' ? m. i
// executed in a specific order, but these steps should
: P& w/ s) i( ~& R5 B // take no (simulated) time. The M(foo) means "The message
( }% f( y) B3 Y Z: ?4 u2 l // called <foo>". You can send a message To a particular
5 g' [* D# S6 _! r$ B( z // object, or ForEach object in a collection.8 |0 ^+ r2 [9 a$ L) l/ q) ?
1 U7 V5 p* }0 }. b% y/ u% J // Note we update the heatspace in two phases: first run/ p7 r: M7 t. D% f# N8 U8 t
// diffusion, then run "updateWorld" to actually enact the
3 Z( {# z$ U1 q4 }7 r& O2 M# h // changes the heatbugs have made. The ordering here is5 P2 i' s8 B" |3 f. @* i5 `6 M
// significant!
7 u, x6 W1 a% q2 \7 P5 p
8 o' p/ K7 x5 ?# J" l8 S // Note also, that with the additional
3 Z9 O4 | p* G/ j: C1 Q. J! L$ j- D* i // `randomizeHeatbugUpdateOrder' Boolean flag we can) V9 ~; s- U6 L
// randomize the order in which the bugs actually run
% m" J2 {, q0 z' O% J // their step rule. This has the effect of removing any
" O( r) a# ?; }4 E6 P2 W; } // systematic bias in the iteration throught the heatbug; W( G4 b6 `/ c+ r- B6 R ^5 ?
// list from timestep to timestep1 a8 C+ P4 l2 I$ i8 O* S
4 B4 _; x/ y, ? m# O; a" H
// By default, all `createActionForEach' modelActions have" V, u/ O$ A" [$ J; @8 p+ k1 g
// a default order of `Sequential', which means that the6 ^- o9 `; a+ B0 O& M/ [
// order of iteration through the `heatbugList' will be
* f7 y9 _) E: L/ z // identical (assuming the list order is not changed `( C, P9 `( X+ r7 F
// indirectly by some other process).
& l1 w- a) m8 l- j3 F
" W- @: M' z" G3 O# W { modelActions = new ActionGroupImpl (getZone ());
7 t. j0 _; c$ B; @1 ]) F
, Y: {( u. t7 |6 a$ r: w try {
3 q' R6 J7 `6 \ modelActions.createActionTo$message, T7 C+ d. }2 p* B- b m8 ]
(heat, new Selector (heat.getClass (), "stepRule", false));( u) z0 n" I0 U9 s U5 S) A
} catch (Exception e) {
1 K& F9 Z0 e5 z% R System.err.println ("Exception stepRule: " + e.getMessage ());
; v2 {9 F! J! |0 ?# s }/ R- y, g$ c' E a+ t0 v
- w' b1 c2 s8 x" X try {, Y( q/ ~* r5 G% o$ `
Heatbug proto = (Heatbug) heatbugList.get (0);" N! M4 @, M/ b9 }, u" l5 s
Selector sel = 4 L: j# ]; C, I( }
new Selector (proto.getClass (), "heatbugStep", false);; R {8 M$ }* M% W
actionForEach =
/ t* K% c! H* z modelActions.createFActionForEachHomogeneous$call6 ~; E9 @! {. G1 t7 P7 \
(heatbugList,
) O/ e" |% p3 H1 k3 Y/ @4 D new FCallImpl (this, proto, sel,) T" F: e5 H9 I1 p- v
new FArgumentsImpl (this, sel))); X9 ~( l5 X) Z9 |7 H. D4 K/ R
} catch (Exception e) {; p0 _7 D6 W3 U, z4 s7 E
e.printStackTrace (System.err);* W( r: Y; {- L1 n" T
}
. i1 M6 g( g5 m1 [6 k. S
* s9 B) O& s G3 b) o2 ~ syncUpdateOrder ();
6 |# o0 l" O" g3 S
$ v( V) r; r1 a2 Q' x try {; C0 ~8 G7 ?0 N* e, r: c* g* e6 W
modelActions.createActionTo$message * v$ g' O& A3 m7 R( T5 e, X
(heat, new Selector (heat.getClass (), "updateLattice", false));
" [& t: S- U+ O# I5 E* ~0 y5 |" r } catch (Exception e) {% Y; w8 S5 ^. v$ {; h) ~, P5 l) t2 Q
System.err.println("Exception updateLattice: " + e.getMessage ());, h/ v4 P7 l5 h
}
7 N( W6 t2 B p + D1 q. N, F& p6 f4 ]. p9 T' Y+ F
// Then we create a schedule that executes the
& _+ R! @3 ?" m0 `: [2 }, Z! D // modelActions. modelActions is an ActionGroup, by itself it
, g5 S6 D, I0 @( h3 ` // has no notion of time. In order to have it executed in2 {& Z& q4 T- b4 e0 }) ]
// time, we create a Schedule that says to use the2 {' \1 D3 U* f' M! K& s5 V! B: G
// modelActions ActionGroup at particular times. This. @3 b+ w( P6 v* C8 W# B
// schedule has a repeat interval of 1, it will loop every
# f3 |6 F8 H, M( o+ v8 D# J/ x1 z // time step. The action is executed at time 0 relative to
, ]/ o6 Q+ Q% I$ M! }4 X, f // the beginning of the loop.5 W. p4 @; c2 B; Q5 \7 b
: {/ C( Z" p0 N4 o7 C // This is a simple schedule, with only one action that is2 h) w& ?6 G8 d, Y! y
// just repeated every time. See jmousetrap for more
3 V2 [. H% Y7 a( ^3 S // complicated schedules., v, r" ~* m+ V
/ Q7 b5 v i" R% B: p! n1 M# t- K modelSchedule = new ScheduleImpl (getZone (), 1);
8 [$ x* i) b$ c2 u# k+ l( x9 [( N modelSchedule.at$createAction (0, modelActions);% C: J, X( y+ D) h3 O
3 l7 n7 n/ h2 U; t# T return this;
& v# r2 b" r) W" G$ j# F } |