HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) b; M. A2 V. n g8 k- ]2 J
% k9 N: a7 m h- s# [' c' i/ R
public Object buildActions () {
8 x5 _8 Z0 P/ h. A- R1 R super.buildActions();6 ~" g$ ~0 o T X! ]( U% R$ B3 W
# w7 c( P4 N. Q, K$ E // Create the list of simulation actions. We put these in" n( C* s. a# P4 b3 q- {* b# F
// an action group, because we want these actions to be5 ^% }- W) t1 c' R: k
// executed in a specific order, but these steps should
/ O; v! o5 C% d0 ~$ o* a // take no (simulated) time. The M(foo) means "The message
5 `$ |& n# B* f // called <foo>". You can send a message To a particular
, z& g3 M# a5 j, q" x! M+ t // object, or ForEach object in a collection.
8 f9 T8 m4 `! V$ ^ 6 S8 g1 F- I, T( T3 |# c5 A
// Note we update the heatspace in two phases: first run& ?* n7 u; u! v: E: N+ d
// diffusion, then run "updateWorld" to actually enact the# t+ W) L. L) v: ^5 q" G1 R
// changes the heatbugs have made. The ordering here is
; M/ J: g! Y+ A4 P7 D: j // significant!
# }8 w) G6 ?$ G5 i: l
+ S, e9 ^& q) i: T- x, N // Note also, that with the additional
1 @. c$ d! k8 c5 O1 L8 }$ g // `randomizeHeatbugUpdateOrder' Boolean flag we can
, j- E( ]" s8 i3 E7 I7 t/ o" S+ H- ~ // randomize the order in which the bugs actually run, |, z# i9 L+ L' ]% J6 ~
// their step rule. This has the effect of removing any
' W, o- [0 z, z y9 Z // systematic bias in the iteration throught the heatbug
3 y3 q8 k( @2 s# |, P4 P# z' M) k // list from timestep to timestep
: H8 ~% M; C$ C! a% w
; b. U2 F2 f8 H. p- q4 c# ~! N2 _2 b // By default, all `createActionForEach' modelActions have
( H: y2 q) C: M0 y6 o: e- e // a default order of `Sequential', which means that the
! B3 r+ E7 A y // order of iteration through the `heatbugList' will be
- m2 S: @% y/ C3 F* @- z // identical (assuming the list order is not changed' @$ q3 a k9 q# q! Y/ N7 J
// indirectly by some other process).
/ F" w0 p. C( N+ x/ j1 g 5 G$ p' {. C, R3 ]/ \$ m. P
modelActions = new ActionGroupImpl (getZone ());
# r H$ d; |1 i& u* _1 F4 l
0 |9 h) A6 V$ B, G% h) L4 [: d try {' U% U1 x1 g0 B) q+ U
modelActions.createActionTo$message9 S; Z$ @% `# v1 N6 f
(heat, new Selector (heat.getClass (), "stepRule", false));
$ `; i4 s6 j+ g9 Y2 m } catch (Exception e) {4 R% H' `# Q) ^/ e: j A
System.err.println ("Exception stepRule: " + e.getMessage ());
, N* M0 l: H! n+ t( H }" F. V$ b: o3 E2 n' g
# _( e. z2 j9 j2 j- M/ V try {. a' Q6 b _* j- Q, J# a6 k1 u
Heatbug proto = (Heatbug) heatbugList.get (0);( n6 ^, y( r8 J; y' J, Z& t' Y
Selector sel = ' D5 z8 } Z6 d- |+ o# Q6 g% d
new Selector (proto.getClass (), "heatbugStep", false);/ w* `* a% K9 J% X* E4 n3 z. ]
actionForEach =. L2 u6 J) A) G* g( F
modelActions.createFActionForEachHomogeneous$call
d% U8 O; A, f& E (heatbugList,
2 Q8 C: E6 ~6 J$ u; t new FCallImpl (this, proto, sel,- y3 Y& |( A& A2 h- y
new FArgumentsImpl (this, sel)));8 m+ N4 C! k; Y- r: J( g* Y
} catch (Exception e) {. Y" \; V- C! y. N. w2 V, C! ]' ]
e.printStackTrace (System.err);: s: }. e. y1 f+ C w. d
}
3 k2 @4 q2 W, y
1 D4 k' l6 s% h4 t/ D syncUpdateOrder ();
5 e1 |1 L1 D& |0 [4 e0 ?5 e6 U7 D/ q/ |. V' J
try {
3 m8 l' P7 D- ^" v: l# f modelActions.createActionTo$message 6 Y' }: u+ g( m, W( y) B
(heat, new Selector (heat.getClass (), "updateLattice", false));- C/ p D: c( p. O1 K( b
} catch (Exception e) {3 l3 t8 J8 m( _: b) T% R
System.err.println("Exception updateLattice: " + e.getMessage ());
7 D! B5 p/ [3 f }
- j: B( m5 Y' p 0 R, S& Q+ [) Q* {% y/ H
// Then we create a schedule that executes the6 ~* w$ i$ D- A
// modelActions. modelActions is an ActionGroup, by itself it t+ a( a4 O2 K: o/ G b5 k. E, r4 b
// has no notion of time. In order to have it executed in2 }: R0 p1 p$ P0 U" a, E$ v
// time, we create a Schedule that says to use the; o) c @2 H3 t6 y4 l) i3 j
// modelActions ActionGroup at particular times. This: d# i0 G- l! Y
// schedule has a repeat interval of 1, it will loop every; }" H; U2 x! _( _4 q0 k
// time step. The action is executed at time 0 relative to }3 e% d; c0 j' g* m: x; P
// the beginning of the loop.
3 s1 s0 ^( c6 ?' u0 a, ^1 r
' l5 O: b* ?7 H% m \$ V // This is a simple schedule, with only one action that is1 s3 S; Z/ b9 }8 O6 H
// just repeated every time. See jmousetrap for more2 g( K& ?0 e$ D, w3 |. m: V
// complicated schedules.
; R% p# R+ j/ J ! z, o) j0 a7 L$ W; z4 ~# e8 ^
modelSchedule = new ScheduleImpl (getZone (), 1);- o }" h5 M3 q: s( @! o# y+ v
modelSchedule.at$createAction (0, modelActions);+ o+ Z% }- I7 \1 f' b$ n* \
) @+ d4 t6 N4 P return this;; E6 g( T$ q. t/ }, l9 h5 E
} |