HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 y, E+ O4 [) C) s3 K/ B1 a" k6 [" J4 j
public Object buildActions () {
) z' @8 P1 x1 @% Z; ~/ o/ L2 C super.buildActions();( s- t& K! C5 u( Z
2 A- H, ? f. i5 o& l4 H // Create the list of simulation actions. We put these in
' b. ^7 B- D/ I" j& U$ u+ i // an action group, because we want these actions to be
7 o# r- k& {: R# b& d0 r // executed in a specific order, but these steps should4 o7 B s% [) S; y% N& [0 P
// take no (simulated) time. The M(foo) means "The message
4 G. C' j8 f( z/ C7 ? // called <foo>". You can send a message To a particular
' b8 t5 D8 V- A% g* e7 ?7 m // object, or ForEach object in a collection., L1 \+ w. G$ Z, O) p' [; L5 X
8 A0 d( r& j! g0 w2 l
// Note we update the heatspace in two phases: first run
! O# R: y! ~( P$ h7 H- B // diffusion, then run "updateWorld" to actually enact the
0 g) E, b- f+ k2 Q // changes the heatbugs have made. The ordering here is7 n& }& N+ q6 v* {7 j1 E
// significant!! c- c Y5 V) n* U
. c+ n5 `; u- H: o8 H% l0 [4 x- Q4 X9 O! W // Note also, that with the additional
}7 P1 \! F% h$ z // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 B5 Q1 [; _* S; Q0 D // randomize the order in which the bugs actually run6 W7 Z7 T0 @/ Q0 k
// their step rule. This has the effect of removing any
# \5 ?( Y w: [5 C/ m2 C // systematic bias in the iteration throught the heatbug
T2 H) O% \4 }* O* c9 N6 Q$ X // list from timestep to timestep
' ~$ N/ o$ ~8 k5 W' X! }
' ]) o0 h. W3 A) P. N( H# u // By default, all `createActionForEach' modelActions have
j' z: o, u" q& P& L // a default order of `Sequential', which means that the5 e; q* n, h5 a% v5 C5 i
// order of iteration through the `heatbugList' will be
1 E& h2 u2 v+ k( O" N O- X // identical (assuming the list order is not changed" j$ P! d2 a, F3 d9 z* X" o5 X
// indirectly by some other process).
v* e3 B/ M/ B2 O: k( R
5 Y% ~9 X' n7 X! r& B; } J9 M. y modelActions = new ActionGroupImpl (getZone ());- ~; |; R8 v; S4 k
! s- ~* h7 e( } o0 L2 C$ V try {2 ]' b3 {+ Y6 C2 w1 h- c% t
modelActions.createActionTo$message5 f9 L2 T, J! t( \* N& G' N1 S
(heat, new Selector (heat.getClass (), "stepRule", false));
% G4 n. k$ H6 A; @: E } catch (Exception e) {9 M7 h/ i X Z& o3 x7 K/ @+ l8 F
System.err.println ("Exception stepRule: " + e.getMessage ());7 |% @0 s4 y2 |( K
}; F$ B0 B! l5 ]% T, l
2 I/ q6 D' s- U7 { F0 [ try {2 f+ ~! z& [8 K2 B7 S# u
Heatbug proto = (Heatbug) heatbugList.get (0);1 R. h+ z* a4 k1 K! B- y: c$ k. o
Selector sel =
4 @: I2 \1 R: A7 k2 X9 l) F new Selector (proto.getClass (), "heatbugStep", false);
9 t. p2 G/ Q9 t actionForEach =
7 ]6 s6 @2 P( j8 A1 X5 O- n+ b modelActions.createFActionForEachHomogeneous$call
( z' `1 w# s7 I (heatbugList,
+ y6 c" O9 U( G7 j6 j) }, X new FCallImpl (this, proto, sel,
1 L7 `( F' F- o4 R+ a8 {1 h new FArgumentsImpl (this, sel)));
8 U* m0 w6 J( M/ r1 c. f } catch (Exception e) {7 C4 @+ P3 [! r0 a4 i4 o
e.printStackTrace (System.err);
9 T& c- O8 n5 v* i8 }9 g }
8 o% J, y2 f3 Y' N! ]/ K+ W 9 f9 J, A! P) A
syncUpdateOrder ();
/ n2 P( H! O5 e! i0 P1 T2 ~" b; w( O( t# B; A* ?: f
try {! I n& t6 o- J& Q o1 F
modelActions.createActionTo$message
- Z( c3 C% ^7 j$ x1 R! S (heat, new Selector (heat.getClass (), "updateLattice", false)); p4 w4 ]; z2 W0 R% ]
} catch (Exception e) {
% Z7 h$ g# Y# L System.err.println("Exception updateLattice: " + e.getMessage ());% y6 m3 t6 J6 O9 [" }
}5 n( w2 Y5 _. ?# @! k% N: Y
0 M0 P8 t! Y# z6 B // Then we create a schedule that executes the
1 a" K+ A# }8 l4 y // modelActions. modelActions is an ActionGroup, by itself it$ `- w7 _: o! m: f, c' T q" V @& E
// has no notion of time. In order to have it executed in
" l4 X) ~2 U) H/ G6 _1 i // time, we create a Schedule that says to use the9 S$ g, ~/ Y7 U s# y$ Q, u" j
// modelActions ActionGroup at particular times. This: { U9 T& z$ g/ y+ U
// schedule has a repeat interval of 1, it will loop every7 _& m6 F' Z ?( G1 C4 U7 D
// time step. The action is executed at time 0 relative to6 D8 c. g: }1 a2 W4 Q, f
// the beginning of the loop.
, u; r0 P+ @1 _
! Q8 o3 v6 \) @, |. F // This is a simple schedule, with only one action that is
M" N4 \* J% ]! T; l( e9 `: f // just repeated every time. See jmousetrap for more
) U$ t, s9 c) J, z // complicated schedules. X5 K) ~, S- N. C, S7 G) ?
- `0 f3 b/ P" t
modelSchedule = new ScheduleImpl (getZone (), 1);
, a1 J! _8 A2 Q0 s2 X modelSchedule.at$createAction (0, modelActions);- R2 {3 i% M4 Z5 N. A
4 Z1 X: O# ^; Z, ?8 \ return this;
1 G9 s6 Q* f. Z& R6 l* I% w8 R } |