HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ Q& }$ V- Z" ~0 U1 O9 h
$ u$ ?& l# l4 T; r& T. b+ F
public Object buildActions () {7 E( l+ V7 x# f; j2 y: L- p9 F
super.buildActions();0 A# f4 {4 L! J' Y. S* t* ?
+ K7 }- H I0 d2 }8 L j+ N! C // Create the list of simulation actions. We put these in
* F4 `1 @* {. T \) R. X. s- f // an action group, because we want these actions to be6 f% B- j- j6 U0 F4 K/ {- _; t! a
// executed in a specific order, but these steps should
4 v7 }+ x; L: L- g, o4 q2 a- ? // take no (simulated) time. The M(foo) means "The message7 s$ }* d( _' N
// called <foo>". You can send a message To a particular
+ R* `/ m0 l$ J( B- u. ]0 z // object, or ForEach object in a collection.
& X, {9 |2 G7 \- W8 W
+ \1 g, c4 x; p ^ // Note we update the heatspace in two phases: first run$ B, O, K* e# u6 F8 c& c: T' f
// diffusion, then run "updateWorld" to actually enact the* Y; \( B/ u' r4 y0 d& F
// changes the heatbugs have made. The ordering here is8 v/ p) L1 @7 L. \
// significant!
" X1 q/ A) `" P# i$ X c' w' `. i! \, E( I
// Note also, that with the additional
4 ]. D4 |+ c/ b7 Z% \ // `randomizeHeatbugUpdateOrder' Boolean flag we can6 @) c/ d/ y6 D8 }/ M3 c" Z
// randomize the order in which the bugs actually run
9 N' d4 f9 j, j: l // their step rule. This has the effect of removing any
' R @8 e- x1 r1 h6 C1 V- D" d // systematic bias in the iteration throught the heatbug/ y. \) H/ \8 x$ n7 J0 w/ h
// list from timestep to timestep
) D+ s8 b8 i% m( ^7 I
" E6 t) F% C3 Q' {! p* Y, N5 h // By default, all `createActionForEach' modelActions have
- F9 `! S# O3 I6 ` // a default order of `Sequential', which means that the
3 n- {* y0 V& }; L // order of iteration through the `heatbugList' will be+ h2 Z* D3 k- Z7 s6 h
// identical (assuming the list order is not changed
# F# @. S& }+ b( `! J$ K // indirectly by some other process).! n2 K" p7 ~9 q: D# T
. u/ j4 X3 c' v/ H
modelActions = new ActionGroupImpl (getZone ());6 b8 I8 n, y# F1 B
7 l( Q: Z9 y' n: C try {- {6 ~2 M/ w% H
modelActions.createActionTo$message
' S: V, W1 \' u) } (heat, new Selector (heat.getClass (), "stepRule", false));
% `/ M, o2 n/ J& w, e; c5 c* W! X } catch (Exception e) {% M; E+ T0 a2 b1 L; l, U+ t
System.err.println ("Exception stepRule: " + e.getMessage ());
^4 C" t- r5 V) r# q }
7 ?' {2 `; R6 F$ ? Z6 `8 z6 U' K7 k% R a8 @
try {: W$ B9 A' g1 x4 x$ p* B; F
Heatbug proto = (Heatbug) heatbugList.get (0);
1 } l4 X& v z/ S/ Y Selector sel =
- W) r8 {" s9 }# a new Selector (proto.getClass (), "heatbugStep", false);. u0 W" ^4 [- ^% e/ i0 ~# J5 e
actionForEach =
/ {) Y: X* q3 `! |1 ~* ^- k modelActions.createFActionForEachHomogeneous$call0 Y7 {% J$ }2 m& T9 y, z; r
(heatbugList,
9 u7 S# I* a/ @: j# Q new FCallImpl (this, proto, sel,
; H# {! ^$ k. p6 W* x4 I new FArgumentsImpl (this, sel)));
) P* F! G. M" o' { } catch (Exception e) {
5 O% H$ V$ Z# I e.printStackTrace (System.err);% S* H' o g% u( J
}2 @/ O8 {6 @+ m, `/ k) z+ X
" W9 Z: ~! J$ f syncUpdateOrder ();4 H. b5 p% u _( I% u. V/ v2 k% e* f7 {
+ B5 w% h' j! j% c# G/ L; B6 o g" h/ e try {
7 h* R; z' V& i( Z _9 _ modelActions.createActionTo$message
a. f0 }; A% z. b4 ^3 v( m, K0 ` (heat, new Selector (heat.getClass (), "updateLattice", false));2 ~" f% \7 o7 s, G
} catch (Exception e) {, _2 _. n* ^5 q7 Z! ~+ t9 b
System.err.println("Exception updateLattice: " + e.getMessage ());' n' V; D0 h) d( D" r1 g: t
}
% g0 |/ L$ r$ b1 u3 n! Z
1 |( q& R) B- y4 g' N // Then we create a schedule that executes the
% F" @8 w. [' L3 p: K$ A // modelActions. modelActions is an ActionGroup, by itself it
0 [; @1 `# l9 {$ h* i3 s // has no notion of time. In order to have it executed in% l/ [" e2 d6 K6 O/ h2 e
// time, we create a Schedule that says to use the
9 |2 \0 r/ b9 U5 } // modelActions ActionGroup at particular times. This
$ E8 o; ^. k$ }3 H7 y2 C) L6 ^ // schedule has a repeat interval of 1, it will loop every4 ]; _4 [" m R0 E+ z0 N ]
// time step. The action is executed at time 0 relative to
* `$ d' K0 o& ~: H3 {6 P // the beginning of the loop.3 W$ I, \6 `: T+ a2 O
9 ?: {0 _# l0 S4 J# O
// This is a simple schedule, with only one action that is
; E) `. e0 W; D0 Z: v8 n8 }% S* \3 F // just repeated every time. See jmousetrap for more
M9 i5 u. }! h- R // complicated schedules.
9 E* X# F! [8 c* } Q, Z. L. n& Y 7 f: b ?' p# X. d# C" {
modelSchedule = new ScheduleImpl (getZone (), 1);0 r7 S% G5 `* _* L
modelSchedule.at$createAction (0, modelActions);' A* p! }0 e2 \( C8 h/ l" H
8 F2 }7 t. f) T
return this;
) p; o4 @0 ]+ L: k2 |8 i } |