HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 ^& S& Y0 s. s) Q- r" H$ t7 [! |4 N% L) _
public Object buildActions () {
" l& ^* P9 o' v" e0 z super.buildActions();
- o- q0 G9 h/ h) T* F; | & \) q. N7 K8 r
// Create the list of simulation actions. We put these in) I& M( I: A# f2 a
// an action group, because we want these actions to be( @3 J" p& \2 F5 U9 L
// executed in a specific order, but these steps should0 E' U/ _% Z4 R
// take no (simulated) time. The M(foo) means "The message6 n* S: e1 T" S }+ z3 x; `# J
// called <foo>". You can send a message To a particular
( y1 U9 T, t2 b' N // object, or ForEach object in a collection.
- e, I% K# g% E: b% {
& \( w* G1 L v- }# _ // Note we update the heatspace in two phases: first run0 h9 I2 X* ?! x: i8 p/ I m5 O
// diffusion, then run "updateWorld" to actually enact the
( `2 x1 T/ J$ {: k. ]1 t% y // changes the heatbugs have made. The ordering here is
: o2 m5 ^/ G; l5 z% e // significant!" m: X4 V6 U% K" a9 N! U
; T! I7 J- F2 M
// Note also, that with the additional
$ {1 g. n: ]% I. g# Y( E // `randomizeHeatbugUpdateOrder' Boolean flag we can0 z0 m7 v( _! P0 G
// randomize the order in which the bugs actually run
$ s7 u( q# h) x9 c, K8 i3 v // their step rule. This has the effect of removing any7 `1 h1 g+ n, ~+ G% r* q
// systematic bias in the iteration throught the heatbug
0 {3 I. n$ E& J j2 T: Z' e // list from timestep to timestep
. ]9 |+ `' y& D! G
( a. E! I) G$ s& v m // By default, all `createActionForEach' modelActions have
: ?! j2 o2 G8 x' m; E. E, o // a default order of `Sequential', which means that the% i* t7 v/ Y, j' F; p0 B
// order of iteration through the `heatbugList' will be
" [" ?, `: ^- F) j; L$ ~ // identical (assuming the list order is not changed
. N* X) |$ W9 g r p // indirectly by some other process).8 H8 }/ R5 y+ b M3 W! _# Z
; x$ w% f8 K) u# y
modelActions = new ActionGroupImpl (getZone ());
- v1 G$ v, B6 S
8 c& j$ P# ~/ ] try {
' K' |& o c! k" B6 ]- A modelActions.createActionTo$message% p3 v" \& X# M) A }! C
(heat, new Selector (heat.getClass (), "stepRule", false));
# C/ k0 `# B4 l0 C, l: k } catch (Exception e) {
5 _/ w* V1 N% J. b System.err.println ("Exception stepRule: " + e.getMessage ());
& J2 l* A5 D' ?; [ }
6 C: M" m' ]1 _
7 J% G- j" r. ]% x0 D0 R) W7 x try {
. b$ e+ `5 | g) m/ _' n+ ]& S Heatbug proto = (Heatbug) heatbugList.get (0);
/ ^3 e7 F4 j- r1 L- ^: c- q Selector sel =
8 s. O& k( i' Z. } new Selector (proto.getClass (), "heatbugStep", false);
- @; Z6 J/ R8 R& F% c9 ~8 B actionForEach =
: N/ ~/ X' x! B1 f; v, b4 [0 p modelActions.createFActionForEachHomogeneous$call
+ F2 U$ A& H" o. G M! V (heatbugList,
$ k' P3 ~1 ?4 c8 G- Q new FCallImpl (this, proto, sel,8 B1 M5 o9 E4 i" l4 y5 Q8 B* W
new FArgumentsImpl (this, sel)));, M) G% C! N( G+ {; z+ Q5 K
} catch (Exception e) {
' x' \( o0 J N: y2 u8 s }' c e.printStackTrace (System.err);
& ]* }3 H5 U# F( e& { C }" {6 w, C1 A L3 W; R
1 H8 }/ x1 o0 s syncUpdateOrder ();
" t. _4 n1 P; |, }" G2 v5 ]' J. H; S' r$ Q3 N
try {
% D1 L+ t& s( [5 F; C2 W0 J3 v modelActions.createActionTo$message
. ?* D, i+ Q9 ]. _' c. }. j (heat, new Selector (heat.getClass (), "updateLattice", false));
f q- L! }+ M( r6 f } catch (Exception e) {1 A2 ]+ g5 D# n. x' Y
System.err.println("Exception updateLattice: " + e.getMessage ());7 k. ~$ {! p$ Y" N3 I
}! A0 p; E& ~# [) T q
) ~( V" |! @4 J // Then we create a schedule that executes the6 T) i, g; i" R( ^6 ^' q: v/ H
// modelActions. modelActions is an ActionGroup, by itself it/ {- O2 B. G4 z) [- }
// has no notion of time. In order to have it executed in
Z! n5 y# G- \$ z# K: } // time, we create a Schedule that says to use the
9 p2 G% c0 |" w9 U // modelActions ActionGroup at particular times. This
5 e0 ^; s3 _$ f' x9 d* } // schedule has a repeat interval of 1, it will loop every0 Y# Y, l" s2 F4 J+ |- V/ ~6 y% @, g
// time step. The action is executed at time 0 relative to
& ~% S0 o' J: J, o1 @ // the beginning of the loop.
$ F5 v2 T* W' F) d
* W* t( j z# r: @ // This is a simple schedule, with only one action that is5 f5 l( Q2 `% s" ^& ]
// just repeated every time. See jmousetrap for more0 t# |: Y$ h. T3 E
// complicated schedules.3 s% N" }& v6 |9 h- A+ t
8 i3 {" Y9 l7 S) @2 a3 r4 a& _
modelSchedule = new ScheduleImpl (getZone (), 1);) R& C2 e! p# x" `8 {
modelSchedule.at$createAction (0, modelActions);- y# M0 V/ C/ [2 J6 ]; K0 T
& d: b5 @' D' j
return this;% d- c4 s5 g( b
} |