HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& Q1 H* w% g7 [! ~/ n3 ?
3 I# g/ Q8 Y7 [+ a/ l9 \' } public Object buildActions () {, O; |: ^" I; m
super.buildActions();9 j) \0 w; T5 }: f# {
8 I6 L- x9 Z6 N* Y // Create the list of simulation actions. We put these in% s% E, q' U* E, ^6 ^8 e' U
// an action group, because we want these actions to be! b- W) i6 T! P+ s8 {" C( W" s, }
// executed in a specific order, but these steps should* |* W* ~7 C3 _
// take no (simulated) time. The M(foo) means "The message
( ^8 o8 z5 } H3 F2 B: f // called <foo>". You can send a message To a particular
9 @( [" }: t: X9 R3 F: E // object, or ForEach object in a collection.
7 r1 L h. k9 ]+ \+ h; l$ I/ B9 Q1 M7 u 3 _/ o6 r" G# T8 O0 D8 P: a
// Note we update the heatspace in two phases: first run
5 ^" P1 s6 C' R8 j- Y6 Z/ ] // diffusion, then run "updateWorld" to actually enact the
: \8 X; ~: x$ o' F( B // changes the heatbugs have made. The ordering here is
9 p1 @/ N" h7 b1 }1 f( F // significant!
: N' D% h4 J1 M % Y( n' ^. ~" V( b; t! m+ P) \
// Note also, that with the additional
1 ]* T" r7 ? v* P. }7 p5 q // `randomizeHeatbugUpdateOrder' Boolean flag we can" p0 A/ `% s5 a' I5 y
// randomize the order in which the bugs actually run
5 |7 d _% [! u6 o- B // their step rule. This has the effect of removing any
' o4 `) B& f/ ~2 F) X- g // systematic bias in the iteration throught the heatbug
0 s) p. D! t" v: J" N // list from timestep to timestep" V/ j$ H5 D' l+ P8 a0 I/ E) O, Z
) J) X) E6 ^/ k! H0 Z% d7 R // By default, all `createActionForEach' modelActions have
7 }" ?* ]: ~, i3 H6 g // a default order of `Sequential', which means that the
: e/ }! a) z; L // order of iteration through the `heatbugList' will be
5 u+ e9 ], J6 }) I8 c // identical (assuming the list order is not changed& k ~( g# v& D2 f
// indirectly by some other process).
: [* |" R. s+ p8 L u
: e: V' F( n6 t7 @2 r1 ?/ [ modelActions = new ActionGroupImpl (getZone ());
3 G1 o7 t$ O4 R1 c. B& c+ _* d9 ~7 q- q
try {
! I* K. H) A: {* f8 O7 S9 \$ V5 } modelActions.createActionTo$message: g/ e; Q9 m9 p' A
(heat, new Selector (heat.getClass (), "stepRule", false));; _' K* H- x/ j6 `# V( i
} catch (Exception e) {
" U9 ?! [) B: ^" f2 J4 I System.err.println ("Exception stepRule: " + e.getMessage ());
$ V; ^! ?0 \3 c! O6 U( G# O }- j; j3 [* h4 z. ]$ E- k
. o, t9 ^' F" v try {
- i# n) }& m: k _6 K Heatbug proto = (Heatbug) heatbugList.get (0);
( P, }# W u) f Selector sel = h' W4 X3 z$ z- q: \4 t
new Selector (proto.getClass (), "heatbugStep", false);
6 |* v7 l& K$ S4 S* q" W& q actionForEach =2 {" H! k& f2 F1 _2 D( O& k
modelActions.createFActionForEachHomogeneous$call
. r0 Q3 Q, g1 H$ M (heatbugList,& c' b5 [, `2 X1 A4 `0 k& V
new FCallImpl (this, proto, sel,! i8 f) H4 [! H/ p, b
new FArgumentsImpl (this, sel)));
/ U: C' B# m5 j( T! \# G# E } catch (Exception e) {
8 @% y$ y+ O8 \2 y0 q' T" o e.printStackTrace (System.err);
3 _4 @* M! p; e% C1 D5 J }7 w9 d7 K3 M- w! s9 C3 q6 Y {
+ I k0 K% e- F) _* a3 Q, y! I" j syncUpdateOrder ();
1 b4 M; a8 ?; D4 ` H/ H" Y. U1 b% U- F+ a9 N2 G$ N; E
try {" G" d* S6 x+ u6 x1 p( A2 w
modelActions.createActionTo$message
0 n8 y& o" b0 G0 E (heat, new Selector (heat.getClass (), "updateLattice", false));
. [: a7 X: s1 C7 i' ` } catch (Exception e) {. g2 w+ m! @; n: ?
System.err.println("Exception updateLattice: " + e.getMessage ());7 d& G. h2 G* i+ @4 D* V
}7 T5 R) l, t- E( Z3 k# m, L
2 m6 Y, e, K* m% r6 R& g // Then we create a schedule that executes the; _9 S8 _/ V0 b) M2 O
// modelActions. modelActions is an ActionGroup, by itself it, M6 [- V9 G7 M2 |- {
// has no notion of time. In order to have it executed in- `: k7 ?8 `( k% [7 A
// time, we create a Schedule that says to use the' d1 K$ I8 V# w% I( l4 ]$ `4 ]2 Q
// modelActions ActionGroup at particular times. This
- B: r6 ]5 Q5 [/ Z7 O+ m% ]$ g // schedule has a repeat interval of 1, it will loop every) ? O y) o( t( X, x3 i$ {
// time step. The action is executed at time 0 relative to% B$ T! s$ l9 F
// the beginning of the loop., R$ k" v+ E3 j2 k4 J1 M/ c
- E/ N6 l; x3 v* Z' A6 P8 ?: E: n, L
// This is a simple schedule, with only one action that is
$ [5 P% K8 b- D3 g // just repeated every time. See jmousetrap for more6 ^9 C; x; m5 _: _
// complicated schedules.% a( N, p. x7 r8 M) ~
: E4 X# ~+ O+ i5 o modelSchedule = new ScheduleImpl (getZone (), 1);
( N' D$ r, [( `3 h" f/ |( v8 r% ]. h modelSchedule.at$createAction (0, modelActions);4 {& r! b5 d# @# d/ v7 T
# Z# u/ P4 ?2 W" O' t
return this;6 Z! B9 y- s! k& }$ ]2 W
} |