HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, E: W! O8 ?$ X8 f
$ Z( t( p& i' Z public Object buildActions () {' J+ l( m, X# a
super.buildActions();) I$ Q- D9 j! D8 R: z
. M2 y i7 i/ G& Q // Create the list of simulation actions. We put these in8 O0 r Z; @4 h" H
// an action group, because we want these actions to be b6 w( R7 J* D X" O6 c
// executed in a specific order, but these steps should$ d* r$ v+ u, f& \& t
// take no (simulated) time. The M(foo) means "The message
2 Z0 K0 @9 A2 k+ Q, x5 d9 D9 R! u // called <foo>". You can send a message To a particular+ h) y3 ?" R+ O" n
// object, or ForEach object in a collection.
' }4 B; Z' [+ m6 `& m# i : j {1 s/ b( \6 R3 L' A$ K
// Note we update the heatspace in two phases: first run
" I( O) E8 {& e8 H3 ~/ ]# j. ~ // diffusion, then run "updateWorld" to actually enact the, e2 c) M7 ?) W0 s
// changes the heatbugs have made. The ordering here is
; F% D. }0 r2 |6 q) K6 Z1 t // significant!
( u' s5 |; e" f& z" b; F+ t X1 ]% u: E) e
// Note also, that with the additional: Y0 W `! S1 `3 D8 g
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ V+ ]8 f) Y; F3 F // randomize the order in which the bugs actually run
3 _" [0 o3 f! C' v // their step rule. This has the effect of removing any
4 }4 g, Q' ~% R9 e2 f/ ~& e$ o f // systematic bias in the iteration throught the heatbug
' ^0 a8 Y& z. w8 R8 K# u% k // list from timestep to timestep0 i" _: l( j6 |; k/ k/ a1 N
% J1 L v- r* p+ r( _ // By default, all `createActionForEach' modelActions have/ I. i6 L1 [( @) e$ A
// a default order of `Sequential', which means that the- d& k' Q* l( Z' l+ x
// order of iteration through the `heatbugList' will be
. p' e5 F$ h1 M, S9 u // identical (assuming the list order is not changed1 x2 v* q0 H$ C) V, F
// indirectly by some other process).
8 w8 h, O+ e d/ E7 G! U8 v' a / @6 ]8 ^% p( O- j B
modelActions = new ActionGroupImpl (getZone ());0 A5 C0 M( ~5 F% g* T |
5 B4 S; T d. N- _ try {9 m+ A& m+ ~$ \. X g+ ?+ i
modelActions.createActionTo$message9 u: L6 w. K% s
(heat, new Selector (heat.getClass (), "stepRule", false));$ ^1 Z, s- [4 T& \2 ^$ P
} catch (Exception e) {) Y! P' y+ l- }: K
System.err.println ("Exception stepRule: " + e.getMessage ());
" r# b: p! c4 c8 |2 ^( z1 ] }
" s3 E' p! m5 m) z+ u% X" G6 |. i. ?3 o4 o$ S% V
try {6 ]* |. s# O0 L' I8 C4 g
Heatbug proto = (Heatbug) heatbugList.get (0);
# `/ L8 n& r+ R4 x3 a6 e Selector sel = ( c/ b! V; r9 b, G
new Selector (proto.getClass (), "heatbugStep", false);
6 J6 @1 _6 r9 t$ I5 V. Q, l actionForEach =9 c4 `( L8 E D) d. `
modelActions.createFActionForEachHomogeneous$call$ s6 M7 H5 |& ?" ~' o( Z
(heatbugList,
/ U' J- j4 H2 F new FCallImpl (this, proto, sel,& ?$ _7 P8 ~4 B: s3 W4 W
new FArgumentsImpl (this, sel)));! C8 W7 j1 H1 O0 _3 \9 v
} catch (Exception e) {/ y; `. Z! D( ?6 N' Z- v h% D4 z* _
e.printStackTrace (System.err);
2 \# A- \# Q" s. F4 \ }
4 d+ M9 g' _0 h
1 {( p A* {3 p6 h C syncUpdateOrder ();
0 f" r# m/ u" c5 I4 w4 E
! c, h6 C9 |& b0 Q) C9 ?3 |" e* h try {
2 `9 W0 K' c; q0 J+ v( S) y: B3 S modelActions.createActionTo$message
' A1 K4 x0 P2 y2 L$ G y (heat, new Selector (heat.getClass (), "updateLattice", false));
: L4 L$ l4 g1 ?4 R: w } catch (Exception e) {6 F3 k9 S4 z9 `" r1 v5 t
System.err.println("Exception updateLattice: " + e.getMessage ());
% J% {! A. o- C7 h }( h2 t# U/ O1 D- q2 b; s
. I: U0 K; P& \1 h2 c! Z+ O3 n8 \ // Then we create a schedule that executes the* C( @, n" H/ i: X; |
// modelActions. modelActions is an ActionGroup, by itself it
: W; b o7 Q n* {1 c) }/ q ~ // has no notion of time. In order to have it executed in
) ^3 K0 {9 |. h% { // time, we create a Schedule that says to use the) c. s5 \/ t" _. ]/ R0 @
// modelActions ActionGroup at particular times. This
' l8 E/ Y- [+ i% \ // schedule has a repeat interval of 1, it will loop every
5 M. ^: I+ x2 l/ g0 j5 u, F // time step. The action is executed at time 0 relative to9 @0 d1 a- u5 g, {2 D. U
// the beginning of the loop.
# W: z9 [. [; F2 C6 A
- p/ |5 S; x& g, }) X5 [ // This is a simple schedule, with only one action that is
8 x9 d% u) o0 W9 Y% J# u+ I // just repeated every time. See jmousetrap for more& h5 X2 ~5 Y& W3 {7 }4 C5 h1 t! d7 P
// complicated schedules.
; C/ A0 i) i, U4 B+ ? n5 w ; J) o; z. |) Q5 r2 I
modelSchedule = new ScheduleImpl (getZone (), 1);+ z3 J( T9 f( @4 G; R0 V
modelSchedule.at$createAction (0, modelActions);
, l+ }) q+ A2 K % q7 z4 U2 K0 U9 e% I; _! I
return this;
+ ~- G: d3 B" a- q } |