HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ k* v# F: e: t' H! A8 D7 l4 @" Z$ G7 X
public Object buildActions () {
2 [5 v1 x& F# s7 w super.buildActions();
. K1 s# G; _8 Q8 I# l) z; l r
' C Y5 v$ M' |( n // Create the list of simulation actions. We put these in
) T) s* W$ C+ c/ g$ e, V, S' H# ] // an action group, because we want these actions to be- g# _8 X* L$ Y7 W
// executed in a specific order, but these steps should
C9 }6 m2 h4 L% {. C/ ~ // take no (simulated) time. The M(foo) means "The message* i9 V" ?+ ^- ?' Q/ c( Q7 |6 N$ h. t
// called <foo>". You can send a message To a particular8 i2 ~: k3 c4 x. R h) j
// object, or ForEach object in a collection.
& H! K+ m& s( X; x% k9 t! j. S" r. g
- `6 v1 j5 ?0 K" [9 p; N& {3 N! ^. B // Note we update the heatspace in two phases: first run, a6 c3 _' v: d+ X) ]) y
// diffusion, then run "updateWorld" to actually enact the3 E( p, Z- [+ V: E X A
// changes the heatbugs have made. The ordering here is5 Y( D0 H! C+ f3 ?( v
// significant!! S$ j: s K1 _: @6 y. G( U
- F# r, b' c' ^) q9 t // Note also, that with the additional
- v0 i! H0 k0 N( g& } // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 [* b4 w, k$ l) H // randomize the order in which the bugs actually run0 L8 M: F1 x2 w
// their step rule. This has the effect of removing any
3 z* h' C- t0 c // systematic bias in the iteration throught the heatbug: G- d# y/ `2 U2 s8 P' b. e! Z4 x
// list from timestep to timestep
$ x6 \& A( H' k! }4 _* t6 t8 O " a2 h1 l+ o2 n5 G* b
// By default, all `createActionForEach' modelActions have
& B e, Y8 r, x. o' w" K // a default order of `Sequential', which means that the
6 w3 [( y1 i2 K1 t) m w // order of iteration through the `heatbugList' will be
9 V" `9 [, d M: S4 D1 ]- K) u5 y" E% d // identical (assuming the list order is not changed: A" R' c7 M0 b7 |* h
// indirectly by some other process).! y9 [7 a. v, u, L
/ T M k$ z4 R! j+ D modelActions = new ActionGroupImpl (getZone ());
7 p) t6 B; X: o( q9 W- t" A
5 ?8 e' s+ H r try {
) ^/ `1 r5 x& k, V" g! X2 A3 B( G modelActions.createActionTo$message
+ b/ b0 p( C, @8 _ (heat, new Selector (heat.getClass (), "stepRule", false));7 I7 [9 |/ c, {$ W
} catch (Exception e) {
! L8 | o& L. T System.err.println ("Exception stepRule: " + e.getMessage ());
& t$ G; p1 `0 R2 g* @+ d }
8 S9 z2 ^' j) m7 {6 Z) b9 b) }% p* u4 x' `! S2 C$ r1 ]
try {; _6 g' N4 S7 V6 m
Heatbug proto = (Heatbug) heatbugList.get (0);
8 q6 M; a* v8 q) N Selector sel =
6 q; |7 G' P' n" q4 ^/ f j new Selector (proto.getClass (), "heatbugStep", false);$ F2 l- @" t& ~* Z! v
actionForEach =
, Y9 o! i/ L2 V2 z1 F) J. O modelActions.createFActionForEachHomogeneous$call
' P1 }& I/ t& r/ Y# I G/ X! l; l (heatbugList,. ?4 J$ s( e" k* q! L; I8 \$ {
new FCallImpl (this, proto, sel,
) u7 Q! Z) Z; [% r! K6 g new FArgumentsImpl (this, sel)));
" f) Y3 p1 k( l1 x. x" {( t) D+ p } catch (Exception e) {
, Z, u0 y! F4 ^4 A% O6 Y: z e.printStackTrace (System.err);! x' I) y5 F/ K. z
}" ]! T# d2 u6 C
) A2 V" I. g \ syncUpdateOrder ();5 ^$ |& ]3 B' r, j
! T5 n% p) s( r" A k6 x try {( _% m* T* H7 F' \3 ~& E8 ^
modelActions.createActionTo$message 5 P! l2 T6 ?$ ?. Y
(heat, new Selector (heat.getClass (), "updateLattice", false));
) _+ f# c9 R: \- F" `9 M+ w } catch (Exception e) {; b9 B$ R) g, B# f9 j% Z+ V% Z
System.err.println("Exception updateLattice: " + e.getMessage ());
( w) {" y X, F7 o5 \+ i }) J/ Y5 B" q2 S0 W% w" L, W; R
4 k$ }1 R2 E4 [0 r9 V // Then we create a schedule that executes the1 Q4 O- _4 w) J$ I+ _5 e1 j
// modelActions. modelActions is an ActionGroup, by itself it
/ W5 F0 z9 }* V, s1 J // has no notion of time. In order to have it executed in6 Z m/ t( e3 `2 d7 t: P
// time, we create a Schedule that says to use the+ y2 z. H* m: }6 x, M( q- x
// modelActions ActionGroup at particular times. This4 D! E2 j* E( `. a
// schedule has a repeat interval of 1, it will loop every
: M- n- L9 P' I // time step. The action is executed at time 0 relative to
* H# v# ]8 d( D2 S5 Z) m& i // the beginning of the loop.$ ~0 a/ ]* M+ V* X" W8 k
# Z" V$ V5 ^* y7 q! b$ X, ~9 C // This is a simple schedule, with only one action that is
4 E" E; d; O4 U+ ?3 i$ y // just repeated every time. See jmousetrap for more8 V% p/ p9 j2 D8 {
// complicated schedules.
( |1 i3 k4 l0 U" z; w* I4 k _ / I3 K- c+ R8 w2 _2 n6 }# V
modelSchedule = new ScheduleImpl (getZone (), 1);; G+ V( C) ~6 S
modelSchedule.at$createAction (0, modelActions);$ P! t4 F3 c$ ], Y3 s9 O
& k. u8 `3 y. J$ X1 A+ {
return this;
$ x9 w6 U6 h2 v" B) f+ a* |. ?% } } |