HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 [! o7 @/ ]; [9 b# P) K
) I9 m3 I- U; d a public Object buildActions () {
7 o0 o: P% Z; y. Q super.buildActions();
; ?, _$ R% q5 s. b: A! k ; W% L, _# a" A3 O
// Create the list of simulation actions. We put these in" q3 q- L3 S1 F9 F# u
// an action group, because we want these actions to be. g+ O, p& X! v4 Q5 S
// executed in a specific order, but these steps should
1 R+ a) N0 X. i* {; @ // take no (simulated) time. The M(foo) means "The message
0 U7 f" \ z) Y0 |+ d1 j# w // called <foo>". You can send a message To a particular4 E) a# ?% r0 C, H/ ^
// object, or ForEach object in a collection.6 I: T3 b& m7 ^* i. P4 l
. i! r6 a u! _" x' V# s // Note we update the heatspace in two phases: first run
$ _9 I W, d- V, J% j' |% S% f( c% J6 X // diffusion, then run "updateWorld" to actually enact the0 r7 o9 G. z2 C. u6 j8 O
// changes the heatbugs have made. The ordering here is
: R( l4 W. p- ^- C; N // significant!
4 z. f( m& U. e4 z. `5 Q2 t
, J# b% ~% [& T* S // Note also, that with the additional6 s( k+ l: x `0 n, t6 h( d# V
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 y2 X* `2 V8 F z e- d( @
// randomize the order in which the bugs actually run
# E7 h3 m4 e- X! J3 u- F+ n. T5 C // their step rule. This has the effect of removing any0 a9 H1 l( ~- b# {
// systematic bias in the iteration throught the heatbug. j4 t2 D( q0 S& g# [
// list from timestep to timestep" V; P' S" y' e. E k( A/ V' ]
0 T7 u0 i% q2 y; g( H // By default, all `createActionForEach' modelActions have% H* }* y" }0 P8 U+ ~3 F8 M A
// a default order of `Sequential', which means that the7 X& D, j! F. K- X' X& x, e% S0 @
// order of iteration through the `heatbugList' will be
1 _0 M& Q; U( V // identical (assuming the list order is not changed
5 q+ n0 j, [, g* |/ L0 h // indirectly by some other process).; R# H. A$ A! S9 M$ Y* T
, D: x( f+ l# g4 c modelActions = new ActionGroupImpl (getZone ());
( M' Y6 y3 E I/ L& C
0 R& _" f9 Z- ]0 A) U try {
! d2 E w* b8 L3 @+ [ modelActions.createActionTo$message
) I: ^ }2 L+ V6 Q$ R (heat, new Selector (heat.getClass (), "stepRule", false));
: L* U" t" n+ F. ^* } } catch (Exception e) {
$ S3 _- D( R' B: r: x1 ] System.err.println ("Exception stepRule: " + e.getMessage ());9 ~' t/ z/ U$ q! X" L
}
6 d9 M/ U" p0 p# h( D+ J3 |- x3 b0 L" a' B, U
try {
, e9 E4 @ ?) v Heatbug proto = (Heatbug) heatbugList.get (0);' s' L l B. e/ R& `, f- N0 D& F
Selector sel = 9 s' |1 [1 Z* x' y6 O
new Selector (proto.getClass (), "heatbugStep", false);/ W7 P# V( @9 C# y; u
actionForEach =
/ w. w5 a# L! j# I: q1 H, t modelActions.createFActionForEachHomogeneous$call
" j6 k5 H7 Y% g8 A (heatbugList,2 D7 z7 Y9 @2 a& ]1 u) p m
new FCallImpl (this, proto, sel,, [7 ]; C6 c8 s6 o9 [
new FArgumentsImpl (this, sel)));& e+ W! s* x: E9 C
} catch (Exception e) {" M, l* V% v' Z2 R
e.printStackTrace (System.err);
2 s9 E- s. U% K7 \: y8 J }
3 H/ S+ ~( G: {& m) N! y; Y Z4 S
: J5 K1 P6 g; q+ J. u! ~1 _ syncUpdateOrder ();. ^ n/ h; ~9 |4 z9 r' ~3 O
" z( U6 c1 c Y9 R. Y+ k
try {
" J& _2 [7 |2 W0 S8 t8 I& H z modelActions.createActionTo$message
. b* N. i* E7 ^+ x K. q (heat, new Selector (heat.getClass (), "updateLattice", false));0 M0 ~; y$ t& d& E; J+ X/ O: q9 F
} catch (Exception e) {
# g' p) i# p9 S! S( J* V2 ^; O System.err.println("Exception updateLattice: " + e.getMessage ());1 W% {& M, x' X# ~+ K
}, d; G& v- e C) v, ^0 y4 m/ |
$ Q- j+ |; _ B5 F4 _6 y+ E // Then we create a schedule that executes the1 m5 P6 ?3 D% U+ G- r% Q
// modelActions. modelActions is an ActionGroup, by itself it
) i) e& k5 m! S% P; Z* x: F) N // has no notion of time. In order to have it executed in
1 k- _ m9 j; S3 d! r. n6 ?7 N // time, we create a Schedule that says to use the% P) r4 x' Y6 M$ N5 v$ z
// modelActions ActionGroup at particular times. This) ~0 |2 m& c n: ~$ P+ F! \# n1 R5 W( ^
// schedule has a repeat interval of 1, it will loop every% a! G1 Z1 D9 |7 O4 A5 t: Z7 U
// time step. The action is executed at time 0 relative to
* V9 b* ?9 h- n$ @4 ~, R // the beginning of the loop.
# e0 f& v% v* {0 V+ j. P# H; W! C! U q0 K. G% f7 S
// This is a simple schedule, with only one action that is
' f. {. l% g$ S // just repeated every time. See jmousetrap for more6 T/ q- s1 D3 i
// complicated schedules.
, w8 y4 ]" [- l ' w# N) F) c$ Q
modelSchedule = new ScheduleImpl (getZone (), 1);
) y4 E0 @7 p4 a* t. Z) L G# r modelSchedule.at$createAction (0, modelActions);% C9 I& P- ?8 e/ i& m" t6 t' h
) Y7 g9 o1 k" A7 R6 E* \# N return this;
, B" W) {7 B5 D1 n } |