HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; d* j. s; _" K6 d# A) H1 m
- q. h5 y6 |; Y. A7 ?4 u
public Object buildActions () {
, I3 L: E) N6 V0 R3 Z" G0 A super.buildActions();
1 Z0 p$ M; `5 T g% u$ S2 j ! }+ r% O, T2 Z; g" P6 f
// Create the list of simulation actions. We put these in2 F L' ?5 x- y/ s% N
// an action group, because we want these actions to be
. a+ w- t& z1 D& Q$ t // executed in a specific order, but these steps should
7 U3 Y5 l K/ Z0 d M! | // take no (simulated) time. The M(foo) means "The message
2 A8 V; D7 t4 T* \ // called <foo>". You can send a message To a particular
* i' t' \/ F. K // object, or ForEach object in a collection.( @% m/ T/ S) I1 _' T6 H& A: z
6 y/ v; B0 o. h9 ^9 q2 v // Note we update the heatspace in two phases: first run4 N2 R$ n/ A( n6 H3 j
// diffusion, then run "updateWorld" to actually enact the. U2 i! O# e4 M$ B4 b
// changes the heatbugs have made. The ordering here is
8 |$ S$ ~9 x+ B' R1 {8 ^( W // significant!: l/ N3 |( ]9 M5 ?5 M( m
4 O! k* b. L4 |
// Note also, that with the additional
) L; v! m* E y: r; C( N // `randomizeHeatbugUpdateOrder' Boolean flag we can3 w" A) \+ F8 }8 c. c
// randomize the order in which the bugs actually run
2 }& _" b% I* g: X" @% w1 T8 V // their step rule. This has the effect of removing any% a1 p9 i' g; P ^6 R- \
// systematic bias in the iteration throught the heatbug. G9 v& Q; M* E2 ]( y. H
// list from timestep to timestep" \4 i: y9 c8 T/ S
& @, N+ _, J( c4 Z+ d d a( R
// By default, all `createActionForEach' modelActions have
5 g5 ^, ?9 G; d& j // a default order of `Sequential', which means that the, B# }" l) Q2 a( y; Q
// order of iteration through the `heatbugList' will be( L7 K h' E# A. t9 a$ L7 ?# e
// identical (assuming the list order is not changed8 H) X5 O" q/ H$ F
// indirectly by some other process).2 X% |( K2 D$ @
! \5 u5 D' x: L modelActions = new ActionGroupImpl (getZone ()); I$ Y! F Z9 C9 G4 E: u. X9 j
7 T) D! I# y/ T2 v
try {
2 M( `- P g3 C3 a modelActions.createActionTo$message
- e7 l( P8 e* ] (heat, new Selector (heat.getClass (), "stepRule", false));
% Z4 _4 B( ?9 x8 s8 s8 ^5 \/ ? } catch (Exception e) { q8 X# Q$ N# I" l6 F
System.err.println ("Exception stepRule: " + e.getMessage ());% W! o( y0 `2 b3 ~! l F- z
}
* ]% Q- t/ }+ g& X. B1 \5 g8 T7 u% Z: a! r* H
try {
! d' X J4 P$ l6 w1 ^, J/ y: O* X Heatbug proto = (Heatbug) heatbugList.get (0);7 P: i0 D- I' |' P' G+ l! M( w
Selector sel =
7 p4 A+ ~: L2 Y8 @- T4 g/ v5 J new Selector (proto.getClass (), "heatbugStep", false); E% C+ L- H0 _1 D# ]5 X: t
actionForEach =
$ I9 x0 \' C% k+ Y( z. M modelActions.createFActionForEachHomogeneous$call \9 }' r5 i- z* C# _3 z
(heatbugList,0 t4 \0 A0 U8 q% t
new FCallImpl (this, proto, sel,1 ?0 C. Z- f7 H0 X& o7 M* f
new FArgumentsImpl (this, sel)));
0 s; P& M( Y7 t1 ~ } catch (Exception e) {
; M, k- t8 v# a$ I5 E e.printStackTrace (System.err);$ w# O8 M+ C, j1 y; F" }* X
}
3 c' W, [! R. l r+ R
' B4 {) B$ S! R3 p" K& P' ~ syncUpdateOrder ();
7 N9 B5 X- w4 @. W# e! L0 Z. O7 R. E9 [$ c2 W; A1 Y
try {$ e9 F- G( Q/ p P7 h5 X! p' b
modelActions.createActionTo$message
5 p: U$ A3 x7 \$ A (heat, new Selector (heat.getClass (), "updateLattice", false));. G5 q9 T, x* W @7 A/ E2 @* D- D; ^
} catch (Exception e) {# m D3 U! e2 w8 Y( ?
System.err.println("Exception updateLattice: " + e.getMessage ());9 e% d+ p4 f! H1 z
}& D$ Q8 U4 H9 ]% ?. Q. b
. ~5 e7 c2 R0 A) d* F' l
// Then we create a schedule that executes the2 C+ K7 l: F5 `3 ^3 \0 w8 R
// modelActions. modelActions is an ActionGroup, by itself it+ \8 h& a4 D, t/ A; X4 C$ S
// has no notion of time. In order to have it executed in3 Q! E1 u I c6 `) q
// time, we create a Schedule that says to use the
$ n" t9 Y2 {# r* R2 p$ a$ n // modelActions ActionGroup at particular times. This
" f/ o& d) Q& D2 u5 _0 s' N4 X$ u // schedule has a repeat interval of 1, it will loop every
, c) ] y6 u" R/ Z // time step. The action is executed at time 0 relative to
}& ?/ ~2 P+ _8 g5 Z* } // the beginning of the loop.4 M/ H) Y+ I J- D+ l! v: K
+ e5 D$ f% h. |# y9 s
// This is a simple schedule, with only one action that is) _& t% {' K- s$ n
// just repeated every time. See jmousetrap for more4 }& h- P: n, z% G
// complicated schedules.2 G4 H. p3 B0 C4 Q% f8 T
2 j7 B% `: f, E* Q+ M% J modelSchedule = new ScheduleImpl (getZone (), 1);9 O7 c5 X/ s( |3 p- J3 R
modelSchedule.at$createAction (0, modelActions);
9 S0 e c$ J F3 x' B " F7 K3 V% l g, s
return this;
/ A' ^: s) s% y0 j. ^ } |