HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 `4 X( ]% a, ~6 i
5 \ T4 {' f2 P2 g0 U1 H# m public Object buildActions () {
5 E- Q' x" o# S, [/ @ super.buildActions();4 j% r, p7 ]" o n' \
& h; a4 b- Q3 x# L1 v // Create the list of simulation actions. We put these in1 a* ~& R! g3 s2 Z6 R8 |
// an action group, because we want these actions to be
4 v2 s) e6 z: @* J \ // executed in a specific order, but these steps should& ^0 Y+ E2 d0 l4 ]) a$ N
// take no (simulated) time. The M(foo) means "The message
( s4 l8 C% x% [6 F3 q // called <foo>". You can send a message To a particular" z$ t$ J: A4 q% k: c
// object, or ForEach object in a collection.
6 |/ [6 J; \0 h0 L# D( o
8 N8 H7 \- n* ?4 g // Note we update the heatspace in two phases: first run0 ?* S4 z0 D- S
// diffusion, then run "updateWorld" to actually enact the( @4 H O5 h( d
// changes the heatbugs have made. The ordering here is
3 i( g/ Y$ j1 E+ }+ J+ B // significant!0 T; X" `; @' ~' R, \' i4 J
. g$ N2 B' |8 y" R
// Note also, that with the additional
) D0 c e* K% N9 s, x // `randomizeHeatbugUpdateOrder' Boolean flag we can6 Q" H, |( Q0 X
// randomize the order in which the bugs actually run$ O$ @$ S, F% K& P& G- n
// their step rule. This has the effect of removing any! v U7 @" Q0 N$ B i8 ?1 G" h w( ]
// systematic bias in the iteration throught the heatbug0 _6 y+ m1 V2 s4 S: c$ F9 \
// list from timestep to timestep
; B* c1 c) \6 m5 `* d ! H" @- Q) q/ C. o$ \
// By default, all `createActionForEach' modelActions have
o& S) B& t& E' n) ~. i // a default order of `Sequential', which means that the
; y/ _4 D$ x; u // order of iteration through the `heatbugList' will be
; \+ ^$ f9 d! [" ^, y2 J1 G // identical (assuming the list order is not changed; H% `6 j% l& u7 x" S
// indirectly by some other process).
& ?% h' R6 O) M9 R3 ~ 0 I6 L7 D+ K& \) L9 n/ ~
modelActions = new ActionGroupImpl (getZone ());
8 r( f/ Z, C* F5 A6 |" Z9 \3 Z, K, _6 ]/ n" j! B! ~
try {
5 w/ h1 c# ]) e/ q modelActions.createActionTo$message
2 U* P4 h# Q- s. T5 Z$ ~ (heat, new Selector (heat.getClass (), "stepRule", false));& g7 v+ l6 I$ c* k5 B4 U9 |# _2 F
} catch (Exception e) {% `1 R, q8 y% C1 F4 X3 @
System.err.println ("Exception stepRule: " + e.getMessage ());* @+ g$ }$ E7 x# x, Z$ C7 F
}% h. B% g, Q. n1 G! B, S
6 |( D3 J5 v3 J' [4 Z
try {
+ u$ y3 i& H7 U; d# N8 [2 V& F1 s, V Heatbug proto = (Heatbug) heatbugList.get (0);$ y0 b" \+ Q+ f6 d
Selector sel =
1 Y" {, D' }) X* b# U2 G6 T new Selector (proto.getClass (), "heatbugStep", false);
4 E* M# O4 s; {, }! M actionForEach =
5 \! F5 `- J) J0 L J modelActions.createFActionForEachHomogeneous$call1 a( Y" ^7 A7 C! k: R0 a- o2 S
(heatbugList,
" ~) D% J$ J/ t, E& G new FCallImpl (this, proto, sel,# ^2 v; j$ Q0 }; h% B
new FArgumentsImpl (this, sel)));: b; |- J2 ~% |$ ?" y
} catch (Exception e) {
( Z7 h1 p. f d% y0 W e.printStackTrace (System.err);
( z1 j( S. G" Z: x t6 i9 } } D; _! h4 b+ a# z0 v: w! v0 k
; K. |% i! \2 {8 P, l( w
syncUpdateOrder ();( M& C/ [3 J3 I" |, C- V. G
( @ f( C t, D) g# t7 \
try {
$ G. }+ O9 o5 S+ o3 j modelActions.createActionTo$message * i( V8 t5 c/ F/ c
(heat, new Selector (heat.getClass (), "updateLattice", false));
" n0 ~, m3 C; k- |5 z# U+ v4 V } catch (Exception e) {
4 `4 L# u- I+ \) P, d8 x2 X/ a System.err.println("Exception updateLattice: " + e.getMessage ());
B$ T% ?9 A! r' G& p }
0 \$ f9 y: [% N. l8 w
`4 n# b& C! x B3 F+ l // Then we create a schedule that executes the
4 ]8 |* h; }/ {6 a% W: @3 H // modelActions. modelActions is an ActionGroup, by itself it
; Z- C! l& T# V9 D$ p( x // has no notion of time. In order to have it executed in
. B; l2 N( B- q$ Z7 n5 E t$ e // time, we create a Schedule that says to use the3 \+ E# I+ b, J( V
// modelActions ActionGroup at particular times. This
5 q9 N. q7 ]0 ~+ ~9 g // schedule has a repeat interval of 1, it will loop every: _1 ^5 J0 U' g2 F+ r
// time step. The action is executed at time 0 relative to
* j0 i; `) {+ O9 T% W // the beginning of the loop.7 }4 z0 ^; b ]2 h+ ?
; z E: P5 z& L' S
// This is a simple schedule, with only one action that is
6 E+ K- `" e* R6 A // just repeated every time. See jmousetrap for more+ @. m+ e# } m- O: w8 i
// complicated schedules.% x: V% ]( f6 S9 G
% Y; N, C8 q* G, R9 @2 o3 B+ Q modelSchedule = new ScheduleImpl (getZone (), 1);- h) m/ a( M$ X3 V
modelSchedule.at$createAction (0, modelActions);3 i0 \, J- A- W" U1 H
5 e, A+ s2 b- ]0 l+ i) P/ M8 j& ~" T return this;
I, L$ J: P/ t& w& _7 D1 b } |