HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% s4 j4 a2 W0 ^$ \; {, O
2 ~1 J E6 G) L" _! z& C public Object buildActions () {
2 C0 ~4 h. [$ U2 f' p4 I4 y super.buildActions();8 Q6 q$ q) ?9 u9 R/ `
8 @9 O) R- a: |. L8 x/ i( F- d // Create the list of simulation actions. We put these in
" J8 W* j+ F8 j+ ^ // an action group, because we want these actions to be! k; @8 H! v: l/ C% g+ R' h
// executed in a specific order, but these steps should
7 {, S% N- [7 ~8 `6 u // take no (simulated) time. The M(foo) means "The message5 j$ x) i1 [+ N7 @0 m) y
// called <foo>". You can send a message To a particular
, J H% ?/ c- {% u* v( C // object, or ForEach object in a collection.
2 I/ X9 o+ H% g- Z7 X
, b* c9 r4 V' B% h( v d! r+ Y! ? // Note we update the heatspace in two phases: first run1 y* x$ ~) f+ B( p8 A, ~
// diffusion, then run "updateWorld" to actually enact the
- }7 h0 U5 t" r' y6 A* r // changes the heatbugs have made. The ordering here is
# x6 N/ a0 g2 g% T1 R5 p# ? // significant!# ?3 _2 p$ [3 @' x( e( H' g
) s( {9 S5 G& Q // Note also, that with the additional$ ]/ M6 C% Y% k
// `randomizeHeatbugUpdateOrder' Boolean flag we can% f6 V8 |- i, W. c2 X2 |; h. `1 O
// randomize the order in which the bugs actually run+ v) A; c, `7 p
// their step rule. This has the effect of removing any% j6 U( c g: q+ h0 j
// systematic bias in the iteration throught the heatbug% h+ y( ?: G% u9 I! y
// list from timestep to timestep
3 V8 n! s3 n* @. p, ^8 ~
2 q0 J$ X* ~+ }( ~+ g9 A/ B* V // By default, all `createActionForEach' modelActions have
: g5 g* E Y+ l; Z // a default order of `Sequential', which means that the( I$ a0 k' ?* j$ A
// order of iteration through the `heatbugList' will be: e) X9 c. D6 T; `
// identical (assuming the list order is not changed( f" ?$ T' ?% c5 Y5 d" `. O, D
// indirectly by some other process).% d$ g% ~4 c8 w+ ~" }* o
& B! T& O4 I% J7 Z# q3 H; f) l" V modelActions = new ActionGroupImpl (getZone ());
$ Q- S$ |5 [+ k+ r7 f
9 ]% d5 `6 @% B1 O( b try {" w [2 `' X: L9 O. o3 h
modelActions.createActionTo$message! J6 Q1 O6 i! \" c, Q; B& L
(heat, new Selector (heat.getClass (), "stepRule", false));
. H' D. O# p) d2 X R# X3 k } catch (Exception e) {
# d3 c0 ^9 @# K7 t- O4 q1 ?4 T7 ] System.err.println ("Exception stepRule: " + e.getMessage ());
' O4 ^* o# ^' U4 v& {. o o: J) q }
7 `4 V$ n+ q, h: f
. f3 ?$ @* z9 s3 }' T0 k try {
( A5 m( Y5 T+ ~0 |4 Y Heatbug proto = (Heatbug) heatbugList.get (0);1 V" z' ^+ N/ H. y5 \- m
Selector sel =
$ A y8 Q& n( s7 G3 S new Selector (proto.getClass (), "heatbugStep", false);. m% K+ r0 j' O! v; C: i
actionForEach =
5 t8 G9 b: H7 C! n1 x; g, V6 S( l modelActions.createFActionForEachHomogeneous$call
' I/ }! M$ U+ _1 k. Z (heatbugList,( l$ }) |! c9 n) p+ e$ `, l
new FCallImpl (this, proto, sel,
3 l, _! k) d! z new FArgumentsImpl (this, sel)));
4 M8 Y) w2 |2 c6 T } catch (Exception e) {# _0 o1 b! G9 G
e.printStackTrace (System.err);1 P( J! V* K0 @+ q4 B* P" y4 T
}4 H |, L/ m+ d% v( o* e
; Y3 u& l9 L4 `- @
syncUpdateOrder ();
& b$ K* {8 m( x f# [4 A6 K5 [
1 k9 T5 s$ K5 H' C6 T9 t try {
. C9 d: U( V* f" W, t9 ] modelActions.createActionTo$message
9 J3 J) E0 |# g9 U5 |, S+ _ (heat, new Selector (heat.getClass (), "updateLattice", false));4 G9 |3 }! i. e2 O8 N
} catch (Exception e) {
/ W1 o, I( O4 A; Y1 c System.err.println("Exception updateLattice: " + e.getMessage ());
/ q) I0 r8 w8 y6 Q6 }1 X7 Z }+ m6 ~: S* `; o$ G; ^+ J( t+ B8 @
! d" E6 y$ ]* }6 M' F
// Then we create a schedule that executes the8 l# O C( a! N; S' b- k
// modelActions. modelActions is an ActionGroup, by itself it! g; ^; q8 Q" o' ~: J7 V2 ?5 b% ~5 ~
// has no notion of time. In order to have it executed in
% b0 Z+ q! z. t5 G) S# {% a& J // time, we create a Schedule that says to use the* |" q; j0 ?/ ]8 R. e2 M
// modelActions ActionGroup at particular times. This0 L5 x7 Q) A" j0 H/ J
// schedule has a repeat interval of 1, it will loop every
* f" K5 G0 u4 r, r+ R$ I- | // time step. The action is executed at time 0 relative to6 i- G, r6 Y Y/ D, n& `8 I) A
// the beginning of the loop.: A3 }$ g3 \; B2 J2 v
" I* {8 `( ~* {9 G4 w
// This is a simple schedule, with only one action that is- V0 d, B2 ?# Z6 i
// just repeated every time. See jmousetrap for more- Y: [7 Y/ o" O8 Y8 O! k4 i
// complicated schedules.6 w! t! G4 l* ~' ~) Q
0 f& q) P& X. I0 o8 [
modelSchedule = new ScheduleImpl (getZone (), 1);4 N% ?8 G$ Q4 R' s% l3 _/ i3 {
modelSchedule.at$createAction (0, modelActions);
$ v) F# y g: n+ Y
9 f& K0 b( t' L3 z% D( _' L' Q* |7 v return this;
8 x1 |. ?' n. _6 Y } |