HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# O- t! D7 r9 y$ P8 m% a, Y; n# K; A$ s' M8 N/ ~: `
public Object buildActions () {9 M' U) q4 w& s
super.buildActions();
: g/ H4 N1 k, z' @" B, `( Z" d 3 B7 |) l* n8 I# C; K) q; C) v
// Create the list of simulation actions. We put these in( K/ b3 Y+ f' |& k F. a0 u
// an action group, because we want these actions to be
# n1 u2 z p; @$ m) V) ~- k* {: m // executed in a specific order, but these steps should) K9 O! D3 x& u w
// take no (simulated) time. The M(foo) means "The message
! T% d7 P h- W& r // called <foo>". You can send a message To a particular
5 c; z" }& C- {9 ~ // object, or ForEach object in a collection.! i! h, y$ p) Y! ]5 y* D* P! [
5 [, F; M4 P2 F3 R: o
// Note we update the heatspace in two phases: first run
6 Q( m+ B& @& q( Y U // diffusion, then run "updateWorld" to actually enact the
' C8 t$ q$ L I8 t( x& Q! V // changes the heatbugs have made. The ordering here is, Y- ?: Y3 \; a5 I$ O+ `
// significant!
$ r1 t" C( t# g b7 h+ c* C( \" E" Y7 [
// Note also, that with the additional5 i5 k! @5 x6 [5 s! P) u
// `randomizeHeatbugUpdateOrder' Boolean flag we can) O4 n; |& K* l' ^ u
// randomize the order in which the bugs actually run9 A1 C U2 b, _; q( b5 v
// their step rule. This has the effect of removing any
" l" e$ O; c0 S // systematic bias in the iteration throught the heatbug* m# c2 R# Y: B( ^+ K1 Z
// list from timestep to timestep
4 `6 u2 i2 [- I8 I7 x2 P2 X( Q
+ m6 y, S8 Q0 `7 l9 G // By default, all `createActionForEach' modelActions have
* ?% x$ s8 ~% A' h8 O$ x // a default order of `Sequential', which means that the
$ l: y4 @- w- f // order of iteration through the `heatbugList' will be3 W# s9 E* h6 L0 J
// identical (assuming the list order is not changed
6 a+ u+ ?0 k9 s6 I+ C5 Z( |. Y9 z3 {$ f' u // indirectly by some other process).
2 u4 d4 ^+ G/ d* J/ O# j2 `' l, } 4 @& ^; S6 W3 ~2 b8 v
modelActions = new ActionGroupImpl (getZone ());
$ I/ b* _9 H1 x4 O* x2 ~8 w% }& w. ?- d, e, U
try {
& m3 U* I3 }. ~: E2 q modelActions.createActionTo$message' `+ A/ D' i6 f r) M; O
(heat, new Selector (heat.getClass (), "stepRule", false));
/ Z0 x5 z7 ]+ x2 {2 n& j C } catch (Exception e) {
4 @9 {6 \- V$ q9 V1 [ System.err.println ("Exception stepRule: " + e.getMessage ());$ j, j/ w6 p# P
}. C& L/ R2 o# q5 G* @/ W
& a8 p% [) Z# Q- ^! P try {4 f3 x' g. l4 A6 J0 C# R9 S
Heatbug proto = (Heatbug) heatbugList.get (0);: A1 p& E, c: v9 g
Selector sel =
- ?5 ~, _' ?- M. t new Selector (proto.getClass (), "heatbugStep", false);% ^5 @1 R+ w8 L9 x' J o# ?
actionForEach =
( N8 M$ `& s [% q modelActions.createFActionForEachHomogeneous$call3 h5 ]# S) B1 p$ h- s
(heatbugList,& B6 ]8 O/ j5 D, o6 k" ^. j' W
new FCallImpl (this, proto, sel,7 ~6 s; U+ x0 ]" D7 n" z5 N6 ?0 T
new FArgumentsImpl (this, sel)));
$ Q" N! f0 t4 Q+ [ } catch (Exception e) {0 z+ Q' I i9 g
e.printStackTrace (System.err);
# `* b5 Z- ]! j4 T5 T }3 U3 X: [& R; K0 A
, I' i) W' K! S7 h6 }/ {* L2 j syncUpdateOrder ();0 ~8 }- Y4 g% h- @9 r2 s: n: `
# _6 b) _$ U \7 a& j6 c
try {
; g. q1 F( ~* O3 `( \/ h- c modelActions.createActionTo$message
2 _% c4 @* c& Z/ ?& S (heat, new Selector (heat.getClass (), "updateLattice", false));
) E' Y; N, y) M } catch (Exception e) {
2 @, o1 t _3 N+ W% | X) C- l System.err.println("Exception updateLattice: " + e.getMessage ());' V" X' f6 M3 g G, \9 o3 k
}8 ]( \$ m* q/ u& T9 ~- W, ]) A* l* b
5 x- e( u/ C& U( r7 p // Then we create a schedule that executes the' q1 l: d: C N: B }, E
// modelActions. modelActions is an ActionGroup, by itself it
. I! m5 q$ Z. |7 V# U // has no notion of time. In order to have it executed in* R. X5 J1 O2 `+ i
// time, we create a Schedule that says to use the
, ?" }, F# a" r) J$ t$ D // modelActions ActionGroup at particular times. This
& o4 S/ w& {4 [2 ~0 v // schedule has a repeat interval of 1, it will loop every
1 G o0 \# L% Y: v" S // time step. The action is executed at time 0 relative to
( Z3 @; Q" s Q1 J, \ // the beginning of the loop.: G& }6 `( V* h l0 g
& O* Z+ A/ a) n6 e, q- p
// This is a simple schedule, with only one action that is0 w8 q8 p$ f# i, J! e. B7 G; }
// just repeated every time. See jmousetrap for more
: M/ J' P" u6 ^3 F/ x" q U" Y1 l // complicated schedules./ |& } g% M2 A5 {3 _
. S- T( |" P/ h
modelSchedule = new ScheduleImpl (getZone (), 1);
4 D2 f2 b0 j) }9 L$ c3 [ modelSchedule.at$createAction (0, modelActions);" p4 I$ a# J5 b5 D, D) P# b" X
( u- C6 y: [8 v return this;
' f5 w8 [3 _; V6 h3 t9 G } |