HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 Z: y; m; q- c) F. I9 F. D3 n5 L; V& i1 {
public Object buildActions () {
' d( D2 |) b) u8 P8 I super.buildActions();
8 y2 D, V! z" z. j ' K, @/ E4 a4 E* r0 E' G5 m
// Create the list of simulation actions. We put these in- G4 M. i6 K% t+ O* V' J% ~! c
// an action group, because we want these actions to be
1 O8 I3 V+ g& r& x // executed in a specific order, but these steps should
: [* B6 W1 D/ X8 m+ t( S$ _) l // take no (simulated) time. The M(foo) means "The message
' L1 }9 \6 l5 `" \0 e3 w1 ^ // called <foo>". You can send a message To a particular
2 t* U, \3 [* h3 g3 N# S* n // object, or ForEach object in a collection.
W# }$ h5 Q0 x( ?/ i5 p! s; E- k % y7 N5 e% @" I
// Note we update the heatspace in two phases: first run
0 T0 H" l1 C- R- I9 w5 y7 L2 A // diffusion, then run "updateWorld" to actually enact the
- Z- @6 Y) N1 |0 ^, I // changes the heatbugs have made. The ordering here is
3 e& P. e% s, I& e1 }6 P // significant!4 C! W: j9 S/ E5 E" f6 _6 c
7 j) ?6 u$ `3 r; `! ~" j
// Note also, that with the additional2 J+ z( ^' |! h# k9 S
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 i; f( l t. _) t7 I' ^
// randomize the order in which the bugs actually run
; C- I4 I% ^! N* y // their step rule. This has the effect of removing any- k/ E y2 I2 y& s# B
// systematic bias in the iteration throught the heatbug
0 y# A7 O9 ]( A5 }8 n0 z5 ? // list from timestep to timestep
' j9 c+ K+ U; R/ f2 P2 e ) n9 m6 }! o& m( n9 @
// By default, all `createActionForEach' modelActions have
/ r4 u5 G$ S1 Q; L // a default order of `Sequential', which means that the
9 c0 l% e6 M! z$ O. e5 \ // order of iteration through the `heatbugList' will be
; p; s3 k8 R8 n) c3 Z. R0 |4 T2 c) i // identical (assuming the list order is not changed' g# q' j u5 _
// indirectly by some other process).
: u7 {2 q/ L% Z2 a5 \# S$ G
* ~8 @, u, a& B0 l( j) c( b6 n modelActions = new ActionGroupImpl (getZone ());; x' x# I; M1 o9 A
8 T6 j. ^3 a- i5 F) _0 D0 R7 C$ S% H
try {
5 O6 c! [4 h o modelActions.createActionTo$message
' u. e" F5 [3 T8 P. q/ y. o (heat, new Selector (heat.getClass (), "stepRule", false));9 G+ e0 l' A4 @+ d/ C
} catch (Exception e) {4 N, j1 A% }# a7 O7 C
System.err.println ("Exception stepRule: " + e.getMessage ());
* P6 t; x2 ?# z' V" G }* l% V8 X; @+ z( p" S& M
% s! M4 s! H% c: t) s0 F
try {
9 M! D/ J0 p* C2 c! K Heatbug proto = (Heatbug) heatbugList.get (0);% b" m+ @6 {; H+ X
Selector sel = # r. ~# n& c; M- I
new Selector (proto.getClass (), "heatbugStep", false);
* g! z" y9 I3 o6 w& d actionForEach =
$ k! P1 Q& k) p( C% H modelActions.createFActionForEachHomogeneous$call
5 l. e9 N" _7 T3 s. ?0 n (heatbugList,
/ y' ` _5 P% s# d new FCallImpl (this, proto, sel,
5 e! M4 S& L, I7 j# b new FArgumentsImpl (this, sel)));
2 [* |8 p8 y) Z, | \6 X% H+ U* ` } catch (Exception e) {
# }8 Y: M& `' b G5 k e.printStackTrace (System.err);
- e" M- s& K" S: f- y# \ }
+ I8 Q( E. h) e ~ i2 m" l 0 A& t. ~/ g. a; P. |8 J
syncUpdateOrder ();
8 B, c/ A0 B' {) ]# a2 o- t0 R
+ p+ {0 n+ E$ ~ try {& r# ]. o* N- w# P& n+ z- g$ D
modelActions.createActionTo$message / H8 b. d( W! K6 g0 ^
(heat, new Selector (heat.getClass (), "updateLattice", false));0 ~9 [1 M& O$ c7 r7 g4 J
} catch (Exception e) {
) ~- G& F1 l' @. }7 _1 \2 l System.err.println("Exception updateLattice: " + e.getMessage ());
3 X0 Q. A* M6 E0 I( n- V }
" n2 \- C" F9 `6 A' O, q$ S/ O$ N ' D0 w2 j8 L T6 S
// Then we create a schedule that executes the+ V/ `) h' Z8 z. z% H( R% O
// modelActions. modelActions is an ActionGroup, by itself it. _3 P6 ]' e3 F- L) b$ R' V
// has no notion of time. In order to have it executed in
( `3 r O5 r0 }: S* z# E. l( D // time, we create a Schedule that says to use the3 s4 P9 ~ v) v$ Z: h$ p; Q
// modelActions ActionGroup at particular times. This
3 e+ X' g8 ~# ]) T4 i" s# m: w6 K // schedule has a repeat interval of 1, it will loop every
( b) s( V7 [8 T // time step. The action is executed at time 0 relative to
) E3 E" L' ]- l3 k& T // the beginning of the loop.
, L1 I! g, e4 c( F& \) V2 B+ @4 a, u, C- B! u& X0 ^. b* {
// This is a simple schedule, with only one action that is5 v, t$ e m( h$ l
// just repeated every time. See jmousetrap for more4 F; ?1 i7 p* \
// complicated schedules.- h9 Y Y% m9 ]/ F* K% }" ^' g
& n5 I. Y. d( J* s; j4 H/ j
modelSchedule = new ScheduleImpl (getZone (), 1);! Q# [# p/ ]/ o% ~6 a
modelSchedule.at$createAction (0, modelActions);
* p. E9 @" b: L 7 s: g/ J4 y# a* g6 o4 M
return this;+ T" k' v5 A1 C3 D4 X
} |