HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, E4 }3 q/ k( t6 c3 P5 E- O3 _3 j3 u4 }5 p" w
public Object buildActions () {$ V3 N8 A5 I. g3 Z" Z4 x* v& d
super.buildActions();
( e- ~* o1 h% {5 Q4 V2 U- _, B9 _" W
. ?9 y$ _4 R6 Z8 F+ ~ // Create the list of simulation actions. We put these in& H0 A, n' d( ^* U5 m; I
// an action group, because we want these actions to be$ R8 ~7 Q- C; \( V
// executed in a specific order, but these steps should
0 ^/ ^" b& ^+ z4 M9 c: V- J. u& f // take no (simulated) time. The M(foo) means "The message
6 u9 n# p l, G7 J/ @0 m // called <foo>". You can send a message To a particular6 h4 B/ g W- t# }4 R/ a
// object, or ForEach object in a collection.
q" e4 Y$ b, e# v" c
1 R7 v5 N% [2 K& m& L // Note we update the heatspace in two phases: first run+ L2 U% v, M N, Z6 B$ @+ n
// diffusion, then run "updateWorld" to actually enact the0 C+ T' L! m1 w# t* ~2 r6 i7 O- V
// changes the heatbugs have made. The ordering here is
+ F5 y1 w4 G5 `# v5 h // significant!. P8 |1 s1 x0 h& y) q
0 P" m. b0 J) M/ ~# X
// Note also, that with the additional
9 O7 @5 j. e+ s // `randomizeHeatbugUpdateOrder' Boolean flag we can0 x6 o* t1 |: |9 ~
// randomize the order in which the bugs actually run
4 k4 O$ ~- `$ u$ U! c9 K/ |) G // their step rule. This has the effect of removing any
" j' M3 F3 z( [7 i M, m // systematic bias in the iteration throught the heatbug0 N) w4 A! f2 b7 F
// list from timestep to timestep
3 _9 D( B+ k& ^- @
' ]* K7 `( c$ S2 d+ W* S // By default, all `createActionForEach' modelActions have
4 i3 w/ n. j- j# ] // a default order of `Sequential', which means that the9 R; @# \2 k7 {
// order of iteration through the `heatbugList' will be2 j" [( [" F5 q" u2 N: q
// identical (assuming the list order is not changed
E' G! `' R8 o% z4 X% }& H% \. ? // indirectly by some other process).
X6 v8 {& A) u- G C& P
& J& z+ f {" ?& O) g1 d modelActions = new ActionGroupImpl (getZone ());* d& c7 z+ c+ v9 _8 c2 G
2 }2 A% h$ F# g+ |. ^
try {4 I* R% ?$ ?* ]; f, x @7 j/ F
modelActions.createActionTo$message3 I4 e8 u8 N2 ~( v+ C" U) O: M
(heat, new Selector (heat.getClass (), "stepRule", false));+ }+ @, T" ~5 m
} catch (Exception e) {
: B" g) W' D+ J6 e9 ~& F System.err.println ("Exception stepRule: " + e.getMessage ());
|8 h5 L$ P5 @2 f# }: t* r }
/ {( U x/ c5 x, N( l
, L4 U: ]. O, ^: w try {4 }4 o) @& K/ M" F& c1 r
Heatbug proto = (Heatbug) heatbugList.get (0);
4 l- i: K$ N; [ Selector sel = * K0 p/ u* \9 |3 V& }
new Selector (proto.getClass (), "heatbugStep", false);
) a% H1 _+ o) L actionForEach =
3 |9 V% H4 `: ` modelActions.createFActionForEachHomogeneous$call
2 E5 y& s8 E* r' N5 l/ s$ N( Q (heatbugList,6 A6 d; x' K/ v" Y
new FCallImpl (this, proto, sel,
& l, }+ m: {% V( @. U0 n new FArgumentsImpl (this, sel)));
/ [; f! z* U5 J& ^2 I } catch (Exception e) {6 M- S% W: g7 m# E" Z+ W
e.printStackTrace (System.err);$ y- R( M+ {1 v' i2 P
}+ _' _: \, C7 ^. e& O- N
. Z' e# G& ]" H6 e; m* E* S4 b1 y, F
syncUpdateOrder ();
3 u' v- p) K: f) I6 L7 K w
9 j7 M. M$ `$ x) } try {
, h$ T, ^4 i% r/ x. P modelActions.createActionTo$message ' }9 O. m6 D. K
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 t+ J ?: j/ B$ @% u } catch (Exception e) {
& w" D* G( k6 O& j; A: ` System.err.println("Exception updateLattice: " + e.getMessage ());+ B# c0 [% S: {$ b# \
}+ L. Z) E/ z3 m+ V6 V+ l0 j
; N6 s; A, T3 t5 b // Then we create a schedule that executes the
6 l3 K8 j" n/ C) u& ] // modelActions. modelActions is an ActionGroup, by itself it
. d; Y( e0 f8 O // has no notion of time. In order to have it executed in
, A( n1 e; F2 W3 ~/ d // time, we create a Schedule that says to use the4 k- e9 l6 T1 d: ^. \9 b) }' p
// modelActions ActionGroup at particular times. This
M% T! `3 ~& [ // schedule has a repeat interval of 1, it will loop every2 X( c3 t* f) i$ j) l- ?# j1 G
// time step. The action is executed at time 0 relative to6 Q; Z8 N' E0 g
// the beginning of the loop.
/ ~+ x: x" O$ j/ w- g1 ^& {& b. C& ^
// This is a simple schedule, with only one action that is
$ u1 O- J" {+ s( r // just repeated every time. See jmousetrap for more
7 N' a( n3 Q( l @% K) x // complicated schedules.8 Q- z- c1 m# Z6 R- k' L+ Y( A
- G+ Z; P6 ^* h! n modelSchedule = new ScheduleImpl (getZone (), 1);
9 Z' y' p# o2 `+ \7 E5 b a modelSchedule.at$createAction (0, modelActions);
' N( ?9 H3 T( n S0 x/ b , L7 u; K3 ]& S: ]& |, n
return this;
' A/ k% k/ W a } |