HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 b& p% C) r/ ^% w L3 I2 T( t0 p0 l, p+ m; }% \' l/ l- p* g
public Object buildActions () {
- }8 a$ t1 M, a! d6 I! l7 q2 | super.buildActions();
, B8 l) v+ J! W
3 I& ^$ O" A* r/ a% w, S3 c // Create the list of simulation actions. We put these in
O& j- {, p/ a0 Z3 R0 V4 O // an action group, because we want these actions to be
2 c0 }3 t& p2 G // executed in a specific order, but these steps should0 h- _& H9 U8 L/ A% R5 f6 H
// take no (simulated) time. The M(foo) means "The message- O) D8 o- L; A! C, U
// called <foo>". You can send a message To a particular
9 F( V8 ^; ?; A# l7 O // object, or ForEach object in a collection.
0 n; k$ v/ C! R* n: f' m) {4 J1 u
$ k) U" L9 @/ {( x' L. Q! t( V // Note we update the heatspace in two phases: first run
) R& @4 B8 G! [& P6 g // diffusion, then run "updateWorld" to actually enact the
) J5 W/ H0 @4 H; v x/ ?& R4 ` // changes the heatbugs have made. The ordering here is
w* f- c' ^& `2 E7 j, c8 a // significant!
. ~- `2 }) j2 c" v% D; y- V: G0 P # X S. v' u% f4 ?
// Note also, that with the additional
- P9 \) w& T8 V7 f& s! | // `randomizeHeatbugUpdateOrder' Boolean flag we can4 O& }5 d- A/ `1 L
// randomize the order in which the bugs actually run1 V0 y: t% ]$ `: D
// their step rule. This has the effect of removing any& L' a$ ?8 Z$ U
// systematic bias in the iteration throught the heatbug
% k4 V8 w) _2 N& z // list from timestep to timestep% E, i+ k$ Q9 s h
" s5 Q- D8 P' C; E. Y2 k // By default, all `createActionForEach' modelActions have9 I9 i% U) w+ m# w# m9 B
// a default order of `Sequential', which means that the2 k2 B; ?1 U7 m7 {8 H% \7 @# C
// order of iteration through the `heatbugList' will be' J6 v: z2 b( a# H2 f
// identical (assuming the list order is not changed- ^/ C6 w$ b/ z" q- H
// indirectly by some other process).
/ q: r: A5 a' Z7 Y/ P7 \
8 _0 b' c+ l* K# _, i. F modelActions = new ActionGroupImpl (getZone ());( d/ \* d; e7 R+ x: s' y# w
" ?( b4 P6 j6 z/ |3 m
try {- c4 N" W. l5 t% i
modelActions.createActionTo$message% W0 D `4 ~0 P) T: p( H
(heat, new Selector (heat.getClass (), "stepRule", false));
; u# h- c; J# \0 g. S } catch (Exception e) {5 ^2 @) H ~5 \ x6 D% @7 N
System.err.println ("Exception stepRule: " + e.getMessage ());
' C+ P- k5 G- l; k( S6 D# ? }
8 g: ]* M/ Y/ a, ~! b
0 p# R( ^7 Y+ C9 \+ L; n8 Z2 m1 T try {
% T3 n5 e' l- G* r A9 }5 E, k/ @ Heatbug proto = (Heatbug) heatbugList.get (0);! }5 Y6 `6 K3 |0 l
Selector sel =
4 B4 ]9 [8 w9 l$ ]- ?( m- o" v4 [# q+ z new Selector (proto.getClass (), "heatbugStep", false);
, F: l( R; |+ A/ J actionForEach =! e4 p( E9 B7 R m6 U4 l3 C
modelActions.createFActionForEachHomogeneous$call
8 _3 n: K6 D3 h2 ?, V! u (heatbugList,
9 U7 d* S2 y: W* m$ z new FCallImpl (this, proto, sel,6 P7 ^2 ?6 T' K) m2 m3 x+ M
new FArgumentsImpl (this, sel)));
) h/ F" f: K" {2 Q) d( Q } catch (Exception e) {* @7 b3 I( [* R6 [$ D) _2 c
e.printStackTrace (System.err);
1 j- V1 m+ j+ u }
, B4 s+ y# G6 q J6 r' {
" _; i% x& I! e b syncUpdateOrder ();
3 y. c& @) @$ }9 b+ z
% e) _2 W# f& d- r% ? c8 T try {
9 Y! g, r* K) J modelActions.createActionTo$message 0 N* V% `4 k3 B3 z; S
(heat, new Selector (heat.getClass (), "updateLattice", false));
- G% h0 h, a$ s } catch (Exception e) {
g6 ^/ ~7 ^7 ?( e5 f2 |. e# b% w7 j System.err.println("Exception updateLattice: " + e.getMessage ());
& j) D1 g8 A3 C }
; q! p& F) A; ]8 E6 k. Y 5 y3 U) H7 U! ~0 ^4 P" v! N& p
// Then we create a schedule that executes the
/ ?' x0 E9 z2 Y4 }/ P: I // modelActions. modelActions is an ActionGroup, by itself it5 }% k6 R' x o$ S; Z6 L
// has no notion of time. In order to have it executed in
: r3 f: `$ \. T7 H' [ // time, we create a Schedule that says to use the5 E6 Y" j6 J- [+ X) N# Y# W; {6 z
// modelActions ActionGroup at particular times. This0 }! z$ v8 p# b3 W7 i
// schedule has a repeat interval of 1, it will loop every
' n, j$ U1 j- r* _2 s // time step. The action is executed at time 0 relative to! w/ S9 ^8 E) S6 T4 ~
// the beginning of the loop.
6 i# N2 p w8 f1 W) M+ @
G/ G% D/ V- p& s% B$ W: g/ f) x // This is a simple schedule, with only one action that is7 ^5 h7 H5 }1 C% ?
// just repeated every time. See jmousetrap for more8 Q z3 L* h4 a$ n! b
// complicated schedules.
C. x; T5 `; z# [' ] + ^2 x- L3 H, y+ y8 a( c& ~( J, }
modelSchedule = new ScheduleImpl (getZone (), 1);
% m! e3 Q5 Q; o( X# `+ J2 Y+ _3 b! ~ modelSchedule.at$createAction (0, modelActions);
8 U5 H7 q5 \) U9 X
# h! K' R/ f# @8 Z. j6 r+ i return this;
. L: J" _: y5 [& A } |