HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: m5 D' N- @+ ~8 G2 w( \+ k3 k. M
# `. h- G3 d( x3 {6 U" d6 E public Object buildActions () {2 Y8 E0 y9 m2 p; F* N
super.buildActions();
; x6 i' m( p0 W$ Z' w
; X' s8 ~' ^3 A9 @6 y! s4 b+ {' H2 M // Create the list of simulation actions. We put these in
. Y( Y/ i* w) K // an action group, because we want these actions to be
6 i0 r- X B! ~- Q; B1 p$ _9 M // executed in a specific order, but these steps should5 P2 ?# v8 @ P9 V$ m
// take no (simulated) time. The M(foo) means "The message! C% U& Q* |9 R9 G- B
// called <foo>". You can send a message To a particular Z `) K" a& v- ?2 A$ ^/ N
// object, or ForEach object in a collection.
9 H6 ]- m. }/ m8 E / h& k7 ~3 h; |7 h0 m# a) X
// Note we update the heatspace in two phases: first run2 H# V) X, N* x" m9 t" r
// diffusion, then run "updateWorld" to actually enact the4 s/ {( ?8 u2 D' ^3 A
// changes the heatbugs have made. The ordering here is/ r; l+ ^% j* @ q, L4 x
// significant!. ~! ^0 I9 B; m: H; B
5 N. `4 u) l7 k, ~3 _* b3 z
// Note also, that with the additional
h- z! |. V1 K, v // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 v( W9 N4 ^; i/ Q) U7 H // randomize the order in which the bugs actually run
! _9 L( k4 p8 F9 e- J7 K // their step rule. This has the effect of removing any
( N: W" U+ x' l1 ?9 x // systematic bias in the iteration throught the heatbug/ g# r8 d. {1 X2 J2 v- }/ |
// list from timestep to timestep) Z9 _: O5 {1 H( g! J
' @$ s2 z3 Z# c. r$ h% I. _" g // By default, all `createActionForEach' modelActions have
9 p6 o) ^% _# d2 v1 B // a default order of `Sequential', which means that the
2 h" m: d. b& `, Q4 d // order of iteration through the `heatbugList' will be: v9 ^( a$ r0 w4 g$ g/ _! Z9 U
// identical (assuming the list order is not changed/ i5 m# F4 r+ i' v+ M* D
// indirectly by some other process).2 b9 A; Z2 y, J
8 b- r: x7 U2 B/ c5 x modelActions = new ActionGroupImpl (getZone ());7 B4 [# Y/ m. c! y
- a" [1 G8 Z/ C. v/ Q/ z3 C try {
/ u' o6 F5 C6 O6 h; z modelActions.createActionTo$message' F5 J5 _5 P% I b5 a& [/ ^
(heat, new Selector (heat.getClass (), "stepRule", false));
# c* }, ]! N% r* {# i6 z } catch (Exception e) {
6 E9 P0 U# D6 A+ [1 Z2 [5 D System.err.println ("Exception stepRule: " + e.getMessage ());
g8 \" U1 F. j6 l1 K }2 x. I9 t3 O* w# X6 |8 M
1 }* m) Y9 O, E/ t try {/ `# A9 W- _- z3 H0 Q% V) Q$ y$ X
Heatbug proto = (Heatbug) heatbugList.get (0);
. C# l7 i( {8 M$ I Selector sel =
2 u* k" z) L( _3 o; i# w new Selector (proto.getClass (), "heatbugStep", false);
, j7 ]' V- c0 V+ h4 N# u$ l actionForEach =/ i6 e6 }' }4 [8 b6 A4 P
modelActions.createFActionForEachHomogeneous$call
" `( F0 o; ?# b (heatbugList,
7 S- E" u7 h( Z7 r2 m new FCallImpl (this, proto, sel,0 q, A5 X) e7 z( r* J+ p
new FArgumentsImpl (this, sel)));9 A2 p( s: v1 h) E3 R/ W. T# c
} catch (Exception e) { Y- d2 _4 O. \
e.printStackTrace (System.err);. \$ D0 g! I( }2 P
}
/ W8 Z* s$ b r: N9 j
5 m4 S( t' b1 t! R6 v syncUpdateOrder ();: D6 x p( d7 t5 f& N
, X- V7 ]: A$ t. d6 F# d try {
' t! [, ], G9 ]5 |7 x modelActions.createActionTo$message + z! e, w, r8 g. R5 |# a w
(heat, new Selector (heat.getClass (), "updateLattice", false));! D+ w1 R( B- i8 c* V- l9 h
} catch (Exception e) {
g) w) y1 u+ T* [: w System.err.println("Exception updateLattice: " + e.getMessage ());
' V+ Z0 c3 a) d6 h5 |2 T5 r/ M8 m }
7 ^5 _* ^+ Q5 c8 X0 A5 G 4 b9 p% B0 O3 L- f& e5 t) ~4 b
// Then we create a schedule that executes the" h( a9 P* ^( Y& l/ X( J+ w
// modelActions. modelActions is an ActionGroup, by itself it
) k- S& ~( n. b // has no notion of time. In order to have it executed in
" G# k d. p' c8 r- r* b ~ // time, we create a Schedule that says to use the
4 z8 ~# o9 f' {7 E% a- L1 M // modelActions ActionGroup at particular times. This
' ?" p v. ^/ ~& k) L& s // schedule has a repeat interval of 1, it will loop every
% \6 {1 w2 I1 |6 P" a7 t // time step. The action is executed at time 0 relative to
0 a2 H8 T; @5 A2 @3 }3 s7 ` // the beginning of the loop.
% T: W" E& f6 X7 m7 |; W
/ M. I1 q* \2 }) Q4 r/ E; r // This is a simple schedule, with only one action that is0 d- s& S4 N }2 |
// just repeated every time. See jmousetrap for more3 B' K/ O' s2 N; j9 D# R$ g- M
// complicated schedules.
0 P' r" A+ d9 i6 F: \! l" h: x- r ; ~/ M: ]3 u1 g9 R* e0 c4 w. K' f- p
modelSchedule = new ScheduleImpl (getZone (), 1);& F7 p, E4 Y# a: j
modelSchedule.at$createAction (0, modelActions);
/ e4 S5 S" z, X) f M& | j) Q' J4 w/ m" ~
return this;
8 Y' v; V: d& r6 X& k3 ? } |