HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 w' ~) y) |) c, j8 P- L
3 T2 d. M6 g: H/ I' ?7 D$ V public Object buildActions () {
2 x2 _5 T2 b. s# {$ u super.buildActions();, i0 }/ X# V* g8 B, f- J, h, a
& I) B- m' }; x0 [
// Create the list of simulation actions. We put these in
: Y. u+ h$ n8 h V9 C2 ] // an action group, because we want these actions to be
( [8 S; y: j" l2 t# H/ z // executed in a specific order, but these steps should
( M Q: d2 B1 X) O+ `' X6 o // take no (simulated) time. The M(foo) means "The message
5 S1 [1 N, }9 I# w' V // called <foo>". You can send a message To a particular, `1 Z7 ~4 k5 Y( O4 p0 ~8 u
// object, or ForEach object in a collection.$ I, Q) I4 u A2 |
; { c+ x: N, V( M
// Note we update the heatspace in two phases: first run
+ k, a! d4 E7 j% H // diffusion, then run "updateWorld" to actually enact the
- h; h6 |" t% p( w: K // changes the heatbugs have made. The ordering here is
: i e0 p+ D) c- c5 f // significant!* c* e) G) u' `, ^: V
! S' u# c, B" b2 ]% b. Z v // Note also, that with the additional/ i. R6 c/ K( N- ? l7 r
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ c! a, w. ~; z0 N! g; L8 C& L+ y( O, W
// randomize the order in which the bugs actually run
9 w% a% B' y( v6 w8 a // their step rule. This has the effect of removing any
7 T" ^; w5 |2 @* q* Q( g' C // systematic bias in the iteration throught the heatbug( Q F' w2 c) i1 O% r% ^
// list from timestep to timestep" ^5 A8 U& \5 n( s* u( Q
2 F' b* b! n: `# q4 Y3 n1 K
// By default, all `createActionForEach' modelActions have
5 X4 v- x( [3 W. X4 x // a default order of `Sequential', which means that the% _9 L( r9 e# ~$ b3 P* h
// order of iteration through the `heatbugList' will be
- ?3 U9 a3 V6 Y // identical (assuming the list order is not changed
* _/ ~# t2 F* g. c // indirectly by some other process).
2 a( {; F* v( T- Q
3 [" s" D% J! O modelActions = new ActionGroupImpl (getZone ());; o- W3 i8 v2 I. Y' }
/ y( }8 L( ?, e% P; ]; Q
try {% Y$ m9 M$ k$ k' f5 E' s3 O4 K
modelActions.createActionTo$message2 l: ]( ^; T/ e3 J7 f' F
(heat, new Selector (heat.getClass (), "stepRule", false));
w( q2 X$ L+ j } catch (Exception e) {
* L" B* Z" _" r/ _ ^ System.err.println ("Exception stepRule: " + e.getMessage ());1 n6 H8 c# ]( y/ U& g, ^0 T/ M
}( ?: Q# ~4 ^2 ^ }
& @/ o* h0 X- Q# s& m
try {
8 r( D- p2 O* j; [ Heatbug proto = (Heatbug) heatbugList.get (0);
1 N& L8 L3 f4 H! q Selector sel =
; o; j) c4 \$ [/ q, h new Selector (proto.getClass (), "heatbugStep", false);
6 H5 v2 Z8 j; @5 \ actionForEach =
9 a1 U+ }. ]# }) r" y0 z3 A' V# d; ` modelActions.createFActionForEachHomogeneous$call1 _7 H" ^2 _# @5 n$ ?
(heatbugList,
2 e% j" j% V- q$ [. M new FCallImpl (this, proto, sel,
; o+ D1 V8 ]) M) C: u1 T new FArgumentsImpl (this, sel)));
/ T" `5 N4 W% `0 w) t( B. Y } catch (Exception e) {9 ?, U6 {4 A/ {- T6 E6 [
e.printStackTrace (System.err);) d. J. `" t6 x* ~7 ]% m
}
! @) m; P) y1 S ( D, V U- N8 k0 K" h
syncUpdateOrder ();! I# ]6 r; L% g7 A4 q3 N% G
+ e$ {) X$ {5 G+ K0 G
try {8 I7 ?4 }' N. b m: m' L
modelActions.createActionTo$message 8 p8 _$ Y) N+ v0 _# e
(heat, new Selector (heat.getClass (), "updateLattice", false));# [ C1 @0 \% e! U2 ]1 _+ j
} catch (Exception e) {* U# o; A, r0 I" P/ O: O2 F
System.err.println("Exception updateLattice: " + e.getMessage ());+ l; I" ~* p: k
}4 o v1 O9 Z6 O3 [- u. x
# U `% C E& n4 F // Then we create a schedule that executes the9 Z! B9 R& X) g
// modelActions. modelActions is an ActionGroup, by itself it
! o/ [& \( {( a" i/ ` // has no notion of time. In order to have it executed in
+ f) |/ r; W9 g- q2 o+ e // time, we create a Schedule that says to use the
9 u" w3 t' w3 @5 s1 ] // modelActions ActionGroup at particular times. This
1 g& n9 o* V+ ?3 Y5 t' m3 j! I // schedule has a repeat interval of 1, it will loop every
$ o# k4 [. @. t% W // time step. The action is executed at time 0 relative to
9 z4 C9 Z0 o0 a: ^& _ // the beginning of the loop., c+ S: U& W) M1 Y# J
- B# g9 c0 y1 q Y( }4 [ // This is a simple schedule, with only one action that is
( G Q* C8 R/ k, q3 c0 T. I // just repeated every time. See jmousetrap for more
" L+ ?3 O& ]! P6 v" I Y // complicated schedules.
: R- K% Z: \3 F5 H+ G/ @ % s8 A5 w# |7 V# S, n5 l
modelSchedule = new ScheduleImpl (getZone (), 1);
8 }' O+ V) c' ^; Z. P& s# i modelSchedule.at$createAction (0, modelActions);
$ q2 o) |0 M% J' a $ r; e- N% u# O: |* O0 T# ^
return this;! Z& I& y3 J+ {4 R1 z9 S) @3 Y
} |