HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 @# \5 M2 ~2 m: c
9 h4 s3 F4 z4 p. C2 o, S ]! N# `/ B public Object buildActions () {
# Z& R6 ], v F7 l4 Y, e* g/ i8 P super.buildActions();
0 J$ E2 B- c( W% V- e/ Q
# {9 S( n& v( I // Create the list of simulation actions. We put these in
3 T7 n' N& b+ K& d6 Q6 A // an action group, because we want these actions to be: D" T- b* E# n( h& b' q c
// executed in a specific order, but these steps should/ {3 d2 z ]8 u' \7 T2 r4 @
// take no (simulated) time. The M(foo) means "The message/ c( N* h) {& W1 R& G
// called <foo>". You can send a message To a particular
0 K% E/ |, k |- N6 A* x // object, or ForEach object in a collection.3 h% _1 l7 c* G6 j& ^
0 y- r G2 e4 o2 x. P D+ @ // Note we update the heatspace in two phases: first run# G* Q- e0 q S; b o/ E& D$ B
// diffusion, then run "updateWorld" to actually enact the, h9 k- _' e! h* r7 _: |) N- Z
// changes the heatbugs have made. The ordering here is. T: d. A$ e6 Z9 {& Z1 {
// significant!' G$ X" r% g+ I6 L/ t' {
. \ r! h; M8 D* W
// Note also, that with the additional7 l; x; i- M( P; p j7 E
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ p$ b' Y& H& m+ D; P' w ~
// randomize the order in which the bugs actually run' @4 @" o* C. Q: m' T6 E3 S) B
// their step rule. This has the effect of removing any8 i3 n/ [! s. M0 N2 f& ?& B v
// systematic bias in the iteration throught the heatbug
9 M8 L% J; |! o' j9 s8 {2 j4 x! N // list from timestep to timestep
' R* y, p. F: i! y9 u
1 b. l! b+ ~! |. Q // By default, all `createActionForEach' modelActions have, L3 D( Z7 W' T: A
// a default order of `Sequential', which means that the
0 u9 D$ ~. ?; {/ [% G // order of iteration through the `heatbugList' will be& z& U- D7 W8 e4 y9 H
// identical (assuming the list order is not changed% ~* e; q6 B- x6 s( `
// indirectly by some other process).' {% {; J, Y$ E# B
: i, c% @- q, e- Z+ ?. \ modelActions = new ActionGroupImpl (getZone ());
5 M6 M8 ?% y3 f! @9 Q# B
7 _' }0 y5 x% | ~9 q6 w try {+ V( q9 @* \6 ] H& |, E0 C
modelActions.createActionTo$message
9 ^/ N7 }, d# s' f1 ?1 _ (heat, new Selector (heat.getClass (), "stepRule", false));, @9 W8 X8 v# M8 V
} catch (Exception e) {
5 D3 Z2 k& k7 |; | System.err.println ("Exception stepRule: " + e.getMessage ());1 Q" y4 c4 ^( r# p) K+ F
}1 U: u6 ~9 M% `4 Q& W
3 Z0 g& b9 D0 u" q3 }
try {
, X& N- V- `* M; m Heatbug proto = (Heatbug) heatbugList.get (0);7 y5 y( ?( U- g' D. T6 i
Selector sel = - F5 C8 ?. D; c6 t: O1 T$ _
new Selector (proto.getClass (), "heatbugStep", false);
( X' C2 c t- Y/ V' ^ actionForEach =
2 J! j, H' I% ]1 b& c; ` modelActions.createFActionForEachHomogeneous$call
+ o, p$ ]) P$ H1 w2 B (heatbugList,
6 i, C! x6 i) R2 U8 g new FCallImpl (this, proto, sel,9 O5 Q; b8 P4 z7 p0 E" f3 \0 U
new FArgumentsImpl (this, sel)));5 Q1 d) k* s) P8 `" j- P, `/ s z' Q
} catch (Exception e) {7 D# D; N @7 q* ?5 t8 R
e.printStackTrace (System.err); b8 P2 k5 A2 y
}
& e- e+ p1 q- E
! r) i( l% Y. z3 b, r syncUpdateOrder (); }. T' v# c) w7 O& \
/ u1 }5 o7 v9 Q' o try {( }' z1 H+ ?) m/ f& T. M6 r
modelActions.createActionTo$message
; S2 M6 w: |* U2 n (heat, new Selector (heat.getClass (), "updateLattice", false));
9 Z- F( O$ y6 h! O1 }5 P% w/ @ } catch (Exception e) {
& p% E6 @' i' Z6 h u1 O6 n- g3 K' i System.err.println("Exception updateLattice: " + e.getMessage ());$ T. ]5 X2 R1 P3 k( J
}
T, F: g# s) Y " j( k9 U& @# G7 K$ A
// Then we create a schedule that executes the
* {/ w* ~4 R+ F9 N // modelActions. modelActions is an ActionGroup, by itself it- o: p! P+ `6 c
// has no notion of time. In order to have it executed in f, ^) o1 b% m j% m; r* l+ _
// time, we create a Schedule that says to use the8 E1 ~- U$ z3 [& _; _
// modelActions ActionGroup at particular times. This2 t1 u( f% c7 h4 g4 [6 U8 n
// schedule has a repeat interval of 1, it will loop every
# q$ c+ U3 x* t6 |2 u1 N // time step. The action is executed at time 0 relative to
- a1 e/ s/ s$ n5 ^) C- C% Y, ] // the beginning of the loop.
8 `$ s: V) v/ ]; [+ ~* l& Q, M7 H$ H: i" E
// This is a simple schedule, with only one action that is
. n; y1 c" [# P // just repeated every time. See jmousetrap for more# a0 m5 g( M! `" q: Z
// complicated schedules./ x" I7 z( y2 K& K* Z
) b, Y0 x; Y- \) R: R modelSchedule = new ScheduleImpl (getZone (), 1);
) ~$ l- V7 ]% w& W. M; U& m modelSchedule.at$createAction (0, modelActions);# O& u' h* Q. y) {
& G2 D% C5 f# O4 L, U return this;* s7 Q! A: N: W8 {; ]
} |