HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ L7 g7 C+ d' T8 G% R
( o3 q- m7 j$ c5 V public Object buildActions () {0 j$ I' j8 N/ c( M8 L* ]4 I
super.buildActions();" m; K: L6 ^# T* P8 {5 v: q* L; e7 J
5 u8 `, |" {1 n: @ c // Create the list of simulation actions. We put these in
+ }# z# T3 p' X$ d! u. r) h) Q // an action group, because we want these actions to be
: i8 k3 ^: X6 a' g // executed in a specific order, but these steps should+ m' @5 s/ P8 e: o# m0 Y' e
// take no (simulated) time. The M(foo) means "The message
$ Q2 s* Z1 }) S4 }5 x // called <foo>". You can send a message To a particular$ ?. N; N6 u* O4 I9 y6 m
// object, or ForEach object in a collection. R a. ~: ~0 U9 u8 P6 o6 s
% y+ C3 C6 G% |# \4 ~1 Z0 @" Y
// Note we update the heatspace in two phases: first run
) ^. K! h, [. L, R! l) n8 q // diffusion, then run "updateWorld" to actually enact the! f9 x' S, L. n$ r0 y
// changes the heatbugs have made. The ordering here is
- B" R4 A. W/ h* u: q // significant!
2 e5 ]% U7 S1 Z
/ g! h5 ]9 L7 Y: }# }( m% H // Note also, that with the additional! O7 i, U( R3 _) I
// `randomizeHeatbugUpdateOrder' Boolean flag we can
A% O# k1 W9 E% i- U# S6 U // randomize the order in which the bugs actually run7 @5 R8 I9 _( I& Z7 e" T
// their step rule. This has the effect of removing any
' b& ]. m6 m) m/ I: p7 _ // systematic bias in the iteration throught the heatbug
( q7 Z2 K) r. H# T // list from timestep to timestep
- B+ ^$ m! ?; f3 ^* D' _ - J {* L+ W! d0 k0 J9 Y% {$ Y K
// By default, all `createActionForEach' modelActions have# A9 k8 |4 s% Z; V$ ~
// a default order of `Sequential', which means that the
4 B; F- s6 _! g+ K3 B# b# T // order of iteration through the `heatbugList' will be) ?8 A) h. P: O, j( S' y
// identical (assuming the list order is not changed
" T) b9 o2 H! f) i; \* _& d- Z: z. | // indirectly by some other process).' G1 f% h; e9 i+ A* p' @* Y3 F5 d
4 H/ n8 ?% ?, ~3 X, k0 v modelActions = new ActionGroupImpl (getZone ());
7 T. i# P2 i a4 n, R
3 ?) s" c, z( o. ` try {
2 i3 y% W& C- ]# E/ a" W modelActions.createActionTo$message
# k9 m2 |0 d4 m7 s M/ W5 o (heat, new Selector (heat.getClass (), "stepRule", false));
" K! g' e- u# D } catch (Exception e) {- t7 Y+ i+ v/ I/ m' `! v" d! g% O
System.err.println ("Exception stepRule: " + e.getMessage ());
2 \$ M# P+ ^7 P9 ? }
; O2 H+ {% I) m! Z+ _. r: m& S8 D; ]3 \: s+ l" q( |7 I- K
try {
; E: b) U3 ^0 N6 z. H Heatbug proto = (Heatbug) heatbugList.get (0);
, a6 |- C$ O- L3 |; k) b6 m Selector sel =
1 D" H/ U' K# F& X4 L new Selector (proto.getClass (), "heatbugStep", false); `& F$ c6 S0 e$ c6 V
actionForEach =
* ?% T" P- B5 j8 w3 { modelActions.createFActionForEachHomogeneous$call* M h j; w7 o, ]% ^" V
(heatbugList,
' I- V0 d6 h2 Z new FCallImpl (this, proto, sel,
3 S( ^3 |) A2 g new FArgumentsImpl (this, sel)));0 U9 d9 Q" |, H9 z( c
} catch (Exception e) {
' B$ c R1 d9 d e.printStackTrace (System.err);
, W* r: D! _* ^% V+ j f7 | }3 a9 ]. w& A8 W, y( Y1 z
$ V& Q" b( `5 u& Y! g6 G
syncUpdateOrder ();4 V2 l% U* L5 U: _3 I' A6 f
9 V2 v& M& J" b/ a! s0 L try {
m: I6 I3 [2 T0 L- F7 i modelActions.createActionTo$message - I9 b2 z3 }8 M
(heat, new Selector (heat.getClass (), "updateLattice", false));
) U! D0 b2 s8 a! g' T& h3 O9 P2 B } catch (Exception e) {" E: b4 B: l/ A9 ~. c% ~5 B9 k
System.err.println("Exception updateLattice: " + e.getMessage ());) [; j8 x3 q6 [! ]8 z# P
}
: ^, V, M0 ?& [; V. `( u) t - D0 m" L2 k, e' J+ T# L1 d) k: P
// Then we create a schedule that executes the
; a C/ C! y, o // modelActions. modelActions is an ActionGroup, by itself it
( n0 a5 F: p) `3 l- h // has no notion of time. In order to have it executed in
! O t" m8 B$ w" Y- t g2 H // time, we create a Schedule that says to use the0 x9 K1 U) L+ z- M$ k
// modelActions ActionGroup at particular times. This7 j$ A% r2 K1 A C/ b1 V! P' Z3 J
// schedule has a repeat interval of 1, it will loop every
: g3 c9 W! e& w; h7 T // time step. The action is executed at time 0 relative to% H* p5 d" h5 C) e
// the beginning of the loop.
" r q, q, d6 U5 D1 Q0 }" R) o* U9 M
- X; l4 J- v$ { // This is a simple schedule, with only one action that is
6 Z: r6 } B; U! X& \. x' y // just repeated every time. See jmousetrap for more
8 e- U3 B6 U! A1 y% X# J // complicated schedules.9 i/ O+ E, c& _
, ^9 A% h, V$ r2 r6 _+ R modelSchedule = new ScheduleImpl (getZone (), 1);5 `9 a" Q: H4 R
modelSchedule.at$createAction (0, modelActions);; W, T2 h7 }7 O, u; ]
! K& W2 D; t( [2 U. `
return this;7 M3 W9 P( v* G
} |