HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: h& ~, r. r' w' S# e
, u n4 A' X) `- e' i3 w public Object buildActions () {
2 ?9 v& P8 ^ }! c1 j: U. t super.buildActions();
9 V3 y& g' U- K+ m% H. ?3 P
2 o& B& p6 b4 \; D5 ] // Create the list of simulation actions. We put these in) u8 M) v7 h+ M
// an action group, because we want these actions to be
' x- \; \8 f7 E7 U$ u7 W, \ // executed in a specific order, but these steps should- l+ _3 Z/ V: U: H" {
// take no (simulated) time. The M(foo) means "The message( @' @0 I8 ^& y0 H; c$ M
// called <foo>". You can send a message To a particular1 I7 z4 ~& e9 Z7 T5 _, x3 ^
// object, or ForEach object in a collection.
$ v6 f+ z7 L, f! U @/ ]; @* H6 i. ]1 V8 B * J8 J, h& J: @% S$ }
// Note we update the heatspace in two phases: first run
$ x) m3 S/ @4 C // diffusion, then run "updateWorld" to actually enact the
* K* `% M3 B& Y. ~* x, @5 w // changes the heatbugs have made. The ordering here is: u2 E1 C9 A+ |
// significant!
" b$ V) O; r% z6 X6 S 8 Q2 I8 f# r2 F% }4 ^% T1 p# i! c
// Note also, that with the additional
8 D1 D3 S7 ]$ H // `randomizeHeatbugUpdateOrder' Boolean flag we can
- Z1 P8 R3 ^9 F // randomize the order in which the bugs actually run6 q+ w1 g& j3 w2 r' D4 k4 C% S- i
// their step rule. This has the effect of removing any
& ]" x* _$ T# c( V) { // systematic bias in the iteration throught the heatbug
5 W' c' Z' P* \ // list from timestep to timestep5 ~. `4 T, k1 i
1 {/ N4 \6 H* l, I. L // By default, all `createActionForEach' modelActions have" u7 \) y3 S9 |& p, r: h! J
// a default order of `Sequential', which means that the
: X1 e4 H% c2 M9 |+ g- Z! z4 ?# Q // order of iteration through the `heatbugList' will be# O, F: y9 a# ~, J* Z
// identical (assuming the list order is not changed+ K4 l8 Z' i: A! N! o
// indirectly by some other process).. i1 L" N- o$ m* L; u* A7 D9 ?
- D' j9 g; @6 k
modelActions = new ActionGroupImpl (getZone ());
: n1 R5 v+ h" U: F0 L% _% r( q5 o! V6 \; g
try {9 A/ {8 R* n+ ?& T/ o0 e
modelActions.createActionTo$message
% o/ w8 T# S8 }/ K (heat, new Selector (heat.getClass (), "stepRule", false));8 N; ]% z: T5 R$ v: i' x
} catch (Exception e) {7 `, i( D6 p( r, e
System.err.println ("Exception stepRule: " + e.getMessage ());- B( f; A4 q1 c& z9 g
}$ |0 ~% [% ~9 g7 A& U# k
, O$ @! Q% `5 ~4 o1 Z! N4 M! Y
try {1 g9 q6 n2 z( U' j: Y
Heatbug proto = (Heatbug) heatbugList.get (0);4 d3 t8 Y/ p+ `# f; ^7 ~; }
Selector sel =
, U' B7 t' D! U; P2 o2 s7 p new Selector (proto.getClass (), "heatbugStep", false);# \% B. ?! E$ \& @2 P
actionForEach =
% |) m& K0 |! X6 o" Y# U5 C- G3 @ modelActions.createFActionForEachHomogeneous$call
, {; k {" b/ b7 `% F% y (heatbugList," S) Z1 l0 c& `( s) ?
new FCallImpl (this, proto, sel,
# L4 G. a4 }0 d+ c2 w5 ~ new FArgumentsImpl (this, sel)));# {! M- X9 q1 p' _; B
} catch (Exception e) {
4 {1 O- U1 N% J0 B) d7 _ e.printStackTrace (System.err);* B. H) F$ ^' R7 H `9 m
}
3 L; e2 L, g# z4 R& ?* V( V
( M9 |* o2 [- r9 K syncUpdateOrder ();" m8 K; K6 y2 ~: ]
6 H& M) L: H' w, e3 w4 Z! ]! H try {
. }+ o% G6 t- M+ N modelActions.createActionTo$message 3 L9 q" M, _: g3 N; ?( Q% C
(heat, new Selector (heat.getClass (), "updateLattice", false));
* M( C, H1 q5 {; Y# b: r. y2 j } catch (Exception e) {( _% O0 k0 x" y1 s0 D
System.err.println("Exception updateLattice: " + e.getMessage ());
! b. y- G! n# e3 W+ I; Q5 q3 p }
% F/ l, }# F7 E; u% p0 @, d4 s3 [ " Z9 q/ t3 v( H* N0 e8 c
// Then we create a schedule that executes the; }- [6 c3 ~' Q2 F
// modelActions. modelActions is an ActionGroup, by itself it
+ _, i8 k- u+ ~+ O/ L // has no notion of time. In order to have it executed in
r6 c" _( D- o // time, we create a Schedule that says to use the% \+ U) w0 [0 o% `- r1 @6 b. k
// modelActions ActionGroup at particular times. This( ^3 x7 S7 v0 `4 ]8 j% B
// schedule has a repeat interval of 1, it will loop every
9 S, ~6 o9 i# x9 H // time step. The action is executed at time 0 relative to! S6 M. m$ l+ ?! {
// the beginning of the loop.9 { o* Q3 ]% T- S+ p. u
8 p8 D3 N7 U* |7 D5 ^ // This is a simple schedule, with only one action that is
7 L3 ?; j* Z, H: U/ K, X // just repeated every time. See jmousetrap for more
7 i2 x. `( N6 z2 H7 \ // complicated schedules.) [/ f6 m2 l8 P1 G- X
/ K0 }! D! ]5 K modelSchedule = new ScheduleImpl (getZone (), 1);
( P) Q4 S2 Y4 i' v$ l modelSchedule.at$createAction (0, modelActions);& x) V1 U% e( V$ X3 H4 U
8 Y7 n; b/ l+ M& m# D9 c2 ]
return this;
4 X8 _6 V, Y# ` } |