HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 j+ b' O r1 S8 r$ z3 w
% c! H# l; Z! b public Object buildActions () {
0 s1 D# U% ?2 i super.buildActions();! S. k3 Q, j, {
( q( ?/ D3 u: Y9 N2 z* C: f& l& A$ b // Create the list of simulation actions. We put these in, Q! w9 W. x, O/ i& s( m5 _5 V
// an action group, because we want these actions to be
! O0 H$ U( q6 P5 G. C // executed in a specific order, but these steps should# o& ]0 l; P4 l) ?
// take no (simulated) time. The M(foo) means "The message, @+ S- z h# _7 q
// called <foo>". You can send a message To a particular- a) c1 ]$ k5 V! |4 c2 \7 K
// object, or ForEach object in a collection.
2 K3 z H% Q7 b8 E) q . k0 F) l u; z D6 T' r
// Note we update the heatspace in two phases: first run
4 h' } h% b" e // diffusion, then run "updateWorld" to actually enact the h* I" v! u$ y/ w/ ^
// changes the heatbugs have made. The ordering here is- z/ k- \& h& ?: p' b
// significant!0 X! M$ w4 Z O% `* Y% W7 k& C/ W- B
# J# d3 ]" V! `+ K. k0 u1 f // Note also, that with the additional5 J+ w' H3 P, b
// `randomizeHeatbugUpdateOrder' Boolean flag we can# m6 o) a/ v* u9 V4 e: c2 R
// randomize the order in which the bugs actually run% G _1 c8 H. x; @7 t/ }! c3 @3 S
// their step rule. This has the effect of removing any
8 Q7 m2 g! V1 v2 x5 u- c% P, n% v7 Y // systematic bias in the iteration throught the heatbug! c+ G) Z6 n5 x* T1 I- H: x
// list from timestep to timestep6 j2 o5 X. t c
+ B, [: e9 G: u4 o8 R
// By default, all `createActionForEach' modelActions have/ x2 `& P, X. k0 G% i
// a default order of `Sequential', which means that the
5 s, O; B7 }$ [- U# ~ // order of iteration through the `heatbugList' will be2 c4 e2 W5 Y2 h* k- ^9 q
// identical (assuming the list order is not changed" X2 |% V6 w# r/ F! x. J% i) e
// indirectly by some other process).
1 _, u$ M& c/ |1 ~- G4 Z- S: E
5 D* j, ]( n$ T" X; m" _ modelActions = new ActionGroupImpl (getZone ());/ s% b" v9 J: |: L4 A( @ s
# g) Y i* r9 c' }
try {
. ?8 j& Z" Y$ g; K8 Q3 f- ` modelActions.createActionTo$message
+ e. Q% h+ E. T5 W& m4 ` (heat, new Selector (heat.getClass (), "stepRule", false));
- N( x1 I9 n8 g- }5 ] } catch (Exception e) {
. K. D+ s+ ]2 K% O( r System.err.println ("Exception stepRule: " + e.getMessage ());" |! |3 p( ]. S: o2 v. |
}6 ]' g+ u* i0 h1 _+ p3 Y
+ q) d8 g. A6 a }4 b2 {% I
try {& o1 W! I/ [2 y% v. ]7 p" }
Heatbug proto = (Heatbug) heatbugList.get (0); {3 E0 H/ M0 x8 z" r& ~
Selector sel =
, ^8 r1 _9 _* E5 G1 G$ I; p2 P; u new Selector (proto.getClass (), "heatbugStep", false);
( z% d' j! b3 e" |% Z actionForEach =
4 a: H2 I2 x. T7 h modelActions.createFActionForEachHomogeneous$call1 E4 [7 a( _* W! T
(heatbugList,/ w) b" f" b$ e, @
new FCallImpl (this, proto, sel," q4 p8 [2 i1 }5 W h" I
new FArgumentsImpl (this, sel)));
5 P0 ?( X* h, c+ ?% U) u( J } catch (Exception e) {
9 O! @3 d3 |% A* F7 X6 T N# z e.printStackTrace (System.err);
9 _1 Y. Q: `) { }
4 O0 x G( }' v, q; Y
) p2 V- N- l* R0 B, Z4 s4 [. a syncUpdateOrder ();
8 R7 l+ c1 I2 ~3 ^6 i `
8 ~0 j# m. g) k* T% e try {
4 s7 T6 ]" |6 n; { modelActions.createActionTo$message ! r* G+ f) k2 {9 r! W6 m
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 z/ R+ j- f$ b, h1 M } catch (Exception e) {
1 ]0 P2 [* T1 n$ t5 b0 x System.err.println("Exception updateLattice: " + e.getMessage ());
2 h! B9 o& N! l4 J } ?, m% d9 z" U+ M) z0 O; }
" g$ G$ w- M0 c. d$ }. z // Then we create a schedule that executes the
4 p, [, Q: V7 K5 n' w$ y // modelActions. modelActions is an ActionGroup, by itself it
! `2 ^, ~8 X0 U // has no notion of time. In order to have it executed in
2 g. O2 t, Y& ?( t: _ // time, we create a Schedule that says to use the
$ a0 V8 a6 X7 o, ] // modelActions ActionGroup at particular times. This
( t/ k, e: c+ ~. n9 j5 j. W& c // schedule has a repeat interval of 1, it will loop every' A. S4 ~6 k: `. {
// time step. The action is executed at time 0 relative to
/ n# l3 z, ]! _! g; q; ^. | // the beginning of the loop.
3 ^3 q+ @$ x( i U$ j. L- e' n
5 ]2 U2 `: I9 r5 _ // This is a simple schedule, with only one action that is( U2 d$ B8 l2 U1 Q$ E' m
// just repeated every time. See jmousetrap for more
, l9 N8 B1 a( J* |2 m: e/ Z // complicated schedules.9 t& t# |) o7 b: P1 V) n
7 P/ a4 a; J) j4 s6 A @) N: _
modelSchedule = new ScheduleImpl (getZone (), 1); E8 A" t, ?' k& y
modelSchedule.at$createAction (0, modelActions);' g# g7 m- G& W \9 y! r+ m
. h! b4 j: O3 ^* Y" e1 k" X return this;, G- M) y! _0 T: h* ?
} |