HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 V- M3 B/ ?! |# q8 E% x
; J" L3 O, a `( D, t
public Object buildActions () {
- }8 y$ `) ^: c8 W' @ super.buildActions();
0 M2 C1 X0 I7 E1 k; d
! c |( P- _" D) ?, z // Create the list of simulation actions. We put these in( U& h/ U& ^5 O5 G3 o# L/ g
// an action group, because we want these actions to be! j, R( ^- d( O/ \1 P, x) c
// executed in a specific order, but these steps should, E/ O: b. m; _
// take no (simulated) time. The M(foo) means "The message
5 _1 Z- c" c3 I% v w // called <foo>". You can send a message To a particular G9 V, v" _# k* v( T
// object, or ForEach object in a collection.
5 L/ k& K K z4 J+ X ( O1 F) R q# H' ]) ?& P
// Note we update the heatspace in two phases: first run
. o! v1 o( I. m* I" f6 W/ q1 h) w7 C$ J // diffusion, then run "updateWorld" to actually enact the. p$ v' e3 x. a/ A4 T0 L
// changes the heatbugs have made. The ordering here is
C- ?, ~% M. c. x // significant!+ ?, J3 |4 L% R/ s9 a. N
* {) i- a6 W" M7 W8 S; W
// Note also, that with the additional
" `# u) @ {% F& p- R# T // `randomizeHeatbugUpdateOrder' Boolean flag we can! [ E' @- d% M, p
// randomize the order in which the bugs actually run; H+ ^9 }1 j+ C+ ^. d8 l3 d6 D
// their step rule. This has the effect of removing any7 | D: {* R9 g# s% O* g9 C* o
// systematic bias in the iteration throught the heatbug
4 Z7 @- S4 c: j; M // list from timestep to timestep
3 e& G/ t0 c/ T& B/ J1 T9 }
/ J! H) ]& P- `9 O" \9 E // By default, all `createActionForEach' modelActions have" `% A( T( J. C
// a default order of `Sequential', which means that the$ W3 g' \* C: l* n7 l
// order of iteration through the `heatbugList' will be3 C1 K D# `) d% B/ `2 P* V
// identical (assuming the list order is not changed# S! }/ T: l* T3 ~
// indirectly by some other process).: X& V$ [* A- Z7 D3 i" e! w
* S8 S6 p% Z, [ modelActions = new ActionGroupImpl (getZone ());' k% N5 ?4 _2 `3 E. P& q+ D4 P
2 k3 w7 I! u0 q* e4 _ try {* p' H" T- B3 i3 V1 ]
modelActions.createActionTo$message
) t. L8 I' e; o+ x2 o (heat, new Selector (heat.getClass (), "stepRule", false));6 ?& t0 N/ m2 w+ @0 _4 P# T8 f
} catch (Exception e) {
8 v$ K% h/ c' J# d# {. c" A System.err.println ("Exception stepRule: " + e.getMessage ());
2 V) i, H7 z2 E- S, I }1 d+ v) h( N+ q( i% W7 f
( i |: l& J8 r
try {. Q9 Z2 b) z8 q7 d2 D% q
Heatbug proto = (Heatbug) heatbugList.get (0);
7 j0 D( u. t) Y6 x" \& X: S E Selector sel =
% O2 F- O7 z* p e8 N new Selector (proto.getClass (), "heatbugStep", false);* R2 }6 c3 ~4 Z1 \1 {
actionForEach =& x7 h/ e- E& y& t/ e& ]" c
modelActions.createFActionForEachHomogeneous$call
1 x$ u, f' B. T7 }: x4 [- @ (heatbugList,% P) ]) l3 l5 K% t) G4 S
new FCallImpl (this, proto, sel,
; Q, }1 }- Q" j& a! r% t; @ new FArgumentsImpl (this, sel)));9 Y- F; i6 \# q' }& P
} catch (Exception e) {9 y7 g4 U5 |9 k j6 G
e.printStackTrace (System.err);
- }$ e2 f! a, j* m$ s" P+ V }
- \) P K0 T0 N; D. u) i. n0 W& L; o 3 z5 Q1 p( ]7 Y, n# f
syncUpdateOrder ();
1 K0 `7 Z2 y, r7 u' ?
/ k2 h% A( p0 ^2 N9 a try {
$ u3 ~0 h$ ]8 d" R3 c7 G modelActions.createActionTo$message
3 D' Y; J( T" @# D3 q8 c. l+ j (heat, new Selector (heat.getClass (), "updateLattice", false));* K$ p3 O2 ]( W. M( I4 K
} catch (Exception e) {
0 H. l( g0 `9 g System.err.println("Exception updateLattice: " + e.getMessage ());
) M0 Z/ d9 m }: _) o+ x# W0 V% X }
& U* H! M, i0 t; F7 E
: G n% I+ S" {0 K1 Q& g- r3 `* T // Then we create a schedule that executes the
q0 A# F/ G5 L // modelActions. modelActions is an ActionGroup, by itself it
8 [& q" C% B5 H3 K H! V. Y // has no notion of time. In order to have it executed in2 l& K* y- G7 u) W; i: t
// time, we create a Schedule that says to use the4 D9 S& O D2 E- A
// modelActions ActionGroup at particular times. This4 H8 X& y E( {4 w1 v$ u% E, C
// schedule has a repeat interval of 1, it will loop every! k% I# g7 r: A4 v' K- t' y' ?8 E, p
// time step. The action is executed at time 0 relative to
) O7 w; h* Q6 ^7 \( g: L# n // the beginning of the loop.' g' h/ F$ O$ i7 ?7 Z8 J
4 J( x, z7 Q! Z- ^3 ] // This is a simple schedule, with only one action that is
! H0 _1 n, S) V, W0 A // just repeated every time. See jmousetrap for more
$ t. ?9 u9 O. t- D& Z1 c' W // complicated schedules.+ M( G7 H! }5 f! o1 ?
0 ]; T3 T7 Z1 r) x: j8 I modelSchedule = new ScheduleImpl (getZone (), 1);! k. P/ F7 v/ ~! V
modelSchedule.at$createAction (0, modelActions);9 e6 v4 c( r/ U8 p" H4 E
6 g$ T& o8 R6 y& T d2 ]$ A4 C% j
return this;* _) G N; f" Q. r2 W, c* C4 F7 k
} |