HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- b4 Q# Q& ` U* a
) N5 j1 N$ R6 M; ` public Object buildActions () {
8 f5 m4 X5 f+ I$ u% p super.buildActions();( r; X" a+ T4 D' @0 h6 f
b& m: X. r: n // Create the list of simulation actions. We put these in# @0 A, `6 u* o, a
// an action group, because we want these actions to be
) j! F" c+ A8 g. }( S5 n // executed in a specific order, but these steps should# z/ [) P! k3 N5 r
// take no (simulated) time. The M(foo) means "The message
& H# Q9 {8 X; z/ O/ I3 t C // called <foo>". You can send a message To a particular9 B' p, h- t) X7 M2 s
// object, or ForEach object in a collection.
1 u$ m( d/ {8 h* A! U" S4 f; d 1 h, t4 [" P1 \% `+ x j, q
// Note we update the heatspace in two phases: first run
9 H4 u' J& E! f9 n- n // diffusion, then run "updateWorld" to actually enact the
& e# L2 Y0 o- d$ ` K# Q // changes the heatbugs have made. The ordering here is
+ }7 S6 i p" Z% ~! v // significant!
, b( ?2 F. n3 z3 O Z( l 6 t: i" s* x' N7 [: v
// Note also, that with the additional6 d+ z' `$ R7 p$ ?6 c- V
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 h& y2 v- J+ o) t3 i$ \1 u // randomize the order in which the bugs actually run) O7 G! t& Q0 a9 W1 s
// their step rule. This has the effect of removing any! G" ]2 B6 J5 ^6 v4 @3 a j
// systematic bias in the iteration throught the heatbug$ |- i* V0 d+ }1 B
// list from timestep to timestep6 ?) ~. v: k# T3 _7 ?
& _* X4 [% `' Q
// By default, all `createActionForEach' modelActions have% H' |$ W* `6 V1 }9 i: K
// a default order of `Sequential', which means that the B( {2 y2 t" r9 k2 Z! W3 e8 E
// order of iteration through the `heatbugList' will be% e0 v+ f% E' X; _, P4 e
// identical (assuming the list order is not changed
! k6 Z K/ F! d! [# z' o // indirectly by some other process).
; v! B D; o0 A! }) ^5 o 8 V( g* B5 y; e* m
modelActions = new ActionGroupImpl (getZone ());7 h7 M4 i# d- s0 x
8 T; E# c4 b0 h4 D try {
c8 Z- Q4 G. r; R% T1 _ modelActions.createActionTo$message$ n6 v# ], Q6 k7 B: a
(heat, new Selector (heat.getClass (), "stepRule", false));! y2 D& ?3 E6 J( N
} catch (Exception e) {
, B g7 K# f- s# |/ Q4 R System.err.println ("Exception stepRule: " + e.getMessage ());
- Y: r7 J) Q9 y8 M( s1 b; S& j }7 a- q/ r% \* q) B
+ J1 Q6 e- l1 i- Y6 A) ]3 l4 \+ j try {
5 Q( c. k' I/ h! o( s5 D" l Heatbug proto = (Heatbug) heatbugList.get (0);/ @5 z: j" b; W. `3 `0 U
Selector sel =
3 p0 I3 k% N- @ p0 T1 ^+ I new Selector (proto.getClass (), "heatbugStep", false);
3 h! K+ X3 r8 \+ j actionForEach =2 g; T9 r6 m a! u; a: G
modelActions.createFActionForEachHomogeneous$call- e2 w& _% A1 B8 M7 H' N* C
(heatbugList,5 d! e; ^9 ]8 E& @) Y8 E$ ^
new FCallImpl (this, proto, sel,
9 c3 K. X/ ?0 y: O3 P/ `, f' t new FArgumentsImpl (this, sel)));
/ {3 ?5 u" p, x+ \" i% \7 ]' \- p } catch (Exception e) {. t3 L: l4 {! m3 h- a; y
e.printStackTrace (System.err);
+ f) U. M2 y9 d }
4 @: O% k H& ], r' t% f( W 4 m6 Y/ M* s- c1 e5 u) l
syncUpdateOrder ();
% \; Q" e9 j. I, z; Y2 h
, W% G) j' y0 { T" Z* d5 ? try {, y8 P; b$ L7 z, {/ X2 J7 M
modelActions.createActionTo$message
K% z K: n5 M3 v5 \: [( k* V2 z (heat, new Selector (heat.getClass (), "updateLattice", false));
& w; R' h1 ]5 f) H5 F& O } catch (Exception e) {2 r( I( a4 X X$ D
System.err.println("Exception updateLattice: " + e.getMessage ());
$ P+ L {8 N2 Z+ Q5 e }
% I' y; X( T/ ]5 S
( ?& o* K, M, B. V$ S // Then we create a schedule that executes the+ D( {+ s" q0 F1 n5 Q. X0 X% L5 t I
// modelActions. modelActions is an ActionGroup, by itself it/ m( |2 m, i: _; C2 x
// has no notion of time. In order to have it executed in
- W! _6 v, m- ? W. Y$ c3 p; m // time, we create a Schedule that says to use the2 r0 F7 G' }# _1 R
// modelActions ActionGroup at particular times. This
d4 d: S& y! \5 R P( X' a! u1 i // schedule has a repeat interval of 1, it will loop every" z7 v" Z& V0 \$ [$ |
// time step. The action is executed at time 0 relative to+ \8 w4 Z2 J5 [0 `
// the beginning of the loop.
% Q7 y h. A6 |& U3 c) x/ J- h0 |- r( U
// This is a simple schedule, with only one action that is
. u. G0 I" }! T$ M" Y8 o' x8 ^( { // just repeated every time. See jmousetrap for more
. g7 H9 Q# f% ~& l1 A7 }1 o. g // complicated schedules. N, R: _* G( _/ k
- m' \* t1 i! K% ]* Q4 V modelSchedule = new ScheduleImpl (getZone (), 1);4 O. r# V p0 P1 q/ s
modelSchedule.at$createAction (0, modelActions);. k8 T6 c' w0 E* n$ x7 q2 V, d3 T
/ p& x4 K$ b, c$ _1 T* H/ z
return this;
& p8 M' W& }* G. Z) z } |