HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: p( t+ y# ^+ [1 w" w! j
' s) C" A) I+ Y( t; W. ? public Object buildActions () {
! {- Q8 u( }: x9 I" G4 D; s- f super.buildActions();
# A* X: |( d8 {2 _! I$ K
$ C+ C2 O, i* T/ T9 D // Create the list of simulation actions. We put these in
, A4 o! d T, l // an action group, because we want these actions to be
* a; W( s1 U3 j' \( ] // executed in a specific order, but these steps should2 y+ ^% h3 r/ G& j+ j* j2 ?
// take no (simulated) time. The M(foo) means "The message @' e$ d" H1 [8 M6 C4 e
// called <foo>". You can send a message To a particular/ [2 E# q9 A; R0 S2 u# l P8 W3 F
// object, or ForEach object in a collection.( P' D- P6 b* g o, Q; @# d. q
/ T9 y1 ?& P2 H1 y. \+ u/ L
// Note we update the heatspace in two phases: first run# {# x0 L. P: h/ s+ m1 x1 O
// diffusion, then run "updateWorld" to actually enact the
3 P) _4 D/ g/ o" |3 D8 M // changes the heatbugs have made. The ordering here is1 c E7 C! u! T" B/ _$ \
// significant!0 y4 n! Q* Q3 g9 c2 i/ w
! h3 Q2 p ^- T, g F2 _
// Note also, that with the additional) ^$ I( c9 v2 n7 d9 ] r' a
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 m2 u" ^ I* |& a- o9 G# R
// randomize the order in which the bugs actually run
( b- n5 S/ R, @( s3 d3 q // their step rule. This has the effect of removing any, q/ \3 k. X/ T* O
// systematic bias in the iteration throught the heatbug3 I9 m. F. o0 L2 S( P
// list from timestep to timestep, Z: C+ |! H# O- [$ C: l& s Z+ S
# m t6 t. c5 A$ d1 Z8 H
// By default, all `createActionForEach' modelActions have
% ^ \! {( m! P2 w; O" q ` // a default order of `Sequential', which means that the
8 @7 }( {! O" k5 P // order of iteration through the `heatbugList' will be5 D2 e8 \3 g( y# Y$ L7 T3 h% i- G
// identical (assuming the list order is not changed
3 ]0 o1 L. k* u2 o* a- v4 l' H/ ~ // indirectly by some other process).
3 o' i/ m' _, F0 ?1 O G+ C5 n
! {" b. h8 c, N* U modelActions = new ActionGroupImpl (getZone ());
; r) q! }* X6 c& A4 h; }$ K% G: `# |" I5 O5 f g
try {
6 N% m9 j+ m X% \ modelActions.createActionTo$message& ^7 W: ]7 Y N
(heat, new Selector (heat.getClass (), "stepRule", false));
) F' a8 O. @1 V) o) S } catch (Exception e) {
# A0 H* S5 }: V+ k/ |7 F3 a- K System.err.println ("Exception stepRule: " + e.getMessage ());: G/ X( ]: p9 @4 f- p
}
* w5 U. ^4 O7 @/ L G! |. y, W6 I, B6 \" J' ^
try { z! ~1 X; [0 R1 J; ~9 U
Heatbug proto = (Heatbug) heatbugList.get (0);. I) L) Q7 j' b8 t9 r& J
Selector sel =
; A1 o1 x, t# c# |1 [3 c new Selector (proto.getClass (), "heatbugStep", false);
2 L5 p! k3 \3 t; J* r! O actionForEach =
4 a! ]$ L' U- `9 \& V/ S modelActions.createFActionForEachHomogeneous$call
/ D1 ^* N: `1 L% V7 [ (heatbugList,
( w2 n9 [0 l0 t- b' B3 X+ r0 m* g' H new FCallImpl (this, proto, sel,8 X X1 K# ?3 f- J E$ g& ~1 E
new FArgumentsImpl (this, sel)));5 ~/ o' `/ y# U2 e2 N
} catch (Exception e) {
F. h/ `6 o/ b3 m e.printStackTrace (System.err);
. W5 o6 i6 S2 D% h5 F }
* x7 @8 B$ g# u" ]% }6 F $ S C$ p3 n5 H! A; J; ~
syncUpdateOrder ();
0 V- t8 d! A& f. f: K
3 k5 ?' p! K% T7 v; h" i$ S- D try {
3 m" j A! D5 M3 h3 T5 M/ y modelActions.createActionTo$message
/ v0 U1 u D, U- T0 r9 V (heat, new Selector (heat.getClass (), "updateLattice", false));
" n' J2 S. ]3 _: k } catch (Exception e) {; c8 K) \2 E6 }& e5 M1 r: s
System.err.println("Exception updateLattice: " + e.getMessage ());8 C; [; \" y. f% R
}
5 ^& H6 M. X* H' c6 Z/ K% e
1 t- x9 v1 i& y' }6 c! Q // Then we create a schedule that executes the
8 ^8 ?# u7 E1 |; l9 M // modelActions. modelActions is an ActionGroup, by itself it g1 r& Y+ ]/ d2 `$ S& Z
// has no notion of time. In order to have it executed in
4 K& h! e* s! s9 e! ]9 p* {& D // time, we create a Schedule that says to use the
; Q2 ]2 Z0 O. G5 f7 w // modelActions ActionGroup at particular times. This! G7 q5 ]" z* R5 h I) w
// schedule has a repeat interval of 1, it will loop every% x. I# w4 I7 m# P- Y
// time step. The action is executed at time 0 relative to
) ^1 e, A2 F3 I+ A- n# Q // the beginning of the loop.
n3 ^" k9 r+ F! L* x6 r
" Q# r l8 y/ [$ e1 a; ? // This is a simple schedule, with only one action that is/ {- t3 @ D3 |! s
// just repeated every time. See jmousetrap for more/ A, B( W) @' e, ~0 ]9 D
// complicated schedules.- x5 R0 e. z" C( w6 O, F, N
2 `& |$ w+ Q( s. j modelSchedule = new ScheduleImpl (getZone (), 1);
( _+ c& R' ^1 \( l4 _ modelSchedule.at$createAction (0, modelActions);5 s; W* j# z, i" k% E5 }
$ l2 A! C. [: @8 g5 g+ E/ G
return this;# b/ s& I9 `: g+ a
} |