HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, }9 c& {+ ^# M' @ |- H: n f( d6 f. a: h: j
public Object buildActions () {
6 S9 K: g" f: }6 R0 D% M super.buildActions();5 x* K4 m) B: w4 Q
! n: K B$ v# |" y // Create the list of simulation actions. We put these in
7 ^' q; m$ r+ O // an action group, because we want these actions to be
. Z" _/ I- o7 Y8 C5 j. ` // executed in a specific order, but these steps should
- W9 G# o& S' }3 @/ _2 p$ X // take no (simulated) time. The M(foo) means "The message+ K2 x" P! |5 X+ ~5 e& s" ~
// called <foo>". You can send a message To a particular
. r; N V, e: `% J1 K // object, or ForEach object in a collection.1 l, h/ z! h" C0 P3 t* r
2 z. y f1 D( w
// Note we update the heatspace in two phases: first run
4 M2 g1 S8 N/ x' o2 H // diffusion, then run "updateWorld" to actually enact the' b; @2 Z9 Z* ~6 Q$ |
// changes the heatbugs have made. The ordering here is
2 H/ z% w# U: n% R) U' T5 E, G // significant!
" |9 v, e% {" Z7 C 6 x% u0 o7 _+ I
// Note also, that with the additional* J* U- l* U3 x, L9 @, Z; ?! O5 i
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 W, a" @9 F% i/ L
// randomize the order in which the bugs actually run1 z" E/ e; J9 g4 ^
// their step rule. This has the effect of removing any
8 q% S' x( O1 l9 q! K // systematic bias in the iteration throught the heatbug
) `! m% o& N0 _8 Y // list from timestep to timestep$ T* k( K! R9 {: F+ o3 h
$ n- u T5 I; o) I8 e7 T // By default, all `createActionForEach' modelActions have
\; G# o& _+ _8 Z6 |& U- l8 ]. T // a default order of `Sequential', which means that the$ w( _, u& }) ]: [# F* f F
// order of iteration through the `heatbugList' will be
/ ~+ |7 Y6 v5 x1 g- I- { e5 L! a // identical (assuming the list order is not changed( N) o* w. F/ k5 h, x
// indirectly by some other process).
: q9 h1 ~$ n0 D: ~4 u5 s- h3 x T$ |& F( g+ e; F7 n, u
modelActions = new ActionGroupImpl (getZone ());4 k' X! x c6 d* v& I+ v
* W; W" _( x' Y' a$ c9 _0 H. t try {/ t. k1 Q; {9 y* A: r$ h
modelActions.createActionTo$message
4 G W6 Q5 X7 E/ I% e* U (heat, new Selector (heat.getClass (), "stepRule", false));
0 V# H- V% j/ T } catch (Exception e) {
e6 d' {9 m: e0 R System.err.println ("Exception stepRule: " + e.getMessage ());
+ r ?+ a7 A4 N2 x) s3 x2 s/ c }
- [3 }; x7 m$ x- j
5 @3 ~5 i4 x: R try {5 x% H5 |. M7 a* r/ k
Heatbug proto = (Heatbug) heatbugList.get (0);
* \/ y6 S# j" k4 C; M8 b0 u9 S Selector sel = : ^/ Z4 t$ t; M! Y' D* \0 B6 {$ S
new Selector (proto.getClass (), "heatbugStep", false);1 z) W& G5 }: }1 z
actionForEach =/ j( ^; a3 R( V7 P0 }. K# o) q
modelActions.createFActionForEachHomogeneous$call: T1 R1 s# a! M- V: ~% @2 d& P( ^
(heatbugList,0 P0 ?5 x" b% @. l4 Z) k0 {2 a
new FCallImpl (this, proto, sel,
4 g9 l' E, k$ n2 o, M7 ?+ x) U- V new FArgumentsImpl (this, sel)));
7 y$ H5 q6 i4 E; p# ? } catch (Exception e) {4 \2 g( F7 E6 X/ M
e.printStackTrace (System.err);4 `6 ~3 `/ W7 ]6 @% Z5 e
} ~1 l; m& H, L% D. q( v/ [: r
- w6 Y ?# S1 n: o- U syncUpdateOrder ();: g2 u, s0 z. L) [1 W8 U6 h
8 F7 `$ z4 E. a }: f' u( k4 Q9 H
try {5 v F. b2 o' p2 }
modelActions.createActionTo$message
) S, K3 T3 |; ~7 f. x2 g- A1 ^ (heat, new Selector (heat.getClass (), "updateLattice", false));- S0 I: \! c8 a% p
} catch (Exception e) {9 ?( g- ~; s" h8 w! a5 p. K* F* C- a
System.err.println("Exception updateLattice: " + e.getMessage ());
9 j' I9 d: ?0 X- s } p# \" @5 j# D5 C( U. B2 b) ]
+ A }, n: p4 e! ^) S+ [+ f3 N // Then we create a schedule that executes the- e0 U( t. S7 L4 ^
// modelActions. modelActions is an ActionGroup, by itself it
1 \3 I; e- j- B7 w; U6 I' E! B // has no notion of time. In order to have it executed in
% I$ e5 H* j5 [( n // time, we create a Schedule that says to use the
- v- |0 F9 O8 |) U+ S S0 ~ // modelActions ActionGroup at particular times. This: m% n/ D# w, G, W* q& N
// schedule has a repeat interval of 1, it will loop every& ]( j% O) a0 r
// time step. The action is executed at time 0 relative to
: z$ H/ b. H9 S. p) A // the beginning of the loop.
- ^1 U; O9 p! z, f
6 ^! W- B+ M X& q // This is a simple schedule, with only one action that is) j5 U% I8 [6 E
// just repeated every time. See jmousetrap for more
8 v" T" U8 O0 t9 ?- p d8 _ k // complicated schedules.
% |) a/ T i7 l! B
: r; `+ t( O! g% c: z modelSchedule = new ScheduleImpl (getZone (), 1);
( ]" H! g! e; i+ w modelSchedule.at$createAction (0, modelActions);
: r- K- X) {% R0 A. p; U6 p
* U: X c' `6 [5 {$ y: M return this;+ V1 V1 M. j- X
} |