HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; M. a' m/ z. \1 I1 { N
1 d8 k6 p/ D( h% l public Object buildActions () {
; y) y( w" y, d% |3 \ super.buildActions();
& w0 c/ M8 L- @6 q0 h : ? [3 @+ Z/ C: U3 q( o5 A N1 K
// Create the list of simulation actions. We put these in
" K9 J& o( h5 _8 g" `4 T# ]. C // an action group, because we want these actions to be
4 \( H) S* e& `$ O& Z0 E% o7 P // executed in a specific order, but these steps should) O1 K& G1 M# U k% n, R
// take no (simulated) time. The M(foo) means "The message7 @' `1 f) I- c
// called <foo>". You can send a message To a particular% j3 W4 k; P; T, Z3 t6 ]
// object, or ForEach object in a collection.
3 H, a) e' l& p; J$ |, E& c ) H% Q7 j- e X6 K! }1 j
// Note we update the heatspace in two phases: first run
. i$ w9 ]+ v* B" M% w \ // diffusion, then run "updateWorld" to actually enact the# p- l3 e! L4 k0 s+ J( Z# |8 N
// changes the heatbugs have made. The ordering here is
, S s( @# N; Y7 B d3 p // significant!5 D) i" ^* I. E( ]
0 _' E3 e2 ] o- K6 n# ?8 W // Note also, that with the additional) \* u: X6 j3 n4 f g( r
// `randomizeHeatbugUpdateOrder' Boolean flag we can) B) @5 R: S, ]* W
// randomize the order in which the bugs actually run* l( I; w; q" C
// their step rule. This has the effect of removing any
3 N) s6 H* T/ X! x4 l/ K // systematic bias in the iteration throught the heatbug$ R4 B) h7 f+ e% n" S# e( ]' Q8 x$ p0 m
// list from timestep to timestep
! V+ N b9 C8 T: I2 `* m& i3 B/ m , x2 l8 T: [, M' e
// By default, all `createActionForEach' modelActions have
D. Y4 }6 X0 q" Z3 p4 I // a default order of `Sequential', which means that the* A# ?' v/ L$ z6 {+ H% b0 a% X
// order of iteration through the `heatbugList' will be
7 F5 c2 l0 _2 g! ^; V // identical (assuming the list order is not changed
$ i$ @7 W7 L! \ // indirectly by some other process).
; T2 X% b0 R0 Q& @
8 `' V5 S4 J/ Y' F1 F modelActions = new ActionGroupImpl (getZone ());
0 w3 z F7 n4 z& y$ v, n- x4 Y0 Z3 e' ^$ s; G8 L4 o, I
try {2 q1 a" H" F. a5 U& O
modelActions.createActionTo$message7 p) ?( J+ ^0 T! o- S; ?2 e
(heat, new Selector (heat.getClass (), "stepRule", false)); L! T" e& l1 L* P1 n% X/ f- t8 }
} catch (Exception e) {
! x2 |1 I# Z/ ?; k( ^0 ?) D x: P System.err.println ("Exception stepRule: " + e.getMessage ());+ X" C6 J" ~5 D! e
}
1 K( f2 @, e& O, X$ Q# W; c# G% N$ I" [. a8 @- f; c
try { L# u; K% j! E4 g/ H
Heatbug proto = (Heatbug) heatbugList.get (0);
" } L& ]. q8 y Selector sel = 1 w) `+ | `/ ]
new Selector (proto.getClass (), "heatbugStep", false);
( U7 b3 }. g6 W& | actionForEach =
( ~/ d" R, Y' n8 h: I modelActions.createFActionForEachHomogeneous$call
2 H4 K, }" g# Z: r/ ^ (heatbugList," J( y6 I9 h. k
new FCallImpl (this, proto, sel,' n; ]0 }2 A% {
new FArgumentsImpl (this, sel)));
1 @4 X" _8 h4 }6 h. } } catch (Exception e) {8 }& ]- _& h/ O" ?( d( c* l
e.printStackTrace (System.err);2 |4 q& x' |, R9 ~ ?
}- i- @: R& A& v
5 v; I4 Y/ D! Z
syncUpdateOrder ();
2 k1 w$ |2 d9 p# M# {7 }
; u8 r& {2 X" X. v3 Z5 p try {' p1 m- y) `2 @- ?) K A8 F
modelActions.createActionTo$message 1 }; J8 X/ q$ ^7 ~0 \$ T
(heat, new Selector (heat.getClass (), "updateLattice", false));) R# x$ M( T; G- H# p" ]
} catch (Exception e) {
* O8 \9 n( t/ j( a System.err.println("Exception updateLattice: " + e.getMessage ());# t0 _$ |4 ^# M* j
}5 Z4 E4 J0 S" K( I. O
6 c/ q; X. {. b' g // Then we create a schedule that executes the
V/ G8 q- r6 N( X# o // modelActions. modelActions is an ActionGroup, by itself it
6 j4 F8 s2 M1 [& \5 a' Q // has no notion of time. In order to have it executed in4 D$ _, {) R6 R1 c
// time, we create a Schedule that says to use the8 F4 B$ i% _! Q l6 N" m
// modelActions ActionGroup at particular times. This9 r$ Q7 v4 @2 X7 v! P8 H. [+ c
// schedule has a repeat interval of 1, it will loop every# m) m& a0 O& D8 R
// time step. The action is executed at time 0 relative to
3 ^- e2 u3 |# [ // the beginning of the loop.4 ?' O7 @. @6 Y- @! K( k; N* E5 [
( Z* A+ G) i4 q: A5 t) p0 F. b // This is a simple schedule, with only one action that is
9 {" {( y4 k. _3 o3 h0 F // just repeated every time. See jmousetrap for more! u. \6 _2 j& m) O
// complicated schedules.
' w0 i/ X2 ~; O; J- o; A& Y! k
7 p, W5 c3 W I( T" d! Q# q, J0 l' a modelSchedule = new ScheduleImpl (getZone (), 1);
, W1 J7 C# B& F9 {* q0 t modelSchedule.at$createAction (0, modelActions);
; @9 o6 s8 k* ?, D, t
% Q1 t ?8 V2 V2 i P return this;
" X* I; g2 l- ]3 { } |