HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 }- a0 N4 W# g2 X" P8 |1 _6 @! t
9 z' s2 b& I: S, _' {7 f5 ?
public Object buildActions () {
9 r9 P* Z# b/ { super.buildActions();% m- b) ~0 u* Q8 b: k `2 B a/ v$ t
/ Z; d" p8 ~' L# b& _
// Create the list of simulation actions. We put these in
4 K) L9 c+ j* x) I# Y \( S8 o5 D // an action group, because we want these actions to be
( U* r9 a; F- e% `, \+ Y // executed in a specific order, but these steps should) E! D( f. }1 G2 \) f
// take no (simulated) time. The M(foo) means "The message
8 j, g0 Q( } `) l! \8 r0 k" v // called <foo>". You can send a message To a particular
/ f2 A& A* X. b+ S // object, or ForEach object in a collection.
! @$ }3 d) q9 E7 d$ ^' w- t ( G7 U+ i8 L. k1 c, k1 }. P) k* f
// Note we update the heatspace in two phases: first run+ b7 c K* U) @5 q/ @3 E
// diffusion, then run "updateWorld" to actually enact the
4 P. m' S% F l5 k5 O- P // changes the heatbugs have made. The ordering here is
& D/ k" A8 Q& r- s+ W0 z // significant!
: _0 ^0 G7 s5 g. E4 M: i
r1 ~6 P# [+ Q7 q // Note also, that with the additional
3 Y! ]4 J, p- c4 e' F) C6 [) S3 S // `randomizeHeatbugUpdateOrder' Boolean flag we can E+ |1 [+ {5 @$ ]. }2 _" C
// randomize the order in which the bugs actually run
, [- ~/ G& U% d0 A% b7 C) d" r // their step rule. This has the effect of removing any
! }" Y( S$ F; G9 h // systematic bias in the iteration throught the heatbug
$ M+ [( n8 F( l; h* `3 h // list from timestep to timestep$ J& b# V& L- U% u5 r& _9 p
: o2 U+ }, F' w) \! T
// By default, all `createActionForEach' modelActions have
6 }3 k2 m. a4 m // a default order of `Sequential', which means that the$ ]* m! d& ~( g# f Z
// order of iteration through the `heatbugList' will be
% \) J2 ]$ }( ? // identical (assuming the list order is not changed3 T$ M! [2 R; Z: L N% H7 G! E
// indirectly by some other process).' p& r2 {2 [ U' g6 D& y, g
* j, I4 M$ S0 m. m8 ~& O& B modelActions = new ActionGroupImpl (getZone ());
: L' } z$ s6 o% t4 _% y3 ?1 E4 E+ `8 I+ r
try { |6 a6 n5 g: B2 m, v4 K" s9 J. s
modelActions.createActionTo$message
" h( d" |0 t) Q" b: F! w. e0 _ (heat, new Selector (heat.getClass (), "stepRule", false));
^5 R8 f! J' g9 H3 T } catch (Exception e) {
* b! K* z. o+ v! G. R System.err.println ("Exception stepRule: " + e.getMessage ());
+ S) u( _1 u" `; `" k, n; ^ }
' p+ A( ?% G& a& H: C
4 l/ b ~* U$ C try { m5 a: J& R3 k i0 \
Heatbug proto = (Heatbug) heatbugList.get (0);
2 G9 f, J5 h' G | Selector sel = # a0 [" l& X9 O( q Z! p
new Selector (proto.getClass (), "heatbugStep", false);
7 d9 P9 S) ]% e. I8 T actionForEach =
# r \ L" R! e modelActions.createFActionForEachHomogeneous$call
! d: j. ]5 w- B" ~ (heatbugList,' u* T m! k$ Y: _4 s
new FCallImpl (this, proto, sel,) G9 k1 ^- x; q3 m0 s% {
new FArgumentsImpl (this, sel)));
+ n1 |# L7 T B0 n5 U3 n$ T$ A } catch (Exception e) {
2 |/ r( t2 T0 {9 n e.printStackTrace (System.err);
2 {/ I2 j$ e( ?3 Y5 w }
4 J6 L$ L! R* e; K+ h* U3 } # U, h e+ @" B% B
syncUpdateOrder ();
! c- U8 ]; w& ?( r l$ t2 O ]; n. x' o2 w
try {
0 A& R% ?% A1 c modelActions.createActionTo$message , A* C- _1 K: q3 X, ]& I7 J0 W
(heat, new Selector (heat.getClass (), "updateLattice", false));+ o$ {) f& Z. i, f U* v
} catch (Exception e) {8 \: q# f' d/ n7 K$ c- Y- s
System.err.println("Exception updateLattice: " + e.getMessage ());& T1 a. d/ C4 G1 _
}
% w% g' ~8 l+ O
7 R# _2 U; f0 b // Then we create a schedule that executes the$ _# S& o6 z! d" V9 R9 Z; w- U% q
// modelActions. modelActions is an ActionGroup, by itself it7 S, f1 @ g7 _/ M7 U9 ?+ d
// has no notion of time. In order to have it executed in
2 H* {7 G, i0 j ~, V+ ]. [" i+ ` // time, we create a Schedule that says to use the7 R j: g; M( t0 f" [
// modelActions ActionGroup at particular times. This9 i/ b# Y3 e. M/ o5 F( @3 r
// schedule has a repeat interval of 1, it will loop every
* E1 U4 |* F; ~6 e' a1 `. p // time step. The action is executed at time 0 relative to& Z [ p7 P! ]) u6 G7 S7 T" W5 [7 e
// the beginning of the loop.5 R! L: o% o( L( ^
: O0 s2 w& w0 z6 d0 @
// This is a simple schedule, with only one action that is1 ~) @* t* s. ~0 m
// just repeated every time. See jmousetrap for more
. u- O- ?7 ]% Y1 h // complicated schedules.
5 n# B; O3 ]* `/ V / X0 }) L" m5 z R
modelSchedule = new ScheduleImpl (getZone (), 1);
0 N* N8 l/ h' k; t1 H8 U modelSchedule.at$createAction (0, modelActions);
) K. s/ l7 q4 e2 k. U' D 4 N9 ]6 `" d# f, ?, x2 d- A0 W$ W
return this;
/ O2 [2 B; K# f7 u8 [ } |