HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# k7 x( e; u6 y% t
: e* [) O1 u* j8 w0 [. o7 j3 e5 D public Object buildActions () {
4 T3 ]' r' q$ x% M super.buildActions();
\ G: M1 e1 f" b4 C6 J $ ^# a, Y& t/ o, b; z$ s1 l" M
// Create the list of simulation actions. We put these in
3 F$ D8 l4 g4 \* ^/ `0 c // an action group, because we want these actions to be4 J" k$ f6 y/ W0 F
// executed in a specific order, but these steps should* N# h, u, R" ~7 {% Y
// take no (simulated) time. The M(foo) means "The message
0 O3 c) V7 f$ j, I7 U0 l: Z& [9 { // called <foo>". You can send a message To a particular
% p3 [: x. p# Y$ s+ a. g // object, or ForEach object in a collection.
/ V6 Q, a: a# b/ s9 T
- _, V# N9 I8 _* V // Note we update the heatspace in two phases: first run3 y V8 z3 ^( @# |! n
// diffusion, then run "updateWorld" to actually enact the* d" k& r! d2 O, _ }
// changes the heatbugs have made. The ordering here is! D/ b# G+ l% a9 |6 d" h2 @
// significant!& D- T: F, n) n! R. G5 I* g0 R
% \2 A" P# t% N- C, K/ R, |. [" l! W8 G // Note also, that with the additional% d* y; N# l# q0 b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 o. Q5 _/ m7 } A0 @ X, L // randomize the order in which the bugs actually run
6 M$ N( Z1 b( Z ]7 M // their step rule. This has the effect of removing any& U( C- S6 C+ A9 }4 w7 l
// systematic bias in the iteration throught the heatbug& i* ~9 {0 p# Q" U( K( I
// list from timestep to timestep
/ G+ y r; r( g4 X3 h
$ X1 W* _- J" W% d6 A* } // By default, all `createActionForEach' modelActions have
( s( T3 t6 \% T1 R& R0 t // a default order of `Sequential', which means that the
/ h: F' L W1 S6 F, ^ // order of iteration through the `heatbugList' will be8 n+ s8 O6 {1 t% I @
// identical (assuming the list order is not changed# L8 O% j6 i' c# l* g
// indirectly by some other process).
]) {4 G/ e9 f, c# {
1 R8 T8 ^& B4 |" h modelActions = new ActionGroupImpl (getZone ());' X; j$ n1 c. x9 s+ Z( U2 O9 E: X- U
' X } O% m. c, S1 c& g
try {
" t7 A3 {& p8 V" ?9 m. o B modelActions.createActionTo$message
# N# W, t& S0 h7 {( k" I (heat, new Selector (heat.getClass (), "stepRule", false));
9 W8 p+ k7 I5 o } catch (Exception e) {4 S4 N6 v- y3 z3 K) [ B4 T/ Q
System.err.println ("Exception stepRule: " + e.getMessage ());2 }6 O( i( W; Q% f7 v
}6 A! M" u; U+ P: u* a5 V, B* N4 M9 S
; z5 E) T3 v# H
try {6 t+ u( N7 d( ` M6 j
Heatbug proto = (Heatbug) heatbugList.get (0);
Q+ @+ G% Y% [) j7 \2 n ]" W Selector sel =
( [/ G9 @/ O; Z7 U8 [! \+ T7 x* W6 j new Selector (proto.getClass (), "heatbugStep", false);
. T$ K7 e- t% O' }& q actionForEach =
, x- z0 y' d5 F J6 [" S modelActions.createFActionForEachHomogeneous$call* q( ?7 U7 j8 P2 g
(heatbugList,8 `8 F3 T* y) Q4 T9 ^
new FCallImpl (this, proto, sel,1 C y6 f }/ z6 J: S( p
new FArgumentsImpl (this, sel)));
! M) D5 U' U5 W8 `0 K } catch (Exception e) {
3 _1 n$ h. |+ V, v# p. n7 f4 S. s e.printStackTrace (System.err);
$ o7 M/ ~# V4 Q& q$ e1 o }
4 s* `2 Y2 e& h$ }. a 1 E/ o4 H* O) i# x
syncUpdateOrder ();3 K% U6 E* X2 ?5 X% J- l9 o
( K$ F* p6 J5 m
try {
" j0 i$ o6 t0 x4 k( K6 L modelActions.createActionTo$message 8 M) ^. o7 `: O. z- i& g
(heat, new Selector (heat.getClass (), "updateLattice", false));' g! U: K( u; }4 r4 q
} catch (Exception e) {9 i, N7 m H! `0 t3 x ` U
System.err.println("Exception updateLattice: " + e.getMessage ());
. M3 T+ R0 D/ b) |. I B4 x }
+ Y0 M) N- C( N j+ g \
9 ^4 G* `. `: m5 r% e // Then we create a schedule that executes the; Y" ^1 Y1 H6 I* Q& \( M
// modelActions. modelActions is an ActionGroup, by itself it ]6 I1 z5 F5 x+ I5 D
// has no notion of time. In order to have it executed in" K" h6 h+ R3 e6 G
// time, we create a Schedule that says to use the
5 L7 z# a6 q6 [) |) m% d& t // modelActions ActionGroup at particular times. This3 T$ m2 \5 }+ `4 s+ c) L
// schedule has a repeat interval of 1, it will loop every
7 N" s/ g. a% `- M6 I* ~8 I // time step. The action is executed at time 0 relative to- l$ m: H. h# n6 {
// the beginning of the loop.& g/ l7 R, n3 F% @4 i: t1 L1 k4 s
1 J2 [+ ]0 x/ d% y
// This is a simple schedule, with only one action that is7 \. g; `* T& w, [; {" y
// just repeated every time. See jmousetrap for more o- G7 P% u/ y2 O0 @$ P
// complicated schedules.# f3 a" T: T5 h0 H
+ k+ K$ ?, d& ?9 ~1 t# V! s modelSchedule = new ScheduleImpl (getZone (), 1);
; |6 C- s- Z4 ]# w) T% c modelSchedule.at$createAction (0, modelActions);
% g0 m7 W1 R& D1 q& n/ D7 S
' Y' C* q( C& ?3 O% Y" f i1 v return this;6 C- c* g4 D7 T# w
} |