HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: r. G, e' b3 o- R7 T4 H/ f* Y4 d7 _" c9 e
public Object buildActions () {
& a2 U5 x" {3 ^0 n0 c- `7 r super.buildActions();
% `* ~& b3 o" | ; a6 `: ]# J- O, t) `
// Create the list of simulation actions. We put these in
4 O) ^, O- i" P( S // an action group, because we want these actions to be
( L3 z0 o. _8 @9 p: h // executed in a specific order, but these steps should4 P! H* C. Z4 s: T$ E7 Z
// take no (simulated) time. The M(foo) means "The message3 i; n) V q; D3 d
// called <foo>". You can send a message To a particular D6 H0 {% n- I# d2 t/ g' I
// object, or ForEach object in a collection.# `" F w8 {7 y' }8 I. W- \
' d% B: C7 M* X4 k" m5 S
// Note we update the heatspace in two phases: first run$ H7 u0 B6 _! F: f
// diffusion, then run "updateWorld" to actually enact the
# _ K S1 W3 ~2 e // changes the heatbugs have made. The ordering here is+ r6 H% o, G4 M+ z. H9 {) U: ?4 r- \
// significant!) R$ A' ^8 o9 F
7 P8 L6 D. N) a/ V" o, O- M6 {, v+ s& ?4 b
// Note also, that with the additional
) d1 v) W) u3 n# F+ r // `randomizeHeatbugUpdateOrder' Boolean flag we can/ h7 q, ~5 @" ?
// randomize the order in which the bugs actually run7 G! h# R5 V. k" z: o8 q
// their step rule. This has the effect of removing any
5 n6 j6 M; B, `' p* e' v // systematic bias in the iteration throught the heatbug
- k Z. M$ T. ^; U+ {9 @1 U // list from timestep to timestep
$ F- o) J* D2 U! O 1 l, M, V1 w! v2 q0 J! u4 M
// By default, all `createActionForEach' modelActions have2 S& R0 H# [/ r# G7 O: g
// a default order of `Sequential', which means that the' P# m- Y* T. ?% x
// order of iteration through the `heatbugList' will be! L, U1 T# M) J
// identical (assuming the list order is not changed
2 ?. z+ a! N% y$ A. J: X; `, _* } // indirectly by some other process).
% Z( B: |( X- c* y l) i6 F" M + m& A( _1 X( z( D4 ^3 i9 o* I) I
modelActions = new ActionGroupImpl (getZone ());
0 {( Y5 X# Y3 P& p y
$ A5 S. X( r C- W9 K try {8 }( m5 d9 M8 H; @; _
modelActions.createActionTo$message4 L$ N+ c1 `7 Z
(heat, new Selector (heat.getClass (), "stepRule", false)); f' Q! V3 t) M {
} catch (Exception e) {
5 ~0 D5 | B: t5 d- B: w System.err.println ("Exception stepRule: " + e.getMessage ());/ N! r" l& s8 _: D* b5 n1 e; _
}5 ]8 u8 m# ?1 w: T* l ]* y
$ s! S! u$ B0 ? try {" J$ i4 J B* n. c+ A- c, l
Heatbug proto = (Heatbug) heatbugList.get (0);3 X0 P) I) [, b
Selector sel =
4 u% [0 G$ b# W& @; c3 k new Selector (proto.getClass (), "heatbugStep", false);
& Z0 _1 z4 U8 X; N' [# q* r7 n actionForEach =
" |3 a) R! l* ?8 r: t modelActions.createFActionForEachHomogeneous$call6 S ^" E. O' ^
(heatbugList,
1 q! A G0 t m$ U. s9 v new FCallImpl (this, proto, sel,
" M4 u3 d4 E+ ?* s- ?' x new FArgumentsImpl (this, sel)));
. p" ~" J& [9 s! u2 n! ] d } catch (Exception e) {
; R% l( { v4 @# A* i% P/ w e.printStackTrace (System.err);
1 K/ Y$ s5 a7 [- _" b }
: b* X! s4 Q" {* B; _# M: ]" c+ q: s
8 |/ c% m x6 F" ]5 m syncUpdateOrder ();
" w! n: S Y! S0 q* k# @+ l7 v0 g; q+ {7 A7 y# c" | A$ Z
try {" P7 a) \; I% i5 U" o. E% h% n: P
modelActions.createActionTo$message , E! B' [4 V8 Q4 J
(heat, new Selector (heat.getClass (), "updateLattice", false));# X. O: z/ f; f+ U9 z( d
} catch (Exception e) {( [0 H. ?$ H7 c. t; N- U
System.err.println("Exception updateLattice: " + e.getMessage ());
/ o# B) [$ h3 |' y. E/ u }0 {( ^% c6 V6 j& ~
" i" _3 a( Y$ b! k3 M& \ // Then we create a schedule that executes the' Y6 i1 Q! m& X
// modelActions. modelActions is an ActionGroup, by itself it
, [$ r. u6 k3 \& P7 B // has no notion of time. In order to have it executed in
' W6 {6 ?$ f- w2 q1 r+ o( {( R // time, we create a Schedule that says to use the5 _3 j8 s* ^* n
// modelActions ActionGroup at particular times. This
+ Z! ]0 z1 b. T3 y5 h // schedule has a repeat interval of 1, it will loop every+ Z0 I: Y9 T* p4 r! p: i
// time step. The action is executed at time 0 relative to/ I, Q: v# g& F6 g% z ?" o
// the beginning of the loop.
& a% J, d4 {4 L
+ Z N$ V$ }( J# ~* i, s // This is a simple schedule, with only one action that is2 ]" B4 y. l- Q- x( G ^
// just repeated every time. See jmousetrap for more
6 I( y5 q. `$ J( r% J // complicated schedules.) D) D6 l" k9 K; K* _0 H* x
* Z* C$ |/ p3 d; P& F modelSchedule = new ScheduleImpl (getZone (), 1);9 b/ A/ f) |" F9 j, I Q- V
modelSchedule.at$createAction (0, modelActions);
2 V6 u+ [+ [' `2 m: J# I1 `
( S* U# \& `) [* a return this;- m8 N* ?7 H% t5 q% z' c2 I7 e7 K
} |