HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( Y. X9 \) i2 O" N/ f& X, p
, v% A3 K3 g# t* m! r2 _ public Object buildActions () {
) w4 ~) L7 E7 I super.buildActions();
1 i+ j9 A1 A' }' r 4 a3 Q0 K; |) V: d2 Q
// Create the list of simulation actions. We put these in3 o& X! @: @+ _, ?4 G8 O
// an action group, because we want these actions to be
' X6 t* c" S9 `6 ?$ B7 ^& L: Y% X // executed in a specific order, but these steps should- O+ U* C& x; m' s$ j
// take no (simulated) time. The M(foo) means "The message% ]9 m; q. N1 M
// called <foo>". You can send a message To a particular
' b- q# s. B' }" A7 i9 s# C! |$ c$ T // object, or ForEach object in a collection.- g# |* q+ |2 ~
j" P2 r6 S% d0 q; v
// Note we update the heatspace in two phases: first run
1 L; y" c$ w9 } A/ e3 d [ // diffusion, then run "updateWorld" to actually enact the
# [/ n+ D! g) |$ a( p // changes the heatbugs have made. The ordering here is5 B8 R; T1 S& z5 L
// significant!/ ~) t n4 G9 `2 S/ Q, J! K
; ?8 O# G8 j1 s, K2 Z* T' V
// Note also, that with the additional
1 n1 i! ?! Q, Z) X3 W // `randomizeHeatbugUpdateOrder' Boolean flag we can' z2 l5 H% Y) t5 X
// randomize the order in which the bugs actually run, w+ o* I: B6 z& ` m. ]
// their step rule. This has the effect of removing any
. x! {9 W, l$ n, M. l // systematic bias in the iteration throught the heatbug
8 D2 S9 n* M) s: S, \, l8 \ // list from timestep to timestep; N) {5 v9 }0 ` A9 T" X
& ]3 [& S2 p0 K- K- J; v, V. {
// By default, all `createActionForEach' modelActions have( v' S+ _2 e8 a
// a default order of `Sequential', which means that the9 N2 Z' M: p; p$ O8 E+ [. b# U
// order of iteration through the `heatbugList' will be
" ]% f d/ U3 |: V // identical (assuming the list order is not changed
" t5 ^% N4 i2 y; E0 D: ? // indirectly by some other process).
+ Y/ o* ~- w$ B% p& d; O
; }( v& E; k+ a/ M modelActions = new ActionGroupImpl (getZone ());
, U7 E, o/ I/ k3 E8 j6 p9 H; l% J( ~4 C( X( @+ {* z, Q1 v3 b
try {
% ?- ^/ `/ I P modelActions.createActionTo$message
* p( @/ x* [ _4 H (heat, new Selector (heat.getClass (), "stepRule", false));
8 y* J+ A0 b: G( l+ G# U8 ?4 ` } catch (Exception e) {
) w; L r( t. i% k/ m1 o7 k System.err.println ("Exception stepRule: " + e.getMessage ());
8 r+ D$ i' w# d. H1 W }* @1 E' o' n& Y* ?- F' P0 t) x8 I' L
) p0 B+ P, }0 B9 q try {& l% ~: [: |! M8 D' o2 ?
Heatbug proto = (Heatbug) heatbugList.get (0); Q) N3 t0 c* }6 n' a8 S# V
Selector sel = ( i$ Q5 G( U* O# [" c, Y1 p
new Selector (proto.getClass (), "heatbugStep", false);9 m% y) q# W# X+ {, c3 E1 P
actionForEach =- Q$ Y, N! Y9 E1 E. @' j; g
modelActions.createFActionForEachHomogeneous$call' d0 w p& x) k& |5 r! @6 E$ `
(heatbugList,
: O* @* d1 I. l/ Q, { O new FCallImpl (this, proto, sel,# N) z+ [: P7 Z! h1 n
new FArgumentsImpl (this, sel)));
: V) e& W; ]' ~0 s. Q: K$ r4 z+ @ R } catch (Exception e) {
5 ?5 q3 p4 r( }6 k e.printStackTrace (System.err);7 N7 O& P/ Y, D3 v0 T$ {) P
}3 x$ r, V' U" z8 s( [/ J
' w+ ]. t, v+ s
syncUpdateOrder ();
4 _1 N, w% p: [* K" d1 |
! s( K) ?0 S' m7 a2 P try {
% k3 l$ W0 E9 G3 ]3 x modelActions.createActionTo$message
* t. g# f7 P/ h7 X (heat, new Selector (heat.getClass (), "updateLattice", false));
5 O: d [9 t) F } catch (Exception e) {
( q& O3 G+ e% z System.err.println("Exception updateLattice: " + e.getMessage ());1 Y+ L) ^- x" Q: w8 Q$ m* W4 G( [
}
5 ?. _9 Z& t% A* S" \) ]
: p# U/ k- n: v- y7 ]- ? // Then we create a schedule that executes the
) y2 K0 c {, k8 S& Y' b // modelActions. modelActions is an ActionGroup, by itself it
2 ~$ q& M! L' R6 w o6 j$ y // has no notion of time. In order to have it executed in
/ S$ R( j7 a& |7 f$ U, J5 ] // time, we create a Schedule that says to use the, f$ {$ p" x: F. @: ]- b4 {
// modelActions ActionGroup at particular times. This
% x0 ?' U5 L) n L: B" y# W$ ? // schedule has a repeat interval of 1, it will loop every
* |/ e7 D* z1 x3 w4 x! U // time step. The action is executed at time 0 relative to
& U$ `: u# p3 G8 K, o S7 T4 ? // the beginning of the loop.+ r6 \; t. F$ `$ Z: b" ?: Z
3 i8 A X( _! f/ N2 w // This is a simple schedule, with only one action that is
; `" g% R! q$ p // just repeated every time. See jmousetrap for more
& N) ~8 u3 L5 Q. K4 O // complicated schedules.
0 l% Q* M1 M1 [
5 r* P8 d- L1 s7 S* W6 O modelSchedule = new ScheduleImpl (getZone (), 1);9 S- b5 {/ u0 X: i
modelSchedule.at$createAction (0, modelActions);5 ]$ p6 p& g# U( c
, z- ?; B5 D3 S9 v/ y return this;& v5 [( n. S" \& x% E; ^4 a
} |