HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# w1 }& H% c( o# S/ ^$ T
- x: g$ M/ }! E5 }1 x public Object buildActions () {
- X8 e+ v, R4 ~% i: R super.buildActions();' ]4 D7 f$ L. H# I9 g8 U
, j" n: O2 m1 r5 v X1 V7 w5 v" S // Create the list of simulation actions. We put these in6 a# p$ x) [) p0 j& X
// an action group, because we want these actions to be+ A- x! |/ a8 r0 g/ l
// executed in a specific order, but these steps should
3 {- E3 T8 n2 |+ I' n! E, b // take no (simulated) time. The M(foo) means "The message
3 T$ S3 f1 D$ G5 E0 O R* Q // called <foo>". You can send a message To a particular
) A5 n t9 Y/ o) y9 b R/ j // object, or ForEach object in a collection.6 n7 e# d+ v! [
4 D' Y# w, n$ q% N0 S // Note we update the heatspace in two phases: first run3 j3 w; o. H% g
// diffusion, then run "updateWorld" to actually enact the( p+ m8 F# G; k Q8 {7 [
// changes the heatbugs have made. The ordering here is
% T* V S% M. V // significant!
& m1 ^$ V% ]1 c1 N1 Y5 {4 r$ k& l. b5 L
/ N9 f" S: r \: ]( U // Note also, that with the additional
' {( t+ D8 t- e a* `9 Y* M* Z: ] // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 \) i4 L& E* {8 j/ J$ z // randomize the order in which the bugs actually run
( e9 F* J: Y4 A- I& N$ ` // their step rule. This has the effect of removing any
3 [0 {) H5 h3 M! c // systematic bias in the iteration throught the heatbug
6 l. J+ _" A( ~ // list from timestep to timestep/ Y0 ^, s6 \$ d9 t: {
1 @) ]: N; n( M1 L8 T
// By default, all `createActionForEach' modelActions have1 R2 O1 F* ]& J! ]$ z' [
// a default order of `Sequential', which means that the
( B4 Q3 S! h2 ~" b: i // order of iteration through the `heatbugList' will be
8 o; M; x- M& s7 a4 m; C // identical (assuming the list order is not changed+ l. m* ^- q! m" ~5 M8 X7 j- d
// indirectly by some other process)., L0 `6 O1 e- O. O; ~
# o( S( _4 R5 I7 s k* A
modelActions = new ActionGroupImpl (getZone ()); ^, [1 S% u2 J- L
) [1 }( W( T/ S% b try {
9 N( H& a; \" v% t! @ modelActions.createActionTo$message' y6 J( p. ~7 ^1 f' f7 l) x
(heat, new Selector (heat.getClass (), "stepRule", false));! |. o6 P _# H% l
} catch (Exception e) {
( N9 o4 F! W5 w. r9 w$ B: S* X System.err.println ("Exception stepRule: " + e.getMessage ());9 E# I; W. h5 S( y) ?2 f
}
$ n$ Y _- ~# q7 Q+ m
* F7 C/ E+ z' g) {( H7 q \ try {, G, X* J- M; L$ ]' V: V" r/ P
Heatbug proto = (Heatbug) heatbugList.get (0); v) ?2 W1 H. [( e( i# U: X' Y0 s
Selector sel = ) i$ [0 G( U6 Y
new Selector (proto.getClass (), "heatbugStep", false);
7 H$ e2 q7 E7 s, p: w" _+ D6 w, R' n actionForEach =
8 x! H$ g' I2 O$ B, q modelActions.createFActionForEachHomogeneous$call
2 S7 _5 w7 s4 y. \8 V. w (heatbugList,
2 [0 X1 I' K( T8 G; e4 x new FCallImpl (this, proto, sel,
! p# J# b5 f: J @ new FArgumentsImpl (this, sel)));, g; F% F. Z" t- r8 X; ], X9 R
} catch (Exception e) {
& e; |: v) h4 y2 j0 [7 v d {8 R e.printStackTrace (System.err);' W) s& l* K# n
}
5 O$ F% e( V- d
) P) g3 h3 g* ^& C1 K0 H* U( ^7 u8 y syncUpdateOrder ();
( f7 O8 U1 j# e4 W# ^) P: \. Z% |6 _; J
try {
7 q/ C0 ^1 H5 B2 b! X( s% Z modelActions.createActionTo$message
5 T5 J& ^ _' O% G) V! y- s$ C (heat, new Selector (heat.getClass (), "updateLattice", false));) g& q: I# F I
} catch (Exception e) {# ]9 K6 ]1 [ a/ z! ^7 G
System.err.println("Exception updateLattice: " + e.getMessage ());
* c6 N% O, m2 V4 _ }( n+ U+ N' l. H1 u
: S# b0 X- _' N1 ^0 S // Then we create a schedule that executes the. L. l8 M& \. Y D: ~/ |' `/ H
// modelActions. modelActions is an ActionGroup, by itself it; P# R3 n0 T7 I m( { c8 f( A
// has no notion of time. In order to have it executed in
. I" N* T. M0 k% G // time, we create a Schedule that says to use the
5 _, {* V6 ~ d2 H0 Z // modelActions ActionGroup at particular times. This4 q. u( M1 D& [" e2 s, `8 `
// schedule has a repeat interval of 1, it will loop every
0 d7 P) N6 p! J) E9 W# x // time step. The action is executed at time 0 relative to( N# J h/ H- W* `5 `" V6 U
// the beginning of the loop.0 O$ I3 {. `" D! {
. f8 K' x2 S! ?$ P/ ?: q
// This is a simple schedule, with only one action that is
, F7 V( `/ I' |. Y // just repeated every time. See jmousetrap for more6 u) E0 c/ g6 L7 L! t: g
// complicated schedules.( ?: Y( D6 }0 j; j
& V3 Q8 z1 E. \- V& z modelSchedule = new ScheduleImpl (getZone (), 1);
/ H3 i0 V- c, v# Y0 a5 ?. | modelSchedule.at$createAction (0, modelActions);# z6 f* k. A3 x% n5 X2 d6 k5 J
1 B- ~1 w# q6 C c+ L6 ^
return this;
/ {' F! R6 M* ?1 {2 S } |