HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ j( X. h5 |+ Z1 N
0 t$ m% i' A& F0 l' _
public Object buildActions () {# O' r/ j8 `: A( b2 @( t) m" `) k
super.buildActions();
5 ~+ G% S3 w) t: @; { ' E! D2 R$ q3 l
// Create the list of simulation actions. We put these in6 A; x! G; U: k# ^% J) H W' @
// an action group, because we want these actions to be$ {* O( w+ S5 Z' \9 @' j+ w2 u U
// executed in a specific order, but these steps should
2 ?: V4 g' c3 C# e // take no (simulated) time. The M(foo) means "The message& Y; P6 w% E; @2 I: {* H. A) v5 ^
// called <foo>". You can send a message To a particular
; S) Q, z3 [8 V5 @% u$ q // object, or ForEach object in a collection.& ]# ^) ^0 ^, |" Y
9 p# m& w+ J! D6 j4 _! r
// Note we update the heatspace in two phases: first run
" _6 B3 X+ X6 F' x" d // diffusion, then run "updateWorld" to actually enact the: M( _, y; U% r- n
// changes the heatbugs have made. The ordering here is1 p/ D& z# z( Y( b
// significant!& D9 O6 C% W, h3 i6 d: V, y
2 B- Z* V7 {0 ?4 d: B$ A
// Note also, that with the additional
: v% |+ X# @" d6 t7 N' w9 P // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 r' L/ G7 I7 g ~. {% |( V // randomize the order in which the bugs actually run
/ R0 b9 F9 N: |$ W3 A // their step rule. This has the effect of removing any
3 x0 h' b# o! F( b# W; b // systematic bias in the iteration throught the heatbug& j& h- z+ w- K0 h9 w9 C+ p+ x: V
// list from timestep to timestep8 c" L1 J' P; \: \# b- a- M, K
, f+ d) O- Q- G1 B8 Q; l8 X2 D
// By default, all `createActionForEach' modelActions have
" Q+ j, @1 ]' l+ E // a default order of `Sequential', which means that the
' E* `$ a, n. g Q1 ~% d // order of iteration through the `heatbugList' will be2 ` ~( @* l# z$ U" v
// identical (assuming the list order is not changed, t" |; T1 N" T9 K5 v
// indirectly by some other process).; j: q' |% R- n( |2 p
4 G0 I {/ b5 Y0 J7 V/ @( U5 `% [ modelActions = new ActionGroupImpl (getZone ());
5 T6 [5 h2 P# C2 |7 ^
9 D7 V! k) L0 Q& p+ n0 c& r; y try {
: C# R4 [* Z+ a0 _( F" x* E modelActions.createActionTo$message
2 i$ p" _0 `* n& A! r (heat, new Selector (heat.getClass (), "stepRule", false));
- j2 `" m" H/ A$ T } catch (Exception e) {
! Z& @5 h- q/ H9 @ System.err.println ("Exception stepRule: " + e.getMessage ());" s- x$ ~! j1 F
}. Q3 H9 M; v1 p& ^& M8 q2 r. v/ i
/ F& G9 Z9 [8 k2 D0 i( f& X- ?9 M: p
try {
( j; q2 `" j4 G& ` Heatbug proto = (Heatbug) heatbugList.get (0);9 E; o w* c0 ~8 J- f" T
Selector sel =
, U# i# m/ Y/ Z& O- F new Selector (proto.getClass (), "heatbugStep", false);8 j- b* U9 ], q9 ]
actionForEach =7 X6 G$ T$ D3 s! W! d$ Q6 T
modelActions.createFActionForEachHomogeneous$call+ N1 d! n1 q. Y5 \8 I% d
(heatbugList,
" _& A( [9 l, i& y( z n s( N new FCallImpl (this, proto, sel,
* n: e, K/ S; ~5 H# \* ?( V new FArgumentsImpl (this, sel)));
" D5 n" ~' |5 m! ]# C; v1 Q( l' U+ I } catch (Exception e) {4 D! A4 ?4 @$ n! @+ [8 k
e.printStackTrace (System.err);
4 u/ b* X$ Q5 ^) D' j- c( ` }/ y- w. m0 V3 ^# T; Y- Q
! z; n# H, Q* h- d: V) ^7 Z) |( A: V6 C
syncUpdateOrder ();6 Q. d d# g k
( ^* c# r2 @% Q: P. z
try { C/ k) g" P, n" E
modelActions.createActionTo$message
' T; i" I8 }% h; ]5 u2 _' y2 y (heat, new Selector (heat.getClass (), "updateLattice", false));# B3 n6 D& h2 f$ Y6 Z7 \6 Q, P
} catch (Exception e) {
) i( g6 w3 M: V# j! r System.err.println("Exception updateLattice: " + e.getMessage ());1 y, U1 {3 ^) q# V" U6 ?
}0 r6 X$ A# c- }, j
9 f6 }/ j9 O! k% f // Then we create a schedule that executes the
5 q! j- N: ]( G6 K0 Q // modelActions. modelActions is an ActionGroup, by itself it& g1 C8 }6 [, ~3 N2 T3 i/ ^
// has no notion of time. In order to have it executed in
0 l# j E- ~8 \& D // time, we create a Schedule that says to use the) e& x" T9 J4 w
// modelActions ActionGroup at particular times. This: {2 T5 M' N% V+ W" {7 E- b
// schedule has a repeat interval of 1, it will loop every( u; C! @# X' I3 }3 Q9 A& X
// time step. The action is executed at time 0 relative to3 g+ u0 O; ]% u" l. u4 u0 [4 P
// the beginning of the loop.
' T6 a) h5 q$ C
4 g) V3 ?$ B# D, P9 E" L // This is a simple schedule, with only one action that is# d, u% d3 s- T, p. _
// just repeated every time. See jmousetrap for more; G0 Q9 X$ m# I
// complicated schedules.# ^ x4 F( l- U* y/ g, l
' N6 k" C2 B+ z8 m7 c modelSchedule = new ScheduleImpl (getZone (), 1);
0 k. J1 {# I* d( u6 F modelSchedule.at$createAction (0, modelActions);2 l* a6 Q( [9 _; `1 v% n
! ~1 R+ d& L4 F" z! T, A$ h7 F return this;
P1 [+ ^/ i* o' u- C } |