HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( r3 q3 \0 h' }! M7 g$ b' h p
0 v1 ?8 D! R1 g9 r. `# u public Object buildActions () {
$ O/ a- }" c3 g super.buildActions();6 W# u; i, a5 e$ E4 G/ n/ i" o/ x
6 s: U3 B2 i! R# Z; `( i
// Create the list of simulation actions. We put these in- e) r2 |+ b2 b7 P
// an action group, because we want these actions to be* I: p+ a. J8 |' d! |* ~
// executed in a specific order, but these steps should" f; E- ?( k$ e9 U
// take no (simulated) time. The M(foo) means "The message
$ u6 O% B% k; C @$ | // called <foo>". You can send a message To a particular
$ D$ U3 c% _4 { // object, or ForEach object in a collection.
1 v Q$ W7 S( H " U6 @) h' C; k5 ~ j7 v0 N; _
// Note we update the heatspace in two phases: first run9 [# G8 T& x1 m/ x0 o2 I3 P- \# }
// diffusion, then run "updateWorld" to actually enact the
+ B! M; A4 l( b( v9 I6 @) I4 k // changes the heatbugs have made. The ordering here is
7 N- a2 U5 {, M // significant!4 i/ L$ ]: G5 E: `
' k; i- }1 m% s/ A4 }- a
// Note also, that with the additional( W/ Z" h+ U" S& L
// `randomizeHeatbugUpdateOrder' Boolean flag we can: O% B& e$ I1 `3 G8 ?: j
// randomize the order in which the bugs actually run8 r% g# e$ ?. U5 n7 p/ Q: n
// their step rule. This has the effect of removing any
/ n% ^( P) w+ ^# I" v, L2 v // systematic bias in the iteration throught the heatbug
0 P; ?( R* T, x2 @# y // list from timestep to timestep- V; O: T4 ^$ O9 C/ _ {
Y6 I$ @) m. [# o/ y, J" w
// By default, all `createActionForEach' modelActions have w, u" K; x! X$ p
// a default order of `Sequential', which means that the. J0 g, N" f: l# w
// order of iteration through the `heatbugList' will be
8 y6 t6 G C; @ // identical (assuming the list order is not changed
9 `- _6 e$ q0 q4 u // indirectly by some other process). i( Q: {+ ?$ N" t
' K# I- }4 d) ~# m$ Y8 b modelActions = new ActionGroupImpl (getZone ());
$ c) ~& g- D( I; x) D
# Y8 I6 ^5 I& U" |/ ` try {4 w6 q- |& h! m( y
modelActions.createActionTo$message: l9 ~% U1 W' ^: t( |3 N
(heat, new Selector (heat.getClass (), "stepRule", false));
/ T1 I _7 `! m5 F8 T1 { } catch (Exception e) {
+ Y6 L& P4 D+ D System.err.println ("Exception stepRule: " + e.getMessage ());9 k4 X; D4 e$ Z7 j4 I8 c
}- }6 [# ^' V) w
* y' q4 m! O& G1 G# |( s
try {: f6 k6 \; i/ H
Heatbug proto = (Heatbug) heatbugList.get (0);- X2 v0 k' Q; R
Selector sel =
$ ~$ ?$ {/ \2 o( F; v# u5 t6 K new Selector (proto.getClass (), "heatbugStep", false);2 s$ e7 @5 G+ r9 t. o
actionForEach =; F* s% k$ G# [% {* `7 B7 D0 g
modelActions.createFActionForEachHomogeneous$call$ r0 V. C6 ?/ E" A8 O9 K
(heatbugList,
' i; \* \! \6 C* M5 t. V new FCallImpl (this, proto, sel, p0 n0 H9 d$ N- b8 F
new FArgumentsImpl (this, sel)));
, Z% T' W/ ]' Z, M } catch (Exception e) {
" ]: j# x5 c2 o" C6 J8 x3 w e.printStackTrace (System.err);0 t' w# d! P4 o' s9 e% @) e; E( |5 w' S
}$ \' A6 V% E* t/ t2 s+ G2 _
6 s! }) n' \, T/ S syncUpdateOrder ();
7 r. M6 N. ?6 g1 \5 m1 W4 b! O8 z, b; Y3 V* n
try {
" |& S8 R% J9 F1 | z2 R( E modelActions.createActionTo$message
( z$ w/ [6 ~( M$ u4 X% ^ (heat, new Selector (heat.getClass (), "updateLattice", false));
+ u J& \$ e8 Y) z- v } catch (Exception e) {0 p* i. m, Z6 k) g' T0 I7 v
System.err.println("Exception updateLattice: " + e.getMessage ());
4 w( ^3 P: ^2 M G }. H, h) P1 m3 h5 g# h0 H
# U8 H2 G) A" Z- q3 G
// Then we create a schedule that executes the
. X* s" T( o1 T) R$ ^ // modelActions. modelActions is an ActionGroup, by itself it
) t1 A$ c% [1 z& r1 s: B // has no notion of time. In order to have it executed in8 h1 Z! ]# q5 R1 N) \* d
// time, we create a Schedule that says to use the
. Z# }0 V' v, e7 Z! }8 G# Y3 v // modelActions ActionGroup at particular times. This' P* C) Z! L+ y* a
// schedule has a repeat interval of 1, it will loop every
* H' v2 C6 ^' b // time step. The action is executed at time 0 relative to; t( Z8 L, I# t7 h) l
// the beginning of the loop.7 G8 V8 L3 _- @( O) k( U5 W( ^+ P
: I8 Z, w- Z9 P% S: n // This is a simple schedule, with only one action that is
: A% [+ F' _5 a // just repeated every time. See jmousetrap for more
- }( \4 `. F. P% L // complicated schedules.
( ~" y3 r7 D% K9 O* g9 _4 E 2 h2 l7 f2 Z+ @- l+ w3 S
modelSchedule = new ScheduleImpl (getZone (), 1);
: ]5 C/ H2 n9 K c" E r+ b1 e modelSchedule.at$createAction (0, modelActions); G( [9 e. x* u! x6 f
, {9 A7 U8 l: O
return this;2 a" Q/ S- o* a6 M
} |