HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& v! a1 V$ X+ V% @! V7 s" D8 Q0 I2 \9 y3 e' R5 G* j4 t
public Object buildActions () {
$ I$ G% U1 H4 U& f6 }6 [* c super.buildActions();. u6 J0 @" J3 J3 z! ]% I
Y2 s4 Y" l/ F, k0 ^
// Create the list of simulation actions. We put these in
/ j% y6 X1 B" c9 q: O; N0 K // an action group, because we want these actions to be+ }5 l2 B9 f1 j( I
// executed in a specific order, but these steps should# v/ K" W# [7 n6 F0 r6 y( |
// take no (simulated) time. The M(foo) means "The message0 r' p4 r. ^" N. ]* i; e! |6 s
// called <foo>". You can send a message To a particular2 x* O1 F( F9 q
// object, or ForEach object in a collection.3 r7 B" b: A1 x9 J: r+ o* u- k
& ^. A8 D- M. T" V // Note we update the heatspace in two phases: first run. `4 T: q7 ^: w# D4 F
// diffusion, then run "updateWorld" to actually enact the
$ k: T* F1 Z9 h // changes the heatbugs have made. The ordering here is
, ^, \9 n+ F- H' Y7 Z9 _ // significant!
0 o0 g9 `! [2 v , e2 b- U- B$ f$ r& S
// Note also, that with the additional/ M: X; S* U# H/ ^. v( |3 n/ t) N# X
// `randomizeHeatbugUpdateOrder' Boolean flag we can. `9 i) x" k" w, J$ D! Z5 c
// randomize the order in which the bugs actually run
" |3 ?) f5 B. ~8 Z+ p5 b5 S // their step rule. This has the effect of removing any
7 t5 y: m: Y8 Q // systematic bias in the iteration throught the heatbug
8 Q* p4 X/ y1 h; N' E // list from timestep to timestep
, }' b' d* |: R3 t2 E1 @8 G( R
* g% K* h/ u4 @ // By default, all `createActionForEach' modelActions have+ ?% k1 O! L g x; \, E
// a default order of `Sequential', which means that the
- R3 e+ S- W0 i" G5 p8 }% n/ c, r // order of iteration through the `heatbugList' will be
7 Z# i* y6 I# A. S // identical (assuming the list order is not changed
# l$ Q) ?- j+ [! G // indirectly by some other process).
i9 ~; V2 Q* R2 Y. k
4 D l: ^; h5 b2 ?3 g: X( A modelActions = new ActionGroupImpl (getZone ());
: p) f3 f6 O, v
0 L) H( A( A* q) G8 I% D try {! ~2 W. J- b- ]: d8 q5 d
modelActions.createActionTo$message
8 f7 z; r- Z2 s4 f (heat, new Selector (heat.getClass (), "stepRule", false));
# y' O$ ?; L1 ^ } catch (Exception e) {
6 C+ y, U8 K; K" O& e2 S System.err.println ("Exception stepRule: " + e.getMessage ());. R% _' q* a' F
}2 a1 h8 c" X& |% D0 o1 ?
4 R1 o U6 G. e% T" R% _! @ try {
3 T4 _) L( E/ E) ^ Heatbug proto = (Heatbug) heatbugList.get (0);% ~+ g9 p, r- W
Selector sel =
6 q; H* C. I% P: b; A new Selector (proto.getClass (), "heatbugStep", false);
6 w1 C+ u0 B8 u8 l/ P. n" {5 O actionForEach =* |& b" z" T- n; L
modelActions.createFActionForEachHomogeneous$call% k8 k5 j! n( \; t$ C, j
(heatbugList,
+ v0 c2 f$ ?. d; J new FCallImpl (this, proto, sel,* N) _8 x4 L. C* _
new FArgumentsImpl (this, sel)));
/ e! |, {3 j& n7 V4 M d } catch (Exception e) {
* W; R" ~3 y! c" _+ a* }1 ` e.printStackTrace (System.err);
; M% I4 d1 F5 C: L9 K: m }+ e. o# g, F( y* B2 h
4 |: N9 ~( h8 G8 i7 S2 k syncUpdateOrder ();
5 y" }1 [4 m/ @+ O8 {9 Y' B, |) k6 [2 T8 J, ~5 z
try {$ t# c# `" Y% C7 G" H. n
modelActions.createActionTo$message & u+ ]2 g! n2 X( u7 }7 [* F- n
(heat, new Selector (heat.getClass (), "updateLattice", false));
- l: X! N9 x4 D1 D$ b5 u } catch (Exception e) {
# m+ c8 g& w* }$ l3 C; U e. f8 N System.err.println("Exception updateLattice: " + e.getMessage ());3 k. `4 w v# v0 k5 E; I( i5 g
}, l! i0 N5 Y0 W, {% M6 W
6 h( w4 A$ D! ]9 D; Y- b // Then we create a schedule that executes the
% I. f6 c2 X+ }2 h* f2 V8 p- s" ] // modelActions. modelActions is an ActionGroup, by itself it
+ {' q1 ~1 r8 T' [$ z; N0 ^% f2 ? // has no notion of time. In order to have it executed in
) }- Z3 G" h5 H // time, we create a Schedule that says to use the
1 P9 w5 Y( A, z // modelActions ActionGroup at particular times. This5 {* o! U6 H3 C" e
// schedule has a repeat interval of 1, it will loop every ]" |6 r' @% ^3 y1 S+ h& g/ m
// time step. The action is executed at time 0 relative to
! @; l; {5 d; p9 O // the beginning of the loop.9 I2 j* ~, }4 U7 f$ z
& T' `- B( u3 Q' `
// This is a simple schedule, with only one action that is
5 L$ H' @7 d& m( R" }; ] // just repeated every time. See jmousetrap for more4 `+ l! w6 g# t" y( E/ u
// complicated schedules.
2 q- |5 X; A# K 5 a% n" N1 S7 `/ O- ^$ w1 s# [+ J
modelSchedule = new ScheduleImpl (getZone (), 1);
! T @" Z; ~' }9 |3 B% _ modelSchedule.at$createAction (0, modelActions);
, }# M' n/ T4 C) D
5 v( S' [4 L: C9 J4 v7 `. z _- k return this;
( T3 M# I8 V$ B3 N% M+ k$ d8 L } |