HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 E9 r: `3 U! W# ^9 A) f0 ?
6 w4 Z! u$ K8 h8 o% r% Y3 p public Object buildActions () {' r( I- ^1 \+ ]4 N: R0 A. X
super.buildActions();! p. ?; h2 s8 g2 A. j: \5 p
" _2 ?- X% k* d! I2 w3 o
// Create the list of simulation actions. We put these in
. I+ \! ^4 i1 Y$ P: H6 f1 V! W // an action group, because we want these actions to be
+ G6 z* Q; J$ h: o3 b: \9 d, \5 c+ w // executed in a specific order, but these steps should8 ^3 p$ z8 O' i+ O# ^4 G
// take no (simulated) time. The M(foo) means "The message
0 \. I. Q/ e8 D // called <foo>". You can send a message To a particular/ ~6 }1 n# D0 E# n
// object, or ForEach object in a collection.6 K! V0 E0 j6 v# q
' m6 l) E- I) p# t0 ~
// Note we update the heatspace in two phases: first run) s4 [# k0 j4 e
// diffusion, then run "updateWorld" to actually enact the/ u; n+ B; ]4 R9 {
// changes the heatbugs have made. The ordering here is" s Q) @7 p, M1 H
// significant!" Q5 K. f! i% A' q/ p3 Y7 D
4 r4 K) _& `7 @+ o$ c' `% \ // Note also, that with the additional
0 E+ y' Y2 v1 V // `randomizeHeatbugUpdateOrder' Boolean flag we can! f' c5 { R1 B/ @
// randomize the order in which the bugs actually run' ^/ }8 |- f( \$ c% I. o _
// their step rule. This has the effect of removing any* F" {* \- N, g; z/ t# Y
// systematic bias in the iteration throught the heatbug
- ^: h/ t. g, V4 n* ^& ~ // list from timestep to timestep' M1 u6 y4 @% g& C" K0 H% r
. o5 U7 u* p$ G' X; T! n // By default, all `createActionForEach' modelActions have
3 L( U6 J# ?, J8 L. ?( m8 Y: @/ k' | // a default order of `Sequential', which means that the# G! x. G$ F9 l: _9 `
// order of iteration through the `heatbugList' will be
$ U; H1 s u" C; @ // identical (assuming the list order is not changed
0 L9 F- U$ O4 k/ h // indirectly by some other process).' y3 ~ n1 ]1 K2 q+ ~5 Y
# S& I/ S4 P6 L( f7 c+ e' L+ v' g modelActions = new ActionGroupImpl (getZone ());
9 ^8 {* e1 M2 C& w8 o+ {: c
2 k8 t. v$ `8 D+ Y try {2 i# G4 }, `% i- ]/ y. R
modelActions.createActionTo$message A3 d/ Y7 \( B4 u6 |
(heat, new Selector (heat.getClass (), "stepRule", false));
7 A( g& }% g: p* X6 L } catch (Exception e) {$ l" f- { I/ k# d' P4 W
System.err.println ("Exception stepRule: " + e.getMessage ());
) o. T- C* G% j4 U- ^ }& ?" W* X0 z, G3 `
. u7 s* o. q* T2 q. p* l' b" r# K
try {
" w- b1 `" f5 y) G' F+ n1 O Heatbug proto = (Heatbug) heatbugList.get (0);; t& U0 t7 Z( Z: R9 n
Selector sel = + r% w, P6 Q( H; s
new Selector (proto.getClass (), "heatbugStep", false);
3 ]+ x/ N* v( |/ r. R actionForEach =
- U% g. p! ]0 E6 V modelActions.createFActionForEachHomogeneous$call, I k, W, q9 h
(heatbugList,
$ \9 b. g5 Q5 X6 ^ new FCallImpl (this, proto, sel, a- V( j$ d, F" x& d
new FArgumentsImpl (this, sel)));8 d) B! n+ t- p3 S
} catch (Exception e) {
$ {% A$ U( H$ [7 m e.printStackTrace (System.err);2 D1 C# E' K: w1 b/ G( S3 o0 G c& p# \
}
9 T# F/ E* @+ ?# b) I, ]: n! o
1 {8 E* z4 ?. c' q* E7 R% F syncUpdateOrder ();
' E: S" v# ^$ D" \% L( U8 |; P1 }, R+ P ]
try {
# Z7 @% r* l8 s modelActions.createActionTo$message
8 X/ @8 Q2 `' U# _" Z7 }/ m7 ]4 O' |( r (heat, new Selector (heat.getClass (), "updateLattice", false));" q# ?7 S6 F: i1 T# D: Z
} catch (Exception e) {- s2 U/ }5 f% h" s! W% b. A# w
System.err.println("Exception updateLattice: " + e.getMessage ());6 {7 ]; z, o7 |' F. M5 f$ p
}* p) ~4 Y+ | \; U
' D0 ~9 Y; L# b# Q2 m. s3 v7 Q" ?0 E // Then we create a schedule that executes the
c7 U4 ]" S* X" q$ V6 v# ]9 p // modelActions. modelActions is an ActionGroup, by itself it
6 I$ q. g9 ?0 \$ C // has no notion of time. In order to have it executed in
. c) z9 g7 d! z4 Z, w4 f // time, we create a Schedule that says to use the* Y( q) O" ?8 U, y) R" j
// modelActions ActionGroup at particular times. This
1 J8 n" w" Z$ I" f' |# \$ m // schedule has a repeat interval of 1, it will loop every
6 |- X& D/ H3 ~1 G // time step. The action is executed at time 0 relative to
2 u5 N | _" f& C; Y$ T // the beginning of the loop.
6 Q- D) B) F2 n; Y$ q6 E- f% F5 @
// This is a simple schedule, with only one action that is
9 a8 |0 ]' V# g // just repeated every time. See jmousetrap for more
) b8 z7 q0 ~3 C# @" v7 w& _$ @% I // complicated schedules.
/ ]4 L" V7 @, x ; h- ?* ?7 J2 @( t% Z! R
modelSchedule = new ScheduleImpl (getZone (), 1);
) l' J+ y7 @3 H9 |1 ?% ?* O modelSchedule.at$createAction (0, modelActions);& p' M4 }2 G& q% B* f: F& C6 V
3 b* ]9 B- k6 T; _7 N$ x return this;; T0 d, Q7 P# \" |2 K. U6 R' m& C
} |