HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. [0 s/ B' x _6 H2 o& u- N% e2 B8 _+ s6 F
public Object buildActions () {
7 X6 |& d* m' H super.buildActions();
- b7 r" J! Q( v$ F % A: U) F* g9 ?! ^- \9 Z) P# e
// Create the list of simulation actions. We put these in
8 L, f9 N% l3 x- a) m9 _) W9 q // an action group, because we want these actions to be
: W' y' ^; n' U! J2 y. p+ G! a+ s* b // executed in a specific order, but these steps should
Z' ~; T3 L3 d! J; k/ C1 V // take no (simulated) time. The M(foo) means "The message
I0 ]7 k; q' {6 P* e q& l // called <foo>". You can send a message To a particular
/ Y9 K$ G: {( f2 o. k$ | // object, or ForEach object in a collection.) E, [5 ]6 n. w% P8 {
' p5 P7 S9 {! {# }( n, y5 U. k4 N/ \
// Note we update the heatspace in two phases: first run% e9 M! h7 p0 b' e6 e, G v
// diffusion, then run "updateWorld" to actually enact the- B7 k! Q6 q+ J5 Q; Q
// changes the heatbugs have made. The ordering here is N/ N, K0 ]! l' V0 n( |
// significant!* w/ q: x6 V* w
3 D ^+ U3 b& }5 H$ G$ p // Note also, that with the additional6 f4 {3 Z7 O- k3 @& ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 {$ C9 x- F7 l3 |) D // randomize the order in which the bugs actually run, U2 h5 [4 [7 p! g
// their step rule. This has the effect of removing any! a! R0 m' K3 h
// systematic bias in the iteration throught the heatbug3 X9 B8 b- v5 M9 V; w" y" U
// list from timestep to timestep
: \ c/ h* c% N. T( j1 p8 H
* C* ^1 ]9 w* ?9 e$ U // By default, all `createActionForEach' modelActions have
$ j$ T& h- Q' w! t/ v8 L // a default order of `Sequential', which means that the# g0 n' @# v' t" q1 c
// order of iteration through the `heatbugList' will be+ m4 ~: e5 f0 K8 m9 t
// identical (assuming the list order is not changed
8 H8 u5 p# w( m! G // indirectly by some other process).7 Q6 z% M; ? @# d: V' J
+ _0 C6 C1 R2 v3 E* ^8 F& ~
modelActions = new ActionGroupImpl (getZone ());
9 {: l7 C" X) c$ G; z' W" G/ E" y) y% N0 F8 y, d
try {- O3 d0 `+ n7 R/ D
modelActions.createActionTo$message
, v% E7 S" X, s% e8 _ (heat, new Selector (heat.getClass (), "stepRule", false));
) P3 h; L* d9 u$ d/ v# F4 w: K: d8 e1 s } catch (Exception e) {5 @; i( q& f: V) E! K
System.err.println ("Exception stepRule: " + e.getMessage ());* n1 M: ^6 e3 p4 T2 M n
}
3 W1 L* g" k4 N$ z; d; ?8 b
5 V, \8 e# p1 b5 [4 q try {
. ~+ U/ Z; |% N& l0 c Heatbug proto = (Heatbug) heatbugList.get (0);
- I. W& q, S) j0 G Selector sel =
+ E$ K: f# l; R! g3 f6 X5 u: T" ~4 n* B new Selector (proto.getClass (), "heatbugStep", false);7 w' k! `) J, f0 y9 K8 K1 n- p
actionForEach =( I- _) P& {+ d% f# d. v _. \
modelActions.createFActionForEachHomogeneous$call2 m- Y0 v- X, p3 a+ _+ F
(heatbugList,
* D, e q# R% Q9 S1 D" }; h2 E new FCallImpl (this, proto, sel,
! Z4 W4 ^5 v+ x, N+ h new FArgumentsImpl (this, sel)));
1 Y1 z N2 S! L5 X } catch (Exception e) {
& x' y: [ b- v e.printStackTrace (System.err);
5 v7 J" j7 x9 p) H+ T# l. {9 N }! n* a7 n" o0 j( j2 f
( R( S9 A; s4 i- L' F6 i7 J. G syncUpdateOrder ();8 o1 R& M, @3 R& S4 } q
) G+ C7 p4 e1 @
try {) n/ b% h* ^4 I5 J% R& h
modelActions.createActionTo$message - u, I/ A- F, o+ H0 y3 c
(heat, new Selector (heat.getClass (), "updateLattice", false));
( O# d) N& F) p) D) q0 h- m } catch (Exception e) {8 F, w0 `1 N0 G' |6 `8 i% Z% R& M5 s
System.err.println("Exception updateLattice: " + e.getMessage ());- D* w* u. h+ Y: d$ L0 U
}8 W" b9 t) F P3 c+ c7 j8 q8 N
# _. r* a7 ]. z( S4 b+ K // Then we create a schedule that executes the
4 B5 V5 _0 z# I, J0 q) j* F // modelActions. modelActions is an ActionGroup, by itself it
$ C7 o1 r' d5 E0 v$ T0 @' h' [1 ~ // has no notion of time. In order to have it executed in7 x2 s0 I1 u" |. Z% a
// time, we create a Schedule that says to use the; d; u/ c8 h. |: o! |1 b* p' E. p
// modelActions ActionGroup at particular times. This
6 Y1 u: C8 b3 d0 F. D // schedule has a repeat interval of 1, it will loop every- \. I" N% G$ }) e3 L& P
// time step. The action is executed at time 0 relative to
% I4 W H+ Y$ ]! ?1 B // the beginning of the loop.3 D# Z3 U. H! u; i( ]
0 t. W) x L" |. l4 Z/ _0 N' n // This is a simple schedule, with only one action that is6 a+ M4 C* o$ e$ e
// just repeated every time. See jmousetrap for more
* U3 e' y& \/ | // complicated schedules.
& e4 `! I u [9 d! O
$ {" h% a! c- X. v0 [ modelSchedule = new ScheduleImpl (getZone (), 1);
5 ^* C) |) v2 e+ r- |# z modelSchedule.at$createAction (0, modelActions);
7 ^/ g* P' |; c& L% r' n% k D
5 D3 K+ q9 c; Z( I return this;+ A1 a" P' b0 @
} |