HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 b5 q8 h' t! i
v j2 `2 z& s7 d public Object buildActions () {: A z7 x2 ~7 ~. q. R
super.buildActions();
* C6 H4 Y2 k) P% X6 X ]( O 7 I6 H1 l. b- n( O0 R
// Create the list of simulation actions. We put these in% H- ]; l+ S6 W& q$ z% Y
// an action group, because we want these actions to be
& `; V4 o+ D+ `$ i& M- r" S, x: ?' ] X // executed in a specific order, but these steps should
" C6 g. q; i# w' L& v5 X // take no (simulated) time. The M(foo) means "The message
S7 S" E1 P& o/ D# @$ r // called <foo>". You can send a message To a particular
1 l9 x! a% [, u6 v! E' q // object, or ForEach object in a collection.
& g2 h% I! E5 l" c% _+ t
# H/ k, @8 A% _ // Note we update the heatspace in two phases: first run
$ g& r# z3 _7 b2 \; R, G // diffusion, then run "updateWorld" to actually enact the; l& r' v( ]( e6 k6 X& R/ [6 W
// changes the heatbugs have made. The ordering here is. \2 q4 l* p7 b. h9 ~0 J
// significant!# o9 i7 [8 w( a% E5 c
/ [" f$ H; U6 [ l7 C
// Note also, that with the additional$ F: a+ [$ O+ l- q! W) X5 n, P2 r" C
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( Z3 d; U- T8 \3 q/ W // randomize the order in which the bugs actually run* B/ M& S8 c4 Z3 \# B% O4 {
// their step rule. This has the effect of removing any
: N" _& ]3 ^! N. K! ] // systematic bias in the iteration throught the heatbug/ I# c! f8 B3 W Z& c' U5 ?) \
// list from timestep to timestep( r3 |6 K/ J" ^, T: U9 p# K% n
1 x- B; y: i3 R x3 d/ R // By default, all `createActionForEach' modelActions have) ~6 z% e0 |2 W8 G5 x5 A2 \
// a default order of `Sequential', which means that the1 c0 |/ i) E' ]# ~; \# n. X. g8 c) ]
// order of iteration through the `heatbugList' will be
q6 [; @ _0 G" i+ w/ R, Z m // identical (assuming the list order is not changed j# x! `: c" g' _5 Y) B! p& @
// indirectly by some other process).
0 f; X, G: j; Z0 j( W2 | # O1 r; e6 T3 l
modelActions = new ActionGroupImpl (getZone ());
/ Y$ T. g' N2 s! u* L
* N' P* [) u- q' P8 P- K try {
' _! \/ n4 E \. E, G modelActions.createActionTo$message1 R, a9 I" k5 C$ m5 [2 A4 O- q
(heat, new Selector (heat.getClass (), "stepRule", false));1 f4 @/ \% d$ Z
} catch (Exception e) {( N& d$ ~- K: { S$ |" u. \
System.err.println ("Exception stepRule: " + e.getMessage ());
3 m9 s3 a4 }. |* l g }
; l# e$ u4 M3 w+ I5 \7 E6 U9 b% W6 F# e( [5 O; ?
try {
" a0 y8 j! ^ f1 ~2 b% ` Heatbug proto = (Heatbug) heatbugList.get (0);
% x* Y; K4 ]3 [% l) c Selector sel = , j6 }; } _3 e( O" W5 m) }, c- X1 G
new Selector (proto.getClass (), "heatbugStep", false);( T( X# x4 m" d" U$ j
actionForEach =: F0 M" T* S ~# ?
modelActions.createFActionForEachHomogeneous$call
! X' ~" n6 {7 l0 p8 H (heatbugList,
3 `1 m% I: j: v+ f new FCallImpl (this, proto, sel,9 {& w/ ?" w* [3 m ~
new FArgumentsImpl (this, sel)));
! O% t4 k. F/ B+ ?) [ } catch (Exception e) {
5 P) z5 V# z2 n# [9 A e.printStackTrace (System.err);( P6 t, h# o# U6 Q
}9 l4 l* f) R; U. U# u) Q
0 ]$ H0 A/ L$ K+ w3 [5 k/ P0 F5 v5 B syncUpdateOrder ();0 Z; @' Z1 C) K# ^
' Z# q4 H! N, f! K* S2 e try {5 p( g, X9 C2 Y/ q. a7 ], J" |: b
modelActions.createActionTo$message 4 F' v6 N/ S3 {; z) Y3 B E: }) z7 H
(heat, new Selector (heat.getClass (), "updateLattice", false));
- o+ @0 j. K, v } catch (Exception e) {4 V6 {( o! b; ^! P1 v4 L+ \& S& n1 k1 x
System.err.println("Exception updateLattice: " + e.getMessage ());
) G) ]& A4 M3 H. ]- q6 y r }9 X: p: ^. f" U* O7 s
4 y& g" Q9 D) Q5 R // Then we create a schedule that executes the
% |8 Z6 q8 G) p3 q: Y4 q4 y // modelActions. modelActions is an ActionGroup, by itself it
; B5 K$ w; e4 m( b! @: v // has no notion of time. In order to have it executed in5 \& R, v" p( z: v& `
// time, we create a Schedule that says to use the
/ f& Z% Q# o) ~' e5 n) v5 i. ]8 Z // modelActions ActionGroup at particular times. This+ O9 z8 [; E0 T1 b8 T% o, m8 Y6 A
// schedule has a repeat interval of 1, it will loop every6 U8 K' ]/ G% ~7 A% j
// time step. The action is executed at time 0 relative to
8 k9 W2 s9 `- U- ]$ P$ I% c // the beginning of the loop., j1 A: M5 | ]; a
9 t6 c/ G+ q9 b // This is a simple schedule, with only one action that is+ @. _3 l' Q5 ^
// just repeated every time. See jmousetrap for more2 Y5 a- \. J2 c; m8 I
// complicated schedules.! }" ]. j% F0 e) n+ |
6 W9 [# ?8 |/ ^7 X- B
modelSchedule = new ScheduleImpl (getZone (), 1);& W8 U4 d0 g% a4 @0 d
modelSchedule.at$createAction (0, modelActions);
8 B+ d% v: \3 ]8 K/ ?
_& ]! P Y0 K9 i1 A% X& @7 { return this;
+ d' L% T3 Y" e0 J5 L } |