HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, l9 |- Y# N& P" e
( s$ a! X; e, H4 M
public Object buildActions () {
/ ]: n# D' c0 } super.buildActions();* b/ U- |7 h) S6 E
\+ Z& H, X P- H R, D1 V
// Create the list of simulation actions. We put these in
1 `* o+ b) c+ ~9 {! O. c1 r1 \ // an action group, because we want these actions to be# v6 t3 a7 f: N1 q$ v% | x7 y
// executed in a specific order, but these steps should
% i& S7 z! w, r' K // take no (simulated) time. The M(foo) means "The message9 w$ X! s3 U, Y9 N! T; f o
// called <foo>". You can send a message To a particular
' w4 t ?. `, }, I4 H9 T // object, or ForEach object in a collection.' C J3 P. h* E$ F' C- r
4 T# W5 X( C3 G% E* t2 h // Note we update the heatspace in two phases: first run& p" g$ \& r% s
// diffusion, then run "updateWorld" to actually enact the
9 _5 N; r$ J" J0 t0 C // changes the heatbugs have made. The ordering here is- z& W4 H6 a3 V1 ]' a) }" L5 {
// significant!7 k& d4 n: w6 c# I. e
# ?3 m9 }& i Y1 P. ~6 C. A# W
// Note also, that with the additional
. g( S( E* V Q* G7 r$ z8 t0 f // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 k5 p" R0 K! u/ e* n // randomize the order in which the bugs actually run
% o+ {- s& L7 j& w$ B5 h // their step rule. This has the effect of removing any: m6 P/ H$ v; b' o2 D3 z1 @& g
// systematic bias in the iteration throught the heatbug
$ k& q" Y: h* O# a' ~ // list from timestep to timestep
* ?5 a+ H* P4 V5 V j, h8 @8 B* [
// By default, all `createActionForEach' modelActions have$ |% `% Y3 N% q6 B$ y
// a default order of `Sequential', which means that the
3 s; _9 k9 f0 ^+ G2 n // order of iteration through the `heatbugList' will be
/ ?6 u: N% U/ V* e5 E! U // identical (assuming the list order is not changed2 R: w4 Z) G$ f2 {
// indirectly by some other process).9 B: H q) R# r+ H& _( ] @
$ U* C, ^( p2 J$ ?& `# p {' x5 D1 ` modelActions = new ActionGroupImpl (getZone ());
/ l$ J. A/ f+ |8 b# |+ Y3 N2 o. H* P6 w$ f) P
try {6 u+ ~' K% M& h h }
modelActions.createActionTo$message
+ \* ~( o% G; L+ v% U I$ i( [ (heat, new Selector (heat.getClass (), "stepRule", false));
! g1 r1 w. i9 b. U } catch (Exception e) {
, b( I& H. a8 B7 A: f System.err.println ("Exception stepRule: " + e.getMessage ());8 j7 z% ?8 G2 }& c0 t6 N9 }: ^3 e
}
7 M* P0 T; S+ R0 A0 s6 d# j) x3 p# |3 x, n% v
try {1 y7 r n/ t) k+ [, m/ a- u! j2 P
Heatbug proto = (Heatbug) heatbugList.get (0);6 }5 q6 ?+ ~# I( m" h
Selector sel = 9 M) a- n& v a" ?0 l* k9 t
new Selector (proto.getClass (), "heatbugStep", false);$ ^) ]) ]: ]3 C& z
actionForEach =# t M1 H6 W8 n( N
modelActions.createFActionForEachHomogeneous$call
+ y/ O0 E! R1 m, B/ [7 C (heatbugList,
* b3 M; m: l1 _8 u" q new FCallImpl (this, proto, sel,9 x3 |/ E' P) G6 C3 e
new FArgumentsImpl (this, sel)));# t1 R$ }0 ^$ [- X- g4 Q* u) e
} catch (Exception e) {
1 B' }/ U9 c. | e.printStackTrace (System.err);
7 V8 d" H g! j' s8 P }6 |2 j) n: k0 A. D) s' M
1 f# P# d1 l% U x syncUpdateOrder ();
4 L; ^. A/ r7 p6 ~+ ]" ?; l/ _- b G! x3 \& z& ]+ M
try {
6 p7 [6 D$ c# f$ o4 e, E3 ?; \ modelActions.createActionTo$message
* p+ m8 i& y! X9 l# @ (heat, new Selector (heat.getClass (), "updateLattice", false));
; M6 ~! B) I0 p/ w } catch (Exception e) {8 R" U* G1 j. U5 M4 N
System.err.println("Exception updateLattice: " + e.getMessage ());1 ~/ D$ k4 j2 _( @/ x( v
}$ A! J! i3 h( O6 d# {- `
. |$ q& i$ L$ V& [7 m* H4 U
// Then we create a schedule that executes the" R5 B) k* c- A: @; e9 ?& ?& F# K
// modelActions. modelActions is an ActionGroup, by itself it
4 F( l$ b0 P# ~0 A7 X1 c: K' r // has no notion of time. In order to have it executed in8 t# }0 @0 ]& V1 {
// time, we create a Schedule that says to use the+ g Y3 \4 s; i8 S
// modelActions ActionGroup at particular times. This) h% _! ?) L# v( `, c* o
// schedule has a repeat interval of 1, it will loop every
6 P$ g* i$ i. M, b+ }4 B$ S // time step. The action is executed at time 0 relative to
/ Y" J' u' \* c! C // the beginning of the loop.
3 F: V0 T# h: C5 G2 N7 }6 j+ p( `" v/ M8 y, ] G1 x. Z
// This is a simple schedule, with only one action that is
, C3 G' x. s' Y // just repeated every time. See jmousetrap for more l( O3 f0 h ?; c
// complicated schedules.
% }$ a3 B8 H$ T% ~+ R# e% s3 U
$ z: Q. ?% C6 o; x: C7 x; K modelSchedule = new ScheduleImpl (getZone (), 1);5 \# C( b- [- \% a& i
modelSchedule.at$createAction (0, modelActions);4 q3 R( o$ x6 }4 S" { {, ]5 a. Q
9 w) B# \ O Q: m* @ return this;
( _( S# K4 z/ O( D } |