HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ N& b* m# C3 U6 G. Q% ?# s) }2 U+ N$ e( K1 S
public Object buildActions () {5 y- S2 t" E+ s6 Z1 M
super.buildActions();
$ z( _7 p* a/ j) o: F. ~ 7 @( K% R2 r ?% `; m h
// Create the list of simulation actions. We put these in* D% `3 l6 F8 N2 r q9 v8 Q
// an action group, because we want these actions to be
; q$ f* R% i6 G( L" x // executed in a specific order, but these steps should
, D1 ]& C$ U, q) g4 [) y$ B- G) y0 \" N // take no (simulated) time. The M(foo) means "The message
# ^3 ]3 ~- e+ m9 L // called <foo>". You can send a message To a particular
; B; T& Y- y& Z6 | // object, or ForEach object in a collection.
% J+ k2 A+ R4 M' s& n6 y ; H7 r. @- k) f( @$ @9 N) r4 F
// Note we update the heatspace in two phases: first run
) |. C& t9 j Y2 Y1 v( c // diffusion, then run "updateWorld" to actually enact the7 w1 D7 d" ]( a
// changes the heatbugs have made. The ordering here is% d, Q) s! V5 F$ ]- T7 R4 l
// significant!8 _& p" Z$ `3 h+ a8 q. u
8 S, w4 U. o. R; r Z. |, h2 A // Note also, that with the additional
+ A' E: E- u d j" k/ H1 u // `randomizeHeatbugUpdateOrder' Boolean flag we can8 k) T) h J( `8 w
// randomize the order in which the bugs actually run
/ s; ^! i: J/ Z+ t$ b5 E // their step rule. This has the effect of removing any
* h8 a% b+ _0 g C$ n0 @' f, ?. M0 A // systematic bias in the iteration throught the heatbug
$ q4 s$ m2 \4 g // list from timestep to timestep
9 e$ {6 E" Q; g @7 Y1 N / F& r% A9 x) h2 q( v8 t6 \4 C5 A
// By default, all `createActionForEach' modelActions have+ m, J' @7 \+ D2 [( k
// a default order of `Sequential', which means that the# Z7 _" Q1 R5 A8 _/ U* m2 Z7 H
// order of iteration through the `heatbugList' will be5 N9 m# H7 b5 T _
// identical (assuming the list order is not changed% V* f, }6 n* H* t: E
// indirectly by some other process).& \2 i) A0 n& Z) }5 @
) d) s0 e" t7 g4 I/ B- G
modelActions = new ActionGroupImpl (getZone ());7 {- p% K) v& \- {
5 N& k$ P4 p) K# k' b; K
try {
. f4 Z, S, a- ?% N7 I/ C modelActions.createActionTo$message! d/ c: g+ g8 L! {) g- o, S
(heat, new Selector (heat.getClass (), "stepRule", false));- D j9 N, z- z, `* q8 o! _- o3 H
} catch (Exception e) {: G9 t1 [' y1 s* L) A, a
System.err.println ("Exception stepRule: " + e.getMessage ());
& d. V5 G" C( L( n/ p, ] }
9 F: k# N& H+ ]6 k2 J' \- ~# C8 q. R+ A8 d7 Q' s6 ^
try {
: r$ \2 I- b7 ~, e, h3 {$ s Heatbug proto = (Heatbug) heatbugList.get (0);
N" G7 s; ^- T! Q+ v& r1 O' x Selector sel =
8 c4 `) L- i7 S+ d: s new Selector (proto.getClass (), "heatbugStep", false);
' k* `5 F' ?* @2 E- g2 a actionForEach =2 D3 T3 F# {6 Z5 x* d0 x
modelActions.createFActionForEachHomogeneous$call
9 S1 s0 [2 [. L+ x' z2 S (heatbugList,
8 R. U! [4 `" n# q2 h new FCallImpl (this, proto, sel,
" B% p/ Y1 m- a' m4 J. k new FArgumentsImpl (this, sel)));
* d: }1 \! T+ d" ]# P8 l. e } catch (Exception e) {
( { H, X0 |( m9 ] t+ ~7 I/ @ e.printStackTrace (System.err);0 g0 w$ B2 h0 n; P2 ]: _* U$ f
}7 X% } y& W9 G* H/ V
- ^* M+ I" u' j2 y7 b; d4 c
syncUpdateOrder ();
% [" k- Z B$ T: Z
5 A" \% o/ Z: Z; T try {& Q" q7 U! V: Y) a/ }2 R ?7 s7 C
modelActions.createActionTo$message / G" K: j& U9 @' C
(heat, new Selector (heat.getClass (), "updateLattice", false));
* p: [6 [& N) k, w3 H3 E } catch (Exception e) {& q' D. \# @- O
System.err.println("Exception updateLattice: " + e.getMessage ());9 x) Z5 ?* H9 N3 F5 t, f
}5 W4 G" N" d" j" E$ s
! L/ l; ~. s {* K# R' E+ e. X3 M // Then we create a schedule that executes the
' ^! ~9 o5 }/ T8 q // modelActions. modelActions is an ActionGroup, by itself it0 A+ n$ T! B$ Q
// has no notion of time. In order to have it executed in
7 I' r7 a4 J7 s# U# j& I3 N+ V8 R // time, we create a Schedule that says to use the" M' _* J+ |+ K1 j# T# G+ ]
// modelActions ActionGroup at particular times. This" |+ ^# M$ K, \5 o6 n0 w2 u$ f4 h
// schedule has a repeat interval of 1, it will loop every5 i- ?; r" K! R3 H: y% G# H+ P
// time step. The action is executed at time 0 relative to8 ]; }$ i; E- T) l1 Q; O
// the beginning of the loop.
& I1 T; N) s9 q) O4 @) C g
& \ w9 }- F0 L: E1 _ // This is a simple schedule, with only one action that is
% H/ O0 t+ y$ k/ }8 p: n, X7 c // just repeated every time. See jmousetrap for more$ m* E, @3 H, i* U' O1 G: _
// complicated schedules.
# q1 r" Z% N. }% C 7 H P4 F u3 W8 B! q" j
modelSchedule = new ScheduleImpl (getZone (), 1);
7 D2 {# o5 n" k- T: @0 I E' } T2 b modelSchedule.at$createAction (0, modelActions);
$ O% K! t" c# k; f / p9 V/ t @+ ], l) p
return this;
( B, A3 @7 _! D4 w. f } |