HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* s9 h! K5 B0 i' F7 u$ C! ~, m
) r2 v+ R N7 Q l O5 d3 Q public Object buildActions () { K; q& ~$ U8 K3 D) @2 _
super.buildActions();
$ T5 d, a6 P3 C' N: L; e% T
" {. ?0 C4 r- M5 T1 r( R // Create the list of simulation actions. We put these in
/ m3 I2 f/ L( o( u0 B" ? // an action group, because we want these actions to be
8 ]' B6 c; a7 m4 O! b' Q5 p6 X // executed in a specific order, but these steps should2 n+ S* W$ d; r& W( l" A
// take no (simulated) time. The M(foo) means "The message" D3 S C, S4 E# s2 D2 k6 X
// called <foo>". You can send a message To a particular
! F8 D4 K1 T: ~ // object, or ForEach object in a collection., d6 g8 U) \8 d6 {& [% w! _
9 G# a- W# x6 q3 u% a
// Note we update the heatspace in two phases: first run
# C! H0 m, r& v& i+ H // diffusion, then run "updateWorld" to actually enact the
, L2 M# t. ]& g/ v3 ^+ Y1 J // changes the heatbugs have made. The ordering here is
/ x. D/ L0 u3 v0 [# r // significant!
$ V: v L! w7 r8 h
~- _; m) D& e8 O // Note also, that with the additional9 a1 E" }6 c/ U! I
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 w' g* v. K' x4 } w0 S' i( m
// randomize the order in which the bugs actually run
0 O3 `! y/ r% Y( T // their step rule. This has the effect of removing any
, b; J6 ^4 x3 ]# G: [2 a // systematic bias in the iteration throught the heatbug
; }- N5 x! o8 p6 t K' I L! S6 C // list from timestep to timestep
+ R5 T5 }1 d5 Y9 X0 ?1 n8 L5 v % _- x7 n( B7 A. ?( G* ?6 D8 Z
// By default, all `createActionForEach' modelActions have
5 R- K. M+ F. b, n4 t( K2 b // a default order of `Sequential', which means that the( R; f F: c1 G8 x( m) t
// order of iteration through the `heatbugList' will be
' O% z! J" o9 W8 k // identical (assuming the list order is not changed9 G% p M3 ]+ B2 }+ V
// indirectly by some other process).2 W. S3 L! I! p; L
: s2 z7 [! e4 `$ j; L" B7 p0 H modelActions = new ActionGroupImpl (getZone ());/ p$ p% h' u( q
. f5 ^: z' d7 d. d
try {
% j* {. X( S' x, M" a# h* i7 \ modelActions.createActionTo$message
2 x0 Y( \$ g9 Q4 @, x* f7 T (heat, new Selector (heat.getClass (), "stepRule", false));* n& A$ G& m; S
} catch (Exception e) {
4 z, o8 x! @4 }% D) |! X System.err.println ("Exception stepRule: " + e.getMessage ());- m0 U4 k0 X% p9 T
}4 P. n S& T1 A0 C
7 B0 ^; |8 F0 X* |, c- p
try {4 P) B( f; H* l6 K: I% L
Heatbug proto = (Heatbug) heatbugList.get (0);, ?) s1 o% A+ E& U& Q
Selector sel = : P& Y" S R1 u6 R# k$ O- q
new Selector (proto.getClass (), "heatbugStep", false);
5 ^( Q1 u& E. U4 L actionForEach =
. C, @. d: ~8 }: S3 D0 p- l modelActions.createFActionForEachHomogeneous$call* F4 m# A, {3 N+ [: z
(heatbugList,9 q% V3 J" A7 E% U1 |
new FCallImpl (this, proto, sel,
5 M+ k$ V7 @( z. J: A6 @" i! K& T new FArgumentsImpl (this, sel)));& G f' V7 f8 ~# E; b( K
} catch (Exception e) {5 f1 g: j s R7 x
e.printStackTrace (System.err);
$ S2 T& N: t. Y* h5 H }
# F8 u9 T7 ~) y7 p4 w# t3 i" W$ M & }$ u$ O/ [+ {6 S
syncUpdateOrder ();* g" c* r. L& A) ~* \* f
& V6 i5 L4 Z4 `: G try {1 F1 g6 f3 ~+ z1 S- v) F# Q
modelActions.createActionTo$message 4 [2 t( x/ Q4 W V! ~7 A
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 x0 a- Q, q. V T5 x- R3 T6 w } catch (Exception e) {
- I1 j! N- N# X% i0 f System.err.println("Exception updateLattice: " + e.getMessage ());
! y) A% m! m' j, B. r! L. F; C }" K' t( V* s: i- x5 `7 U$ Y
! I3 A; d% |( Z8 @: @" r; g9 c7 Z // Then we create a schedule that executes the
4 F6 P1 H2 w' N- q6 V- Y* H! _( D // modelActions. modelActions is an ActionGroup, by itself it
( y' s7 B- Z% |! s7 [3 z) v // has no notion of time. In order to have it executed in
4 I6 d3 k( ?' e Z1 c // time, we create a Schedule that says to use the6 @. H) r' n) }. h3 E
// modelActions ActionGroup at particular times. This. F2 \$ y6 N Z" g0 j7 h! D
// schedule has a repeat interval of 1, it will loop every
9 N0 t/ L3 E c" v // time step. The action is executed at time 0 relative to
8 N( q) `6 i' S" o" P // the beginning of the loop.
6 A- I9 E* G, Q" [& {" k, f: q. I5 X0 N. S" x
// This is a simple schedule, with only one action that is
, ]' w2 i& l& M: ]$ Y+ o$ a; j( Y- E+ z // just repeated every time. See jmousetrap for more
; \1 U6 K- I- F: H ? // complicated schedules.
5 _7 c8 D$ S8 l4 p0 ?+ x2 U& ?
" h% o k7 T5 v5 l modelSchedule = new ScheduleImpl (getZone (), 1);
$ W/ n( Q( P) X4 i9 `. Y! Y9 b modelSchedule.at$createAction (0, modelActions);
" d/ d! i$ X" b B$ V- j4 w
) U+ G& s# y, K! B* W$ f return this;8 y3 h8 r& V+ s7 C5 w
} |