HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: @# d: u% w) @; P& ~
9 ]& ^/ L9 ?' r public Object buildActions () {- r9 P* d2 k Z# q) ?7 n7 O
super.buildActions();
C, R, _/ |7 M) m. s: g % H3 ?: X* `4 ~5 y/ B7 y* a6 @4 z
// Create the list of simulation actions. We put these in0 t5 _: Z+ d# ^" v
// an action group, because we want these actions to be
: H/ g7 B% ]9 G3 n! }1 Q/ ` // executed in a specific order, but these steps should e! g7 J# Z! G. h. x6 |
// take no (simulated) time. The M(foo) means "The message
: L$ c# q% |: I6 `8 @ // called <foo>". You can send a message To a particular' i# ?: A! J' n p
// object, or ForEach object in a collection.
0 e% C& m1 r3 w! q! y$ I6 l
# L" q9 X* c- t // Note we update the heatspace in two phases: first run& J8 v6 V! I% c6 K# y
// diffusion, then run "updateWorld" to actually enact the
* G; y3 n/ c' h8 a$ l // changes the heatbugs have made. The ordering here is
* k6 E& ]0 r9 g, f( H // significant!. U0 m, i6 r9 K5 r! c) x
8 a% [$ C/ U: V" K& B5 r1 l // Note also, that with the additional
: f: y$ [" F0 l% s* B // `randomizeHeatbugUpdateOrder' Boolean flag we can
# j8 {, T; Y; X6 }! j( ^/ A' f // randomize the order in which the bugs actually run `9 e: ]3 t% X
// their step rule. This has the effect of removing any
. D5 q7 Y+ [3 R+ g# G3 m* e // systematic bias in the iteration throught the heatbug
$ z7 [& g+ I+ ?, }* b/ Z2 Z; M // list from timestep to timestep; p$ V3 A9 H/ ~4 T8 P( Y
8 J/ G" J) [; t
// By default, all `createActionForEach' modelActions have
" x4 M" ]3 r8 X7 Q) b // a default order of `Sequential', which means that the
) N( U+ F+ R( c+ c // order of iteration through the `heatbugList' will be
3 ~. g4 c N+ w* i! J // identical (assuming the list order is not changed X# `' t( s! p r! t1 v, \
// indirectly by some other process).; R* D3 w1 ^9 T% ^
" h0 s% A. ~9 u
modelActions = new ActionGroupImpl (getZone ());) P2 b, c' O1 u4 R7 X: }7 P4 ~
4 ]( r) j5 E! `- V* m6 P) v try {
( n, M: |) t/ X% G8 ^) q' F modelActions.createActionTo$message
3 B& U4 d5 |6 ~3 i (heat, new Selector (heat.getClass (), "stepRule", false));
0 t3 K$ U5 T; I4 A7 `" k } catch (Exception e) {+ B1 l& t# d4 p4 {0 o! S; [0 Y
System.err.println ("Exception stepRule: " + e.getMessage ());4 b. o. L3 R, k7 u4 c1 ^' |
}; Z: {( O) I: Y w' \7 O
- b; \! f5 [% Y- k7 y
try {; E. F- B+ K" w
Heatbug proto = (Heatbug) heatbugList.get (0);
2 {8 t9 v8 O% m9 t+ E9 f/ E8 K4 J Selector sel = 2 k2 f5 E/ C* I6 U9 Z M
new Selector (proto.getClass (), "heatbugStep", false);
. J5 ]" E' S X" V$ d" g actionForEach =5 `, E9 o4 o. D0 n
modelActions.createFActionForEachHomogeneous$call
% v6 L7 g3 V) o0 Q7 r, ~ (heatbugList,
8 h: d+ f% r4 ?# w1 W- z; d+ m& M% B new FCallImpl (this, proto, sel,# w3 W9 J2 Z* ?2 f3 o
new FArgumentsImpl (this, sel)));) O0 c# O- l4 K6 W
} catch (Exception e) {+ u+ U" J( C; A7 M' [
e.printStackTrace (System.err);9 W L5 ?' t' x) G
}4 y+ {* w# g& }+ L$ W( h
6 v4 `0 }7 G: ^3 f; L syncUpdateOrder ();
% ]6 ^/ p& T# C; X
% _! V& y h$ a8 ]' \ try {5 C7 N0 @7 {: `* Q! s, Q
modelActions.createActionTo$message
) S9 T+ c" e5 H' v (heat, new Selector (heat.getClass (), "updateLattice", false));
/ H4 Q* K* f9 N4 N# A8 _ } catch (Exception e) {5 Y, q( f- v; q; k0 g% _9 d) s
System.err.println("Exception updateLattice: " + e.getMessage ());
1 @8 U4 D# }) V# V, K+ i5 t! c H- f }
+ m2 @, }- C7 i" w) n$ r* x" M
) n6 H, S) C+ s. [4 g, K // Then we create a schedule that executes the' T+ S k! K' J3 |
// modelActions. modelActions is an ActionGroup, by itself it& F& S. X N8 u
// has no notion of time. In order to have it executed in) _+ N7 T y |
// time, we create a Schedule that says to use the
M. o2 @) j2 ~7 b& r% k4 ?- ~ // modelActions ActionGroup at particular times. This5 x) U7 k# O5 M) }$ s3 M4 F' a
// schedule has a repeat interval of 1, it will loop every$ ^' x3 e9 g8 d7 h5 X8 [) ]
// time step. The action is executed at time 0 relative to4 w- X2 E, Z0 o0 f) `
// the beginning of the loop.4 `) M1 I4 B: |' c
$ P+ a& O5 l5 m1 \3 i4 p
// This is a simple schedule, with only one action that is: I7 c, O' e9 W& I) b% L& s
// just repeated every time. See jmousetrap for more
6 o: o4 d" K% {( \4 o // complicated schedules.
* S$ b* H; ^* Y9 y" j
) e# r: g% ?6 g1 J1 s modelSchedule = new ScheduleImpl (getZone (), 1);
" i0 f* U6 A. K" W7 M. ] modelSchedule.at$createAction (0, modelActions);# g$ s, ]" R4 R( @
' G2 b) g5 Y' l. B
return this;& q8 I! \( _% a6 U1 @% C
} |