HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! Z# b9 v! \$ c
; V/ X- a& c3 @
public Object buildActions () {
& ]7 |# } k/ t super.buildActions();
) H& Z+ A. y/ M% j8 V4 b$ b 9 A- e' o4 Y5 a* u7 q
// Create the list of simulation actions. We put these in/ {8 Z" W9 r+ U! o1 |
// an action group, because we want these actions to be' E. v% l$ v" Y5 T$ m# _% M
// executed in a specific order, but these steps should
2 c% \: ~( v: a) R; O. s4 } // take no (simulated) time. The M(foo) means "The message" h6 y! f5 n% T( S# N5 _+ o
// called <foo>". You can send a message To a particular9 J' e ~( d. I' ~. u$ ]- a
// object, or ForEach object in a collection., I! W8 w6 J! @, z6 x3 I9 D& y
7 W. O6 G0 I0 ^# H# L S // Note we update the heatspace in two phases: first run2 E& A# x8 j) B, j
// diffusion, then run "updateWorld" to actually enact the4 d) b1 U: S& P9 F
// changes the heatbugs have made. The ordering here is n) `/ ?! b; L* l2 H
// significant!* V0 }' N2 Q2 e2 W( N% G. r$ k6 W
# n# i' S `& {7 u5 g // Note also, that with the additional) {) q2 E( Z! C" d- b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 a$ }1 ^) o& ]+ ^+ b- | // randomize the order in which the bugs actually run
, R6 P$ v- C; i0 b% X( ]. T // their step rule. This has the effect of removing any
2 R3 H3 P! r/ `+ S; ~; V9 o // systematic bias in the iteration throught the heatbug8 B" k5 G3 s0 G. A3 t4 M2 d3 ^8 H" i. U
// list from timestep to timestep+ S$ \7 E! d2 I
Q6 ?/ a+ k/ T: O& [' H+ d // By default, all `createActionForEach' modelActions have; z" y/ I9 \! I2 q5 @$ Q" ^' {% @. K
// a default order of `Sequential', which means that the
/ a V m) l/ H5 Y9 X) c1 Y. K! E // order of iteration through the `heatbugList' will be* m% F G0 A' `3 a# f
// identical (assuming the list order is not changed
! Z& M3 M, F$ D* |5 w5 `8 | // indirectly by some other process)./ z4 F2 U; P1 u4 G* r' T( N
2 n7 }! ^' y/ T& r; c9 S; L8 n( ] modelActions = new ActionGroupImpl (getZone ());
+ h5 E, x3 M ~+ X: E) A" G/ n
. M1 T% x- t! Q8 O, C" {4 f try {0 @% H; A! Y. Q6 c+ x$ V' c
modelActions.createActionTo$message( W) y% `+ i* \3 O% m
(heat, new Selector (heat.getClass (), "stepRule", false));
! a* M' e4 [- B' |- [! M) O7 f; ~ } catch (Exception e) { i+ s! R. V7 Q
System.err.println ("Exception stepRule: " + e.getMessage ());: m+ Z; `$ q, q0 X6 C: v( {7 j3 k
}3 t# c2 s) ? U: ]
' ~3 x% E( W1 O7 R: y( z
try {
" \3 S9 i! R) B& x Heatbug proto = (Heatbug) heatbugList.get (0);# r. e! I: ~+ i
Selector sel = D7 N1 z: ^9 M# O; Y
new Selector (proto.getClass (), "heatbugStep", false);
6 k. w( `7 _3 g actionForEach =
& [. X, m* ~4 `% q: l- S% a modelActions.createFActionForEachHomogeneous$call
1 p5 y! \# x7 A) b5 R: P$ g5 T9 w1 x (heatbugList,9 ?( ?9 E0 d5 H3 l: p) h) i, ^* ~9 O
new FCallImpl (this, proto, sel,) {) x* O+ |# e$ w1 x0 G4 `8 |
new FArgumentsImpl (this, sel)));
) a& T8 w; x. ]7 w3 u } catch (Exception e) {
; \) W! h+ k0 E( o e.printStackTrace (System.err);
" U0 h7 i( M" P }" _5 A8 c0 U) Q- K6 @" o
0 F) w4 \5 z: W, f' u
syncUpdateOrder ();4 F0 |* W3 T W
) Y* {8 l. e4 W* j& V4 J
try {1 w p, O8 ]& M0 ^
modelActions.createActionTo$message
% m, x+ p X+ B n" a4 t3 [; ^ (heat, new Selector (heat.getClass (), "updateLattice", false));, l# Z( H) S$ W( Y6 F
} catch (Exception e) {
& b- K1 s" x8 ^: J1 q4 m System.err.println("Exception updateLattice: " + e.getMessage ());4 Y9 z+ e6 k: E2 |' ]2 Y
}( G; r8 z/ u" j# x( ?" P: H9 ]% g
8 s5 i, h# }' R // Then we create a schedule that executes the7 V# H2 Q* ~1 [* Z
// modelActions. modelActions is an ActionGroup, by itself it
8 @' s+ m6 ~% B/ y1 g) h! ]5 @ // has no notion of time. In order to have it executed in
, S G5 n. _' F. C* F // time, we create a Schedule that says to use the
7 s' R e0 h. Z3 ?9 c+ X! n' B // modelActions ActionGroup at particular times. This& `. z( G* W/ N4 y* T1 q) I* ^
// schedule has a repeat interval of 1, it will loop every$ M8 ^0 v( r Q: i0 e
// time step. The action is executed at time 0 relative to& o6 F2 N* H' r& r, _
// the beginning of the loop.
" @; k t" c" R6 V$ j( R5 \& l$ Z' k8 O$ d2 C
// This is a simple schedule, with only one action that is
6 [% Q$ n' P8 Z% [2 c2 _: x // just repeated every time. See jmousetrap for more2 u/ X% I% N" `: B2 R
// complicated schedules.9 @9 I# X3 A" W: m
6 h* Y5 [0 ?8 H0 ]$ @- ~
modelSchedule = new ScheduleImpl (getZone (), 1);
$ K( ?. _3 }9 G7 n0 C modelSchedule.at$createAction (0, modelActions);& c' O# U Z" }: J4 E
) z( U# s8 f- r. d3 e
return this;5 D6 z+ c3 m& H: F* l
} |