HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" T0 ]& P G0 w6 h% ]& x. p- k3 p9 `9 R
public Object buildActions () {
" t% b6 @8 H8 y, J2 _8 c super.buildActions();# ]8 m: o: j- I
! B {* h6 A! m: c' s! ]5 n- O6 p // Create the list of simulation actions. We put these in1 s' N- ^& S5 @! Q
// an action group, because we want these actions to be+ v+ q$ S% _1 X3 \) w6 t- c
// executed in a specific order, but these steps should3 B( R( q, C: D. G; n% n
// take no (simulated) time. The M(foo) means "The message2 L9 t, P4 j- T" n9 r
// called <foo>". You can send a message To a particular
G7 c/ l* \9 g: t0 o // object, or ForEach object in a collection.
% A9 m1 N! d6 ?1 p # t8 s$ r4 G; O3 `7 M
// Note we update the heatspace in two phases: first run1 m% j" [3 h: H
// diffusion, then run "updateWorld" to actually enact the+ e5 I4 T& [5 V6 O( S2 G
// changes the heatbugs have made. The ordering here is
2 ~4 F- D2 g# J // significant!
f2 h2 J# N$ ~* N$ U G) }+ {4 c' p) B3 \
// Note also, that with the additional4 v0 w& w* ]* Q8 i
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ a$ G+ b2 W5 b: U8 ~, S
// randomize the order in which the bugs actually run
1 a, a- \; B# _( I/ ~ // their step rule. This has the effect of removing any
# J+ G: V$ q( Q+ ^0 _ // systematic bias in the iteration throught the heatbug, o& ?' H+ ^; [. [- ]
// list from timestep to timestep
) [2 r7 F. t. Q" s d' i# w5 ]* j9 B5 V8 t
// By default, all `createActionForEach' modelActions have
% U9 O! B) v% I2 b5 } // a default order of `Sequential', which means that the
" {- M4 ]/ J; y1 J // order of iteration through the `heatbugList' will be) W! G6 f$ Y8 e n$ P
// identical (assuming the list order is not changed1 a/ T2 h5 Z% q" M1 c2 O1 I
// indirectly by some other process).
4 ~* h0 D u9 i8 C/ J7 w0 G# f" a
' ^3 p% Y" S: G9 O- n3 j modelActions = new ActionGroupImpl (getZone ());! B3 W8 Q$ f3 y) \4 r7 l
0 _8 l/ f9 B, h. S) y
try {
7 j# X* } Y* a modelActions.createActionTo$message
x4 h5 r4 i6 i8 F (heat, new Selector (heat.getClass (), "stepRule", false));. H. e& Q% q& M. ?9 e* W
} catch (Exception e) {9 ^* C# s9 r- O+ `) i& [3 O: @# o
System.err.println ("Exception stepRule: " + e.getMessage ());
4 z: Z4 l% k- M" }9 W }2 s. _7 h( I0 K! [: |
1 _4 b3 S" T$ ~3 k/ f; H try {
2 W9 q) Z) L/ z% Z5 I# A Heatbug proto = (Heatbug) heatbugList.get (0);
, {" E% ^' b' O1 I" C1 c8 l Selector sel = : f, _+ p4 i; c) z. T5 j$ l
new Selector (proto.getClass (), "heatbugStep", false);' X. \$ ] ~- ?- T$ K* }3 s
actionForEach =
. `$ ~8 a9 T* h modelActions.createFActionForEachHomogeneous$call& n9 y, f9 J/ q7 A J. L) A0 X4 l
(heatbugList," j2 w; C# C0 }& x. }( b& A4 ?
new FCallImpl (this, proto, sel,
0 i8 B+ u' E/ j& D: c0 C6 h new FArgumentsImpl (this, sel)));
3 a4 m+ E0 M5 [1 k n } catch (Exception e) {
7 K9 N' D2 M; V8 H e.printStackTrace (System.err);
+ Y3 V4 R+ [2 S }( I' o2 L9 O2 ^8 y/ _2 R6 K) x' m% T
/ L6 @( {! l8 W& x syncUpdateOrder ();
1 r$ ?5 v a0 ^
& N" t! C3 g. y4 N- {* n ]# J try {7 \" a3 O- `% a& d) \, x2 e* i. M2 x. g
modelActions.createActionTo$message 7 h, \6 P! v% ]
(heat, new Selector (heat.getClass (), "updateLattice", false));. M6 m7 H: t, G" w, J/ `/ M8 j
} catch (Exception e) {
& ]7 m/ }* P7 O. G* v9 s; N System.err.println("Exception updateLattice: " + e.getMessage ()); Q+ k5 e) T$ {: _! E3 F
}& V! s$ @- h7 t& e; y' O u7 f
. J) }- H! y2 B // Then we create a schedule that executes the1 A$ _0 ]' Q. P/ K; b w" e
// modelActions. modelActions is an ActionGroup, by itself it3 ^4 _& b; T( Q: n0 q) A' P9 N
// has no notion of time. In order to have it executed in: E, Z. w* J8 v5 M! q1 g/ x b
// time, we create a Schedule that says to use the
1 s/ Z; H D' O' m6 c0 w* ? // modelActions ActionGroup at particular times. This
; g; E% ^; p+ ^+ O // schedule has a repeat interval of 1, it will loop every
?# I3 R x& @* }9 i // time step. The action is executed at time 0 relative to; |3 V9 Z; m: B+ y5 j6 t
// the beginning of the loop. ?7 {8 Y* r. A7 o: z
/ p+ `2 Y/ \2 R- {. \$ k4 ]
// This is a simple schedule, with only one action that is
1 n3 t$ N- @: @7 F8 ~ // just repeated every time. See jmousetrap for more9 V5 y m) Y3 ?5 V9 M' b7 f
// complicated schedules.
. f( u8 j% z5 t
, s0 u" r v# u' V$ W modelSchedule = new ScheduleImpl (getZone (), 1);9 B7 v4 H. {1 s: q) ] s! Y% x7 U1 {. q
modelSchedule.at$createAction (0, modelActions);4 q6 e% ~) U: F
+ z' H2 Y- ~4 o* y+ ^& w# ~
return this;
1 H- u: B% a* ^5 { } |