HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 p5 R, f, Z$ E- m) R! U
0 t( H* B: I8 O
public Object buildActions () {* m! z" ~: M1 M4 s/ j) F
super.buildActions();
6 @" u, f7 J/ y8 p( L2 Y% y$ D
0 N7 d/ W2 J7 e+ g; H" v, { // Create the list of simulation actions. We put these in
3 S6 e" T% a4 k' Y { e% \# X6 _ // an action group, because we want these actions to be
' S \+ t4 m- \% K G c2 D // executed in a specific order, but these steps should
: E0 u5 ]& ]- h! d5 \ d0 p) } // take no (simulated) time. The M(foo) means "The message/ `" G f" U6 j" |- r
// called <foo>". You can send a message To a particular1 [1 s$ p6 l" `- U; S
// object, or ForEach object in a collection.
' W7 x& m' {7 n9 j. k7 n$ ?" |
" B0 c7 T0 `/ F& `) `; x( a // Note we update the heatspace in two phases: first run
0 ]) o! F' f+ y5 w+ `/ M* \/ i // diffusion, then run "updateWorld" to actually enact the
s: Q0 E2 A% ^9 [ // changes the heatbugs have made. The ordering here is/ b1 ? ?( q' e2 M
// significant!4 h# }. Q1 H% _; G0 q+ P
# m$ F) V7 v/ N/ f F // Note also, that with the additional8 |. r& n" w V C2 _) Z' w
// `randomizeHeatbugUpdateOrder' Boolean flag we can% t, a3 K( l9 g& w9 I/ T( C, H
// randomize the order in which the bugs actually run- ]% G+ j8 Z# v3 J6 R
// their step rule. This has the effect of removing any
+ y4 M8 \& `4 I3 {; c9 l // systematic bias in the iteration throught the heatbug
, r' m/ N- ]4 f7 W0 s/ { // list from timestep to timestep/ P/ G0 H8 z6 H' U R% a0 J: L
# u! n0 a' Q' e$ T. z
// By default, all `createActionForEach' modelActions have
% ?: ], Y0 [# A! j | // a default order of `Sequential', which means that the
~% E& Y( Z3 I // order of iteration through the `heatbugList' will be( h6 B* {3 Z1 h. m+ Y; J' O
// identical (assuming the list order is not changed
$ i5 r4 a2 Y1 J, H# u! K$ ? // indirectly by some other process).3 n. M4 ]- Z+ y4 L3 X0 |
( C3 j* E/ R+ f/ ?; @
modelActions = new ActionGroupImpl (getZone ());
' _9 Z$ Z+ U; `7 O/ K
; P; `* D( ?8 u" Q- C* R6 l try {
3 ?' M- s0 i. O& d6 y modelActions.createActionTo$message! `0 M" a+ d# B
(heat, new Selector (heat.getClass (), "stepRule", false));8 T* @4 y% s, a B& {! s" `$ j7 g
} catch (Exception e) {) v0 n6 c0 ^7 J3 `) y' ^9 z9 W
System.err.println ("Exception stepRule: " + e.getMessage ());) x+ v& _0 ]/ B" U/ _+ z: f# K
}# G3 L+ b1 V0 o
& ?+ C' n! g: J8 }4 U try {
4 V2 V2 K4 J% J/ i/ f* M Heatbug proto = (Heatbug) heatbugList.get (0);: `" i- Y6 x" R( c+ D" q ]
Selector sel = % L' E( ] k, ?# k
new Selector (proto.getClass (), "heatbugStep", false);8 N7 h' a; G$ \
actionForEach =
( W* T# ^+ w, j" m/ [- Z0 ^ modelActions.createFActionForEachHomogeneous$call
% W4 ]" d- j' T (heatbugList,4 d; k! U `% @9 P. o
new FCallImpl (this, proto, sel,5 U1 ^. k3 o5 [8 h }' h
new FArgumentsImpl (this, sel)));
% O4 @) m3 L# ^, h$ C5 Z } catch (Exception e) {
0 g/ |7 c; L. | J6 c. o e.printStackTrace (System.err);& x0 A2 T5 V' [1 b
}
7 B1 j* m$ S4 H" f3 X8 E/ d # ~6 q+ ~" }1 v
syncUpdateOrder ();
6 a# R4 b" k$ L5 \# x2 w3 ]! p; X" t. u, W7 h# X
try {1 }5 m+ @5 n! ^; j
modelActions.createActionTo$message 6 ~' {8 O# h: m) |( f! K$ ]7 s
(heat, new Selector (heat.getClass (), "updateLattice", false));
" U+ v, ]" Y/ }, O. v( c6 | } catch (Exception e) {
3 R# {/ |$ R2 a; @5 Y& k System.err.println("Exception updateLattice: " + e.getMessage ()); b' U) J V; y( m9 }
}) E+ R1 X4 c/ v2 l; L
8 r# b+ C% a7 _4 T
// Then we create a schedule that executes the$ ?8 T% T# k6 |/ |$ E R, r
// modelActions. modelActions is an ActionGroup, by itself it% f+ W% @ M- U+ W
// has no notion of time. In order to have it executed in5 Q% u4 w# {2 t+ t# m
// time, we create a Schedule that says to use the/ S+ E5 F- [8 ]' r. x$ e. x, y; N# V7 I
// modelActions ActionGroup at particular times. This& P) s7 a1 t/ {3 ]7 H
// schedule has a repeat interval of 1, it will loop every+ Q# O+ m" Y, ^0 |6 ~" D% B
// time step. The action is executed at time 0 relative to- H* H; e* m, Z
// the beginning of the loop.
. X; c B- B7 h: `1 _5 ~* I
8 A' B. A5 w( a8 @+ H$ ]% r: M // This is a simple schedule, with only one action that is
' T# U- F# W0 S& p // just repeated every time. See jmousetrap for more6 I6 x- u- m; N, S* [+ e
// complicated schedules." r! D* {* _) J* x7 n* l
* Z: g o6 [6 l: |* |
modelSchedule = new ScheduleImpl (getZone (), 1);: q; U3 y+ e4 p4 @
modelSchedule.at$createAction (0, modelActions);
2 L [* D: F; K5 x* z8 c9 x 8 ~) N' Y, H H, W' B2 ^% g, r) T
return this;
, l8 L( A8 d: d/ Q } |