HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 ^; C$ J, f4 J0 A# [: S7 i, a6 `. C1 B' ]% m v. X! Z
public Object buildActions () {7 R& r5 u G+ V# V, x+ w2 e' p0 ^
super.buildActions();8 r' \# y* ?1 k, e
3 q, b- \+ A4 z* ^6 |
// Create the list of simulation actions. We put these in+ E: _" v) F% B
// an action group, because we want these actions to be) A' Y# h- _3 G# `% `
// executed in a specific order, but these steps should3 K) m1 V' n K: B: J1 ^
// take no (simulated) time. The M(foo) means "The message
8 {" I/ l1 ~! ^ y7 |4 u# S // called <foo>". You can send a message To a particular# Z$ c0 l* Y( L: x
// object, or ForEach object in a collection.
1 _8 ~' O K6 ~
! Z3 I3 N" s1 `6 o5 M# V // Note we update the heatspace in two phases: first run* H. K2 ]) [4 s8 N' L; U8 g
// diffusion, then run "updateWorld" to actually enact the
7 w2 n- S* P& U# h7 g/ C // changes the heatbugs have made. The ordering here is
* {8 _! ^+ |$ B/ U- @ // significant!9 Y; H0 u. E. x- @! p
7 u5 q) B) z0 Z$ |6 x. X6 Z
// Note also, that with the additional, X0 l) R7 u( K/ L6 u$ I/ `
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ p" F4 [- f" D
// randomize the order in which the bugs actually run, p9 T; n+ @4 F+ r" g: |+ |& y
// their step rule. This has the effect of removing any O: X: E j- s4 o4 _/ L3 d' f
// systematic bias in the iteration throught the heatbug* L+ W) q3 F! _$ Y3 ]
// list from timestep to timestep- W" g* H: N+ ]
/ W E @& @+ C, N# e3 C
// By default, all `createActionForEach' modelActions have
- f+ O" f; j6 Q3 H // a default order of `Sequential', which means that the; [$ `4 x1 ~+ R6 F' M$ A
// order of iteration through the `heatbugList' will be0 B$ K$ p$ E' N( _- @& z! H
// identical (assuming the list order is not changed$ [% L( A9 p5 m1 u1 T0 f4 I$ d
// indirectly by some other process).6 }# A0 G( ?$ V0 N+ ~$ \ x& N% X, K
8 E" X3 F" j! d8 h) Y. [
modelActions = new ActionGroupImpl (getZone ());; Y# f4 t+ p# B4 M$ ?6 F' @* b
0 d- ^ [# ?; I7 q
try {; y f9 Q: }/ m8 G% u; z" G
modelActions.createActionTo$message: R' U5 x7 b# W8 {* O& b! o- W# U
(heat, new Selector (heat.getClass (), "stepRule", false));% [* U! D" a- ]4 \* [# {1 i2 l
} catch (Exception e) {: w* m& z# _9 N& _1 P
System.err.println ("Exception stepRule: " + e.getMessage ());2 l3 V' G- G$ W6 K o
}
6 `5 d) t6 Q$ K _7 ]; [0 p" Z" F! M0 M) L& t/ b7 h8 ~
try {/ `' M: _ I+ x6 h& y( L! c
Heatbug proto = (Heatbug) heatbugList.get (0);
0 s8 ~# P" _5 Z$ @9 Y% \ Selector sel = ( p( f8 c6 m" U* }5 c
new Selector (proto.getClass (), "heatbugStep", false);
* o4 I6 X) d. b. Z- w" S) { actionForEach =
# }. y7 Q' T5 I/ Z6 b" `, n4 \4 ^! c5 ^ modelActions.createFActionForEachHomogeneous$call
: q$ ?; L" k$ c7 }% G# |- S (heatbugList,, G: M X+ S* h( S% D
new FCallImpl (this, proto, sel," p% ^- O K! e6 B" ]8 M @
new FArgumentsImpl (this, sel)));) r8 }( Z( _; j, p$ P A1 M, b
} catch (Exception e) {
% {% J( P# l0 i e.printStackTrace (System.err);+ u7 Q' }; L9 ~. Z: s7 ]( b
}# a2 S7 \/ U. B" j# w3 u
+ z2 \! A n& Z
syncUpdateOrder ();" e' B# Q. W, m( I, h; W* W
6 P0 t' J1 K S# G* V try {
0 B( u" k# S8 t1 ~8 w3 m8 n modelActions.createActionTo$message
( K, ^! O, L- [9 z$ B7 J" X5 n. | (heat, new Selector (heat.getClass (), "updateLattice", false));1 w1 o0 W1 u% a3 u7 E8 ?+ h
} catch (Exception e) {% q' C! [+ N9 @
System.err.println("Exception updateLattice: " + e.getMessage ());7 I) L2 ~: e, i) r
}) Q6 }5 b g, l& e0 B5 ?
O6 g' \( [- u) m8 J // Then we create a schedule that executes the
8 M" O' y3 V L3 ] // modelActions. modelActions is an ActionGroup, by itself it+ V7 S; y' ]$ j; b- t
// has no notion of time. In order to have it executed in& ~' y7 @9 j4 h- b
// time, we create a Schedule that says to use the
) K! Q1 ^# e5 I% Y. m% k // modelActions ActionGroup at particular times. This
" T7 S }5 }) ?! h, F // schedule has a repeat interval of 1, it will loop every
- Y% r# [) Y3 r+ [; [ // time step. The action is executed at time 0 relative to, Z" u' l; c+ a) ^% V* H; X- p0 ^
// the beginning of the loop.
" q: [* F5 R( |* _
0 |" W/ x4 Y* `/ w // This is a simple schedule, with only one action that is5 f* I# a1 U/ u4 L4 O7 C
// just repeated every time. See jmousetrap for more a& a1 C8 b* O, D5 _* s& [$ Q
// complicated schedules.( d; n. I0 l0 u8 s6 G
7 W2 k5 r Y' f2 y8 `9 z( J modelSchedule = new ScheduleImpl (getZone (), 1);5 j+ G9 r( i) |+ r1 |# [" ^
modelSchedule.at$createAction (0, modelActions);
6 n) K+ m( [6 f: }
. r' f9 P9 d! f9 `. F9 U7 }$ N, v return this;
# _! w# u$ {3 Y8 u2 \/ ~ } |