HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 J5 L+ m' z' q5 @7 C+ a/ u0 n; F6 S1 G
public Object buildActions () {2 @5 U& K, d( H' K y" C
super.buildActions();
; u3 p3 U: J- f! n& x3 I
) d7 t7 Q+ y" V) J* Y // Create the list of simulation actions. We put these in) Z" k8 W3 B8 M" q
// an action group, because we want these actions to be
) w: l1 y3 T M! j: s // executed in a specific order, but these steps should
* k) e" ^* a- y. o // take no (simulated) time. The M(foo) means "The message* M; y- t) Z7 ~$ O
// called <foo>". You can send a message To a particular: W3 k! h i" }% w
// object, or ForEach object in a collection.) f$ W' C4 Y7 p- z3 i& K
! P$ ]( u& @1 B H9 j // Note we update the heatspace in two phases: first run
! u. Q% c* I1 V. @' B // diffusion, then run "updateWorld" to actually enact the. [. i' Q- }6 @. C3 L- v
// changes the heatbugs have made. The ordering here is
$ Z/ P: f! E& E* H // significant!
2 \, x- g4 h* n# O& q- o5 E V* R ! ~+ Y+ P4 h7 X5 F1 |) n# n
// Note also, that with the additional2 @5 P$ o4 C% ^, g" C
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- j0 a, Y4 L4 F! |1 j; m6 P1 t" x // randomize the order in which the bugs actually run; B3 m2 @4 L. G0 ^: J; G9 d
// their step rule. This has the effect of removing any
0 X* y$ Z. H8 R9 \$ b // systematic bias in the iteration throught the heatbug
8 m j: P) i' W# C% ~3 j4 S // list from timestep to timestep
$ A. }5 c! R0 I3 r" r3 H' e3 [ . o0 O7 m2 N& B* c: A; Y: ]/ n! g
// By default, all `createActionForEach' modelActions have
# O( D# X- B) r9 j4 r4 G // a default order of `Sequential', which means that the
, ?. z! w2 _7 E5 b. t, J' _ // order of iteration through the `heatbugList' will be
" _) a- K0 |( W- r8 Y( M9 m6 K+ @% A // identical (assuming the list order is not changed
, _8 V# l$ ?; {, c+ K // indirectly by some other process).9 T+ \/ d6 K C% e. J4 a
& w' F$ V8 q+ `# w" t
modelActions = new ActionGroupImpl (getZone ());# V9 o" f& \- Q9 n# M2 S! E0 V
! N1 n9 ]# w" B try {
( s- w( F( a2 w: G modelActions.createActionTo$message
2 _4 s: _% P0 @7 D- R3 Z (heat, new Selector (heat.getClass (), "stepRule", false));: Q! C% F0 B+ a& t
} catch (Exception e) {4 l. ^6 k9 y( L) F8 h/ a; T/ G
System.err.println ("Exception stepRule: " + e.getMessage ());
6 \4 J6 o; [* Z X4 u. j z( \ }8 ?8 W6 O' P4 |! E8 [
9 @+ u" J& t6 p& \& j7 i4 ^
try {
3 g( F/ j) k! b" ] Heatbug proto = (Heatbug) heatbugList.get (0);, H; f8 P* I! l j& q
Selector sel = # E4 l: K7 |; O: z& J
new Selector (proto.getClass (), "heatbugStep", false);
3 h6 ?9 L! B; I* z( h actionForEach =9 @' B! P3 T7 C4 E) ?
modelActions.createFActionForEachHomogeneous$call! ?! h- J2 L4 f. Z$ [$ \1 h# w
(heatbugList,9 L9 a$ h/ f4 M4 f8 h; U
new FCallImpl (this, proto, sel,
1 ]: n# G; u7 a) l9 r new FArgumentsImpl (this, sel)));
1 k& P& K% [; d" z } catch (Exception e) {
* j- y! U. }% j) Y q! u e.printStackTrace (System.err);3 }8 f$ M. }6 i' H1 g) s6 T
}, t7 i3 x& p5 M9 ~9 m0 j `
E% F2 }& E/ p. p/ A* h) s& k syncUpdateOrder ();
, s# z5 D g ?: o- u5 e4 x
9 t7 V; i4 Z, O- [9 w, W" O. H( ]; b try {
" ?( N g' _( ?5 f8 h/ y modelActions.createActionTo$message 4 k' j( q9 d9 H6 L ?5 u
(heat, new Selector (heat.getClass (), "updateLattice", false)); x! s2 C. [3 [! S/ A: w3 }
} catch (Exception e) {
0 G/ Z: y; H. E8 {: e# j: c4 ], C System.err.println("Exception updateLattice: " + e.getMessage ());
; k( G1 J/ _1 U9 g) T }
! \# F K5 u- R1 O4 B5 k1 K% Q
% f' R( P6 _8 `" W, \ // Then we create a schedule that executes the1 B4 P. J- @! C+ M' d' z
// modelActions. modelActions is an ActionGroup, by itself it
; `& A: d& E/ A k0 U$ a( t // has no notion of time. In order to have it executed in
! A9 K/ r. @( E' g // time, we create a Schedule that says to use the
( j9 k- ?- S# N( {$ @$ H // modelActions ActionGroup at particular times. This2 W3 J( g1 \' `" B3 f' s! j
// schedule has a repeat interval of 1, it will loop every3 E3 a& H4 D' w$ p4 J5 C4 }8 Q
// time step. The action is executed at time 0 relative to
$ }2 @& s$ l# g( X+ b2 ^3 F // the beginning of the loop.
% E4 \! I0 Y9 k- j/ F* m
' b [/ X% u! @ // This is a simple schedule, with only one action that is+ j. M9 J" J% Q( z
// just repeated every time. See jmousetrap for more
- s0 S- c9 q- P // complicated schedules.
0 {) Y8 i3 w; S0 a! G
* y4 X1 L: l! ^ modelSchedule = new ScheduleImpl (getZone (), 1); }- }+ v! r! c2 w- S) d
modelSchedule.at$createAction (0, modelActions);+ d, o# k* N1 G2 I+ U# ^% Z& u% ^+ @
: C! t) Q- \; l! |: K; F4 T0 S8 ^$ [
return this;
2 N" E" V7 n' h0 n } |