HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 _+ j3 g8 X) ?/ x
8 h5 Z3 T, G3 E( T5 T& U! ^( w( i! N" _
public Object buildActions () {% e; _ j0 \: T/ h; W V
super.buildActions();
$ u* m0 y9 O: L ! x4 `# \# C2 E- \) @4 p
// Create the list of simulation actions. We put these in
# Z" J( R& F5 ^6 e // an action group, because we want these actions to be; t6 J- ~( l N
// executed in a specific order, but these steps should- Q1 K# @" X8 w- [6 A
// take no (simulated) time. The M(foo) means "The message5 z9 E! h) b" g: r! q7 D
// called <foo>". You can send a message To a particular
6 e# b3 O3 @6 m/ b // object, or ForEach object in a collection.
# ]; L% _- ^, \9 |; u + |0 ^# r! }8 J7 Q K
// Note we update the heatspace in two phases: first run
! K4 t. b* B& l% \* C // diffusion, then run "updateWorld" to actually enact the
5 r' P2 |7 F5 m* N: D // changes the heatbugs have made. The ordering here is
3 o! @9 T% ]% s f) u // significant!2 k" M& ~+ d, I' n5 i( n8 A0 Z5 r
8 W0 B0 I( a4 X6 L) A+ F
// Note also, that with the additional
9 a8 Z6 G1 s' t% ^/ a% s' m( S // `randomizeHeatbugUpdateOrder' Boolean flag we can
) {& E$ w' `7 ` // randomize the order in which the bugs actually run
1 b2 O2 a! F- x! k( q2 Z // their step rule. This has the effect of removing any
7 o2 r4 x* @% x( C/ }- D // systematic bias in the iteration throught the heatbug6 N+ R5 {6 I( U" A
// list from timestep to timestep" N: a4 T0 U) D" T
) f8 i" }. _( c( _8 ]5 y // By default, all `createActionForEach' modelActions have( V+ C9 J# a+ X/ P/ L$ m+ W
// a default order of `Sequential', which means that the1 Y8 n; b5 L3 W* F' {5 g+ Y
// order of iteration through the `heatbugList' will be# _1 ?0 x9 i" |/ [
// identical (assuming the list order is not changed3 y8 M/ O8 u9 j1 L& `% P
// indirectly by some other process).3 R6 @ L' [- V3 z* o3 |
% G7 l. J( R5 t modelActions = new ActionGroupImpl (getZone ());
) l. Z. _3 M( x. |6 D$ W3 l' b% }9 B0 f- g9 F1 W* J# z8 q8 a
try {; z* l; i5 y0 O* M
modelActions.createActionTo$message0 z( Z8 y7 B: q) n7 Q
(heat, new Selector (heat.getClass (), "stepRule", false));3 Y& `+ d" I+ W( p H: p* G
} catch (Exception e) {' h f% S0 V; x, C# U& I7 D, S, l1 i9 C
System.err.println ("Exception stepRule: " + e.getMessage ());) `. j: y% a `" J
}' v9 [5 S& K& I |3 E: k
0 c, o5 Q( \8 {7 r
try {
, ~! s' D* c( ?7 r# I" Z Heatbug proto = (Heatbug) heatbugList.get (0);) ~, k" F; p' f2 r+ x
Selector sel =
# c4 v- [4 R" q. E$ J new Selector (proto.getClass (), "heatbugStep", false);" I. \! |- G: J
actionForEach =5 X3 ]7 t6 `5 R
modelActions.createFActionForEachHomogeneous$call
" I8 u8 J; Z6 O+ B( e8 ? (heatbugList,+ o! v1 D, m) E$ g U
new FCallImpl (this, proto, sel,
2 l' B# y# F3 q5 O7 s) j new FArgumentsImpl (this, sel)));
1 o& A6 o. j" t; q) b7 w } catch (Exception e) {
0 A% L G0 j( e4 z0 H. f1 a' O/ v e.printStackTrace (System.err);
6 ^, ~3 |. h) |% Y$ |% g) ~3 V" R }
+ f. K# h8 D5 x; |& C5 R 8 h' q" J: G5 R4 r4 J. j' q9 Z
syncUpdateOrder ();
4 b( ~1 g" K- ?+ I( g; P7 V* m4 m+ Y7 I" L; L
try {
1 H; x* t1 W( m& d( J# | modelActions.createActionTo$message 7 E. y q6 _6 H. k, u; A3 S
(heat, new Selector (heat.getClass (), "updateLattice", false));
! f8 u7 z% c+ R& }% ?3 t } catch (Exception e) {
* j6 G* Y9 M6 ~: x/ S+ C System.err.println("Exception updateLattice: " + e.getMessage ());$ n1 t6 C$ N0 a, F1 o
}/ K$ ~; Y0 [+ H. `+ O, `. T
2 Z% g4 U; b3 Y) J0 p2 ?* W) {
// Then we create a schedule that executes the; u* m N) b: \5 m9 c! u" L
// modelActions. modelActions is an ActionGroup, by itself it8 a6 N; a: n" M7 t) q/ j' `
// has no notion of time. In order to have it executed in" Z5 m" w; L2 B: E8 _' x
// time, we create a Schedule that says to use the
: g' M; k7 | V7 Q. M // modelActions ActionGroup at particular times. This
X& U5 C8 Q' \! B( @) b // schedule has a repeat interval of 1, it will loop every# |$ j8 r# @2 i9 }, X" G* j
// time step. The action is executed at time 0 relative to
0 A# _; F" v& T: ?# x // the beginning of the loop.2 l, j \, D! b6 A& C; ?% Z4 b
9 _0 T# a( @1 v9 w( n+ d1 c // This is a simple schedule, with only one action that is
$ _& \8 Q' i, \ // just repeated every time. See jmousetrap for more
' @/ N! h( S- x) I // complicated schedules.
$ w7 J& n' y5 Z( d( e 2 c+ N# `5 s. i8 P3 ~7 _$ \
modelSchedule = new ScheduleImpl (getZone (), 1);9 g$ I* T6 c8 I( [3 ^
modelSchedule.at$createAction (0, modelActions);
+ T" l/ p) I' E1 Z 7 y5 N) D1 V3 ?5 o3 h2 E
return this;
. q' Y) ?6 Q7 z w: K5 s } |