HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! Y) K; {8 Q4 D: `+ \5 Q
+ B( b1 j# i0 l% y* z! M public Object buildActions () {
- b$ e7 D( c5 l super.buildActions();
1 M& U- _4 ?! D4 d9 M% W6 t2 ? 2 B$ O6 E( v. V: l
// Create the list of simulation actions. We put these in
8 p# h! G v& K9 m // an action group, because we want these actions to be
2 v3 X" _6 N0 l" | // executed in a specific order, but these steps should
2 |- U6 W% a+ }5 N; C/ S4 s/ ] // take no (simulated) time. The M(foo) means "The message. [2 @: u& ~* o: \1 G; ]
// called <foo>". You can send a message To a particular* f# f2 N" \- x0 g" N
// object, or ForEach object in a collection.
; ~, s' p3 G/ H1 n% P. E: `( _/ ?
* v4 y% I6 I) @4 O // Note we update the heatspace in two phases: first run
" j& p& M i& a9 h+ c9 k' X // diffusion, then run "updateWorld" to actually enact the
/ ?* t/ k/ j& r; O' |, w // changes the heatbugs have made. The ordering here is, l+ ~# _3 ~! j$ Z! I1 ]; N, D
// significant!8 C+ G# L4 y1 e3 K' V
! G& B" j( Z, _) e v# j3 K2 r% R // Note also, that with the additional; s4 a. ^0 j* B( h5 d+ ] _
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% N' H/ F; }# j, ^. F // randomize the order in which the bugs actually run
, D3 B3 ?$ c+ R' f/ l // their step rule. This has the effect of removing any
% w9 P: E3 U& W( U( o% x // systematic bias in the iteration throught the heatbug& v u8 w$ v, R$ E
// list from timestep to timestep
! a) a" N( I3 } ( I( L) y. X8 E& L$ N5 }3 E# e
// By default, all `createActionForEach' modelActions have
/ A+ |, Y L5 G4 \+ ^ // a default order of `Sequential', which means that the# l0 R1 c% s7 t6 S3 p' i7 R3 I
// order of iteration through the `heatbugList' will be2 h( g T9 O: F. f
// identical (assuming the list order is not changed4 @6 z5 A- m3 Y& x, C0 l) O4 q
// indirectly by some other process).+ h- ~+ [2 B& D. i& c+ e
; ]4 w7 |5 t1 \$ R( C modelActions = new ActionGroupImpl (getZone ()); u- l( j1 x$ _8 A! D
, y6 ]# b8 }& O2 p) ?' N# w8 E! i* P try {
- d3 u" `$ H* z/ C/ s, U5 a modelActions.createActionTo$message
; g& A( g( {. u0 p (heat, new Selector (heat.getClass (), "stepRule", false));1 T( p, b# p* ^
} catch (Exception e) {/ T& r E2 J7 N" u
System.err.println ("Exception stepRule: " + e.getMessage ());
. N: N" Q# b: K! O) n4 ^ }
# w6 N$ U* R. C% O! V f
5 s4 s& q) L5 b) ` try {- B; P: |, r0 C1 [ r0 f. [
Heatbug proto = (Heatbug) heatbugList.get (0);0 o0 C; M/ ], @
Selector sel =
! X3 \, \- t4 d; s% } new Selector (proto.getClass (), "heatbugStep", false);/ u; h/ Q' Y3 B* i1 B2 c
actionForEach =
- q" t* N0 w+ {# ~9 h$ W) S* B modelActions.createFActionForEachHomogeneous$call
* E% D! g# Q9 u" O4 J% h (heatbugList,7 J2 _; T1 I- ^1 ^% Z7 ~1 B
new FCallImpl (this, proto, sel,
" N. a' V7 _) g6 ^' K7 j new FArgumentsImpl (this, sel)));
$ G' S& C% c0 h! E* L3 R } catch (Exception e) {& o! V! F8 _3 b
e.printStackTrace (System.err);
5 e$ I/ d, V+ ]( z, S }
6 a: @( P3 ^( e% P3 B* v& K" { , b, N) G) C1 \, B. W- s2 E
syncUpdateOrder ();
0 O }1 J' z. S& n, h
7 T9 ?( ]& U; O, g. i try {
# V" _( o' t" I7 l modelActions.createActionTo$message
* P: m3 @8 D/ c& P (heat, new Selector (heat.getClass (), "updateLattice", false));4 [& f6 e2 V+ z3 n2 C, f
} catch (Exception e) {, S1 B9 E4 {$ s) P/ R* X1 v
System.err.println("Exception updateLattice: " + e.getMessage ());0 L( S4 ~5 @" w9 E2 S
}
( g* W5 f5 e" ^
. l( Q* v0 A. q% m // Then we create a schedule that executes the
# Y! I4 V8 \8 I+ Y; S% a/ [+ ^ // modelActions. modelActions is an ActionGroup, by itself it8 n" B. y% T; ~ s7 d
// has no notion of time. In order to have it executed in' v- g! f0 c9 S
// time, we create a Schedule that says to use the
$ J: h1 i& r+ J# F! `$ J: i // modelActions ActionGroup at particular times. This
]5 O y3 ]( b V6 l! y) y // schedule has a repeat interval of 1, it will loop every
2 g! I& _6 Q0 ?) O4 @) t9 K% k // time step. The action is executed at time 0 relative to
2 ^# M% R8 ? K+ Y // the beginning of the loop.
# s; {; Q; |7 ~/ ~, [' U2 ?# S9 @0 }. p0 w. d* f5 u% _' ^" m
// This is a simple schedule, with only one action that is% t2 l4 I2 ]8 b" R
// just repeated every time. See jmousetrap for more
& D9 d$ ^% h: T8 I5 w // complicated schedules.7 D8 o% _5 M' ~+ n& s; l
0 T" o" F! ?: k2 m8 g4 V modelSchedule = new ScheduleImpl (getZone (), 1);
, ~4 a2 `8 W# {2 i$ ? modelSchedule.at$createAction (0, modelActions);
. }3 j% Q# G9 ^2 d " D) h* Y9 M. q5 @' C7 B& V- @* I. J
return this;' f9 R% o+ ~* V8 r0 N, x2 r$ T
} |