HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 M3 @5 }/ }3 i
: k5 f( J9 L. J% S/ ~ public Object buildActions () {
1 J: ?5 J8 l6 B3 l- M super.buildActions();
. t3 Y. ]2 V/ k8 l# M/ K& H+ u 8 y; D6 s, D" Q r7 J
// Create the list of simulation actions. We put these in
' A$ d: z1 m: v x. e( I // an action group, because we want these actions to be
+ e$ y+ e9 r' K- ~/ Z6 J& ~ // executed in a specific order, but these steps should
1 \' \4 r' }& D // take no (simulated) time. The M(foo) means "The message: @& c, S0 O$ n9 n2 P
// called <foo>". You can send a message To a particular' O7 j, U, j( w9 }* {
// object, or ForEach object in a collection.4 ]; k+ j E( x$ ~
+ K( [6 {: w; z. w3 @7 c% z j // Note we update the heatspace in two phases: first run
% H# L0 h) G, u // diffusion, then run "updateWorld" to actually enact the, }1 n$ t9 c P, f
// changes the heatbugs have made. The ordering here is6 v O0 n. T5 `: Q+ \ D
// significant!
. a0 ~* O8 B" ~. F2 `9 X
0 B/ a+ P. A, D* K6 A; |5 @3 y // Note also, that with the additional, [& _. Y) t& e: R
// `randomizeHeatbugUpdateOrder' Boolean flag we can
" G/ \7 e) X1 W( g5 g // randomize the order in which the bugs actually run
- X2 y3 m' \0 H& y) s. ]! R; a6 j // their step rule. This has the effect of removing any
! Z" U0 g; i. y // systematic bias in the iteration throught the heatbug
6 L& d: V4 S. c$ P, m! g // list from timestep to timestep! A$ V2 v, h3 [, g2 u
; P* {$ k, G- b // By default, all `createActionForEach' modelActions have; j: ~/ s" k/ |8 [% k# M. `
// a default order of `Sequential', which means that the
/ l' G! X' }9 g d" q9 ~ // order of iteration through the `heatbugList' will be: c c4 [) E5 T
// identical (assuming the list order is not changed
0 d6 ~& E+ c7 F5 [ // indirectly by some other process).
6 c. L1 G( w7 w$ F: y( O % ~5 g) v( f/ K9 p+ r7 Y- E
modelActions = new ActionGroupImpl (getZone ());
% O& ~/ a8 L. ~7 j4 o0 s+ c) G4 I1 c( o( t7 ~1 S- r/ R, {0 S
try {3 n! h% T% ~+ ]* U5 w1 q
modelActions.createActionTo$message2 G, ]3 w# i8 w- ~8 Q
(heat, new Selector (heat.getClass (), "stepRule", false));
" B; d1 j& I" q% F0 H) J& ~ } catch (Exception e) {3 d4 t7 j, O1 ]( f( O1 z) A" n
System.err.println ("Exception stepRule: " + e.getMessage ());
1 Z1 e; Q+ A1 J! _/ J6 K }
8 C0 M! J) u3 l# Z" I
9 m4 A4 y0 A4 a# ?5 V. k7 L3 T try {/ \ W7 K" l! y5 ^
Heatbug proto = (Heatbug) heatbugList.get (0);0 G- a% D. G% ?6 P
Selector sel =
& p; c% H% I9 y, W8 H- f, r new Selector (proto.getClass (), "heatbugStep", false);" B8 ], ^5 v. N- L
actionForEach =8 w1 _3 n) ]% \7 J5 B4 `5 @! {: @
modelActions.createFActionForEachHomogeneous$call
! o3 Q0 K- R( C( H: @& Q" {! O' s (heatbugList,
1 F4 h: N! w, s M# w new FCallImpl (this, proto, sel,6 A1 a# d9 P5 c0 z) ]5 v
new FArgumentsImpl (this, sel))); N9 _6 g. o h1 n8 P* P
} catch (Exception e) {" n/ S7 D+ E0 v8 d3 l( b
e.printStackTrace (System.err);
8 F6 f1 B/ Y3 _' j }* z* y; \0 t- m; r
0 k7 ?+ O3 ]" t7 s) q syncUpdateOrder ();
9 x# @& V% ^; w( y& s( f& s/ @+ ~# z% N6 w
try {6 K4 z3 W8 ?! X9 I1 B4 \1 p
modelActions.createActionTo$message
: u, a) A& A, c (heat, new Selector (heat.getClass (), "updateLattice", false));% q3 ~3 T* _$ s5 e0 f
} catch (Exception e) {, a" @1 w4 \' U A8 w
System.err.println("Exception updateLattice: " + e.getMessage ());/ z! g% L" ?& E" I8 V
}
0 }- \* I. s8 u% Y, T7 W 8 c7 C' I! V" W0 c6 N8 J, n
// Then we create a schedule that executes the
* E1 D+ E' @) m# T6 X$ D( R% I3 a( g // modelActions. modelActions is an ActionGroup, by itself it
3 v. I" k& b& X5 u // has no notion of time. In order to have it executed in
! i) r% i7 d' @' A" S7 ? // time, we create a Schedule that says to use the
7 S- u" t$ o' ?5 [* t // modelActions ActionGroup at particular times. This D9 e6 M- E% e5 ?
// schedule has a repeat interval of 1, it will loop every& P( ?9 `4 u3 H6 z4 G- j
// time step. The action is executed at time 0 relative to
+ o1 {* T/ U: Z: u // the beginning of the loop.
) z$ Y3 h9 ]; b% D
|8 r; S# G: Y$ J0 ] L // This is a simple schedule, with only one action that is
& M6 l1 Y% u7 T+ C // just repeated every time. See jmousetrap for more# d4 |! ]7 n) w2 A0 Z: ?: n# W
// complicated schedules.
# o% h1 h {' k& r; }
2 g: [7 C2 Z9 ]: l modelSchedule = new ScheduleImpl (getZone (), 1);! b$ F5 o0 }8 D: L+ [
modelSchedule.at$createAction (0, modelActions);
) P# o, E5 K' b' C- M0 [ 8 A$ a- G- v& S5 h" a; X
return this;" Q! r* R1 s( x6 c. I8 H* Z# g5 n) B
} |