HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; u, O7 C1 b' m+ }; W- L0 N/ G, {9 r% B7 a q4 i7 Y4 H$ X
public Object buildActions () {; O& U5 k5 M5 ^2 {5 d% k$ T) E
super.buildActions();
9 V4 B$ {- D% H# n. Y 5 h: F% h) w' e- ^7 J' L
// Create the list of simulation actions. We put these in3 r5 E2 b7 u9 [& I
// an action group, because we want these actions to be
; U B* T- ~5 ?- c% [; G/ _ // executed in a specific order, but these steps should& d* O! t# H( U; I2 g' c% `, }1 }
// take no (simulated) time. The M(foo) means "The message% G% R- s$ |0 i9 a
// called <foo>". You can send a message To a particular
% x9 {& L* \5 ]4 C // object, or ForEach object in a collection.
6 g7 H" k% k g+ s0 j 4 k4 U* L& n: s; L2 `% t& {
// Note we update the heatspace in two phases: first run2 U2 X. I5 H9 K) R
// diffusion, then run "updateWorld" to actually enact the
. k0 @( f$ H, P2 D9 n I f \ // changes the heatbugs have made. The ordering here is J/ \+ V, ^3 {
// significant!
( D6 Y _ ~7 k: n$ [7 w
/ W1 V O$ l6 ~, z // Note also, that with the additional% a+ u/ O$ @3 n0 S7 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; w, k/ C) |0 [5 g$ j // randomize the order in which the bugs actually run& L: P3 G( w2 G" h9 K/ t- A. d6 ^
// their step rule. This has the effect of removing any m3 U2 ~9 Q0 C: ]& \5 B5 E
// systematic bias in the iteration throught the heatbug
& Z8 F2 B! N0 a1 {9 C8 v // list from timestep to timestep6 ]0 q; |0 M# k' }8 m5 Y8 @
; d/ a! s0 x! |1 e // By default, all `createActionForEach' modelActions have8 P- k( l4 y4 u1 {7 |" F7 x
// a default order of `Sequential', which means that the
( {" t+ `+ ?( G d( G, G // order of iteration through the `heatbugList' will be
1 ~- o" W. i) w, C# n1 u // identical (assuming the list order is not changed
; z. }7 h& w/ [1 U* F // indirectly by some other process).
- O/ R- v3 G+ o6 r' \* b0 ]
3 R* X# ^# T4 f/ W$ C modelActions = new ActionGroupImpl (getZone ());" I$ x2 |2 I! A2 ~
4 f7 S7 P) J& X9 x# z3 r
try {. g4 f& C) {, O i. ]
modelActions.createActionTo$message; K" e2 U- _. b* K0 s3 }
(heat, new Selector (heat.getClass (), "stepRule", false));
: _$ v, ?4 x m7 o2 T) }7 H* i9 W7 m } catch (Exception e) {, J/ X* B1 q* W! I5 h" u3 {/ {
System.err.println ("Exception stepRule: " + e.getMessage ());
" {7 y j6 w* u6 o4 F+ P0 _/ Y B/ Q0 ~2 u }+ c6 N( `9 I1 z; [! w) q w
# ?8 M! ]2 ^9 V) M' W5 q try {- C& n- M5 Y/ ~! ]
Heatbug proto = (Heatbug) heatbugList.get (0);
; U' R* G8 [, O6 Z Selector sel =
! X# w% \+ V, a7 l3 y; \5 y* u new Selector (proto.getClass (), "heatbugStep", false);
1 x% u8 N6 b3 z1 T8 P N actionForEach =
! C. X1 _7 {' Y" F* e8 P modelActions.createFActionForEachHomogeneous$call( P3 x( X0 C$ C0 E
(heatbugList,' p" Z; y3 d2 y) Q1 @: n0 H
new FCallImpl (this, proto, sel,
' Q0 `- x9 E% c/ h; s# M& e new FArgumentsImpl (this, sel)));
& H! @0 A! t; @3 t! e } catch (Exception e) {
+ g' j% }- @0 a% O e.printStackTrace (System.err);
7 X7 m9 h1 ?# @8 ] }
0 [) L, h- Q1 Q/ c* ~! C 5 ~0 J0 k9 [# n/ ~+ [9 s+ i
syncUpdateOrder ();0 Y( Z: v/ W% j# P# O
% Y# K% S/ {. }1 B
try {* | U. y5 s. L1 ^( u5 q
modelActions.createActionTo$message ' ^, u& _: i" j' h7 f ]; V& N
(heat, new Selector (heat.getClass (), "updateLattice", false));9 Y: Y- H6 \5 H% g1 p
} catch (Exception e) {" i7 _. J& y" p8 K! o2 d: @2 x5 {
System.err.println("Exception updateLattice: " + e.getMessage ());* W: ?6 l: ^: q& Y6 y! x
}
1 a2 a! e; S) N& r1 }8 z) l# c 1 t# `- l; h* |- ^' E! v* s9 C) g; g! y+ b
// Then we create a schedule that executes the
1 k1 \" Z# D3 w$ P& h5 S // modelActions. modelActions is an ActionGroup, by itself it/ Q. w) f6 X0 ^& Z M3 \
// has no notion of time. In order to have it executed in
7 \0 g- I9 p& Z1 ^ // time, we create a Schedule that says to use the/ M- W& ~3 |: Z8 @- A
// modelActions ActionGroup at particular times. This
! c* g n1 r! f2 Z/ y5 K // schedule has a repeat interval of 1, it will loop every
8 ?. y0 y4 E, l3 n // time step. The action is executed at time 0 relative to) M1 I R; w/ R$ m- i
// the beginning of the loop.+ T( t: g# K! d. l) ~( p+ A! q
% Y4 @" T" K( J% F
// This is a simple schedule, with only one action that is
6 r* u. ^0 u+ B% v) s$ V // just repeated every time. See jmousetrap for more6 Y$ R2 _9 Z0 O9 J: N* M* h, Y
// complicated schedules.
/ I4 W3 v" h, E* J - H. O, z' a$ L% [: Y* f, @- q
modelSchedule = new ScheduleImpl (getZone (), 1);) f$ v: t6 {; |' E
modelSchedule.at$createAction (0, modelActions);( v* O' J$ F; y: `. n
5 y# o! [5 [4 z$ Y" ^
return this;% z5 b) ~2 s2 f) W
} |