HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 q0 U; i" |! R0 a( S7 ]% S8 a2 s% A6 W D4 R7 s" L
public Object buildActions () {
+ P b" V" Y" l n# o super.buildActions();2 ~( v, z3 c- V5 G2 o$ q# [# `9 M
2 H# L: j( A+ p
// Create the list of simulation actions. We put these in: o: v; h: a# T! B, n2 H- F6 M
// an action group, because we want these actions to be
5 U) A7 }4 r7 o' E$ b" ~ // executed in a specific order, but these steps should+ Z( l \ G6 C4 h4 p. \% Z) [
// take no (simulated) time. The M(foo) means "The message
% @: g: G+ b$ m8 a // called <foo>". You can send a message To a particular5 p5 `) I& `, q! [1 e6 P+ e
// object, or ForEach object in a collection.
$ z. c) X( {' g! q
) K' O, b8 {( V( B // Note we update the heatspace in two phases: first run
( _# x& Y! k$ l9 w // diffusion, then run "updateWorld" to actually enact the
; H Q: t* q7 Z // changes the heatbugs have made. The ordering here is' E- c3 p5 R% K( H) \ U
// significant!+ b7 \ i- C: ?$ h
* Q% _' I* H2 R. r: o7 ~/ A // Note also, that with the additional& D( H6 m* {! V! Y! Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, R# z0 M( F( X! \2 f3 Z7 ~ // randomize the order in which the bugs actually run
4 b% {3 f7 O- v+ @9 C // their step rule. This has the effect of removing any
2 ]% T% Z- M% g8 }+ S$ S* f // systematic bias in the iteration throught the heatbug7 E: @- Q# @& x$ e' Q* ]$ M
// list from timestep to timestep4 ]! J, ^% t3 T5 G5 q7 G( ~
' L9 {; Z0 [+ {+ a \: B% X0 s
// By default, all `createActionForEach' modelActions have
% R8 O+ q e% ]- ?( k3 |7 j8 K // a default order of `Sequential', which means that the+ s4 X7 S! S# _$ g+ c
// order of iteration through the `heatbugList' will be8 U2 F4 r% w7 Y8 S- o! R
// identical (assuming the list order is not changed
4 q) Y! M% H0 a8 l6 K+ H( E // indirectly by some other process).
" F) ^# ~8 _% |; ?/ G7 ]) l' q3 `
4 x" [& S. P. o( N P( I modelActions = new ActionGroupImpl (getZone ());
/ b; z+ ]. g: P( E' ?* m3 K7 U- x; Q+ p& V: j
try {
4 C8 i2 W& n+ Y" b: g$ p modelActions.createActionTo$message. v% L1 k7 h. ]$ U
(heat, new Selector (heat.getClass (), "stepRule", false));
/ L( ^( i! I% c& ] } catch (Exception e) {
& ^/ O- |; [) [# S System.err.println ("Exception stepRule: " + e.getMessage ());8 U1 G. r% S0 {2 D! z0 t. `
}" A1 @5 S4 T; G: n I" G: m# e
# \2 g# I: y( R# n try {" N& n1 S4 h# w8 Q
Heatbug proto = (Heatbug) heatbugList.get (0);
' y% B+ y" N' c3 S Selector sel = 7 u8 z" @) p% p3 c! g, k% I
new Selector (proto.getClass (), "heatbugStep", false);! ^$ Z3 R% q' v$ Q* C! h
actionForEach =8 k" Z; i: u! p$ T9 I
modelActions.createFActionForEachHomogeneous$call
. e4 e; d% C( z' A+ |- Y (heatbugList,
( v+ {2 C1 J7 m( j new FCallImpl (this, proto, sel,) u0 k, `0 g7 r: [: m ~
new FArgumentsImpl (this, sel)));
$ d/ _- D/ \, u9 `/ _2 q } catch (Exception e) {5 G) d9 S# x* Y
e.printStackTrace (System.err);! F7 H ^6 J0 P. g5 `8 P
}
2 ^4 [! n/ t9 |* b" Z# {
' r2 O: s- ^2 A6 g syncUpdateOrder ();' j3 j1 g4 P/ B, U4 O4 j
0 q1 J3 P( Y4 `# m, I' t# a/ [( k8 g try {
* p* m0 Z+ b& B; V: I8 c& w modelActions.createActionTo$message ) A: P# n( G3 E
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 f4 H. I# i, x& l) f3 ^+ S* Z3 B, Z+ J } catch (Exception e) {
+ T g4 b# v1 m System.err.println("Exception updateLattice: " + e.getMessage ());" Q2 ^2 u- R) I" M* a
}- D1 e" l$ S& S% d
; a; z$ o7 D$ F$ X, j X
// Then we create a schedule that executes the
% h M! j4 ` |0 s& E // modelActions. modelActions is an ActionGroup, by itself it
+ I8 s$ r% m g, i) V: i" l) _$ ]2 w5 \ // has no notion of time. In order to have it executed in
0 o: S# T* J S6 D3 ^5 r7 | // time, we create a Schedule that says to use the
; w) z) n7 T2 L$ c. b6 ~4 i9 `+ _ // modelActions ActionGroup at particular times. This
& h5 X+ K8 \' q# I9 w' L // schedule has a repeat interval of 1, it will loop every
4 f: ^% D s% w" C // time step. The action is executed at time 0 relative to% h+ i+ ?: V0 t0 ^6 [4 Z
// the beginning of the loop.1 E9 W- c" |5 q
7 h, ^9 M6 z# }8 `8 Y# j
// This is a simple schedule, with only one action that is, @+ u5 z2 |' ?4 e% H9 w! z6 e1 v
// just repeated every time. See jmousetrap for more
0 ?! t* {4 p' d+ w: o // complicated schedules.
7 ^$ M) M! ~ _. N$ i
# M# w+ _" L) F! ~* n* m+ M8 [ modelSchedule = new ScheduleImpl (getZone (), 1);
$ Z: ]/ R H3 i6 f/ J modelSchedule.at$createAction (0, modelActions);
8 y/ k4 q; K7 t 1 y6 b- ~3 m" U6 u( q0 a `
return this;2 u7 u3 E9 e. R: _! g9 U. D- t
} |