HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ C( @7 v4 S G$ }$ z+ e
* G# \* R1 P4 h7 y; m0 ^) R
public Object buildActions () {
$ s& H, ]7 N5 E9 ~1 \0 K3 h9 j super.buildActions();
* `. p" D6 h, ]2 b, v , `9 y2 Y. k6 c/ @ y
// Create the list of simulation actions. We put these in1 |# a3 `; v& ]( I; L+ _
// an action group, because we want these actions to be" h5 b7 s% d/ U/ S9 \9 n: m
// executed in a specific order, but these steps should/ [. D! ~) t+ [
// take no (simulated) time. The M(foo) means "The message+ ~4 E: j. S3 n, [) V6 h
// called <foo>". You can send a message To a particular2 @3 M7 ~( a: _6 J; ]# U ?
// object, or ForEach object in a collection.
, k; c3 ~. q4 w1 k1 k' }. X% u# x2 |& _% } 3 Q5 j2 ?" j- g6 E' ^6 m5 k6 s
// Note we update the heatspace in two phases: first run O9 L: A: }/ W" { B" |2 J
// diffusion, then run "updateWorld" to actually enact the
) k0 X5 _$ f9 @: v. ` // changes the heatbugs have made. The ordering here is
' E$ L; K" v \# t. m. g // significant!
8 F. x% A6 v( N
& R0 ?" s+ ~& K' {% m/ i% v // Note also, that with the additional8 y$ _" `3 R+ g0 _* b0 b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( U$ {. P0 x& J5 j& ~8 o$ d // randomize the order in which the bugs actually run
( O; x$ [( U' @+ n# Y' n // their step rule. This has the effect of removing any
& V* ?& n4 t2 }9 V+ V! L% h. ?! u // systematic bias in the iteration throught the heatbug
: z+ o" i2 ~0 `% Y- A // list from timestep to timestep
8 T: |: E6 _9 r $ i+ Y% x8 \ B$ n+ u
// By default, all `createActionForEach' modelActions have
# y8 w$ w6 q# r1 H! L. n- @% ? // a default order of `Sequential', which means that the( F8 N" {' [$ r" h& h3 o, W
// order of iteration through the `heatbugList' will be+ J; e _# g: j, [7 p6 E
// identical (assuming the list order is not changed' Z3 D9 q3 V$ q! Q0 o4 [/ c1 f
// indirectly by some other process).
/ X: B' C4 l/ p: L, M2 M. H9 C 2 ]$ p5 Y; s/ v9 `6 r
modelActions = new ActionGroupImpl (getZone ());
: I+ T6 c* g* ^, V) S/ \2 p& `
) P7 ]( }, `5 y+ e5 B. d3 m* d1 J8 D try {
. E% `2 ~6 Y6 _# q modelActions.createActionTo$message6 i. ^6 u: B- K O. \4 _2 I/ F, j
(heat, new Selector (heat.getClass (), "stepRule", false));
% C$ u8 Y+ z- ^. W } catch (Exception e) {2 n; o4 I U% a' @" h' y
System.err.println ("Exception stepRule: " + e.getMessage ());: Q3 U* ]: L4 C/ F8 C1 F0 i
}
C6 ], p# M% k% e2 X4 Z
( m$ f E* P; z2 ~8 b. o try { h+ y/ c) \% S" ^0 z8 c
Heatbug proto = (Heatbug) heatbugList.get (0);
f: Y# K5 k' w$ n; d Selector sel =
2 O6 \+ a% K5 J8 ]' ?% p7 d new Selector (proto.getClass (), "heatbugStep", false);9 q, t/ D; y9 f0 b
actionForEach =8 \2 m1 f( s, y- r
modelActions.createFActionForEachHomogeneous$call
) m8 ~5 J+ e' s3 X' V: c# x) u& O3 @ (heatbugList,5 R! E& J# p) R4 [( K$ w
new FCallImpl (this, proto, sel,% L1 e% z6 n, ~7 S- W3 N7 r( _6 h% b
new FArgumentsImpl (this, sel)));" M- b: O. z1 I2 r" ?
} catch (Exception e) {: v) E, G4 k7 b4 m6 y! A
e.printStackTrace (System.err);3 f* h# `) J! F1 {& B- Z' {
}: y7 g. x0 @$ x4 c! X7 p; q
+ ~0 ?& n8 J6 j; G: | syncUpdateOrder ();& t, c) \* h6 k2 [" j2 H- A
2 l7 g2 U9 ?/ [- e! X
try {$ S( `6 Z& l2 P6 F( |
modelActions.createActionTo$message ' o: i7 V ~7 X' k, a
(heat, new Selector (heat.getClass (), "updateLattice", false));6 F) F E: j9 l
} catch (Exception e) {% a4 F: l/ V0 A( \, {; {
System.err.println("Exception updateLattice: " + e.getMessage ());- T3 h: v- [. m* D0 o3 X. C
}
. w1 F9 y/ k0 Y
- y% r/ w7 ` s$ Y" j // Then we create a schedule that executes the% f. F' l# a4 P1 E# J* }7 C) N
// modelActions. modelActions is an ActionGroup, by itself it
0 L- Q6 e$ a* ? N% T // has no notion of time. In order to have it executed in
0 X( s8 i1 z- ~ t' f // time, we create a Schedule that says to use the& j: V" N% G6 f
// modelActions ActionGroup at particular times. This$ r! d( u8 k8 |8 U
// schedule has a repeat interval of 1, it will loop every
! V9 O+ \( S8 z! J2 E // time step. The action is executed at time 0 relative to
) z0 f3 F. Q7 u9 n6 o! q) m; q: @7 b // the beginning of the loop.* ^6 f; I) [6 c0 Z, M
% _( r$ \% g# P% I
// This is a simple schedule, with only one action that is: b' W6 b* X( t
// just repeated every time. See jmousetrap for more4 q4 S6 S7 o2 Z, u. \
// complicated schedules., \# T7 L, ^8 b: j; E
" n6 s% {* b8 S3 k2 ~6 D
modelSchedule = new ScheduleImpl (getZone (), 1);
H9 a- c8 O$ R# Z( g: N& y: L8 { modelSchedule.at$createAction (0, modelActions);' e, n$ g% A# ]+ x+ H3 U* M# n5 f
- r: D5 }5 {' V& P+ w$ @ return this;
3 } [/ S3 Z% Q, e$ [( f- H4 m } |