HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- @7 [, u* _% y- C, P" y: K$ n' g3 f* B- R4 h
public Object buildActions () {9 L! _: c! b/ o2 ]; U9 F: R7 I
super.buildActions();9 {2 o- `' o% Q6 {6 a
. @# L" N9 d; l! Z# d // Create the list of simulation actions. We put these in& j2 q6 @- l0 f, B# O9 u& R# s
// an action group, because we want these actions to be9 U" @1 e' g" m/ y, \' H
// executed in a specific order, but these steps should4 }- L! {; v- ?5 e s
// take no (simulated) time. The M(foo) means "The message# c y* S+ ^9 b- R: e
// called <foo>". You can send a message To a particular
?* U8 |9 K. [1 |0 s4 G // object, or ForEach object in a collection.6 Q! T; ?9 `" u. H% s4 V
v! w4 ^& W% w- d2 W# U
// Note we update the heatspace in two phases: first run
' ^$ I" z' _2 U: T7 X3 ?% \ // diffusion, then run "updateWorld" to actually enact the F7 T$ P# J& }$ g2 K
// changes the heatbugs have made. The ordering here is3 V# e4 C+ n& O/ d d# S- t
// significant!
' v( P/ v' n! J( _/ u 4 K$ G; I4 [, `
// Note also, that with the additional O: `5 v# Z: y1 F( Z$ V; l
// `randomizeHeatbugUpdateOrder' Boolean flag we can- j- m# s: ?) W$ D4 \* R
// randomize the order in which the bugs actually run5 k) ` {9 H: p9 J0 r
// their step rule. This has the effect of removing any6 g/ m$ e9 w. N. _2 z1 B
// systematic bias in the iteration throught the heatbug6 c1 s/ s8 V- b: d; @+ _# P
// list from timestep to timestep# ^2 k5 r7 b! Q4 l8 Q( x' F" ]( \
# c# q8 _! F8 [! R // By default, all `createActionForEach' modelActions have
- O' H. m# g% G // a default order of `Sequential', which means that the% q; d- G0 r& ]& j6 g/ T1 {
// order of iteration through the `heatbugList' will be( f4 |. g3 N* U% d2 ] e' |
// identical (assuming the list order is not changed
, L0 u% M1 i) I- ]4 \% c // indirectly by some other process).
- o! f1 X( q$ u1 t) H+ u
$ W1 y1 W' Q4 v) W$ M modelActions = new ActionGroupImpl (getZone ()); o' a/ P' _9 R5 _4 b
q) Y/ [ }9 K& ?: t
try {
2 b- \- b8 u4 B$ ?, P/ e! G9 n8 V modelActions.createActionTo$message
* m/ Q+ m& a V: @& x K9 J9 O (heat, new Selector (heat.getClass (), "stepRule", false));0 B a; [3 E. _+ A* Q7 s7 u* S
} catch (Exception e) {$ P9 q) o$ r' c# O
System.err.println ("Exception stepRule: " + e.getMessage ());5 y$ r4 f8 D" w, k0 e0 S& t0 f4 w
}" [# K- k7 f9 F9 W4 {5 ]4 P
7 ^3 f+ E! _$ r1 {6 c0 m$ L5 U% F
try {
4 g' Y( W" Y# [2 o. l Heatbug proto = (Heatbug) heatbugList.get (0);% _+ O% ?- ]& R3 |# m
Selector sel =
/ m; ?6 m \- ` new Selector (proto.getClass (), "heatbugStep", false);
, A3 X d$ h/ A; F! a actionForEach =; e1 D2 N0 c3 L/ @5 v
modelActions.createFActionForEachHomogeneous$call
# @7 s% l3 t9 }5 J$ w8 k* Z (heatbugList,
! i/ N* H8 G* {" y* Z" m; X7 B new FCallImpl (this, proto, sel,
) U( J: |5 J* p( e new FArgumentsImpl (this, sel)));0 p; j) v- E! P- F8 J
} catch (Exception e) {
* o& H3 y) m* W6 D e.printStackTrace (System.err);
6 H- w# g7 L0 x4 Q* b5 z1 s* \: j }
4 r2 W8 a/ K9 b( H. I0 Z & }' r, d$ E: E
syncUpdateOrder ();) l, u0 V: h) t; v$ [0 {/ M
9 x8 z6 Z& a6 _' G- J% Z try {
" H9 c& m8 W: p Z. f modelActions.createActionTo$message
1 G# C" l- m3 w V; ^ (heat, new Selector (heat.getClass (), "updateLattice", false));& A* h1 J8 x0 o- g5 l' R+ k0 D
} catch (Exception e) {( o# ?; d- s" h% d* M
System.err.println("Exception updateLattice: " + e.getMessage ());" q2 f" `, |! e+ m* X
}% O) i( z* h# {( Z; U
3 J- E7 F7 C/ \ P3 P // Then we create a schedule that executes the
3 O! G4 H, t3 m, |' {3 {* w3 P // modelActions. modelActions is an ActionGroup, by itself it- v& }; z2 K( R ?( I1 y) c
// has no notion of time. In order to have it executed in# Q+ x$ V* Q# F0 A- T
// time, we create a Schedule that says to use the) j! S6 d ^: ?3 |$ j& z
// modelActions ActionGroup at particular times. This+ P* x" b$ c7 r3 D. _1 K
// schedule has a repeat interval of 1, it will loop every
7 Q) S; w8 ~/ r, } // time step. The action is executed at time 0 relative to
1 I8 W- o. k( Z, ?' K3 e1 Z$ v // the beginning of the loop." \0 V' Y, Z" k' ^% I& `9 Z+ y
0 c: l) H0 f9 f+ v ~% g3 e
// This is a simple schedule, with only one action that is. M' I% W, A+ I
// just repeated every time. See jmousetrap for more
# Z7 t* S3 k+ b$ j5 S0 D- M // complicated schedules.3 v8 ^5 R( d) N
" p% T0 a% B- `$ ^& a* i
modelSchedule = new ScheduleImpl (getZone (), 1);
8 r# ^9 T2 L' x* t1 G modelSchedule.at$createAction (0, modelActions);
1 `) |9 y4 h2 _, I9 ` ) v/ H, k7 n" Z* w
return this;
) }' U8 D# W7 x9 y: Y } |