HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ |' z' [' u, ? ]" K" r8 C/ J8 e2 C6 l; b x; {* M _: i; ]& t
public Object buildActions () {1 I/ E: K! _4 E( T
super.buildActions();1 B3 Z5 ^* d1 i8 E& X
+ l8 W8 ]$ s; \( Y2 u' h
// Create the list of simulation actions. We put these in! n' d; \- X, Z4 l
// an action group, because we want these actions to be
- k; F# }+ S8 P' _: o U; D1 X // executed in a specific order, but these steps should
" E' e4 i8 O5 U+ O# M // take no (simulated) time. The M(foo) means "The message; e- k) T0 V3 I( V
// called <foo>". You can send a message To a particular3 v1 o' t* V1 \0 A3 T5 E- e8 H
// object, or ForEach object in a collection.
4 M0 i/ m7 k. ?5 C9 Y
, {8 a* e$ E8 V$ H& o- g // Note we update the heatspace in two phases: first run# q4 A" r; Q0 E4 f
// diffusion, then run "updateWorld" to actually enact the
, k1 G, C# J9 r) |" ~: ?8 C // changes the heatbugs have made. The ordering here is
# K- q# ?1 w* L* } // significant!+ ?7 O8 v, `0 l/ C: Z
/ d/ P! D( F1 S7 U1 r+ D // Note also, that with the additional& i( I2 e. L8 j# V u0 W ~% _
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; u" Z4 J. |" m q1 C // randomize the order in which the bugs actually run( n" m9 g* @! Z' Q; t; y4 [ J
// their step rule. This has the effect of removing any
& ~) J2 I: G& ~ // systematic bias in the iteration throught the heatbug( Z I- ~& p0 `* A
// list from timestep to timestep& y9 |- c$ J) N4 s1 T$ \- }: M
0 b+ S9 g) p1 |+ ~7 _
// By default, all `createActionForEach' modelActions have* `! j5 Q9 P y4 |
// a default order of `Sequential', which means that the/ k( R1 u2 Y ?: H5 X2 W6 t8 }" w
// order of iteration through the `heatbugList' will be1 o+ K/ M7 |8 V8 m4 z5 V
// identical (assuming the list order is not changed1 i* b5 z! `2 w6 Z5 [ G
// indirectly by some other process).
, e. @# }) }' n) _# ^ - Q' D2 k* W; E) M. Y
modelActions = new ActionGroupImpl (getZone ());
4 A/ n5 L x3 J, r8 {! n+ W6 l6 S' H/ Z
try {
# w" h; q) I8 }' n0 N$ n% D. V modelActions.createActionTo$message
& n% _& |4 O0 ~6 b" ] (heat, new Selector (heat.getClass (), "stepRule", false));, I7 E. A: H; o+ n' r
} catch (Exception e) {
$ |0 M' B, X0 U7 \ System.err.println ("Exception stepRule: " + e.getMessage ());$ Y9 x& |; W# L6 [
}
3 q* t+ O7 F0 W0 z* Q0 i1 ^3 c
9 n" `7 ?$ D4 N$ ^( n0 f' `& x4 r9 v" k try {
# x( o1 O" C: x/ a o- K7 c# _ Heatbug proto = (Heatbug) heatbugList.get (0);5 u) {( [6 G9 t
Selector sel = ( U2 Q3 V1 g0 |$ H
new Selector (proto.getClass (), "heatbugStep", false);
3 ^9 Q7 \4 L% z- f7 d& Y actionForEach =
" Q) C% u( \3 b( B modelActions.createFActionForEachHomogeneous$call" [) [! N7 N: h, {
(heatbugList,, Z+ k6 O- z# c9 U# J" ^
new FCallImpl (this, proto, sel,
! m$ m& X4 Q7 i- p; Z/ `/ d new FArgumentsImpl (this, sel)));+ g1 I$ K2 a S& j( F
} catch (Exception e) {8 }+ G% q5 C& p: Z& N6 X
e.printStackTrace (System.err);
- D- f; [' A v6 N6 |, j C& W }
9 r. n) x) @4 c' Q R% L
- L+ d4 N; z2 ? syncUpdateOrder ();
% d# H$ V$ u4 W1 o% [5 c% k6 a. |0 j
try {! r& r* E& b3 z7 ^' Z
modelActions.createActionTo$message
3 Z8 a# Q+ ]# u4 u% X- k (heat, new Selector (heat.getClass (), "updateLattice", false));
+ z2 c7 [" x6 y/ z } catch (Exception e) {' A& L1 ~) z5 |3 {. G2 p+ [6 N
System.err.println("Exception updateLattice: " + e.getMessage ());
. Y! c- B% F2 X" \2 m }# c/ b1 E( f( o2 \, D
# }4 r7 ?0 _! X2 Y
// Then we create a schedule that executes the# o' f* l0 p: a J' D( y! [1 g, `
// modelActions. modelActions is an ActionGroup, by itself it3 T% d& Q2 C7 O7 \
// has no notion of time. In order to have it executed in2 h7 }6 W, g: T, V0 U9 P# Y7 \
// time, we create a Schedule that says to use the# Y" N. A" @6 ]! d
// modelActions ActionGroup at particular times. This4 Z( O* {% _# R' l. }
// schedule has a repeat interval of 1, it will loop every# N8 S9 m4 }0 T- e i' H- o
// time step. The action is executed at time 0 relative to9 S3 C6 V& v |: @$ E# s' [0 P
// the beginning of the loop.$ g* o x; l, r
$ p7 M! E H# L2 M8 F2 g // This is a simple schedule, with only one action that is @( w& D8 ?6 s- b! T: A
// just repeated every time. See jmousetrap for more
/ o9 V& c" d2 m8 Q // complicated schedules.6 E2 f% W( Q' N1 N) u- Z
, S: o6 S% ]8 z$ i' f X modelSchedule = new ScheduleImpl (getZone (), 1);, M' h4 b" g* r$ [1 ~
modelSchedule.at$createAction (0, modelActions);" w1 @2 D" D% {! z$ d/ Z8 r, c
1 m. U1 ~# \4 I0 P8 h9 x! ~& C
return this;
* s( R7 b* g% k1 V/ J } |