HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' R3 @" W% y- j# _
; k/ h& J! M% ~4 N* Q public Object buildActions () {& ~( b/ ~. ]- n" p6 _. y& @, c0 p0 O
super.buildActions();
( a$ O$ O) P0 N. p " l' v* g; a& n( M# J( g
// Create the list of simulation actions. We put these in
$ c; t* K, n- L // an action group, because we want these actions to be
! u, D- {1 e% j( j# V5 ? // executed in a specific order, but these steps should6 y3 n# {9 C& Q& n& [4 @( v
// take no (simulated) time. The M(foo) means "The message/ l( z. o' o% y" E1 `. q
// called <foo>". You can send a message To a particular
. h/ F- I h8 u9 O. Q3 Z // object, or ForEach object in a collection.9 e. W) S4 d+ K% V
" g9 y- M# v: F. }
// Note we update the heatspace in two phases: first run
* t; {4 |. ~( n+ I& l- J // diffusion, then run "updateWorld" to actually enact the' d, C, V- i$ K
// changes the heatbugs have made. The ordering here is
# x$ r. F4 e6 d8 r- L3 t2 Z // significant!
& ?8 l0 |, l* z
) ?' d: p/ f8 j+ \2 t! _ // Note also, that with the additional
0 t9 \3 C8 v+ u! s: R // `randomizeHeatbugUpdateOrder' Boolean flag we can: L2 H2 r! P9 y9 m4 ?; C
// randomize the order in which the bugs actually run& R, `1 {! W; U m, R; W
// their step rule. This has the effect of removing any
8 U G: K& O$ p) Q9 i w2 Y) g. Q/ Q // systematic bias in the iteration throught the heatbug
- d3 L# J; A6 ^* c) c- Z1 m; I // list from timestep to timestep4 t H0 z: e( v: o1 O m/ p$ U
0 G3 Y; ^3 q9 x: f& k // By default, all `createActionForEach' modelActions have2 o' G+ T9 d& I( Z" x& E% F
// a default order of `Sequential', which means that the0 p6 ^# h( T' X) H. {) I( z
// order of iteration through the `heatbugList' will be* y! B! }3 T) U7 g6 }5 G4 n$ C
// identical (assuming the list order is not changed% t; T+ m# g6 D4 X" ~
// indirectly by some other process).* a7 O3 Y& h$ I L
- G2 n; A! {( u$ Q2 G) [4 ]
modelActions = new ActionGroupImpl (getZone ());
) D) W/ W$ k# b
# m+ e% Y# q- a9 S try {% o* f9 j& Q1 k4 S$ e4 A
modelActions.createActionTo$message; j, \/ y5 M1 @' v
(heat, new Selector (heat.getClass (), "stepRule", false));
, ^$ O: N3 h' P* C } catch (Exception e) {
p3 T7 G6 Z6 ] System.err.println ("Exception stepRule: " + e.getMessage ());' ^4 P: D/ o3 H5 u8 }3 A
}
2 P1 F7 X! X7 w) i. G. c# Y, f0 s: W q/ z, D( _
try {
' D# s9 ^+ y( L- ]' |$ `' L Heatbug proto = (Heatbug) heatbugList.get (0);1 M r" c& q$ [; V7 B: S6 j
Selector sel =
) b! V( K- q6 a- R' p8 k new Selector (proto.getClass (), "heatbugStep", false);
: ?. Z! N3 e& L9 Q4 f; ~2 ^* s actionForEach =, h6 a) h2 C$ l' T
modelActions.createFActionForEachHomogeneous$call
1 ?! m1 P1 V2 s: E (heatbugList,
! y5 r r" y. j9 M new FCallImpl (this, proto, sel,' R- m- [; _( I$ I6 I( l7 F% E
new FArgumentsImpl (this, sel)));
" Q& F: v5 R# W+ `3 Y8 Y4 I" x } catch (Exception e) {7 y3 R, U% N9 t; c* O; p
e.printStackTrace (System.err);+ V2 i& b3 n4 i! y. ]: L
} G! ]9 A6 y3 N9 Z& t6 h6 @
5 { Z' o% g! Y$ @+ r
syncUpdateOrder ();& n0 W! }' A1 ?: W
; z5 F5 h. j9 \/ `. i' P# k try {$ P/ M- k7 w( Z) v
modelActions.createActionTo$message * `8 \' ~# y5 W. E; t. R
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 F( a2 ~: M% q } catch (Exception e) {
) U4 D, e7 N3 s4 W( } System.err.println("Exception updateLattice: " + e.getMessage ());- i% P- I+ j7 c
}
5 R# N2 @( ^; g ! R4 P: v) K# a2 ~- @
// Then we create a schedule that executes the
* l' Z) n9 ?5 t6 E) M // modelActions. modelActions is an ActionGroup, by itself it
' W8 ?2 \0 N, y; g2 e // has no notion of time. In order to have it executed in! G9 v( \: a7 |0 z/ S; V& C
// time, we create a Schedule that says to use the' i/ l" J9 h, _: `7 q: R' ?4 z: A
// modelActions ActionGroup at particular times. This' o' i2 i6 R+ o8 x! i1 d0 c
// schedule has a repeat interval of 1, it will loop every
$ ~: d1 J2 g$ o+ y# |9 f/ j // time step. The action is executed at time 0 relative to* U+ \0 a9 v' C j/ m( s
// the beginning of the loop.
! {2 D2 J- I% ^; K+ C4 k$ F5 O! a2 W5 Y
// This is a simple schedule, with only one action that is) J+ ?* D7 W# @
// just repeated every time. See jmousetrap for more
# d2 _. l5 ]5 u# a4 o9 k4 G // complicated schedules.
$ G; f' n' \. ]: w. I$ n' X$ G + j! w# S3 _4 N
modelSchedule = new ScheduleImpl (getZone (), 1);( W8 f- t6 f# l7 P3 M( s* B* k
modelSchedule.at$createAction (0, modelActions); `# }( h- W: A s( f, u
' U3 |2 R: N1 @' m2 C5 H1 O. W return this;
' ~2 @* X% a8 H- {8 X } |