HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% ~* i# A1 w2 Z9 j+ w; c% m
7 e- |/ o2 K! @( ~" g$ G: [4 C
public Object buildActions () {+ l" C1 q. I3 V9 q
super.buildActions();) r% a1 i. S S$ r
+ v9 @3 H9 N7 Z0 @4 a // Create the list of simulation actions. We put these in) X i9 t M2 K- j/ a; [
// an action group, because we want these actions to be( S/ Y+ X/ M: {7 c2 t& v& U: B
// executed in a specific order, but these steps should
8 z5 U8 ?2 ^1 X9 J // take no (simulated) time. The M(foo) means "The message# d1 q1 u4 p" W, e- ?3 M
// called <foo>". You can send a message To a particular
) c8 H0 z5 H& a- E: t! k- X* q // object, or ForEach object in a collection." ]) P* Z* q2 `; ?% I
+ p* I1 L' x& J5 H0 M7 F6 r& d4 ] // Note we update the heatspace in two phases: first run
5 T" [# z& k* K7 M! ^2 h // diffusion, then run "updateWorld" to actually enact the
R' }7 U, i4 e // changes the heatbugs have made. The ordering here is& W- A% w( z5 U. R& S/ j& }- P
// significant!
' Q. O! j }( e: K- O$ Y5 K # o" C4 T5 D( s
// Note also, that with the additional/ l$ }6 {, U( B M0 ^/ m
// `randomizeHeatbugUpdateOrder' Boolean flag we can" ^/ G w: _: @# E* O4 H& ^
// randomize the order in which the bugs actually run' \( V: g9 \" a, t
// their step rule. This has the effect of removing any
) Q! u7 i/ D: Z // systematic bias in the iteration throught the heatbug* x( G! |' ^4 E; N4 [* s
// list from timestep to timestep
% q p) ^; q0 K+ i7 @: `! A # X& j1 d& z# ]
// By default, all `createActionForEach' modelActions have
+ J+ C5 k5 g0 s( U+ B, X- E // a default order of `Sequential', which means that the
( y$ m* T2 J9 Q5 W3 s // order of iteration through the `heatbugList' will be, z1 [& l1 k1 H S1 a: o
// identical (assuming the list order is not changed
7 f& p. u; k! s' y+ \' r7 \ // indirectly by some other process).% w0 h& e3 D& D v4 [
2 @+ f' q; h5 q, [* J+ i& } modelActions = new ActionGroupImpl (getZone ());4 k6 u0 l; q2 g" _
* |! F1 [0 a8 ~3 [, y2 L! h
try {
1 r1 `! F9 }- Z4 D2 A4 A$ R" M modelActions.createActionTo$message( r/ O$ d7 q- Q4 Y- H) q5 ]& |
(heat, new Selector (heat.getClass (), "stepRule", false));
; p: ` S2 X. _ } catch (Exception e) {# T5 I; O# l, M1 A5 J
System.err.println ("Exception stepRule: " + e.getMessage ());
) {4 a# f4 {$ c' w8 q! X" ^. ] }
, X: {. }1 P K \# l- R
* v/ L8 x$ Z7 x0 P- C# T) l try {
! l# c$ c/ I7 L' h( l Heatbug proto = (Heatbug) heatbugList.get (0);$ \7 |9 S, _, b. A
Selector sel = 6 h9 f+ y5 a6 N
new Selector (proto.getClass (), "heatbugStep", false);
$ C5 } n% l1 _ actionForEach =& ?/ S! L* F8 J4 V$ h6 C. |
modelActions.createFActionForEachHomogeneous$call, H9 D9 M' F! s- j. V( f: I
(heatbugList,1 \1 g" { N3 K8 L
new FCallImpl (this, proto, sel,% x( Y' K* x4 t8 t4 \
new FArgumentsImpl (this, sel)));
c1 P" ?0 y0 J( \( {& z3 \. O } catch (Exception e) {4 ?" I4 V S) P3 D
e.printStackTrace (System.err);
0 h8 _, C# A$ d- X }
: N" f& E. g8 l/ d ! o% s& w7 P1 E3 Y& a3 j# m& N
syncUpdateOrder ();8 R3 A, @, g/ U
) t3 e0 r6 D$ t try {7 G# {2 R* T& }$ N9 C
modelActions.createActionTo$message / H* s8 b' L2 I' \
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 H3 J" @, T1 g1 A5 V0 Y6 m } catch (Exception e) {
5 W6 @0 w: \% K U7 X' ~ System.err.println("Exception updateLattice: " + e.getMessage ());1 n; V4 P" L ^
}
2 U; ?: k! z F) Y; L
2 Y2 L" b7 r0 `/ i i% l // Then we create a schedule that executes the
5 r9 o: L! `1 l2 A8 e. ^ // modelActions. modelActions is an ActionGroup, by itself it
% v( M3 F! F( { // has no notion of time. In order to have it executed in( z7 K; g S- W+ ]
// time, we create a Schedule that says to use the5 U) U& l l& r' o
// modelActions ActionGroup at particular times. This3 T6 J1 Q; v4 T
// schedule has a repeat interval of 1, it will loop every( F. w% ]4 B+ N% p9 ], [) o
// time step. The action is executed at time 0 relative to
8 y" B0 N" _/ d- Y' N // the beginning of the loop.
; R) [; K# n' [ z' [( I" Q1 v6 F/ T# f' B
// This is a simple schedule, with only one action that is
+ O* _3 d, s4 Y p* u+ t! u# g // just repeated every time. See jmousetrap for more
% L* s% e* b; n% i! f // complicated schedules.
5 C# V1 ~' Q; t) T% ~4 l
; j* H: a/ R8 a) X0 j modelSchedule = new ScheduleImpl (getZone (), 1);
" M4 ^2 |% @2 P0 m0 e! d p1 _. b modelSchedule.at$createAction (0, modelActions);) Y& z% A V: W0 k( ]: Y6 P
b! V& P% `- a5 ~* J3 _ return this;
. Z* I; R( |/ f: l% h9 o5 M } |