HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 Z3 i0 M6 J' {# W' s
& K7 o- L" g# i4 b) o6 O& B* C3 A public Object buildActions () {
# d" N, }7 K* X, a super.buildActions();
* u& e$ B- r3 w* K/ r. h
2 X$ T* X8 d. `( @# I // Create the list of simulation actions. We put these in
( T3 s. }3 E7 t/ G0 i // an action group, because we want these actions to be
5 e9 G' ]% {" I8 a: q! R7 K# [ // executed in a specific order, but these steps should
) B* f* u3 F5 Z$ q! H& ^ // take no (simulated) time. The M(foo) means "The message3 w1 q3 C$ T; }4 N
// called <foo>". You can send a message To a particular" l4 ^5 \0 T% x) f
// object, or ForEach object in a collection.
, j+ Z6 E& n5 C- D; g. C
% v9 E6 f! t& [- l) Y0 x/ c // Note we update the heatspace in two phases: first run4 _& s6 @) H/ Q6 R$ k
// diffusion, then run "updateWorld" to actually enact the
* c F4 G* q& N" v2 z // changes the heatbugs have made. The ordering here is
$ Y& J5 k+ m9 D+ n0 k- Y8 }$ A // significant!
" r) }, g& k( c3 q& D$ u N " |+ B$ m* y. }$ ]
// Note also, that with the additional
0 L5 V K- p0 E6 I" [! J // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ r% k5 _5 h5 S3 ~/ A5 k // randomize the order in which the bugs actually run
; X. t# ]) Z" f( G3 H // their step rule. This has the effect of removing any8 ~( f+ I3 K) l2 h
// systematic bias in the iteration throught the heatbug4 J+ S ~/ }& F, \3 t; Y) v+ C
// list from timestep to timestep3 y" k( j% |3 J9 R- x" f
0 ` q5 K X4 G2 @! x
// By default, all `createActionForEach' modelActions have
2 s- `# J( R) ~2 N: _5 ] // a default order of `Sequential', which means that the
1 `) c4 U% [+ N( A& V // order of iteration through the `heatbugList' will be
0 K7 Z- Y- p Z; X7 g! v1 ?' z! a // identical (assuming the list order is not changed& M: y& r: B9 R
// indirectly by some other process)./ w8 {8 G' Z) r+ M# k
/ Q" H" `# K! L0 s9 s% @6 C
modelActions = new ActionGroupImpl (getZone ());3 l; g' _5 o% i/ [7 W1 Z$ P% q9 }, G
$ ~6 t1 ^ E+ H try {
* e1 q, ?+ Y/ e' G$ \" e: e3 p modelActions.createActionTo$message
- N/ i# k% N+ a) \2 F (heat, new Selector (heat.getClass (), "stepRule", false));) p& P0 k c! Y k8 n
} catch (Exception e) {
9 {0 C! X& L# R4 _. Y, E- V; i4 v System.err.println ("Exception stepRule: " + e.getMessage ());5 Z% B; W x$ U
}
1 f: k6 u' q/ q, B: j2 }) W1 o1 j! V$ u
try {7 d% E; q/ R, E, H, b
Heatbug proto = (Heatbug) heatbugList.get (0);) N( \- d. D' J3 G6 p
Selector sel = 9 N( G m! j& J* {2 ~; }+ [
new Selector (proto.getClass (), "heatbugStep", false);% J' h" J3 n2 m# I) u$ D9 C
actionForEach =
/ `# p6 j0 X) B t, P; o) b modelActions.createFActionForEachHomogeneous$call
2 k+ Z" u' S" I (heatbugList,
* H0 j+ W2 n9 E( g. m/ J8 b new FCallImpl (this, proto, sel,
' H$ x/ k% q# Z& x7 v new FArgumentsImpl (this, sel)));" s& A# Z$ w7 ] P& f
} catch (Exception e) {
9 P" Y. g# q" t+ E e.printStackTrace (System.err);
5 V3 j0 y2 H( {6 k, w5 F' B8 M }
/ |$ q3 v# |2 y8 f6 U4 t( Z $ d4 `3 f! ?$ B& R5 |
syncUpdateOrder ();+ w p. \3 t+ R
! c8 j* ^1 O. J1 M( R, |/ v: r try {2 R3 r6 C8 D9 o& a6 Z
modelActions.createActionTo$message & [) o) X9 L) J: t8 W6 n# u. }
(heat, new Selector (heat.getClass (), "updateLattice", false));, N$ l8 B5 L& I
} catch (Exception e) {
6 g1 x8 o3 Y: \5 |5 b3 k- V System.err.println("Exception updateLattice: " + e.getMessage ());2 t9 f. W0 _' C5 S8 v
}
^/ d7 U6 E# F2 L8 Y' ^ . o2 c& _1 Z/ A) n$ t
// Then we create a schedule that executes the1 u0 ` C8 k& f) g7 c( n
// modelActions. modelActions is an ActionGroup, by itself it
% \* H' w } Z9 Q9 [ // has no notion of time. In order to have it executed in# P9 J9 X6 w3 M { R
// time, we create a Schedule that says to use the
- L1 ?2 R* @0 d5 l) s // modelActions ActionGroup at particular times. This9 @& W0 P# K/ l* V9 \
// schedule has a repeat interval of 1, it will loop every; P- E. V7 Z M+ a) s. R
// time step. The action is executed at time 0 relative to. M) X6 q- ?9 \
// the beginning of the loop.7 C2 Y$ b. ?) o6 l! o
( Y, K+ a0 _ L: T0 [) G // This is a simple schedule, with only one action that is; D& @9 @" ~2 g# l' Z! x+ x
// just repeated every time. See jmousetrap for more9 S) k, j( y# F( a" x: O
// complicated schedules./ Z# G1 e* v# w+ {+ V
/ L z2 u8 v9 E0 o6 d4 u; c7 p
modelSchedule = new ScheduleImpl (getZone (), 1);
5 r5 v4 a% g! x( p' v$ R modelSchedule.at$createAction (0, modelActions);
- H1 O9 M( [3 I- \ [8 v2 r1 R: w p- s
return this;
3 O0 K* P3 p2 S* G6 ]& J9 Y } |