HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) Z/ ?( A5 k% @4 E; Z7 ~' z
+ ]8 k" f9 ]- q Y" S I! i( M
public Object buildActions () {
7 Y, B4 d! d( |; A7 G5 y* f: ^$ M super.buildActions();
W$ r- M% q; T, S
! C5 \6 t# R4 A* N! H // Create the list of simulation actions. We put these in
2 d9 q" E [2 s$ N; Q // an action group, because we want these actions to be( Q5 V( p; `6 b5 A S0 w: q6 @
// executed in a specific order, but these steps should% S. C, c4 ]2 [7 _/ G9 B
// take no (simulated) time. The M(foo) means "The message2 |9 U k) T+ @1 H1 O' z/ a* _ k/ O
// called <foo>". You can send a message To a particular
7 U* a. e, ~" V- P; f8 L // object, or ForEach object in a collection.
4 _! D+ N7 W2 L8 M + B) m# z6 G$ W! ]: G) @& K
// Note we update the heatspace in two phases: first run
# H/ X4 l4 \1 V // diffusion, then run "updateWorld" to actually enact the( ], w$ L6 m' V4 l3 i; Z& C' E
// changes the heatbugs have made. The ordering here is
5 m$ s b/ N7 u( o X // significant!
4 ]' D& N3 I/ a$ W4 D
+ p: ] V3 }6 k4 [+ _ // Note also, that with the additional
; Q& _+ I( N9 U; V! S6 L! \1 V // `randomizeHeatbugUpdateOrder' Boolean flag we can
. b- I4 F2 r; S3 b1 E // randomize the order in which the bugs actually run7 W/ Y+ I4 v4 i+ M# D( |
// their step rule. This has the effect of removing any- `0 U( B/ i) b) q L' Y/ a
// systematic bias in the iteration throught the heatbug
. B1 s; s* O! t // list from timestep to timestep
\, z W4 J. K3 M- J% A
* F3 G2 @0 ?, E) O* Y/ G1 G: I: q' O& \/ g // By default, all `createActionForEach' modelActions have; u6 Y+ |* [% q$ u
// a default order of `Sequential', which means that the/ x, B0 Q' o3 a' K9 t
// order of iteration through the `heatbugList' will be
8 j. w, f/ ~% W3 a7 f // identical (assuming the list order is not changed
0 Q: ] s% i1 a, ~ // indirectly by some other process).$ q5 I8 |0 P; m) N
+ H) _2 ^; z1 z* X modelActions = new ActionGroupImpl (getZone ());
0 Z1 w% g. U0 y3 A, F ?% m) W( {5 L+ L4 H, w+ X; h9 l1 v' M/ x3 E/ H2 Z
try {
7 s* L, q V( j. f6 r3 X" u7 o modelActions.createActionTo$message6 n4 U; [* i# k X9 n
(heat, new Selector (heat.getClass (), "stepRule", false));
" }" A8 P, i/ [7 U } catch (Exception e) {
7 _5 y I1 l# ?0 _ System.err.println ("Exception stepRule: " + e.getMessage ());
) B$ j, [, t' ~, ` }
8 m8 C4 z6 p# X9 R
' {; z. A6 F# y8 l2 n try {
4 f; m4 p6 }8 ~5 Z Heatbug proto = (Heatbug) heatbugList.get (0);; ?9 J$ p/ T' ]/ i7 N$ d
Selector sel =
& ~! W+ Z; c8 O9 h; n6 P new Selector (proto.getClass (), "heatbugStep", false);0 ^% P! }* ?6 C' i! ~) B* ~0 i
actionForEach =
- n4 u0 P, c7 W% X( I. y; @ modelActions.createFActionForEachHomogeneous$call: S l% z3 V% a4 M+ |
(heatbugList,) B" Y; u6 `9 L6 q/ ]5 C
new FCallImpl (this, proto, sel,4 z+ V$ b- z& x- I7 z% F+ ?" x
new FArgumentsImpl (this, sel)));' T% D# ~) Q( L% D
} catch (Exception e) {
* P/ n# H8 Z( {6 i( [ e.printStackTrace (System.err);
9 _2 N3 v1 W k9 c$ I- } }/ x I% }+ |' ]- v8 d
; i5 \9 y$ r) q( }- j syncUpdateOrder ();! \5 V. _, v. C$ D
9 n: z1 X$ L$ z( S& x& b% i
try {
; R, ~( T* }% d7 f# ]* ^ modelActions.createActionTo$message . R- k5 o3 A# ]$ l- f
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 [$ Z5 C2 _2 g% u } catch (Exception e) {& n. j. |: ?4 l
System.err.println("Exception updateLattice: " + e.getMessage ());* ?) U8 u* m# B- x5 \ |' t: J
}4 |% n9 V9 X/ O/ M7 }' k. A3 V4 F
# F4 |( A( B5 [% x& e6 ?0 i // Then we create a schedule that executes the
6 x3 l# O/ L( W6 F) K6 F- U // modelActions. modelActions is an ActionGroup, by itself it* F$ U2 N5 E# J7 K/ ^; m8 o
// has no notion of time. In order to have it executed in
/ E/ j3 E' ~. ]# K2 U // time, we create a Schedule that says to use the
& v7 v) l" [; z: [1 L% H8 @) Z3 I // modelActions ActionGroup at particular times. This
7 z5 |! L9 F2 V* D+ X m // schedule has a repeat interval of 1, it will loop every
; G1 l' l2 d: n2 \: R! E$ `, X // time step. The action is executed at time 0 relative to
! A0 i' d# G. a6 V; l4 |* k" R# e // the beginning of the loop.
1 l/ {4 |3 F) d, V
! m( M# Q) f+ J5 w& i/ Y( X // This is a simple schedule, with only one action that is, M: q0 ?; I" m
// just repeated every time. See jmousetrap for more# s# p+ `( m. }: U/ `* l
// complicated schedules.
, ^- _( i1 ~& a) ?$ r8 h
5 w4 [7 d7 R7 a/ y$ Z0 O/ N" l modelSchedule = new ScheduleImpl (getZone (), 1);: k. ?- Q ^3 W6 l4 ]+ x& v/ x/ H5 W
modelSchedule.at$createAction (0, modelActions);. h O3 V; N/ z# ]- y8 m' x
7 d% H8 a+ E* ^; q8 E9 p return this;
( j5 W$ b8 E; M* ? } |