HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 ]# o. j! P1 @6 c C
8 k- i2 I1 h6 e) [- ?
public Object buildActions () {
' J: j) X0 B" Z super.buildActions();, j$ ^% S% C& F5 b9 j2 _' M
/ i5 k( u3 W- I& k, X' C# X
// Create the list of simulation actions. We put these in* g! N; k: ~7 @( G) L1 k
// an action group, because we want these actions to be
* a! \3 D: \" [% k6 j0 e6 j% d // executed in a specific order, but these steps should
' p% V4 n/ P! f% |1 ? // take no (simulated) time. The M(foo) means "The message: h6 V# ^) a0 m2 ^% p
// called <foo>". You can send a message To a particular
% o/ O5 I& Y+ u/ ^5 } // object, or ForEach object in a collection.
% i5 v. |& A) a. m
n I* y+ Q! K- v7 h7 K, I // Note we update the heatspace in two phases: first run
( r2 I) T' F0 `% M3 H // diffusion, then run "updateWorld" to actually enact the
4 r2 Y6 c, Y" Z: { // changes the heatbugs have made. The ordering here is
5 `1 L+ O% S8 W7 W2 U // significant!( q& `" p+ y. w+ X
8 N+ p3 r; m" S
// Note also, that with the additional
, @% V3 C' j' N // `randomizeHeatbugUpdateOrder' Boolean flag we can+ I0 \+ @0 G( s0 c' j
// randomize the order in which the bugs actually run
3 s6 G0 b) ~; r$ ? // their step rule. This has the effect of removing any4 o* |; h- N6 w& s% Q; l& [( B
// systematic bias in the iteration throught the heatbug
: u+ ^5 P B( t2 C, U# t7 U7 s // list from timestep to timestep
/ V/ A* {( Z- ]. x( |
1 D) Y8 p, G$ d L // By default, all `createActionForEach' modelActions have% M _. O" N2 z6 G
// a default order of `Sequential', which means that the
Q; S5 u$ h) ] // order of iteration through the `heatbugList' will be- Y) x! _! u* F% w: s, s0 r
// identical (assuming the list order is not changed) Y4 ^1 C; o* @9 q. m2 Y
// indirectly by some other process).' S7 O7 q2 R6 D% t) L7 P% `
7 D5 _1 m0 f. s3 z modelActions = new ActionGroupImpl (getZone ());+ T5 D$ S8 e* ]1 S
0 F( Y7 e t8 ^# Z6 d7 W9 ^& k! i
try {, B: P- `- `; u) M# `% n {
modelActions.createActionTo$message
9 x9 x* @ p% e2 C) w (heat, new Selector (heat.getClass (), "stepRule", false));
6 B2 {7 Z3 E) u2 x& Z+ O } catch (Exception e) {
. _' T3 W2 }4 g/ ?- F$ r System.err.println ("Exception stepRule: " + e.getMessage ());, O4 F. j1 Y- e
}- ~% S Y! e, Q# B
2 j3 _- s& y$ e F, X7 G+ r0 b. K
try { F% t p( E6 J
Heatbug proto = (Heatbug) heatbugList.get (0);+ ]5 @% M$ e m+ j: x$ S3 W
Selector sel = & q1 o* L3 x2 J2 y: j6 [
new Selector (proto.getClass (), "heatbugStep", false);
5 _6 G7 H0 l) O9 s* R4 p/ u actionForEach =, h4 A) k2 C' H/ c
modelActions.createFActionForEachHomogeneous$call) t: p B- v. `4 a% ^: Z6 t
(heatbugList,4 J# M0 Y( I H* d R
new FCallImpl (this, proto, sel,1 W- P* b* o8 v$ O2 n+ v
new FArgumentsImpl (this, sel)));
8 T- ~6 p& k P# w+ Z5 H) Y9 W1 h } catch (Exception e) {
9 @9 x! W9 X1 u, M, v2 q& r e.printStackTrace (System.err);
7 j, u% k: x; ]+ {5 J" t( S% [ }6 x$ X) l, U5 p+ w: P( K4 P
# C. v7 n6 q8 A' ]& Z6 j) ^5 s syncUpdateOrder ();' `. u, X* L& J) @/ K3 ]: C. s
+ `. H! F' C8 s# Q+ r1 B( B
try {
& P: ]/ O, F5 Y/ ?( t modelActions.createActionTo$message - h9 {) E/ F# P! }. o7 X. @
(heat, new Selector (heat.getClass (), "updateLattice", false));! x# q `- B+ Y' T1 e0 S o. E
} catch (Exception e) {
* w2 C7 |' L/ g: g) P System.err.println("Exception updateLattice: " + e.getMessage ());. u9 ^( u/ n# ~# x- B
}* d5 O; K J% C% b
8 U8 l- w- d) K // Then we create a schedule that executes the1 w4 I6 ]0 i( O, k- m
// modelActions. modelActions is an ActionGroup, by itself it7 W! D$ H! S0 C6 M+ T) K j- J
// has no notion of time. In order to have it executed in
- W3 O3 [9 j; K6 m // time, we create a Schedule that says to use the
& c! R; d$ C, ?1 f // modelActions ActionGroup at particular times. This
- F- i8 a$ l- P" ~" h // schedule has a repeat interval of 1, it will loop every$ A$ ?2 r# N6 O4 h
// time step. The action is executed at time 0 relative to
* p, Q6 D1 n) G // the beginning of the loop.
( e$ r0 u/ g# v& t$ U5 I& p
) _/ E1 F, M" U0 V( U ? // This is a simple schedule, with only one action that is
9 |2 Q+ k C1 }! R // just repeated every time. See jmousetrap for more
7 P Z# Z- q# M" z9 J // complicated schedules.
2 r! }9 W1 Y' w! X. d# T% Z1 k* r/ t 7 A8 H5 H, }9 ]# N) l) p" s! e2 }! x
modelSchedule = new ScheduleImpl (getZone (), 1);
2 X& C3 r4 g0 N) |1 @ modelSchedule.at$createAction (0, modelActions);
8 I3 z: M, K' {& |. z
9 } l, V% q% f- z( a return this;
2 i$ z# k+ Q: A. {2 S } |