HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 \$ a+ w5 c% c" X
U/ D" e8 N& n9 E( _6 B2 _. C public Object buildActions () {
) c& R" ~) b1 H* _+ {; _& `* j super.buildActions();
: l+ v& L' |( T6 e$ `8 C% Q: F
! V) X. t3 c& A. \) z8 _/ U // Create the list of simulation actions. We put these in) j4 q2 N! t, f/ J
// an action group, because we want these actions to be4 V0 B$ @- @: ?6 V) y, y& t( H
// executed in a specific order, but these steps should
5 [* p" T& s) v- l. t8 Z // take no (simulated) time. The M(foo) means "The message
+ x) C9 w/ R! C // called <foo>". You can send a message To a particular
4 R1 \+ P+ v. [( F2 F0 c7 @ // object, or ForEach object in a collection.
$ h+ G8 t" G; g % q o' k# \; \
// Note we update the heatspace in two phases: first run
8 ^3 b1 m4 |3 [( f7 Y3 @# H // diffusion, then run "updateWorld" to actually enact the
2 y% Z* ?& e0 u // changes the heatbugs have made. The ordering here is' M. b$ M* r6 V: I/ Q
// significant!1 n" x$ q' A2 U$ G# R
- M% U' d" [6 ]; j R! x: y // Note also, that with the additional
0 H3 n# l+ j" s' c g // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 z; ?+ d' |6 a m' X // randomize the order in which the bugs actually run
0 q! Z G2 M7 I' l // their step rule. This has the effect of removing any/ [3 z' w3 h/ ?# Y" r: e
// systematic bias in the iteration throught the heatbug' A( {) h, p: y9 t& U
// list from timestep to timestep6 |- n9 C# I/ j R0 o$ f$ R
- }2 M1 C* y5 v( D // By default, all `createActionForEach' modelActions have
. H' g0 X- K" {: Z // a default order of `Sequential', which means that the; d# u2 W) d8 Z/ f: F7 [
// order of iteration through the `heatbugList' will be
, T$ g" b$ |( ?2 ?. v% \# V2 B1 S2 W // identical (assuming the list order is not changed
% S8 G" W* W5 t& u0 U. n6 {0 Q // indirectly by some other process).8 S" Z" E/ E/ B/ s" S
: s3 j# D: a* |* C8 S modelActions = new ActionGroupImpl (getZone ());) l& h- h) @- c6 z
. x' c9 t) ^. t/ O- M! w+ c: Z) i try { p- F/ G/ B- g. l9 {
modelActions.createActionTo$message6 v+ w+ U+ x& {1 H
(heat, new Selector (heat.getClass (), "stepRule", false));! ?; q+ T; q7 a7 p5 f) s
} catch (Exception e) { w- J# S' j; W0 T
System.err.println ("Exception stepRule: " + e.getMessage ());
/ R+ R5 n2 L2 v8 O. P+ n }
' o; b3 q3 [- R0 s
9 U* R( P2 f7 a' B try {
% F" z8 g- |# P9 c; b4 \/ N Heatbug proto = (Heatbug) heatbugList.get (0);9 l( c* }: |3 G. M
Selector sel =
! s) }; y, h9 R$ ~4 M new Selector (proto.getClass (), "heatbugStep", false);( g1 m. i) K A" H4 O+ o
actionForEach =8 u4 i9 l1 v3 l* m
modelActions.createFActionForEachHomogeneous$call
4 P' D V y# Z) ? (heatbugList,7 E% b e# V. s* h# w
new FCallImpl (this, proto, sel,
$ t: J" f, J& D5 u ^- n2 i new FArgumentsImpl (this, sel)));$ C4 Q8 W8 |2 h0 q0 c, n w
} catch (Exception e) {
3 @& f1 ?8 x. E w; A5 ~ e.printStackTrace (System.err);) C5 t( ~. s% y/ [8 G" F
}
! \* E+ a: u8 ]6 ?% x) H2 ?. `
: A% x4 n' d, M& X! Y8 v/ B syncUpdateOrder ();' n- Y, ^4 |5 M4 B+ _# u
, {6 u# t& N5 S8 [& i: a `
try {: @' u9 z5 f. m+ ~
modelActions.createActionTo$message
5 e( H- Y0 a$ L (heat, new Selector (heat.getClass (), "updateLattice", false));
) d; n3 G+ k" d; A; s, g( _" ] } catch (Exception e) {" z) R* n4 U: T
System.err.println("Exception updateLattice: " + e.getMessage ());
4 _( d9 v1 P2 O P0 i: d( M }" B3 x, l2 d7 W, `* l& V7 N
! B& @/ v5 @0 D // Then we create a schedule that executes the6 G$ z7 @1 _0 o0 K H3 S
// modelActions. modelActions is an ActionGroup, by itself it" \# R7 |% Z, U1 Z# Q
// has no notion of time. In order to have it executed in- }7 N' I @7 x L9 _1 V
// time, we create a Schedule that says to use the
$ c6 m, Q% f7 |+ T, h- t // modelActions ActionGroup at particular times. This
4 i7 y+ }7 X$ B0 [ // schedule has a repeat interval of 1, it will loop every9 A7 m" @9 o. ?+ R1 i2 r/ r% e+ u
// time step. The action is executed at time 0 relative to" N9 u! k) g1 r2 {5 ~. J& M( t
// the beginning of the loop.
7 ^, i X' B9 _# q9 J. U, }$ V6 {8 F
// This is a simple schedule, with only one action that is! d& \' {- D ~! r" E$ B% ^/ e9 }
// just repeated every time. See jmousetrap for more* ~# R$ @- X8 p8 |/ d# W, n
// complicated schedules.
, i9 r1 E Q5 N6 I) u1 I 3 K6 i, S* _4 t" C9 F5 q$ }
modelSchedule = new ScheduleImpl (getZone (), 1);1 N3 l/ V4 o8 P2 ^% H$ H3 p
modelSchedule.at$createAction (0, modelActions);
1 Q1 j1 f4 ^2 f" ]+ k . T6 j8 P3 L( T2 z/ h
return this;
0 G0 Q9 L# l0 Q% C7 ` } |