HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 E* @9 q* M. E {
5 R+ ~4 H& H2 E- p9 Z/ T; F public Object buildActions () {
7 z: b% I" T* {* v6 X" w super.buildActions();* }( Z2 Q- y* H% y/ j) m" q
" ?+ Q# |* m: g% ]9 p: f
// Create the list of simulation actions. We put these in: d! T/ A2 { {6 I6 m' K
// an action group, because we want these actions to be, S, I! m9 V9 q
// executed in a specific order, but these steps should0 y: B" Z# y \+ }7 k0 s+ ]) @" z
// take no (simulated) time. The M(foo) means "The message) s/ R8 \* R, Y2 L, Z& U0 a. l
// called <foo>". You can send a message To a particular" p2 |' @, A: ^8 N% Z
// object, or ForEach object in a collection.% }2 n. i, _( D3 U# @
1 V! ~; @- K! D* P8 d' D6 G/ N
// Note we update the heatspace in two phases: first run
8 `1 h+ A0 H6 j // diffusion, then run "updateWorld" to actually enact the6 o$ C3 u! k" G& J# g% B
// changes the heatbugs have made. The ordering here is" b6 [" c; P6 v+ r& R! A
// significant!% C0 w; p$ {! G2 a5 c& J1 U4 q
3 E8 B. i& p+ a+ v+ ^) z
// Note also, that with the additional0 p* n4 y) t |# @
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) `9 G7 ]; n; W // randomize the order in which the bugs actually run
3 X. f# Q2 x( }: m // their step rule. This has the effect of removing any2 r- Z$ J; b: g. O% e/ ]! E9 S
// systematic bias in the iteration throught the heatbug: N, c/ T, z8 h S+ P
// list from timestep to timestep
9 C4 Z5 C+ m' I( r) ~) z ( J. }7 w0 ^( D) `- D$ e
// By default, all `createActionForEach' modelActions have0 R1 |- u8 @7 Q+ p
// a default order of `Sequential', which means that the
9 c" }/ G( |* j1 G$ b3 e- S) x5 ~ // order of iteration through the `heatbugList' will be
" l# ]. b& Y A/ S$ ? b! Z8 n2 X // identical (assuming the list order is not changed: E# w" o, g( h1 s b- x+ b
// indirectly by some other process).
$ S6 @6 I+ V' C8 h/ o" F
$ q/ h0 z" F1 X& K: u2 k' z$ B modelActions = new ActionGroupImpl (getZone ());
4 `8 U Q* m* ]4 ]6 v; k( l0 M [. ?
try {7 E: p6 | `7 C/ _$ k `/ Q: O/ j
modelActions.createActionTo$message
; V3 v. t( ?+ o+ A! L (heat, new Selector (heat.getClass (), "stepRule", false));0 j4 k: R. }: o- o; x
} catch (Exception e) {2 }& T' B5 Y& q8 H, `+ Y* p
System.err.println ("Exception stepRule: " + e.getMessage ());
8 I& e9 f+ Q/ F3 C5 [, f, ?( @ }
9 T2 C p9 B4 K j. Z4 M1 K: d9 M$ W4 d9 [: j; r5 V" @- w8 ~& l
try {8 u' z% D, f& A+ c. w
Heatbug proto = (Heatbug) heatbugList.get (0);
d* ^- Z' d# R* `8 |# g Selector sel =
i3 R4 ?7 t3 B0 U, Z8 F new Selector (proto.getClass (), "heatbugStep", false);
( i/ z' U8 F6 S actionForEach =
, k# f; w% x+ e1 i' O6 L modelActions.createFActionForEachHomogeneous$call
( G3 q: E4 O& S" _. S9 O' U (heatbugList,2 I o: K1 r+ Q0 `+ _5 ]' T
new FCallImpl (this, proto, sel,3 a# \% c; p0 d( ^6 y1 Q: ~
new FArgumentsImpl (this, sel)));; t) g. u( Q6 u, |5 T6 v: u9 T
} catch (Exception e) {" ?& q4 }% y; r$ C6 A/ m
e.printStackTrace (System.err);* ^, K; j; U$ q, j7 E9 M
}% r! Q# X4 h! S
9 h4 r2 Z, G) H* f) @
syncUpdateOrder ();! y4 A, d6 W% r) H3 \6 n
2 h' n _2 I/ W/ H0 I$ ]7 U
try {
* I; M z! X: ]4 }& i* l/ R, p modelActions.createActionTo$message # ^& p. Y( S* O! P0 _
(heat, new Selector (heat.getClass (), "updateLattice", false));( }9 Z' j x9 g. u, A
} catch (Exception e) {- F( w! }' N( v% x
System.err.println("Exception updateLattice: " + e.getMessage ());
( S0 y! n" i w" f3 ^ }
! E; J! Z- ]9 H+ c; A; }, c 5 U, j7 F3 p, \& U
// Then we create a schedule that executes the
5 s: X6 d, b- [, R1 q // modelActions. modelActions is an ActionGroup, by itself it
" p# |/ C/ B9 z; K3 f // has no notion of time. In order to have it executed in
B0 w: i+ ~$ s- n7 F7 P // time, we create a Schedule that says to use the2 b! r/ k' p: V5 y$ a! ?7 Q$ `
// modelActions ActionGroup at particular times. This( \5 k2 w! i; u
// schedule has a repeat interval of 1, it will loop every
5 h2 y7 i* K6 ~9 u8 ^: Z // time step. The action is executed at time 0 relative to0 t/ F1 ~& l, k5 M$ h `' u* F* r
// the beginning of the loop., w9 f: @) S" q+ [* y
6 R) i' [- X6 m/ d0 b) T' ]0 O // This is a simple schedule, with only one action that is. A4 x3 C* u9 M R- ?$ {9 b
// just repeated every time. See jmousetrap for more# J* y! t7 |! e' M7 {
// complicated schedules.
% {1 S4 H; P* y! _0 D2 M
4 v8 y5 j( l, H* p3 t modelSchedule = new ScheduleImpl (getZone (), 1);% _* ]; Z1 L( R/ | \
modelSchedule.at$createAction (0, modelActions);# U9 m* t* n6 i W
& N2 n0 n4 T5 f& g5 y' i
return this;
+ f _* y1 G6 V. A } |