HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( r+ C- Z' \# D) `! Y( J( ]9 m Z5 x
public Object buildActions () {
3 Z4 v7 r' |3 ~8 L4 [& D: S super.buildActions();
6 I; {( u y% L! |0 U3 B- \2 n 7 G" \1 r" @0 y0 P9 G0 f8 F5 V
// Create the list of simulation actions. We put these in
% t$ C. A, x* \, d" { // an action group, because we want these actions to be* n# J5 Y; }1 q# G, d
// executed in a specific order, but these steps should& k) S3 C7 ?& n
// take no (simulated) time. The M(foo) means "The message
: Q5 t/ u% D" Y" G // called <foo>". You can send a message To a particular8 ~+ m& T K; y0 b+ A- u
// object, or ForEach object in a collection.6 K. k2 e" T& i6 R a6 V
* g- }+ E0 w: g+ j2 c
// Note we update the heatspace in two phases: first run
1 T' C7 K6 R8 m // diffusion, then run "updateWorld" to actually enact the, w" e0 D @! d, C. N- E. m% v
// changes the heatbugs have made. The ordering here is* T2 q. U# c- u2 i8 K9 o
// significant!
0 J, \& @8 ]; p$ I$ A1 }: L
: R) K, i: f6 k2 I* ~, g // Note also, that with the additional
$ ?( H( S; h$ S // `randomizeHeatbugUpdateOrder' Boolean flag we can" O! c5 j) a. w% J" B
// randomize the order in which the bugs actually run# Q! l) S# _% T
// their step rule. This has the effect of removing any
# I5 i9 f+ l, Z! j' N$ X // systematic bias in the iteration throught the heatbug. V& g( f. P- O5 ~
// list from timestep to timestep. H" I* w+ v4 o0 E7 ], e
# |+ C. f/ V' R! K, n0 |2 g9 v
// By default, all `createActionForEach' modelActions have
5 B: }% H5 w$ V7 P& b" L# Q! L // a default order of `Sequential', which means that the' e7 g% ~& T8 {) G4 x4 }( t
// order of iteration through the `heatbugList' will be: z6 p" A9 p( A8 d( S7 C
// identical (assuming the list order is not changed
: |3 R, p6 V+ l // indirectly by some other process).2 `* P1 ?) O8 Q" j* v
9 V& W3 Z& S( o& G- Y5 d$ M6 a. G modelActions = new ActionGroupImpl (getZone ());
5 ^' x4 D1 g5 t9 ^8 a# }! ?$ k) q9 v0 T9 s$ }% `5 p
try {$ q6 w! k- U' u( x2 b8 c
modelActions.createActionTo$message
+ L, c6 M& z7 ` (heat, new Selector (heat.getClass (), "stepRule", false));2 L/ Z- E2 N- T8 Q3 X
} catch (Exception e) {
, i% k9 s6 N# U2 u+ q System.err.println ("Exception stepRule: " + e.getMessage ());
6 Y" ~& k3 B6 L6 B3 ]- t }
4 j$ D8 @: ]# z2 s: |1 f. u
( \) J8 S# J) L, ?. p+ ~& ^4 r try {0 _9 k4 t! i2 r- r: R+ v
Heatbug proto = (Heatbug) heatbugList.get (0);1 m* x, T4 Z# V8 X
Selector sel =
2 y) O$ V- j5 ^3 e+ [1 i! _3 \4 x7 y- I new Selector (proto.getClass (), "heatbugStep", false);
0 P; x! P& ^# p' Y actionForEach =
# H; S; O1 T: l$ Q modelActions.createFActionForEachHomogeneous$call
# v4 `6 K( \( L( H- F9 H0 [ (heatbugList,
) f& h. o* D3 n i5 K) B0 Z( O new FCallImpl (this, proto, sel,% o) _" K6 F v% A- `
new FArgumentsImpl (this, sel)));
7 }& n1 A5 p# @& i& |' f5 Y1 R } catch (Exception e) {, G* w% S7 `+ _
e.printStackTrace (System.err);
+ L9 j3 L) n% \! L0 u }
1 N$ g+ h9 y' n9 B $ o R- f- f. o& o
syncUpdateOrder ();! Y+ T" {4 F& a
. X- ?# f! m. a0 ]3 Q
try {
1 i! N. J3 S4 R5 X& j6 m5 N8 N modelActions.createActionTo$message ! _9 v% i9 \' w9 j! H3 O! D) I
(heat, new Selector (heat.getClass (), "updateLattice", false));- `% _4 a; m+ b7 I" E+ H7 I
} catch (Exception e) {
- e; M7 W1 E7 Q4 _5 v System.err.println("Exception updateLattice: " + e.getMessage ());
* r5 e; M3 n7 u' ]) e8 m }
4 L( K- W6 H ~
8 |3 O, V2 P* |+ s+ F" Y // Then we create a schedule that executes the
1 P0 H+ {* s; b0 U8 o // modelActions. modelActions is an ActionGroup, by itself it
" H- e/ L) Q o# V) `' _4 x3 g2 J7 v // has no notion of time. In order to have it executed in! c' g( ?' {1 Q5 @4 H' h9 o% e8 a
// time, we create a Schedule that says to use the8 o* {' @5 O; J
// modelActions ActionGroup at particular times. This
4 a, a% V: {) R // schedule has a repeat interval of 1, it will loop every; ?" z5 x4 V) L5 T, H+ Y
// time step. The action is executed at time 0 relative to- P/ r& a; r- L8 }' ?" ^
// the beginning of the loop.
8 E. b0 \' V. ~& I. O; N" C% Z7 |) u# ?, N; M! b
// This is a simple schedule, with only one action that is
* n' `' X r1 h- p3 N // just repeated every time. See jmousetrap for more
! I( K" k( ?: L9 l/ {- ~# H // complicated schedules.+ M' w3 H# D6 r. d
% Z7 Y6 L' V3 N: j C, l( O* L4 i modelSchedule = new ScheduleImpl (getZone (), 1);
+ s7 E( P/ C: Z X modelSchedule.at$createAction (0, modelActions);+ o2 N! y" p# F% O
' R9 u- W$ r3 f6 R" u9 E return this;
( E: n) q: f# e/ I7 q, n } |