HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& w$ }( M5 P- s( r3 C
K2 m/ A* S; G# M, v
public Object buildActions () {* C$ `( P' D X7 S+ N8 ^: K
super.buildActions();8 U$ d% @& Y# L6 h" p+ v* I
: l4 L& j: j5 r! u+ M* R // Create the list of simulation actions. We put these in
' r. N# D: G# w // an action group, because we want these actions to be
- E$ z7 q5 ^4 @6 f" C7 a // executed in a specific order, but these steps should& W- u' d' W$ {5 a; K( o; \) h
// take no (simulated) time. The M(foo) means "The message
- r! k8 X' X/ L; @ // called <foo>". You can send a message To a particular; w2 ]0 U- y, S0 k/ G
// object, or ForEach object in a collection.6 `8 ?8 f6 ~. @/ C/ s* K
# b, Q* b. ?* b/ c6 }' ^ // Note we update the heatspace in two phases: first run
" x l1 o& Z9 P+ V! I/ [ // diffusion, then run "updateWorld" to actually enact the* w- V! p9 V% I/ S. y3 C5 k& W5 p( x
// changes the heatbugs have made. The ordering here is
/ p# R2 K r0 c' k // significant!
b# R+ W7 i% F; [4 ~
. s- L9 R: x; G# |1 b1 O- D' C // Note also, that with the additional" L" L+ q$ j% r1 \. [* k- }+ g7 n
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 l$ @4 ]9 s% Y
// randomize the order in which the bugs actually run
9 A+ x! A& R& |' j0 m // their step rule. This has the effect of removing any
) V. Y' m9 |+ n" c // systematic bias in the iteration throught the heatbug8 G, x9 L% _4 O5 \: s: l/ o0 Q/ [
// list from timestep to timestep' r7 x. w$ ~- M* q0 d
' A D( \- \# i- T$ z+ L
// By default, all `createActionForEach' modelActions have
4 Q4 _' b/ X; u% P8 ^ // a default order of `Sequential', which means that the
7 c2 l; c: E# y7 F8 x+ ^ // order of iteration through the `heatbugList' will be
' a- A9 A" L b( }* d0 a) D' ~8 r // identical (assuming the list order is not changed
, F+ k# H1 W- E; N- \) H& w // indirectly by some other process).$ s! L5 `, ]2 |4 S6 L
( l" \. d; C% N! i% `* y modelActions = new ActionGroupImpl (getZone ());4 _! X& ?' [# }/ m& F t0 c% e
1 o+ X# ?. x. j/ n. Y
try {
7 c6 `7 t5 W" U8 N* v: k modelActions.createActionTo$message4 l3 A/ c3 {( R$ j4 o
(heat, new Selector (heat.getClass (), "stepRule", false));
- K+ }6 v2 v. ?2 ?5 ]. Q( {# K } catch (Exception e) {$ U' E4 }2 f3 t @8 W- `! _1 K
System.err.println ("Exception stepRule: " + e.getMessage ());
/ ^9 [/ ~; D1 q }
9 ^* Z g3 g4 a- l
8 |4 T+ D- u, s0 @; x0 m try {
. D3 e2 [4 p' J+ y Heatbug proto = (Heatbug) heatbugList.get (0);% t5 V# ^, q% ]
Selector sel =
k7 E9 u9 P. x. S new Selector (proto.getClass (), "heatbugStep", false);
* N5 M, f& i0 c; `2 T actionForEach =
% F# y' d0 O! L3 K' O1 K modelActions.createFActionForEachHomogeneous$call
* ~( d u7 Q/ d3 A& C (heatbugList,) Z9 l" i* |: b- d
new FCallImpl (this, proto, sel,
; ^- B, T l) p) j new FArgumentsImpl (this, sel)));0 k% ?2 a" p6 G- ?
} catch (Exception e) {9 [; ]0 ^7 T% R- G% Q# ^+ E6 z, Q
e.printStackTrace (System.err);
0 U6 D5 ~, I+ \5 X( h- D5 ^ }
( t4 _) p% }' K- C9 ~ : G+ Z6 n2 J' W) X9 n# R
syncUpdateOrder ();
. v" L# D7 o" c( ~! w" k/ ]1 _& c) N2 j" q/ W$ J
try {
0 R8 ]# t$ [3 V: h8 Z- _ modelActions.createActionTo$message
5 M: [* r8 p( ]5 Q (heat, new Selector (heat.getClass (), "updateLattice", false)); {! N7 K3 ?; h/ P) K3 F
} catch (Exception e) {9 _6 `" {% a" d( r
System.err.println("Exception updateLattice: " + e.getMessage ());5 e/ [( w7 s7 Q/ D3 \$ T4 \- N
}$ ~+ Z4 D0 B# S5 V% I. W
* w+ l6 _) {- \8 s* f
// Then we create a schedule that executes the
" h- t* t! }4 l5 K$ _: n4 c // modelActions. modelActions is an ActionGroup, by itself it9 a; Q7 o* h1 F3 A/ m
// has no notion of time. In order to have it executed in$ g; |* C* }3 P; c' s6 }+ y
// time, we create a Schedule that says to use the
h) C3 Q$ {) O7 `5 m& w* t0 n // modelActions ActionGroup at particular times. This" s& M1 w9 g8 B; p* g# |
// schedule has a repeat interval of 1, it will loop every
( s- F$ P) S% f* u: z% n // time step. The action is executed at time 0 relative to% K6 R$ ]0 L) f3 `) M4 ~* B
// the beginning of the loop. ^8 A* d1 N+ E" o6 y# l% V
( h0 ~8 x% }/ M% [) w' C: ]- @ // This is a simple schedule, with only one action that is
8 E, N2 r+ o2 b9 k2 z* K. r7 q/ O // just repeated every time. See jmousetrap for more
% M1 v. F5 w; T( H- y8 L // complicated schedules. k1 D: ?" z( z' \; Y7 e
9 A N( s; ~& r% ^ modelSchedule = new ScheduleImpl (getZone (), 1);
" g) M! `0 U) z1 S T( W0 ` modelSchedule.at$createAction (0, modelActions);
: V% d8 ]1 X7 q1 P3 s ( [ x0 a0 z8 |8 _5 K3 m
return this;
* q0 k; J3 z* V8 t } |