HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& K$ l* W* p0 J; b
b6 }* n3 ?/ D* D+ }3 R public Object buildActions () {$ h5 e, R1 u: T0 R$ }6 J
super.buildActions();5 |5 f0 @* F$ s' V+ p6 J( F
- w. m7 @. N3 G u' ~
// Create the list of simulation actions. We put these in
* {" d2 r9 b: n$ W7 O$ P6 A // an action group, because we want these actions to be
$ T7 t$ A, d4 f* H // executed in a specific order, but these steps should
/ }- q' y' Q0 l! S+ F, O6 j$ V3 l2 z/ O: ? // take no (simulated) time. The M(foo) means "The message+ v0 _" {- e5 ~
// called <foo>". You can send a message To a particular% A. s) p9 g* @! o
// object, or ForEach object in a collection.( M" K& a! L0 O
U" e9 L* x& \ X2 v' n. [
// Note we update the heatspace in two phases: first run7 S; T& ^4 k9 J8 }# a" a
// diffusion, then run "updateWorld" to actually enact the0 n0 J/ c2 D0 s8 S" ^
// changes the heatbugs have made. The ordering here is
) u2 C+ h' t+ O- Q0 | // significant!
# a+ S* ~2 y) h3 b7 m7 S. N
P5 s3 P# y2 F( ~ // Note also, that with the additional
* M- C: H6 P* N/ ^. H // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 \' V3 k8 G# x; I) g9 o8 I3 M // randomize the order in which the bugs actually run: D; s& ]; \0 H4 M% T! P
// their step rule. This has the effect of removing any; L0 N0 _/ V* ~. H
// systematic bias in the iteration throught the heatbug
# S$ R5 D6 P# t" u // list from timestep to timestep
8 p$ U+ u) j3 H/ f+ k; l: Q [
7 ?; T6 f7 L! b% g6 X; }+ Z5 B' N // By default, all `createActionForEach' modelActions have
k5 Y; f" x9 z7 S6 {0 t // a default order of `Sequential', which means that the
8 A3 X( C$ f. {* M q4 Z // order of iteration through the `heatbugList' will be
7 p* P/ U* O" E0 V9 Y- B: ^5 { // identical (assuming the list order is not changed2 m, E" E+ ~2 n4 O& M1 u' f
// indirectly by some other process).
& }/ G! M7 }9 l* A' f: ]3 v
- X7 P2 L& E) X1 a: V6 T modelActions = new ActionGroupImpl (getZone ());
0 r- d$ g. @2 ]/ X; L6 \( n
. X6 i! _* m% \" p e0 { try {
m# z2 F$ C$ [" g; N' d+ x( R% m# {8 s modelActions.createActionTo$message
7 R. X- B& e8 s, K" ~ (heat, new Selector (heat.getClass (), "stepRule", false));* x: p9 i' S2 H# S" I& S' |' C
} catch (Exception e) {9 ^5 H9 o( e6 Z$ [7 D$ c
System.err.println ("Exception stepRule: " + e.getMessage ());
# f+ |7 A: Z3 [% P }% y w2 H2 k+ i1 H9 r
9 _1 B8 R# q3 ]6 R+ j9 |0 A
try {7 A( H) O, \* }( y7 E
Heatbug proto = (Heatbug) heatbugList.get (0);5 k/ j4 V9 |+ T6 m( V. ]0 s
Selector sel =
/ ~; s5 e! O7 ] new Selector (proto.getClass (), "heatbugStep", false);
; w' j% M" t! R! l actionForEach =* e! `8 B6 O$ @- K L' F8 Y6 M. X
modelActions.createFActionForEachHomogeneous$call! F, f* v! Q! k
(heatbugList,% h* ]: P9 b/ h4 m
new FCallImpl (this, proto, sel,
2 ]/ O0 q6 u* @3 r) T1 G5 b* g new FArgumentsImpl (this, sel)));) m, v$ k, O$ e4 T- X5 l( P3 `
} catch (Exception e) {
5 A, ?$ H+ @0 C8 y e.printStackTrace (System.err);: H) l6 [( y3 p
}+ g W. a- q7 Z+ ~, s% ~, C
! X! \5 z- o( A4 \% x+ Y syncUpdateOrder ();/ e- ^6 X6 \7 Z2 l& E
6 E) v6 P |# r2 U/ e8 z, z4 g
try {
; C0 a* |! A7 J9 w7 l! `* E/ U7 G modelActions.createActionTo$message 9 s1 T0 q' N% G5 m5 s$ Q
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ m! k1 ~1 F- C4 m* Q } catch (Exception e) {6 X) F4 I) w( [4 F" H, {
System.err.println("Exception updateLattice: " + e.getMessage ());
; f$ P: W3 X! Y0 U. { }
( o c0 ?7 O% [* h
" {4 U* F: D0 A // Then we create a schedule that executes the
9 q$ C# l' O E // modelActions. modelActions is an ActionGroup, by itself it
- T+ M6 |9 x- O8 ]3 @6 _7 w h // has no notion of time. In order to have it executed in
( T, B5 m+ B! ?9 o4 @! x // time, we create a Schedule that says to use the7 n4 E- Q$ h1 A4 Y% q+ n
// modelActions ActionGroup at particular times. This% O* H% i# }* H
// schedule has a repeat interval of 1, it will loop every& ]4 U- C$ E9 c# a' [
// time step. The action is executed at time 0 relative to/ S: N* `# Z* E0 P
// the beginning of the loop.4 m9 V! f) L( E8 B
+ R+ I5 t) I" _. L' b+ X7 Q& e // This is a simple schedule, with only one action that is
% O8 k0 t/ X4 F: y" f // just repeated every time. See jmousetrap for more
) P' Z$ W. D8 ]9 z) t I" F6 }$ f7 d // complicated schedules./ v. H5 Z* Z; e7 L
- h" x& f4 l m5 U* J% h$ f
modelSchedule = new ScheduleImpl (getZone (), 1);+ N& s4 |9 G9 W/ i* G, s
modelSchedule.at$createAction (0, modelActions);& }! h% S4 C; X4 {
. ~5 ~/ f( e4 Y( B- H* B
return this;1 Q9 _' j* q' X' @- O% @
} |