HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 Y! B ~9 Z0 e H
! b5 W/ R% o% d7 @
public Object buildActions () {
4 [ R5 M& a7 |4 Y' N6 y6 v) _$ ` super.buildActions();
0 j1 Y* f s; g8 L. Y2 f2 D 3 e- s9 K U9 B! V, q3 W6 {
// Create the list of simulation actions. We put these in
/ n% m* S4 G0 z( Q* E // an action group, because we want these actions to be
! }+ I! G: n+ l0 q // executed in a specific order, but these steps should
4 t# L6 ~7 E7 L // take no (simulated) time. The M(foo) means "The message; o# X' X- O2 T; |2 r
// called <foo>". You can send a message To a particular
1 P$ z5 i+ B' O! b. A9 _ // object, or ForEach object in a collection.9 x4 y7 I& V$ `7 j/ |9 h
8 s: J8 `7 a; f* Y: q* o // Note we update the heatspace in two phases: first run, K2 D1 U+ G4 ` _
// diffusion, then run "updateWorld" to actually enact the
* C0 C( h5 e5 j- T9 ] // changes the heatbugs have made. The ordering here is
: A6 a2 s8 ~6 Y3 y8 ]( t0 t // significant!4 @) y9 [" D6 w3 w7 C* x
h8 x; q% F; D/ @# i
// Note also, that with the additional
3 {: h, l* _' L4 @7 \: {0 x // `randomizeHeatbugUpdateOrder' Boolean flag we can# t8 ?+ Z: ~/ B K- M7 k9 N9 s
// randomize the order in which the bugs actually run
* k" y2 E6 U) j/ A* P/ u // their step rule. This has the effect of removing any
: l* `7 e! }7 h7 J8 Y% I9 J( t5 r6 M* d // systematic bias in the iteration throught the heatbug/ I4 s% Q6 v- {# B0 ]
// list from timestep to timestep5 h! h# r; z1 T# Y, }* E
% h- {9 r% V, I // By default, all `createActionForEach' modelActions have
* m" x% Z' J% o6 u // a default order of `Sequential', which means that the
4 K- [, m) s) }8 h) `: c // order of iteration through the `heatbugList' will be
, L$ K) ~7 t) p. z% U // identical (assuming the list order is not changed
& ?7 E& @ H3 q1 t8 J // indirectly by some other process).
& ]2 }) T. t$ a$ d
$ K% P& Y9 {: {7 j$ P modelActions = new ActionGroupImpl (getZone ());
# J5 l! D2 _2 t$ ^" {9 @7 }& R8 S: K+ r7 e m
try {
& E. F& x2 r; V) t, B modelActions.createActionTo$message) Y# Y( o' T f$ d* A
(heat, new Selector (heat.getClass (), "stepRule", false));6 E6 A, |# o7 Q/ l: e5 M
} catch (Exception e) {3 H5 M$ d/ E8 Z$ H
System.err.println ("Exception stepRule: " + e.getMessage ());/ b+ U/ n! N& |( t7 `
}
* Z v H! P A8 X# j( D& k- D' L
( {, E( }: b6 a3 d9 J! ?2 U: D. V1 a try {+ n# R `% ]/ r! z" r% T2 F
Heatbug proto = (Heatbug) heatbugList.get (0);
! H9 T/ {& W) p; d6 ? Q! i Selector sel = , q3 e3 Q5 d0 ^) M- G) o: K# }( U
new Selector (proto.getClass (), "heatbugStep", false);5 R! u% ^8 z2 O% X! O4 \! y
actionForEach =
9 O1 R3 c% o) R9 L- i modelActions.createFActionForEachHomogeneous$call7 E" ^% C; r# \8 Y5 k- c) C) [
(heatbugList,; D- m, \. g7 ~. }0 } @
new FCallImpl (this, proto, sel,
- o4 x+ }, X. [% w new FArgumentsImpl (this, sel)));
6 D1 r* K; o7 W- |$ T2 l } catch (Exception e) {
$ A8 `! h- A6 G' B1 ~. h e.printStackTrace (System.err);
; E ?2 k! L6 A5 p7 t3 j2 u }
) n/ W n' I6 K" e# M, k
+ a! a$ L/ y6 o5 k% F! O( j8 @ syncUpdateOrder ();( X% u# t& w8 }1 R
: f1 q' L! Q+ v# \- W; A try {
2 F" r8 g( U" _ modelActions.createActionTo$message . }3 O/ Y/ L$ H4 p; }
(heat, new Selector (heat.getClass (), "updateLattice", false));
" p: T# m1 P2 [+ z( c! i8 c: ^. @ } catch (Exception e) {
- T/ r/ \* k# N: D/ |# [+ Q6 o System.err.println("Exception updateLattice: " + e.getMessage ());
9 F" T% }7 b, b4 I3 E% @ }
/ z* @6 @2 H9 U5 e' l
8 v% o3 g5 [ @+ o$ j // Then we create a schedule that executes the
+ q, V/ {. v8 C, D" Q. Y, S+ f1 F // modelActions. modelActions is an ActionGroup, by itself it0 t1 w. R. {$ G& U
// has no notion of time. In order to have it executed in
- H1 y6 f0 [* h' U3 R // time, we create a Schedule that says to use the
2 W9 }& b7 D' o3 |, N+ Q! @& b // modelActions ActionGroup at particular times. This# T9 P3 }9 N& q- A, z
// schedule has a repeat interval of 1, it will loop every
; t3 Q; @4 ? {2 o7 K( }" [1 M x& H // time step. The action is executed at time 0 relative to# y; x4 J" M0 u3 S6 I0 X. I! G
// the beginning of the loop.4 D, ~( T' w8 O1 B/ d+ Y( H. n
0 Q9 J0 z3 y( E6 a1 [4 t // This is a simple schedule, with only one action that is
) L- _# h4 H& Q. b& f+ h( U // just repeated every time. See jmousetrap for more5 N0 G1 @3 H. u# R3 G) ^- g$ g
// complicated schedules.
& p7 n% z5 p6 D1 }
" g$ @" d3 D* c4 j# [ modelSchedule = new ScheduleImpl (getZone (), 1);
' x8 A4 E; e9 A7 w modelSchedule.at$createAction (0, modelActions);" t& z; f/ g* o/ a8 O
" C& B5 w8 }( ^ return this;
& P; [. ^5 d3 m1 } } |