HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 N* X' i6 d+ G6 h6 ]7 _
: ?# ?: i8 v9 l" {5 { public Object buildActions () {
0 b5 c7 }& l5 u1 Q v super.buildActions();
6 d, S8 {# z7 V7 Q" {/ |) s' d
# T1 ~1 h& _, u" Q6 {- y/ G // Create the list of simulation actions. We put these in8 J4 c* @6 U6 e
// an action group, because we want these actions to be
. D# ~) J- W B# y // executed in a specific order, but these steps should
[0 g3 \8 C) B/ I' @! v! ?5 s* B // take no (simulated) time. The M(foo) means "The message
( Z: k. H; b( J* L1 |" r // called <foo>". You can send a message To a particular
5 P) x+ C. c1 Z% g // object, or ForEach object in a collection.
: V" I0 I2 n. T o3 g! X/ [ ! e% l0 P9 v. X6 ^
// Note we update the heatspace in two phases: first run
; v8 z( s0 O* ^ // diffusion, then run "updateWorld" to actually enact the
- i& h$ C% T2 H* f3 H# ` // changes the heatbugs have made. The ordering here is: ~) [0 K L& U% j+ h7 g6 o9 |
// significant!
. y# o, p3 A# v" o
; ?, m$ O) r; [2 a // Note also, that with the additional
7 [% M* ~- T6 D // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ p. T$ y- D2 Y' b5 t) B! q' v // randomize the order in which the bugs actually run
! B! `3 Q* ~% Z" m3 o // their step rule. This has the effect of removing any( E+ \; { Z* n8 U
// systematic bias in the iteration throught the heatbug
- G" O. N$ w) _2 n5 [ // list from timestep to timestep
2 E+ ~' G" B% T3 h; v2 S8 W
% z, E0 F+ J b1 k- B* T! W // By default, all `createActionForEach' modelActions have
# j8 r5 g% f U# |! A // a default order of `Sequential', which means that the0 C( T- b6 A1 d( F
// order of iteration through the `heatbugList' will be
4 A' g( ^, q0 K5 ]2 `8 L( a // identical (assuming the list order is not changed
) S1 r% U$ n- k // indirectly by some other process).
+ Q6 p1 L1 |& \, [, n
$ f+ K W$ W) j% [9 a' P' d& h" v modelActions = new ActionGroupImpl (getZone ());
0 W' S0 T7 }: ~% Y' {% F. F: {5 l
try {
" A4 j2 m/ O' `) G. ~ modelActions.createActionTo$message( W5 W2 C/ b: r5 R$ V% Q
(heat, new Selector (heat.getClass (), "stepRule", false));
5 Z) ^" m* T. a' [6 V) G( [ } catch (Exception e) {7 T- P. J% Q' m! |9 [ S8 U9 O; C
System.err.println ("Exception stepRule: " + e.getMessage ()); n6 @. l* Y4 P/ ]. O; y
}
# t# e0 e u: H0 X: N5 K
. f# Q* I7 g7 F8 b& k* R; X0 z try {# L; v8 _, A1 H& ?8 n. I+ B
Heatbug proto = (Heatbug) heatbugList.get (0);
- _# `! r4 x/ z T Selector sel = # y$ l1 P& l8 ^' v. n# n8 C
new Selector (proto.getClass (), "heatbugStep", false);. [7 I( l2 X: ~/ M# G7 b
actionForEach =$ i+ x' b2 p$ ]$ q; K4 y3 \/ l9 o
modelActions.createFActionForEachHomogeneous$call
" h5 w# a! s& b( t3 G0 p' O | (heatbugList,
5 l0 ~6 P# A4 ^0 M) _; u new FCallImpl (this, proto, sel,5 S; H+ b1 q% ]9 W {
new FArgumentsImpl (this, sel)));
4 ]6 k1 K+ q! g# I7 k/ {1 D6 m } catch (Exception e) {. Z4 ^+ S4 L4 g$ n5 K! R) s1 o! L9 m. O
e.printStackTrace (System.err);
7 z* D0 C" t9 N( V" [2 v( Z) H }6 d/ k: G9 R! b
4 v* d: p1 i5 C) U syncUpdateOrder ();) t0 X! B" |( j3 L
( J; G9 u! |0 V4 [+ ?8 X* P try {
- d( c9 u. f, f& S/ I- S modelActions.createActionTo$message 0 }0 Y I \4 `- d4 U m
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ L3 j+ L: `" J# e) Y } catch (Exception e) {
, D [3 C* Z4 {! N, p; H& K System.err.println("Exception updateLattice: " + e.getMessage ());
' D& t$ D' Y# Z; N' R7 t7 A }# O' F" Q/ p; u/ U
\8 f" W0 b- |: k/ e( O
// Then we create a schedule that executes the
' k5 s2 G2 Q1 w) p5 V/ _' Y# D // modelActions. modelActions is an ActionGroup, by itself it, D3 D8 K. ]/ \+ L y- L
// has no notion of time. In order to have it executed in7 q5 I9 F8 g1 M% W" Q ~' ]
// time, we create a Schedule that says to use the9 }! b! g' Q- X# Q" |2 ~
// modelActions ActionGroup at particular times. This1 ]) f s% l1 L7 |. {
// schedule has a repeat interval of 1, it will loop every
7 E* X3 _4 O) q/ M# v, l // time step. The action is executed at time 0 relative to0 ^6 ^$ o3 d) ^$ ~5 @
// the beginning of the loop.9 E, @ z5 }& A* `4 M$ w$ N1 t
+ R- S- p: u! M$ |$ v, _) E1 Z
// This is a simple schedule, with only one action that is
* X1 s4 A6 J# H2 T // just repeated every time. See jmousetrap for more
! F; t9 c. R) ^; r! u3 H // complicated schedules.$ s% W- A8 p+ j2 x ^
2 Z. E. {& Y- Q& k* q
modelSchedule = new ScheduleImpl (getZone (), 1);) Y% l% k) e: ~0 e/ `! a
modelSchedule.at$createAction (0, modelActions);0 F6 R8 l# r' K2 Z8 q$ z- ~
4 I3 @$ }& M2 M9 c6 s$ g2 U4 U0 J
return this;
6 x' |7 z# p* E7 r* T2 G) V" Z- J } |