HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" p2 ]0 d0 T; f& r. l
' V1 [( ` @ V9 } public Object buildActions () {" O+ _- U$ b) F5 t' j7 {
super.buildActions();
v8 v) g0 ^- _7 P1 } e4 a: c/ G# g8 p( c H" ]
// Create the list of simulation actions. We put these in
! B3 U) _* _& j% C1 z1 d // an action group, because we want these actions to be* ]( |9 k' h1 E/ S3 k; t/ v
// executed in a specific order, but these steps should
+ g6 E. c$ R, D6 B6 r) X3 o // take no (simulated) time. The M(foo) means "The message
3 \ d2 S# H. u* \' ` w // called <foo>". You can send a message To a particular
6 h1 W( E: x# _" C2 { // object, or ForEach object in a collection.
6 Z9 e" w$ k$ d2 Z6 i 2 q- E, G7 J* e# P. {
// Note we update the heatspace in two phases: first run/ h9 M7 M' X6 W( w3 Z
// diffusion, then run "updateWorld" to actually enact the# s- E. i! c) z$ I- O
// changes the heatbugs have made. The ordering here is9 a6 C7 H+ b1 P* L
// significant!
2 q- D6 e" u( P3 q/ ~: q; y& u, F5 F) n
/ U) q7 n* |" X R# |/ { // Note also, that with the additional; }' a( X# E7 F$ ~5 ^( e# y) t! g
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 W5 J( D2 w7 ^8 x) ~$ u' k // randomize the order in which the bugs actually run! ?1 B/ o& ^+ y# V
// their step rule. This has the effect of removing any0 A+ t' m/ q5 {% k5 N! f0 c: ?! f
// systematic bias in the iteration throught the heatbug. D- F' `& m5 c# N( ]- s
// list from timestep to timestep
2 E- g" T8 b# h4 p( f ' N, W7 e) s; o3 n$ ~9 p
// By default, all `createActionForEach' modelActions have7 g' ~& J2 [- |
// a default order of `Sequential', which means that the: ?6 y9 L% z* e# V7 w3 T3 n; w) Z
// order of iteration through the `heatbugList' will be
& s8 _2 d. J( H6 A3 U // identical (assuming the list order is not changed
- h' v! F% }/ \# u, ~; \# Q // indirectly by some other process).! x; ^0 X3 P: a( Y2 A
k( d+ i+ a& x0 Y5 Q9 \' k- ]; Q modelActions = new ActionGroupImpl (getZone ());
. T! ]" S0 Q z2 |! j; ~2 v7 f$ r% y/ [
try {
/ W/ I4 t" f: Q) r2 [- N! \. W modelActions.createActionTo$message
7 [0 _# I! s5 ], N/ n- Z1 L (heat, new Selector (heat.getClass (), "stepRule", false));
, m) t- e5 P& \! }% \ } catch (Exception e) {
) j* g# z& y, n) B( q System.err.println ("Exception stepRule: " + e.getMessage ());
6 J6 ?& ]) r+ n) }; V( Z" K% d }
! n7 {7 |0 |5 G2 P$ o! Y" W
& X3 W/ V2 k6 f1 {0 z try {
1 D% H8 A, W) d- s/ }9 t+ P6 ] Heatbug proto = (Heatbug) heatbugList.get (0);. g# u# b D' _& B& d0 {
Selector sel = : n4 c& b3 }! d
new Selector (proto.getClass (), "heatbugStep", false);
8 [. q; n# e, R" t$ [% s actionForEach =( t9 C4 I2 {5 \( w4 Q$ N) U
modelActions.createFActionForEachHomogeneous$call7 R7 u) D {8 `, b5 x
(heatbugList,
! |7 H+ y) o# H9 b% g- Z new FCallImpl (this, proto, sel,
4 w" e, x" s r Z) \' ` new FArgumentsImpl (this, sel))); H# w# E+ N4 Y# Q+ B. ?, ~7 u1 M
} catch (Exception e) {) J$ B4 D) ?' x- e5 X$ g
e.printStackTrace (System.err);
" L8 y1 M5 p: e- @0 E+ ^$ g }
- _3 L! H% w Y0 I) E3 A
! `( o, V* {& H' ] syncUpdateOrder ();
8 Y4 y: K' h6 M7 C# k2 t5 A2 J. `$ G2 ?1 m8 s
try {# Q' J. k1 N: ]/ T7 p6 H
modelActions.createActionTo$message - d0 T9 Y& Y# V
(heat, new Selector (heat.getClass (), "updateLattice", false));$ n. w# h2 i) g9 v/ B" x/ w2 h7 E
} catch (Exception e) {% C0 U3 i; w8 d H! s4 ~
System.err.println("Exception updateLattice: " + e.getMessage ());2 d$ I+ {4 V' `/ H5 v
}( g5 [# b- j1 J5 P! U8 @
4 T; _1 [# P: s9 ^ M' t' w- M
// Then we create a schedule that executes the( \$ ?! j& b! U% m4 v8 |
// modelActions. modelActions is an ActionGroup, by itself it
7 }# ]$ r- u( R4 {$ V // has no notion of time. In order to have it executed in# g* W& J! Q& P; k: R0 j
// time, we create a Schedule that says to use the
1 o, w4 `( w5 B( R/ g, ?. D; R // modelActions ActionGroup at particular times. This
$ _: k' u3 [, k$ @+ {( Z // schedule has a repeat interval of 1, it will loop every
4 ]4 J3 V; x" N // time step. The action is executed at time 0 relative to
# C0 s# Y2 O" @0 O) q2 X // the beginning of the loop./ k0 P$ t; e* X# F3 }
9 }2 B J! @/ n+ k5 w3 o
// This is a simple schedule, with only one action that is& S, t5 u4 X% ]3 E( U7 O8 T# ]' f
// just repeated every time. See jmousetrap for more r$ ]1 M( M8 O8 y
// complicated schedules.
% _' X3 u: C& Q- d( [2 x: d
+ _/ q* q' Z4 X3 I7 L8 Q modelSchedule = new ScheduleImpl (getZone (), 1);
! E, s" [; ], j modelSchedule.at$createAction (0, modelActions);
. \- ^/ G! c, v6 P
d2 T/ q% g' E. a( } return this;+ X8 [9 Z m5 \, O% G
} |