HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; G2 C: P! ^1 _7 n) V" e5 a+ V
- n8 k3 [& x' ^' E5 o5 ] public Object buildActions () {
- W/ d, f4 m7 N9 V6 C4 s super.buildActions();9 l6 @0 {3 W1 {& G: [( T
8 A/ H; s3 r& N% c/ n
// Create the list of simulation actions. We put these in
# ]: r: Y: I/ c* M // an action group, because we want these actions to be F+ j: d2 q) B9 d3 ^! N5 @ t
// executed in a specific order, but these steps should! p, e2 w! I, v5 O$ V
// take no (simulated) time. The M(foo) means "The message3 f' D3 x' O4 N3 p* A. D
// called <foo>". You can send a message To a particular# C2 n! s" k% Y% B2 U* \* z9 m0 m
// object, or ForEach object in a collection.
- x, i- h8 {- E" U $ V1 \9 _2 @# f! @( L$ d- Q
// Note we update the heatspace in two phases: first run; w |* x; z. J. t
// diffusion, then run "updateWorld" to actually enact the
5 s9 W, O" a }' o+ v& c // changes the heatbugs have made. The ordering here is
% ?9 w5 d W1 x0 f \# {* ?- M# t! g // significant!
6 r: V6 Z% e- j. o
D( J, _0 W& z4 z // Note also, that with the additional6 C3 U' I. `% k% }* A7 S
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ \, G/ z. V6 \8 M) T9 d* u7 c* e% I" x
// randomize the order in which the bugs actually run7 R% F& x* u# V$ ?
// their step rule. This has the effect of removing any7 |7 m+ O- k2 s, M
// systematic bias in the iteration throught the heatbug
5 V2 `& U& o3 L3 H // list from timestep to timestep9 b4 y& J& k8 e- w, b/ a
: A3 A- S/ Y1 ?. O
// By default, all `createActionForEach' modelActions have
4 M, g) _: o* X0 `9 y/ k8 M1 q // a default order of `Sequential', which means that the
' K3 u& b4 C4 F* i6 p/ w // order of iteration through the `heatbugList' will be8 h6 P. S3 i, z( x5 e) i
// identical (assuming the list order is not changed
! t$ `! Y" i4 L% V // indirectly by some other process).
9 ?# f: {5 U$ ]6 l - a# @" D7 l4 d' P7 P, |
modelActions = new ActionGroupImpl (getZone ());
3 d3 K" M- v" I' Z5 u& x9 y6 H( U1 B$ q2 }+ E
try {' ], C$ y. H9 P2 _" v
modelActions.createActionTo$message9 N7 Z! s2 N. {' b; A
(heat, new Selector (heat.getClass (), "stepRule", false));
+ `, X. w. ^3 M5 F( N! i2 _2 Q& z } catch (Exception e) {
: b6 H$ z; G, n System.err.println ("Exception stepRule: " + e.getMessage ());) ~. n& `! C1 w* i
}
7 H) n: {; m: s A$ L$ B7 F# U% b( k& g- {9 o
try {
5 b' z, W; s! T: D+ P4 n" o Heatbug proto = (Heatbug) heatbugList.get (0);
6 J* w% l) T* i* k& | Selector sel = 4 u A! [6 s' }" } ^
new Selector (proto.getClass (), "heatbugStep", false);% \' N" r/ M- E. J) u
actionForEach =
5 j$ `) k8 h9 }7 m; P1 D modelActions.createFActionForEachHomogeneous$call
# ]4 ^# r/ q- f4 e+ @$ Y (heatbugList,4 c9 L0 s/ F$ z; c# v/ [$ t
new FCallImpl (this, proto, sel,' P( E# \& H5 `% u- F2 `. F0 a
new FArgumentsImpl (this, sel)));4 `3 t3 L3 o# E3 X
} catch (Exception e) {, U$ z) b0 H' q6 h
e.printStackTrace (System.err);
5 v& `) m& N- [0 w }
; G6 U8 p( B! M/ V# I5 U 6 L# x" [" z2 ? a1 T% z# K
syncUpdateOrder ();
3 n! r5 ~; |' c; z2 k T1 w/ D' `+ ?2 f7 r) u+ m I8 h
try {4 T* M9 J7 q$ d9 u5 F0 z& {6 W
modelActions.createActionTo$message
, _9 V4 ^2 \6 _ (heat, new Selector (heat.getClass (), "updateLattice", false));
* Y) v4 U0 T' R } catch (Exception e) {
% ?- y: {3 v+ O System.err.println("Exception updateLattice: " + e.getMessage ());5 _8 y" Z& D7 T) E
}- t1 v0 P$ A7 C- I: Y) t: l
/ ^1 U9 h& T% R" L- d8 F // Then we create a schedule that executes the
" j5 T9 b/ W7 n& k // modelActions. modelActions is an ActionGroup, by itself it& V& X/ m' o2 c! T5 i, o
// has no notion of time. In order to have it executed in
5 O: h# [& J m3 P5 G // time, we create a Schedule that says to use the0 W. L% P& ?! S
// modelActions ActionGroup at particular times. This1 {' f1 Y+ F4 x! b9 F m* D K
// schedule has a repeat interval of 1, it will loop every
9 u: \% S. l9 J: f // time step. The action is executed at time 0 relative to
( d( i- _) J/ E& v* J% ^ // the beginning of the loop.
# A" G$ s' t7 @9 d. b$ i5 O T7 R- j) E$ O& y
// This is a simple schedule, with only one action that is
/ K; y) S4 V: w) W0 N // just repeated every time. See jmousetrap for more0 P+ \* F% Y$ M e' p
// complicated schedules.
' y8 G% }- ]) ?; x: i
6 W+ L, X! T8 d9 O modelSchedule = new ScheduleImpl (getZone (), 1);2 a" h2 M1 y4 V! S% F
modelSchedule.at$createAction (0, modelActions);
0 \2 n* R! p& `* o. b( W3 {* f 4 ~ y5 T9 o& C, G& p/ Q( ~( a1 Q
return this;
5 ] O6 k- j; R5 }" P } |