HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 }( _) x1 x) M3 i
- e, O* T9 y: f3 @
public Object buildActions () {
* x. i2 G9 z, s( c super.buildActions();6 s T, F {$ p- f4 F) I' s
3 b5 h _ o: k4 N# ^; C4 Q // Create the list of simulation actions. We put these in* `' E0 X/ I4 _
// an action group, because we want these actions to be7 C* {1 J; ^4 N5 V
// executed in a specific order, but these steps should
; `% |0 K3 K- R+ X // take no (simulated) time. The M(foo) means "The message% q4 j: [$ a1 {, ^
// called <foo>". You can send a message To a particular
2 p4 a; q# r( g; B; j1 N8 J) X // object, or ForEach object in a collection.
/ E8 n8 k/ u* M2 i* o; o$ }, O 0 T, r9 B& X* L& o( i
// Note we update the heatspace in two phases: first run
7 c: X8 f$ I$ B+ f2 y // diffusion, then run "updateWorld" to actually enact the
; U- X* g9 Q! R // changes the heatbugs have made. The ordering here is2 ?, \ \7 _, @" G# U7 Q4 C8 h
// significant!
' G v! y3 u% F: g$ C/ J " O0 \$ ~* Q& ~9 d
// Note also, that with the additional
* D" a8 j1 r" z3 E6 H" ] // `randomizeHeatbugUpdateOrder' Boolean flag we can+ V) P3 u) _' F0 E7 M @2 f r
// randomize the order in which the bugs actually run$ G" V- P" o; L# H
// their step rule. This has the effect of removing any
/ P# Y6 H- k7 Z8 e4 ~! ~. e // systematic bias in the iteration throught the heatbug* Z# t4 ~3 y7 v3 o( q; t
// list from timestep to timestep
) i8 E% P" E! @! I2 T/ c ' e7 P }4 \' U6 X; ` @ J: [
// By default, all `createActionForEach' modelActions have8 ?- i3 \* x! \
// a default order of `Sequential', which means that the
7 _3 T+ b0 Z! k, \7 { // order of iteration through the `heatbugList' will be
, D, G, D' Y+ P; z6 b4 n // identical (assuming the list order is not changed* l& [) l3 D2 i/ Y2 y; l4 E
// indirectly by some other process)., G/ s& {" W- N+ z$ @$ `
* {' {! ~0 q$ q6 l( `" R modelActions = new ActionGroupImpl (getZone ());
$ s3 ^$ S$ I, Y6 P0 O m8 g6 O- [8 T% k: \7 z( F
try {' q( @4 O2 w, w, R1 d
modelActions.createActionTo$message' _8 {9 G+ J; F: A, }
(heat, new Selector (heat.getClass (), "stepRule", false));
4 Z" b( J- {/ @# w } catch (Exception e) {
: B5 `! U8 r6 f. w; c1 _% ~ I System.err.println ("Exception stepRule: " + e.getMessage ());
, e$ g8 G. E V! j }
& C* Q8 q8 ~; q: W# [1 L( k# b) ^/ h: k# @6 I; Y* q0 y
try {
3 E$ Q3 ?9 p3 G Heatbug proto = (Heatbug) heatbugList.get (0);; r" ?2 p k0 O3 n3 {1 j
Selector sel = ( E ^6 h k( W0 P: w4 m
new Selector (proto.getClass (), "heatbugStep", false);( T4 S9 j6 l' k! P0 n. K6 X
actionForEach =4 Q0 r' v( l- V, a# a8 g
modelActions.createFActionForEachHomogeneous$call
- B/ f8 U/ d& b (heatbugList,; w6 I7 I5 n/ `3 }) S; E) K
new FCallImpl (this, proto, sel,: V) [% A* ~+ x- f5 ^5 U- p( @
new FArgumentsImpl (this, sel)));1 M) x! u" z! n) Y
} catch (Exception e) {( T1 [% B5 |$ l+ ?# U
e.printStackTrace (System.err);
4 u1 e0 t/ \$ a3 k: v% n }
# h# s6 A k/ i! K0 V. Z 8 b% _+ w$ @1 e
syncUpdateOrder ();
, }' u4 W- E& n U( M+ {
' b; A' Q% T6 c! X L$ I2 L try {& X5 z' N+ C+ m
modelActions.createActionTo$message
: Z7 _, V5 j( O: y6 G8 H (heat, new Selector (heat.getClass (), "updateLattice", false));
( Z! }$ K$ {0 o# Z6 n% h4 i } catch (Exception e) {
; y; O/ Y" h7 X& P9 _ System.err.println("Exception updateLattice: " + e.getMessage ());% u9 s, f1 j9 M. S+ z8 y
}
# P! ~+ D7 R2 T/ H/ N' Y , N) d: F: C- P4 ~% x7 J
// Then we create a schedule that executes the( p+ y; O4 F% t" ?* a9 X I6 q
// modelActions. modelActions is an ActionGroup, by itself it
9 ?5 V z" [2 M7 Z; u1 z) | I // has no notion of time. In order to have it executed in, M1 y/ I! B. [% s
// time, we create a Schedule that says to use the
& I) H6 F. l; I6 z4 L; d // modelActions ActionGroup at particular times. This5 } O& F+ J' h. u9 k
// schedule has a repeat interval of 1, it will loop every* }) w, [) T1 D |
// time step. The action is executed at time 0 relative to
2 W! u$ X9 I" t5 _: Z1 {4 D) Y // the beginning of the loop., h) a9 w- y5 D3 h
3 n/ y2 d9 ~9 g // This is a simple schedule, with only one action that is0 C# Z& l: G8 W0 S' v) ]8 [
// just repeated every time. See jmousetrap for more
! N" V+ T5 _& n k' r // complicated schedules.% N6 K! E4 _5 K0 ^2 R
3 j9 T" w- z9 G+ b7 C/ `
modelSchedule = new ScheduleImpl (getZone (), 1);
; i1 t4 u% g8 G, K8 E, H7 ` modelSchedule.at$createAction (0, modelActions);
' e% w) X4 l! _* S
! f# ?2 @# v& p6 N) k8 J( R. v return this;
; O' s7 t$ l7 p/ F' v0 k } |