HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% N, I1 g; ~9 }& i6 B' r% [" r+ n3 u
public Object buildActions () {
5 B# X5 t( e& Q4 B' g) i1 O super.buildActions();6 P l6 F3 i2 U$ R8 H9 ^9 V2 [
! L+ z: S3 @" v+ u9 Y // Create the list of simulation actions. We put these in
+ }9 m' [" }/ j) `* x9 H0 F2 F/ U8 d( u // an action group, because we want these actions to be+ u& }+ p) g5 J8 R8 e* Y2 H! E7 Q
// executed in a specific order, but these steps should
4 v# H; d8 r0 U1 I# l // take no (simulated) time. The M(foo) means "The message1 s2 U* r& j* Q; q9 ~
// called <foo>". You can send a message To a particular0 N5 e8 q; n# c3 B, h$ n
// object, or ForEach object in a collection.
3 y# s0 L e* ]( t q: f ( l# ~& g: j1 K2 j6 Q
// Note we update the heatspace in two phases: first run
: |% b& e r5 o9 c7 M // diffusion, then run "updateWorld" to actually enact the- q2 W o) L9 Y: V# K( i! W0 W) O
// changes the heatbugs have made. The ordering here is | H! F% z% I; k9 K' B
// significant!
0 h9 K5 A8 Z$ m, P v% n
- `" y7 X! Q5 { // Note also, that with the additional+ n M6 U b' F, G
// `randomizeHeatbugUpdateOrder' Boolean flag we can
! _* N5 J+ N: R+ ~7 r8 S // randomize the order in which the bugs actually run
, [4 c' Y, a9 T* W i h+ }; z // their step rule. This has the effect of removing any( x; U3 j% ?9 P7 m7 k& U
// systematic bias in the iteration throught the heatbug+ @9 V0 G. l D3 @& {! Q
// list from timestep to timestep+ \3 X ?2 ]" k$ g4 s7 R
3 D5 p; {) Y, s$ h5 c. t! q // By default, all `createActionForEach' modelActions have
: ]) R4 E2 ^2 o" I1 W // a default order of `Sequential', which means that the
; v( g# N) f- f# C4 n // order of iteration through the `heatbugList' will be
9 V# ]# B: \% \ // identical (assuming the list order is not changed
+ j( q$ {6 j; w$ z // indirectly by some other process).; i9 F! @; n$ e; \! d8 R) o8 A
1 x, J+ {$ c& m
modelActions = new ActionGroupImpl (getZone ());
% p$ T4 v2 r& P% K# v
~' L+ ~8 O( f) ?% u5 n) O& h try {9 w L+ G3 H8 U3 S5 o* S
modelActions.createActionTo$message3 Z$ |/ ]; L+ N8 D, c3 z0 C) \8 C
(heat, new Selector (heat.getClass (), "stepRule", false));! n+ ]* I9 p% H/ o* Y1 k9 `
} catch (Exception e) {
. o* d4 m6 P) F- e& O( k System.err.println ("Exception stepRule: " + e.getMessage ());2 _0 T1 S0 e+ d/ f" j( M
}
4 g) j- e9 ^9 q& ^2 d
7 A7 I4 z6 C, Z/ S. }3 L* n- M0 o/ b try {; J0 }8 p8 F+ }) y# t, y- e0 A
Heatbug proto = (Heatbug) heatbugList.get (0);
5 Q) D! m( x/ O: u9 P Selector sel =
4 @, `: m+ Z: w" G new Selector (proto.getClass (), "heatbugStep", false);
! I! I& S# I$ A C& s actionForEach =% l1 H1 }7 x* K6 o' V
modelActions.createFActionForEachHomogeneous$call
# I5 U$ a, x/ @+ A. k! [& K (heatbugList,
2 ?' e8 u3 h2 ~. O, j new FCallImpl (this, proto, sel,
, J) B( ~/ w: a% q3 s3 n5 l new FArgumentsImpl (this, sel)));
! W/ a' V7 g7 D/ T+ M; X, y1 ^ } catch (Exception e) {& p6 }2 F, s( P5 _! U+ O4 i* ^; C
e.printStackTrace (System.err);
; t7 r% M+ c2 c6 N }2 J9 ~$ q# t" s# n. W2 N
% U' m8 u* y1 m, d syncUpdateOrder ();
& G- E* i, A- [3 M
/ g# W. d( Y' y. w4 Z2 e3 H' W* @$ ] try {
/ V7 F: T9 u7 e p$ V modelActions.createActionTo$message
* j! n, i# R6 S0 E& o1 j (heat, new Selector (heat.getClass (), "updateLattice", false));* p2 e; F8 |# I* r; v. x* {4 d2 U
} catch (Exception e) {: X8 P) e% x& x! e
System.err.println("Exception updateLattice: " + e.getMessage ());
. O* a8 F! w. H, w$ [# p1 Y }( j% B9 U+ n" A* I) t
5 o' F8 b, V+ `) _
// Then we create a schedule that executes the
% q" h1 u, _- U; t" x // modelActions. modelActions is an ActionGroup, by itself it
& ? G7 P$ O& ?2 v // has no notion of time. In order to have it executed in1 M: j' `. X3 A
// time, we create a Schedule that says to use the6 F) P; m! B1 N; N- Z( u3 b
// modelActions ActionGroup at particular times. This2 |5 B' s# l6 r% A# [: K" C
// schedule has a repeat interval of 1, it will loop every" Q! A1 e8 w4 u
// time step. The action is executed at time 0 relative to2 m$ z8 s% ]; u: b
// the beginning of the loop.
7 H8 S6 h7 f( S( a+ C
$ I" `8 G8 V# A7 e, P: I. v // This is a simple schedule, with only one action that is
7 z1 m( U- D$ T- ^2 u$ ~/ H // just repeated every time. See jmousetrap for more3 z. O7 H$ f8 w& N1 w
// complicated schedules.
7 t! H! ~6 A) B 4 G1 c( @. \5 o# B. V$ ^
modelSchedule = new ScheduleImpl (getZone (), 1);
' l" q- T9 }1 f9 @# ?9 i4 e# Y) s5 ^ modelSchedule.at$createAction (0, modelActions);; z- `. Q" E! P) V: L5 g
. W& s; E/ h. ?+ B return this;
7 d, F- p4 q! [& v } |