HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& l! |3 [' U4 k8 y5 V3 ^
h/ W) x7 r1 I0 V* G$ a public Object buildActions () {4 ~+ x6 h# Y9 N! O0 V
super.buildActions();
: } ~8 I1 m* |1 G/ t) Z7 W - ?1 l2 Q& w$ ~: F- N) [2 F
// Create the list of simulation actions. We put these in
5 f: N% _ D. J* F( c // an action group, because we want these actions to be
|, L8 n/ \4 U Q9 } // executed in a specific order, but these steps should
9 I |4 s% b' D2 s/ @0 L, C // take no (simulated) time. The M(foo) means "The message1 m# `$ t, C b/ B/ o; }9 D
// called <foo>". You can send a message To a particular
) K, K1 p. D9 J' w v Y // object, or ForEach object in a collection. i. @- H5 Y) T5 A( [/ L
$ Y, h+ _; L8 a" ]8 w
// Note we update the heatspace in two phases: first run9 C* P9 {2 c& I# `) i' V! }. u
// diffusion, then run "updateWorld" to actually enact the
; ]8 u2 s9 v; Y& R* M5 s: @( s$ k // changes the heatbugs have made. The ordering here is. o& o9 H9 L& L0 Z& z0 e) h/ J
// significant!( w+ b$ a2 g, g6 y8 c8 e J
4 K; ^4 w, v# \ // Note also, that with the additional
, H4 s, \6 I' N9 |9 N$ \ // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 f( t& l: Y; S& k- ?, | // randomize the order in which the bugs actually run
- F. } {9 S, u/ A3 ?* L( j // their step rule. This has the effect of removing any+ q2 |/ z4 P/ W4 F
// systematic bias in the iteration throught the heatbug
7 G5 e8 ^' W# y9 d! S8 N // list from timestep to timestep* N- K* H. z8 @! Q4 t- u4 h4 g* `
: f5 `7 W7 p; x# m
// By default, all `createActionForEach' modelActions have* J0 k9 t8 t$ h4 C8 I% a+ V
// a default order of `Sequential', which means that the
3 C |4 {6 v3 G; {* K% W5 m // order of iteration through the `heatbugList' will be( `$ p* X3 x2 b& {; r) D$ N3 M
// identical (assuming the list order is not changed/ V- A. c/ \0 g i. n4 }
// indirectly by some other process).
" r h+ J' s/ j% y% T& X% I" ^ m3 i ( \4 L) V* {- q4 n1 L8 J4 X& ?
modelActions = new ActionGroupImpl (getZone ());
. A/ l+ x6 Q9 R) n: E, Z) r0 G# h' b- Y u8 B, ^: n1 V! u
try {
5 n. |8 z- j" k l modelActions.createActionTo$message6 X6 V6 L" O8 {9 \2 ?
(heat, new Selector (heat.getClass (), "stepRule", false));) [; x' p4 ~' s1 P
} catch (Exception e) {" H' a) v& P. G- n" m& K( q7 x
System.err.println ("Exception stepRule: " + e.getMessage ());
8 F t( V5 y9 j- B }
' m6 Z) M. n, p& e( r( T; k1 P4 W
try {
3 i. ^ `, f) N/ R5 v. V Heatbug proto = (Heatbug) heatbugList.get (0);
7 C0 i$ o3 S2 d5 o Selector sel = + z7 [& w% r3 I% m
new Selector (proto.getClass (), "heatbugStep", false);
9 k2 n! \+ |- j0 ?' G0 A actionForEach =
& ]/ H7 A& X: U4 A# {) L; q: H modelActions.createFActionForEachHomogeneous$call
) b) B1 D+ t3 l d6 o (heatbugList,% j% M9 w( d$ w, U3 w" B& Z
new FCallImpl (this, proto, sel,
. O- L" u9 [. K5 c) B new FArgumentsImpl (this, sel)));) R8 \7 k1 G! H7 R
} catch (Exception e) {4 S5 _" k! G. E4 i
e.printStackTrace (System.err);
9 E* ]& _, U& d. F7 ^1 a }1 U: B2 @6 }# @" D$ A% q
& D) o* R: b- |! ]( w/ @0 w syncUpdateOrder ();
0 u$ m+ _2 Y7 K$ t% A# E
1 a9 q2 _& J1 i0 J9 E6 u try {
$ D3 b( _+ P0 x" _ modelActions.createActionTo$message 0 C% v* R @1 s6 v
(heat, new Selector (heat.getClass (), "updateLattice", false));
" x7 Q4 a1 y! w } catch (Exception e) {
- S1 ?# C5 i) M2 I5 \) t+ { System.err.println("Exception updateLattice: " + e.getMessage ());
6 ]4 I0 G( b7 e1 h2 A }, r- ~( i1 ^$ n* `+ ?. F. F
3 e) o! U5 i- |4 E/ n- h+ r
// Then we create a schedule that executes the5 X( r8 [# b3 o- G4 M! d- n. w
// modelActions. modelActions is an ActionGroup, by itself it5 N9 y) \; \ v) K8 D7 N1 T
// has no notion of time. In order to have it executed in' J+ X9 z# e5 f3 D( k7 f: B1 [! _8 {
// time, we create a Schedule that says to use the8 { g0 S$ R# U( }3 I7 J
// modelActions ActionGroup at particular times. This: e' e! j# Q3 x; t; p4 A$ ]& y$ A
// schedule has a repeat interval of 1, it will loop every: f. A3 P) L8 e
// time step. The action is executed at time 0 relative to
9 C+ y& u F; f% p // the beginning of the loop.
& K) |+ k. J' j7 U$ j+ \6 f! O# M3 |* \' n# }
// This is a simple schedule, with only one action that is
" t+ b, C* E; h) C // just repeated every time. See jmousetrap for more
}$ Z5 Y/ q9 w# v; G, ~ // complicated schedules.2 v: N4 \4 H1 L( q: A5 U
: R `7 c" p2 I6 }
modelSchedule = new ScheduleImpl (getZone (), 1);
0 L5 C* O2 `# R. L: \" N4 G modelSchedule.at$createAction (0, modelActions);; z* Y, D$ W8 v& F" W9 X' j Q# C6 U. P$ \
- G9 w- K2 g, @; Q- N+ C2 [2 P return this;0 z) ?( [/ y6 P. B- e
} |