HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 q* [# T# \1 N
; K1 Z* p5 X& g5 |2 z: |5 ~ public Object buildActions () {$ C5 T: E' i3 i. ?
super.buildActions();# J/ p: X* w8 v# @5 X& ~
$ Q" N6 j( @ k4 p8 o9 Z. G
// Create the list of simulation actions. We put these in
, n7 v% @, g4 c; c# w // an action group, because we want these actions to be
7 q: u6 @8 A- f1 ^& Z1 Q4 Q // executed in a specific order, but these steps should
9 Y& g" E, v: m5 r+ X // take no (simulated) time. The M(foo) means "The message
. {7 C3 v/ [4 @' L, T // called <foo>". You can send a message To a particular
" s( b9 _& n# o& A2 k. a4 D // object, or ForEach object in a collection.; q8 ^! u) j! K4 q9 e) p
1 B2 B+ w& l7 P' O2 |" r9 d5 M, I // Note we update the heatspace in two phases: first run$ l" V' D) e2 S5 H# a5 ]
// diffusion, then run "updateWorld" to actually enact the# b2 D/ f( z* h
// changes the heatbugs have made. The ordering here is
9 N2 U+ h1 u v // significant!
. @$ M5 @6 n, u6 V. O
) D2 b. ?7 h S // Note also, that with the additional9 J$ X, h, Y* R
// `randomizeHeatbugUpdateOrder' Boolean flag we can! M @$ g0 j- B/ s0 P
// randomize the order in which the bugs actually run/ ^+ x+ [( }' E
// their step rule. This has the effect of removing any1 p8 w; m% P( G9 [5 k
// systematic bias in the iteration throught the heatbug
# G+ O# Q7 `+ N, T5 x ~ // list from timestep to timestep
1 v3 I; f' J2 P9 V* O0 N% P4 h( H
- C; ^& J# h6 E- e // By default, all `createActionForEach' modelActions have
' z: E _/ ~+ y( ?9 ?7 ` // a default order of `Sequential', which means that the
5 W- f7 O3 @0 x ~3 q1 M- e5 q# v // order of iteration through the `heatbugList' will be
( Q; [9 o* Y$ Y E% H9 @: D1 } // identical (assuming the list order is not changed
/ c* ?: q5 L9 M // indirectly by some other process).
6 G( \' P2 c& h7 p- g
6 T6 h+ \+ S" a' p9 k. R: L modelActions = new ActionGroupImpl (getZone ());6 z6 e: ]/ v, L- d6 Y& w2 n1 b
: \0 h0 o$ r( I, C! h2 J4 J2 n try {6 s. W6 ] h7 j
modelActions.createActionTo$message
( k4 Y/ T. g: T7 V b0 Y (heat, new Selector (heat.getClass (), "stepRule", false));+ Z2 d* t8 M/ _4 F- O) H
} catch (Exception e) {
- @7 x( Z W" {2 I System.err.println ("Exception stepRule: " + e.getMessage ());& h- k! a& t+ F4 e# P0 P
}
" H* g" |, e4 e7 J9 B& s* `
1 C' g) i: \ z0 D4 q4 Y try {
1 v; v: @* Y7 Y- n) ^ Heatbug proto = (Heatbug) heatbugList.get (0);; K' Y: J) D2 x7 A1 y
Selector sel = r+ S: N. p: g- m+ t
new Selector (proto.getClass (), "heatbugStep", false);
, C" E F) \( j7 x actionForEach =% M5 j! U- g7 f. o7 {! l' N$ A
modelActions.createFActionForEachHomogeneous$call& d: [7 q7 I+ T9 |8 p6 ]
(heatbugList,6 X7 l+ O% c2 K+ G; @1 h
new FCallImpl (this, proto, sel,* B" ^8 a/ H' @: y0 z* ]2 k
new FArgumentsImpl (this, sel)));8 u% z0 t1 |+ n4 v
} catch (Exception e) {
9 U3 G, u( R- y9 a8 o: Z3 ~, B0 D e.printStackTrace (System.err);& u. ~7 S, K1 R! t; H
}
( X1 C+ I+ u( W, L, g6 O 5 n3 i* t8 e: I
syncUpdateOrder ();3 D0 {; |+ G0 l% B; ?
8 d4 l' V8 q$ ?$ B9 R, ~$ i+ X: m try {. M5 J6 \ u; G" h8 y6 Y# o7 ?
modelActions.createActionTo$message , c& e2 l' l2 |) X2 k6 L
(heat, new Selector (heat.getClass (), "updateLattice", false));
' D% B6 p5 u0 t- v+ b3 F } catch (Exception e) {
e3 v; m* X N0 ]- i System.err.println("Exception updateLattice: " + e.getMessage ());4 l1 P+ ?/ u& v3 V- ~( L
}& x' {- g3 N8 u/ ~8 r% m' T. v
( n1 X1 v0 M, @
// Then we create a schedule that executes the
% @5 C$ M# d6 ~ // modelActions. modelActions is an ActionGroup, by itself it
' q! N. c( P$ U+ U2 A8 u // has no notion of time. In order to have it executed in
/ a3 O2 u, _+ Q' x4 l, E6 k7 k // time, we create a Schedule that says to use the6 N. Y! R: D% |' J) q
// modelActions ActionGroup at particular times. This
# Q8 a* i% \3 S9 ]$ E0 A$ \ // schedule has a repeat interval of 1, it will loop every
7 U: N8 h! R7 t0 F" k. ~ // time step. The action is executed at time 0 relative to
, F6 R; C$ E( u9 y* W1 n // the beginning of the loop.3 }, R; z0 s5 v6 [- G
7 O" x9 Q0 c. O% M) H
// This is a simple schedule, with only one action that is
+ a) p; M# e3 v8 X$ L) Y3 l% x // just repeated every time. See jmousetrap for more
& C+ [% @: S; r // complicated schedules.
: X+ X4 _" P$ e$ R0 L 8 J! G+ G: y4 L* t# {* W
modelSchedule = new ScheduleImpl (getZone (), 1);, R$ `. H# [8 s8 V7 P2 J- O
modelSchedule.at$createAction (0, modelActions);+ w+ p1 f' A2 i3 z5 e [
! I2 U( ]! [: D+ g( s* _
return this;7 H+ L1 O1 u( w7 N' Z
} |