HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! Y6 \# v% T& x: m- |, D( U1 h. Q* _$ c6 P! M
public Object buildActions () {
# Y$ Q8 [" M1 e+ S super.buildActions();2 M# a7 G6 z+ g! x
5 P* S8 O# J; I; N: ?
// Create the list of simulation actions. We put these in4 t; k& U$ o' [/ Y
// an action group, because we want these actions to be
* B7 ]: h4 j$ G2 n3 h4 y. n // executed in a specific order, but these steps should& P; d' F j1 T/ B) h2 E/ U
// take no (simulated) time. The M(foo) means "The message
$ \$ ^+ F3 B3 x( z8 ^5 B // called <foo>". You can send a message To a particular
' s9 o7 \' e0 i6 L# M' n6 s5 z$ a // object, or ForEach object in a collection.
. ?% ~1 d3 \% j1 B 4 G& H- X" e! C: @0 h) B/ C
// Note we update the heatspace in two phases: first run7 X$ N! K/ U% i
// diffusion, then run "updateWorld" to actually enact the, ^2 p H1 Z ]1 G: K
// changes the heatbugs have made. The ordering here is
' `2 h2 W+ M" U, Q, M // significant!; v6 C/ A4 b' h1 K, Y7 {
* l' d- l' y; ` // Note also, that with the additional
1 E( y: Y* |- S1 n0 G3 ? // `randomizeHeatbugUpdateOrder' Boolean flag we can: R' l$ A( F7 T+ F& k) V& T/ e6 R
// randomize the order in which the bugs actually run
; }% i& q, c1 O; C( q; M9 X // their step rule. This has the effect of removing any* v* t5 L( \! P: A- ]& n0 z
// systematic bias in the iteration throught the heatbug1 y+ i0 W7 @7 u7 D* i2 v
// list from timestep to timestep
- ?3 s) T4 \( W4 _- [0 _ 0 m2 E; @! a5 q) t2 _, k: G
// By default, all `createActionForEach' modelActions have$ B. @) Q8 N, x K1 S
// a default order of `Sequential', which means that the
. l' X/ P1 V, U- p // order of iteration through the `heatbugList' will be
/ N: |3 ^* @2 u // identical (assuming the list order is not changed
! t1 I% S; o$ G. ~ // indirectly by some other process)." F; Z, ~- w" G1 b* R1 @$ n( S& p, U* j
- t3 H- C( }; |! W0 Y modelActions = new ActionGroupImpl (getZone ());
3 N5 U, s& l5 ^0 V
) x; ` e: s+ s0 ~4 p, k1 _ try {
2 L. ]0 `, N2 {0 ]: N( h( E modelActions.createActionTo$message: t2 L& T" [1 d b- y6 L- a
(heat, new Selector (heat.getClass (), "stepRule", false));* }) t* g3 [/ m( B0 y+ M- b2 ?
} catch (Exception e) {, r9 _% x& y% y6 W' ]" N
System.err.println ("Exception stepRule: " + e.getMessage ());0 P( t) s# u6 Z9 G/ n
}% @7 e8 S8 E7 z/ b3 }: f, X
1 t6 B4 j' @* Y; M! q% e& E
try {% U5 r5 G, f1 M3 V. b+ R5 _
Heatbug proto = (Heatbug) heatbugList.get (0);
" q8 A0 k2 \2 v, \; X$ x( ? Selector sel =
+ N9 @/ D: ~6 v, Q3 C7 |1 A8 V new Selector (proto.getClass (), "heatbugStep", false);
! F8 j! _. Q h actionForEach =
' O' A, C) e7 ?8 c& b' j% _+ | modelActions.createFActionForEachHomogeneous$call
, t9 _' _: X7 {0 B2 _% l& O0 ?" a: j (heatbugList,
: S! }5 V4 s7 J8 D2 g% p% p4 R new FCallImpl (this, proto, sel,
, F0 ^/ H8 ^5 R/ G& u$ {& p new FArgumentsImpl (this, sel)));
& g4 D; h4 p4 t } catch (Exception e) {) Q$ y7 y* e5 l0 i& Z8 M
e.printStackTrace (System.err);) n+ r: d& v' L( q2 J9 a
}1 |' }0 E$ i" Z; H, R5 K
- D; R5 {" `2 F/ h0 `3 z: |
syncUpdateOrder ();% ^( Y0 U& H; ~! Y5 _
' `0 p/ \( f3 o& Y) r try {
( f+ i! S4 @2 N, O2 C modelActions.createActionTo$message & Y+ i; L% m0 c& P2 V& q
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 c4 A, [2 m3 e {: n } catch (Exception e) {5 d* `6 Z3 W; P0 w( p6 f# W
System.err.println("Exception updateLattice: " + e.getMessage ());
& C: @# z' a; Z t+ {$ t } Y" M& D( {) `" b
+ S& w* Y0 y% Q // Then we create a schedule that executes the
- @ |( O$ a8 x$ G // modelActions. modelActions is an ActionGroup, by itself it, W" F2 y. _6 u0 ]* H
// has no notion of time. In order to have it executed in
; ]! C$ A! m% R& }# `) k // time, we create a Schedule that says to use the$ M# Y$ `8 ?, h& `( [' h3 C, P
// modelActions ActionGroup at particular times. This
' S4 h5 S! H% x // schedule has a repeat interval of 1, it will loop every1 ^: |5 Q1 k+ y& a. j
// time step. The action is executed at time 0 relative to
# b+ s+ a4 {( F# b+ P // the beginning of the loop.
9 F$ S0 T9 N5 {, q C+ k
. d0 R6 o' v' \3 b3 M! {- \$ T // This is a simple schedule, with only one action that is
, O m! n1 S6 [6 e; _& ] // just repeated every time. See jmousetrap for more
+ M1 X: C8 A3 W5 M; _ // complicated schedules.+ ]0 b3 \) J- M9 }2 e9 l
4 ^: [" P7 B: j; w" i4 h5 @ modelSchedule = new ScheduleImpl (getZone (), 1);
1 V+ `) c* \ e; J& {5 n9 u modelSchedule.at$createAction (0, modelActions);
2 j6 x! D% z" q! |" i
- c& K# v! q1 @7 m return this;
! X+ b I2 h, k" C- r } |