HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ j4 C9 x/ ~0 [ y+ b! R8 l" `7 G: W
public Object buildActions () {; N. ]) F4 K! X5 I
super.buildActions();
- j. H1 T4 ^5 n U: P! B* b
& ^- b" Z1 ^: a0 c3 h: O // Create the list of simulation actions. We put these in: u6 N: @& K. @# `5 [% L
// an action group, because we want these actions to be
* C! H" x/ b& |: q/ i: l // executed in a specific order, but these steps should# P( B- d1 w- p7 m' V- V6 e
// take no (simulated) time. The M(foo) means "The message
9 K: r; N* |# ]! {* P! v8 G // called <foo>". You can send a message To a particular
5 x! ~" Q% i) T. n3 G6 i // object, or ForEach object in a collection.* I0 j1 M! J5 N/ y( r- Z+ h
5 G+ A( p `+ A1 v! ` // Note we update the heatspace in two phases: first run
0 o9 m& p! j' F3 J+ _ // diffusion, then run "updateWorld" to actually enact the4 X3 C! a7 O4 a1 q
// changes the heatbugs have made. The ordering here is
. s/ B. H! X1 f3 C: K9 n // significant!
& ?/ |6 l" E* {, p' t ' c \1 |. a6 T1 _0 ~; M8 X, H
// Note also, that with the additional8 t# v3 }* R2 z; |) g
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 ~! V& B; }; F // randomize the order in which the bugs actually run5 O+ p3 E# Z+ P* F1 C7 O# }
// their step rule. This has the effect of removing any4 h% i: g9 y5 J3 e( d
// systematic bias in the iteration throught the heatbug' h( L0 x( K& S+ t) @; \. m3 V
// list from timestep to timestep
2 C! Z) s* o3 `, S6 Q & x4 |/ }! i- T; M- X
// By default, all `createActionForEach' modelActions have6 |* @9 w" m( H$ h4 T
// a default order of `Sequential', which means that the
( u8 B" z& U% D3 ?9 p7 Z8 \ // order of iteration through the `heatbugList' will be
; Y# R; A8 D3 Z7 O1 D( i9 I // identical (assuming the list order is not changed
2 H d; R) y3 c% G" x // indirectly by some other process).
# E1 P6 P U J% B# S1 J& z+ w: W% @
/ Z8 N' _+ a; I: T modelActions = new ActionGroupImpl (getZone ());
$ u& h$ I r4 o# }8 M5 Z0 E8 J) K. O$ q# c( G
try {6 O6 u( i: N5 [" U
modelActions.createActionTo$message2 u4 p; S( I+ Q j; s
(heat, new Selector (heat.getClass (), "stepRule", false));
- ?) G) u( o6 z6 z5 b7 e6 | } catch (Exception e) {4 ]6 d8 T$ m8 y; C. F9 E# K
System.err.println ("Exception stepRule: " + e.getMessage ());
% |5 _1 b& a3 ?9 z" |, b }
/ |( b( T! T2 q# g: m# c
8 `! k5 W$ b6 {, q8 |; N( [ try {
+ V+ c! G4 M5 X W4 B7 z Heatbug proto = (Heatbug) heatbugList.get (0); p9 |& V0 L) [4 M- ^' A$ l
Selector sel =
- E3 W$ v0 |" w- @( K5 r new Selector (proto.getClass (), "heatbugStep", false);# \" J, X, m% m
actionForEach =0 y7 i+ F+ b+ c3 b
modelActions.createFActionForEachHomogeneous$call
1 i4 }3 ]' o/ }- T2 A (heatbugList,
+ l) `6 w9 I) g* @* T4 Z M new FCallImpl (this, proto, sel,9 T( x' ^. V) A2 b4 {
new FArgumentsImpl (this, sel)));: g, P# y) R" O0 C0 u/ F3 P
} catch (Exception e) {
: R1 a7 k: F5 P& Q e.printStackTrace (System.err);
" ]/ @, `; B7 F+ v, p' N }' c* [* @/ e' M9 A
! L. Z* V: E+ ?' t/ m7 j
syncUpdateOrder ();
# x/ _, l& L" P- b# |
1 t' j/ B, ]0 g: A5 Y try {
; u: _- \* A4 m' O/ k modelActions.createActionTo$message " Z- [/ f D* f) @+ @7 U
(heat, new Selector (heat.getClass (), "updateLattice", false));+ w, u7 n+ q5 {/ [$ A! t9 J! h6 Z
} catch (Exception e) {4 A) t b( S3 K) V% f7 s9 _
System.err.println("Exception updateLattice: " + e.getMessage ());& y: t! s* {/ s' h# Q q
}
* N' r5 \0 |. d0 J2 o, }7 `! W
) e @( ~: `6 U' Y // Then we create a schedule that executes the" x* [3 B' \; k$ z0 c4 k! M4 L; F
// modelActions. modelActions is an ActionGroup, by itself it$ W$ C9 D# l! G
// has no notion of time. In order to have it executed in
; g9 p# {9 ?, Z% U // time, we create a Schedule that says to use the
1 H v; |: [0 c! |6 ^4 a // modelActions ActionGroup at particular times. This
$ g0 L* O6 s$ l' Y% z% }/ V* f6 B // schedule has a repeat interval of 1, it will loop every
6 ]& N9 A% t8 A: U8 W // time step. The action is executed at time 0 relative to5 j6 q9 r6 u- [! P0 C
// the beginning of the loop.. v" [* Q$ q6 ?* |
) T& \1 y9 [" V/ V // This is a simple schedule, with only one action that is
! C7 H) A# L. X$ M5 e! c // just repeated every time. See jmousetrap for more F* e# `2 ~+ ?& I
// complicated schedules." z5 w, p6 Q% N/ B) q0 v
9 i* r; G& Z" I modelSchedule = new ScheduleImpl (getZone (), 1);* @& b( ~4 ` L- a! H* x
modelSchedule.at$createAction (0, modelActions);. @' ^, o$ U( l$ y1 g4 R4 S
7 S/ w; W! N1 C( k return this;/ Z3 u+ T0 m$ m a
} |