HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 U: X( m! p/ W6 v4 K0 M w" n, @1 S7 \& g# o+ ^- d8 a3 f
public Object buildActions () {0 R5 D" ~% I2 ~% y
super.buildActions();
) S3 {6 b) r' g: g* P7 d 0 o. Z5 X" ~8 `, ~3 R" y ]
// Create the list of simulation actions. We put these in
( O! q# n/ W3 U1 ~" w // an action group, because we want these actions to be
, |' K l- T3 w // executed in a specific order, but these steps should' z% j# q% j, y! D" p) s$ R
// take no (simulated) time. The M(foo) means "The message
9 b! u, N4 k; w+ w# _ // called <foo>". You can send a message To a particular+ |% b& Y5 q }( M- x4 s: a
// object, or ForEach object in a collection.
6 T* C: R" a0 I# y( Z3 ?% I, Q 4 f2 B9 z S( d# @2 W
// Note we update the heatspace in two phases: first run* o# B4 g7 J4 z- R& E3 S- [
// diffusion, then run "updateWorld" to actually enact the4 g$ d- h* M! J" X g5 A
// changes the heatbugs have made. The ordering here is, `6 z% x1 h/ `2 s* U% | ], h
// significant!' }# J+ O* y7 e- E$ N0 P1 B
7 M* a1 E/ i$ V
// Note also, that with the additional
$ r5 f& |! z2 G& \6 l5 D9 G // `randomizeHeatbugUpdateOrder' Boolean flag we can# e3 Z8 w, O3 j
// randomize the order in which the bugs actually run
1 o$ C( y, T8 I: B* z" x // their step rule. This has the effect of removing any
. p5 U. v' u7 N" ~ // systematic bias in the iteration throught the heatbug6 N' c$ Y' z/ j% M) v* u7 _; Q; f2 W
// list from timestep to timestep, T* _ e$ N/ |- i2 g8 K
+ j0 [8 t" i% ^ // By default, all `createActionForEach' modelActions have
' b9 W/ M% |8 G, j; E // a default order of `Sequential', which means that the. U# w2 ?. f, w1 \, W/ m# i
// order of iteration through the `heatbugList' will be
7 R$ S) i g# k, F8 @+ U0 g# V // identical (assuming the list order is not changed
# p1 S5 c1 L1 U$ z) N // indirectly by some other process).
, [. v' \6 M6 d
. k: n3 `' D- `! q7 S) }- |4 L modelActions = new ActionGroupImpl (getZone ());$ w2 \: ?) g$ U+ c$ L+ _" Q
1 {' }2 I! B' ^1 o3 S& O/ J; t4 I d
try {& G! K! p- _; Z0 `3 A3 H+ x
modelActions.createActionTo$message
* t+ \1 T1 p& M6 Z: d (heat, new Selector (heat.getClass (), "stepRule", false));
0 o/ e. S k, }& f! P4 O7 Z" _ } catch (Exception e) {
3 I' u! c, S* @5 J4 u System.err.println ("Exception stepRule: " + e.getMessage ());1 b" G4 K% u% _
}) J$ t1 \, i6 H# V! X, R9 E- C
( g/ g/ `" d7 q. e
try {; c! G& ~3 k" i- v( x7 i" K3 b
Heatbug proto = (Heatbug) heatbugList.get (0);
: ?4 Y' s. Y4 X( z z Selector sel = - @! k4 Z# _7 ?/ |# |7 C
new Selector (proto.getClass (), "heatbugStep", false);6 h# V' S" Q! n) C3 ?' r( P& X; V
actionForEach =
. |5 E6 k$ L+ ~3 `( _4 q/ B modelActions.createFActionForEachHomogeneous$call
, Z4 A8 w. o% w& K5 ~5 m (heatbugList,' Q7 ~5 t# \4 i: P* l
new FCallImpl (this, proto, sel,, F5 ^1 d- s* `
new FArgumentsImpl (this, sel)));- C5 E. B5 h# S9 T! F2 _$ R3 E$ ?
} catch (Exception e) {3 ]" y- `! z8 {" s( j5 a4 j
e.printStackTrace (System.err);
% i+ E5 z. k. k/ B0 b }/ J6 ?$ Y0 X* `# t! x; R [1 A& ]2 S
% t! V( p; Z0 C6 a
syncUpdateOrder ();7 t& V5 |) o+ o
c" I [/ s) w9 m7 w- {; R try {6 l1 R6 _8 A. @1 @, Y/ F
modelActions.createActionTo$message ! [# d: S1 G2 s" [4 y
(heat, new Selector (heat.getClass (), "updateLattice", false));% X, R4 M* B" Q5 p& J
} catch (Exception e) {
, q- ?7 s( L6 L0 U V( M7 I System.err.println("Exception updateLattice: " + e.getMessage ());1 `4 j( E# `) a7 y \0 }. q: D
}
) |: P u" |( g* f! a
! c2 H: K% C0 `( h // Then we create a schedule that executes the, ]9 A8 d; ]" y# o9 m, i
// modelActions. modelActions is an ActionGroup, by itself it
4 f& @. Q! D5 B! p // has no notion of time. In order to have it executed in8 n, @' l+ S( W- Q* v
// time, we create a Schedule that says to use the
9 l; o% F x8 { // modelActions ActionGroup at particular times. This4 O8 }0 o+ W* ?" [ `: j( N
// schedule has a repeat interval of 1, it will loop every- z# I J/ K( M
// time step. The action is executed at time 0 relative to
* v* T8 n' f9 \7 R // the beginning of the loop., Q- J y- z* p8 E$ o
1 l; g0 v" G' d+ } // This is a simple schedule, with only one action that is
5 W" `$ d3 @; H( Y7 C0 f8 K! x6 Q // just repeated every time. See jmousetrap for more
- Y% l1 \# j# D // complicated schedules.
( y3 {( D9 g3 h5 U1 } 6 A5 ^- ~+ N: Q/ B( u
modelSchedule = new ScheduleImpl (getZone (), 1);: z! g' }0 Q4 F& |/ V3 z% J
modelSchedule.at$createAction (0, modelActions);0 B7 K4 t7 G! W3 X y: F
- p, D$ A: Q. p( g5 D# Y return this;
- a, w: \4 r: I' t0 N0 C. Z3 s } |