HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 q6 _' p1 g3 x8 V
, L& }( r, i6 v% ?; \
public Object buildActions () {5 R, Y8 `' r! B8 Z, l
super.buildActions();- B( c. x/ u1 K1 I/ k
; \) E8 p* j6 u5 m9 ?0 ?
// Create the list of simulation actions. We put these in1 V1 \: X S. ~* `
// an action group, because we want these actions to be
" x$ R8 S7 _, _" V9 p0 x // executed in a specific order, but these steps should4 @; Z+ K8 r" i6 M
// take no (simulated) time. The M(foo) means "The message# ~# A2 T# A3 s; w9 K2 [; U. e
// called <foo>". You can send a message To a particular
! i0 w5 ?+ k/ X // object, or ForEach object in a collection.
2 m1 G$ y: {4 m: n% P8 O: b
# `2 {% b6 K% ?2 p1 G // Note we update the heatspace in two phases: first run& l* j, ~4 u0 V" P7 G$ [4 V
// diffusion, then run "updateWorld" to actually enact the
" e: ?3 u7 k! _. d0 W1 j // changes the heatbugs have made. The ordering here is
" b9 W1 g# O" t! X3 O // significant!' Y3 Y6 N2 b2 h1 n) N" a
! I9 V W+ R$ Q4 x }. Y // Note also, that with the additional
3 f. U- P- _; k& _7 ` // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 n; S7 {2 m# T/ a( B/ M4 x. ~ // randomize the order in which the bugs actually run# P, g$ U, @' h( [# Y
// their step rule. This has the effect of removing any
x6 G3 _" J( [$ F // systematic bias in the iteration throught the heatbug
! r! N$ F( A: b2 U# @% B' ~6 q // list from timestep to timestep! Q+ i- @# e: N! n% }
% `' z8 |/ S) F/ C0 o4 |2 _+ l
// By default, all `createActionForEach' modelActions have
/ F& f, F0 F% @ // a default order of `Sequential', which means that the
; b& n2 \! @ n8 k // order of iteration through the `heatbugList' will be) U+ D$ c. z: C' |* t+ e
// identical (assuming the list order is not changed
4 B/ A6 Y" x# o+ E9 u" l // indirectly by some other process).) a, c% {3 W, k) r. u
0 N6 S/ ?- y8 F4 h* k
modelActions = new ActionGroupImpl (getZone ());
; u' ?& i: b3 {9 o! ?* X
) o3 I% y# X* S; b% ]7 O" @ try {
" I9 n. r+ b0 g5 t modelActions.createActionTo$message
: p* j% C; u( Z0 M) {6 i: W (heat, new Selector (heat.getClass (), "stepRule", false));8 ~; t+ V8 B% F, ^. s
} catch (Exception e) {0 n1 t* g! m; q' B A5 T
System.err.println ("Exception stepRule: " + e.getMessage ()); v0 V" \% Y- T; K0 o
}
o" o+ J7 r7 m+ v1 k2 E k7 Y, N1 R
: C6 c7 p5 s& C8 d% J- l5 ]0 D% k try {
+ P9 `# V- L- ]5 q- ~ Heatbug proto = (Heatbug) heatbugList.get (0);4 b9 D1 B( h, B
Selector sel = / ^7 L0 N1 r$ n! y" L
new Selector (proto.getClass (), "heatbugStep", false);( r- a6 b4 C; `2 m" L: h
actionForEach =) z; h v( l& J; ^4 e
modelActions.createFActionForEachHomogeneous$call
) i' ?8 G+ H2 A u+ @ (heatbugList,
$ ]7 T- A1 k, Z( i new FCallImpl (this, proto, sel,- P3 Z" y9 z. G( r' D$ n3 S
new FArgumentsImpl (this, sel)));$ l. Q: o) Q" y6 k: F
} catch (Exception e) {
8 B; t" t$ ]: f6 @% t1 p e.printStackTrace (System.err);9 g& `* m6 h+ Q0 U8 q+ ]. x
}
( l Q8 u" O4 X2 W
+ d5 j( c! _1 } N syncUpdateOrder ();
' S. R: a2 t4 `+ p( p' L j7 d! ?, E# W( \6 |. y. q. a: C5 w( ?
try {
' a& o4 ~/ |# ] modelActions.createActionTo$message
# _6 {( L7 \/ Q2 m+ I! ?5 Q (heat, new Selector (heat.getClass (), "updateLattice", false));
! o$ }% A5 z# J/ s } catch (Exception e) {4 W' Z- u( R) D6 z$ F q2 N3 S
System.err.println("Exception updateLattice: " + e.getMessage ());6 M, R! G6 y7 M, q1 o
}
9 y* o8 V3 E/ |! O , g* G, M8 o0 t& o' U( g
// Then we create a schedule that executes the% d, ~4 C9 w8 r- v& t3 s x
// modelActions. modelActions is an ActionGroup, by itself it7 x2 ], H" n4 X' w8 e: v% d
// has no notion of time. In order to have it executed in
4 v& v0 g9 y7 K' f" s; { // time, we create a Schedule that says to use the
7 ~/ Z; J- f9 p // modelActions ActionGroup at particular times. This
$ T" F* E0 l, z" p% L // schedule has a repeat interval of 1, it will loop every
: W; h5 A! I- M1 ~; M! } // time step. The action is executed at time 0 relative to+ B: T; E& s6 K+ m, A1 h. ]
// the beginning of the loop.& _" {2 f" G5 \
2 E! d7 V2 w5 W. R3 A( E // This is a simple schedule, with only one action that is; Z" V+ A6 X" m* O0 i! |) O
// just repeated every time. See jmousetrap for more: S& s3 ^8 |: n5 z1 D/ i% b7 }
// complicated schedules.
" O# ~# t. p( @) d# x
& n3 g; M1 h" K& A modelSchedule = new ScheduleImpl (getZone (), 1);
5 a5 H/ W. A4 X# E modelSchedule.at$createAction (0, modelActions);9 y8 t8 N0 T- k2 [0 v5 n
2 V' z! T! {+ W' }. b; W$ I6 z
return this;
; v: a1 z0 \% r# \) w- q% H } |