HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 I' n- _0 J7 D* Q# s6 r- |
' t) ~& i) _# t; B1 z public Object buildActions () {
3 F; P) e2 S8 f3 f/ e" O& v; ` super.buildActions();" o6 F, {2 ~: @# l, S
7 Z* ^% v9 M* i3 S // Create the list of simulation actions. We put these in5 E3 ~" I2 M3 b+ `# T( V4 Q0 t
// an action group, because we want these actions to be* g& T+ x+ d3 e. g" w+ o
// executed in a specific order, but these steps should/ y# J2 h, B; @5 D
// take no (simulated) time. The M(foo) means "The message
% C) q4 `( }, @8 o // called <foo>". You can send a message To a particular
- ~3 u/ b7 M, b' r5 Q7 j // object, or ForEach object in a collection.
h3 s& r! f r4 ` 8 A( h4 ]* Z. g: z! ?
// Note we update the heatspace in two phases: first run* }& ?! @' O: d, M% M+ m; J
// diffusion, then run "updateWorld" to actually enact the$ O; `; D1 p3 z# G7 y
// changes the heatbugs have made. The ordering here is
; i2 [! i# G' E3 L: [- R& _ // significant!
9 X; u: j& O) R3 i8 w; b1 R ! ^) Z5 w8 ^4 U# k
// Note also, that with the additional% L5 Q" ^+ W* \, k
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( d/ _( D- D0 T4 f' J% U0 V // randomize the order in which the bugs actually run
& L* D/ a; g! h4 h // their step rule. This has the effect of removing any
2 }: y8 o5 L; l6 Y' O- m! U // systematic bias in the iteration throught the heatbug
, a7 N: Z1 k9 h# A // list from timestep to timestep: G4 {9 v1 E2 S1 B7 f
' o8 c' I( S$ b; Q // By default, all `createActionForEach' modelActions have# ?* K" m- T$ e9 w8 Z* t/ ~
// a default order of `Sequential', which means that the6 c1 e) m9 M; a, y3 Q
// order of iteration through the `heatbugList' will be
: Z% K8 F# L& t0 v5 ] // identical (assuming the list order is not changed
8 f2 p: c: K5 }6 V // indirectly by some other process).
( F5 v1 s. c* K8 f0 }5 P& I4 Z+ q
^: ^4 D* d @5 C" U% q modelActions = new ActionGroupImpl (getZone ());6 B x6 L; B& _; Q1 T
) z/ I6 D: ~) j# o# M- R" c: b
try {0 B' \8 O1 o3 R" G+ g, e/ B
modelActions.createActionTo$message; b3 L) D; f/ Y. `8 }' O
(heat, new Selector (heat.getClass (), "stepRule", false));) b' f/ k* u" d0 ~
} catch (Exception e) {
( q: L" S) Q# U2 I6 D System.err.println ("Exception stepRule: " + e.getMessage ());. ?) v" N) ?# F+ A: `2 n
}. s" M: `; I r& I8 ?
6 w2 \' ~/ @# n; h5 H
try {
4 X: d& n) [- b# J' C Heatbug proto = (Heatbug) heatbugList.get (0);% L% c( h( H& }# S5 h' \3 p3 H
Selector sel =
- L/ b5 [/ I! N. N new Selector (proto.getClass (), "heatbugStep", false);
. m5 c# h U; [" j actionForEach =
/ b" c$ K# E- ^& F C, e& I1 U modelActions.createFActionForEachHomogeneous$call: a o* }9 \& `6 y$ B
(heatbugList,8 t! m2 x$ H7 w* V r7 t% U
new FCallImpl (this, proto, sel,; a& Z; f% K; ^; \$ t9 K1 O3 x
new FArgumentsImpl (this, sel)));
! B& r1 O2 e6 T5 Q( G* \ } catch (Exception e) {
9 j5 b8 a/ c+ R* Q( j/ T9 {" z0 `: p e.printStackTrace (System.err);) P, B. n( V& D& d: I
}% x0 o+ t% p h2 q
4 ~1 W5 X, c. {5 `, J syncUpdateOrder ();! `& C: p- N3 V* ^8 F& `4 L9 p
: R+ w" }3 z) U" N5 B5 H try {
" Y9 ` U9 j; h w modelActions.createActionTo$message ; r* Y5 Y' i, M3 a7 E0 g
(heat, new Selector (heat.getClass (), "updateLattice", false));; o5 b8 ~* ^; M
} catch (Exception e) {; W$ o$ t J7 X( c: a( b
System.err.println("Exception updateLattice: " + e.getMessage ());) G4 i6 Y( @; a' C. r+ _) u* s8 t& r
}
# E, a: e; Q n# |* {
/ L, p9 y3 z9 E! q) `3 u // Then we create a schedule that executes the, N2 L- c3 s/ b7 ]+ w7 w
// modelActions. modelActions is an ActionGroup, by itself it
! L- n. P* ^' {+ {# y3 I0 | // has no notion of time. In order to have it executed in! {% y7 g0 b: k9 p* Y
// time, we create a Schedule that says to use the
' j3 X6 O# K) r" \3 A! [, ? // modelActions ActionGroup at particular times. This0 h6 w* E9 d) R+ l/ C b2 J/ k# i
// schedule has a repeat interval of 1, it will loop every7 V7 U- v; D9 e
// time step. The action is executed at time 0 relative to
- ~) h. z5 O# }( @" T" ]+ y // the beginning of the loop.
4 a( u; ?0 x! E! A6 V! L |9 `8 y. c5 Q- O
// This is a simple schedule, with only one action that is% E- y; u; k) t5 o+ j1 {9 e( l
// just repeated every time. See jmousetrap for more
w7 ?" ?' j# w/ p) y // complicated schedules.1 u) v( F+ ]5 f& R5 K! t% S
) E; b% W& w+ K7 ]9 k
modelSchedule = new ScheduleImpl (getZone (), 1);+ [9 `4 y' I7 J/ d! J1 @
modelSchedule.at$createAction (0, modelActions);" i4 Z! [ k7 t' s
6 y J( A+ C# W% m. W+ R return this;$ r: j6 H ^# H4 C9 A) c1 g
} |