HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! |3 [, X/ ]7 @ N! N8 Z
. }8 [* f* @% b8 G public Object buildActions () {
) a" B9 X, y9 F! h8 o: d( \3 m* m super.buildActions();
0 |3 A4 t. }* s% L4 e( Y' S : h2 t9 y) K9 D6 m
// Create the list of simulation actions. We put these in. E- O4 D' Z) e7 M* @3 C
// an action group, because we want these actions to be5 s7 O. U2 x7 k$ G3 m4 |0 k
// executed in a specific order, but these steps should- j, l% N. h" N) U
// take no (simulated) time. The M(foo) means "The message
, F! r6 z) I. W" ~7 m- @; ? // called <foo>". You can send a message To a particular
6 X, j+ R/ L) j( x+ L0 i! j7 D // object, or ForEach object in a collection.( l8 u8 v0 c& V3 d% T% P
( d$ j4 l6 Z2 a- P" {
// Note we update the heatspace in two phases: first run: g3 L6 A, F/ E. n- }1 {
// diffusion, then run "updateWorld" to actually enact the
" X. b8 I) R6 u% J3 `+ E4 z // changes the heatbugs have made. The ordering here is. Q/ s4 ~; d5 e8 J4 Y' x
// significant!
1 P1 b9 I2 M2 v8 J t
. N. p. \7 t- i4 p6 f% J2 R5 q // Note also, that with the additional4 W$ x8 ] C, C$ o1 L) v4 H2 b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 S' J# Y) @8 T; i" Q7 @, y, Y) k // randomize the order in which the bugs actually run# R+ e" ~8 y0 R# P0 G8 W% Z
// their step rule. This has the effect of removing any
0 Q* n' Z5 J# K: G: ~% @ // systematic bias in the iteration throught the heatbug
! D9 R: d! _4 B, p // list from timestep to timestep5 r3 `! O6 p8 }, N: Y! @: z2 e6 |
4 W6 G4 Q% v' C) S2 Z* q
// By default, all `createActionForEach' modelActions have
: n6 h/ u q, V% A // a default order of `Sequential', which means that the) i' H, Z0 g' t3 N: P
// order of iteration through the `heatbugList' will be
# x! R' ^+ d+ z* _. K; I. B4 l // identical (assuming the list order is not changed
3 {; K1 |, o/ ~- i8 e' g* h. { // indirectly by some other process).
" n/ N' i) a! l9 J3 }/ A( ~% t / B I# H/ N* [8 R9 L0 y, E1 u# E2 P
modelActions = new ActionGroupImpl (getZone ());1 f4 W7 l7 [: h# y% K' \
3 t& L& U6 K/ }: K7 A
try {
# h; d8 ~0 I4 {$ p1 i: I2 Q modelActions.createActionTo$message
4 F2 G# r& d0 Q. g (heat, new Selector (heat.getClass (), "stepRule", false));
" L% w5 C8 }+ U+ a9 J! L5 R' v: u } catch (Exception e) { b4 E9 _1 @# t' h0 b' k
System.err.println ("Exception stepRule: " + e.getMessage ());
7 }1 G- H! m2 b; M1 c2 @- {2 J }
8 k! `; @( n* L3 H1 ~. G8 s; D1 P0 ~ K/ Y9 `2 Z2 x* a- o3 O
try {! ?/ O. L8 m2 b* L
Heatbug proto = (Heatbug) heatbugList.get (0);2 O6 U9 V1 i6 P3 i! i6 f( N* Y2 B
Selector sel =
2 E# }% m4 Y" s1 N$ E7 k( m! c* { new Selector (proto.getClass (), "heatbugStep", false);
# y, `7 s( N8 d% q+ K actionForEach =1 j @. x& f ]$ C0 _
modelActions.createFActionForEachHomogeneous$call' l" l# p/ Q+ Z6 g2 \5 Q
(heatbugList,$ b& K7 l- Z# A* R. R& {( L5 ]
new FCallImpl (this, proto, sel,$ V; S! G. U- T
new FArgumentsImpl (this, sel)));( V( i3 |# g4 W6 b) v8 ?* L
} catch (Exception e) {
) \; H. N8 B0 a5 x9 L1 Y2 j" f0 N' H e.printStackTrace (System.err); k7 a5 c0 {, }6 D4 O7 [! G
}4 k/ X' q' q9 e$ Z
2 W `5 N. e& H/ } syncUpdateOrder ();
7 Y& Z9 s# u; J0 Z+ V5 A8 B1 m# J, n+ r" C5 m5 z5 N6 [+ [
try {2 Y& u( r; a+ Q0 `1 g, L# U
modelActions.createActionTo$message
& d7 i5 E* X$ ? a; w6 n# w (heat, new Selector (heat.getClass (), "updateLattice", false));
) V1 ^ H2 L. Z! V } catch (Exception e) {" b2 Y, j f( a! E6 S
System.err.println("Exception updateLattice: " + e.getMessage ());
U# a, H6 f( v$ Z: h! z4 \+ ]7 k+ h }0 p: j7 G& R; M( ^: p4 F$ b
2 M* Y5 C+ B v. @/ S // Then we create a schedule that executes the: m5 h( I, g1 t, n$ S. p% ?2 N7 @
// modelActions. modelActions is an ActionGroup, by itself it
/ H4 z* M0 v7 S% D; r$ t% o // has no notion of time. In order to have it executed in
& h; C$ p: ?: M$ p // time, we create a Schedule that says to use the
0 H3 }6 D/ J% U$ q' G. l // modelActions ActionGroup at particular times. This
0 x7 t z6 U7 M1 N C5 B // schedule has a repeat interval of 1, it will loop every
y% n4 [0 P5 q+ w" n x6 C& ` // time step. The action is executed at time 0 relative to$ \" h9 f. X. {" E! D+ x/ Q
// the beginning of the loop.
3 G. @/ \# ?" B- D. u9 g, W* R+ y
// This is a simple schedule, with only one action that is$ m. R$ \0 T! K' m# h: V* C/ O
// just repeated every time. See jmousetrap for more
# t3 A q' i7 i# N6 R4 C // complicated schedules.
2 H3 ?, V8 U7 `9 g0 b. I8 W+ F
5 A: M7 Z( M& ]/ } modelSchedule = new ScheduleImpl (getZone (), 1);
5 l/ Z" e2 ?; q3 I) h/ D4 G modelSchedule.at$createAction (0, modelActions);# D' c: E( ]1 J# ]1 E
p0 ]* y+ z4 Q8 U. z6 d A
return this;/ J0 d+ {1 f" r" ~1 k$ h t1 i
} |