HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# w* L# p" i7 F K) u1 m/ ~) `9 q
0 k( h l$ A1 S( k" w5 ~% e3 [. U2 V public Object buildActions () {
' y2 @6 m! } |6 t7 J' T super.buildActions();$ u- k; V( I- H9 e1 u3 w3 R4 m
. b7 [% l" @6 @( c( I: E; h // Create the list of simulation actions. We put these in2 o+ Q; c' q: T
// an action group, because we want these actions to be8 Y, r3 U0 E1 I0 P- ?
// executed in a specific order, but these steps should9 ]5 e% I! b* ?$ f9 S% A2 G
// take no (simulated) time. The M(foo) means "The message
( _5 }1 p8 Y, x4 `$ t! X9 w // called <foo>". You can send a message To a particular' X# d( ] {' {# n2 y# Z
// object, or ForEach object in a collection.* o5 Q0 w( ~" j; _/ D
0 j+ d; c3 k! Q, q/ a+ @ // Note we update the heatspace in two phases: first run
, H: X( k0 H$ Y' g/ E( I // diffusion, then run "updateWorld" to actually enact the8 C' j) W: ~* _2 o7 f+ z8 A
// changes the heatbugs have made. The ordering here is
/ U2 B% s! y/ C // significant!% e* _( F8 E: X' M8 X/ Q
; C0 t# O6 [! W8 j // Note also, that with the additional
6 Y4 _8 G7 x; D) t* z) x // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ `+ `: w( h( o% q, f2 U, u/ x/ T# } // randomize the order in which the bugs actually run
7 G- o$ i0 C; M3 W; ~ // their step rule. This has the effect of removing any
- v1 ]* q2 ^2 P9 j // systematic bias in the iteration throught the heatbug3 n& b9 T! l6 q% _5 y
// list from timestep to timestep; I: m+ M u; J, Z* X
0 `2 G4 G: {/ Y" j! B* ~$ R
// By default, all `createActionForEach' modelActions have
7 w7 F, G" s% F. j) y, u* T // a default order of `Sequential', which means that the
7 G/ y+ n- f* o // order of iteration through the `heatbugList' will be' \' M5 g+ K' Y5 L0 S
// identical (assuming the list order is not changed7 F G7 Q. t' u6 H: w
// indirectly by some other process).7 B" r- j/ Y7 A
! {% U n3 A! m modelActions = new ActionGroupImpl (getZone ());% Y9 V5 M9 K! |5 }5 {- Q
# t' \* w: r: L
try {5 A) E2 I2 m* {% e% \
modelActions.createActionTo$message
2 j0 I& @& B& J- H2 r" x (heat, new Selector (heat.getClass (), "stepRule", false));( l& y; k; c) N1 t6 c$ k9 w4 Y
} catch (Exception e) {
4 y- I3 i6 N7 S& J System.err.println ("Exception stepRule: " + e.getMessage ());, T9 _5 ^$ A, U6 K
}6 ]% o P+ N7 w# C- H
1 ~8 H+ G) `3 f; {) \& S6 [ try {
' _: T q; N! D5 N5 u Heatbug proto = (Heatbug) heatbugList.get (0);7 z$ a; K7 m8 T0 e* l# ?/ C
Selector sel =
& F. `: z3 n; x- o$ }5 D$ G new Selector (proto.getClass (), "heatbugStep", false);. \: a6 A5 I, \8 L. r! G. E
actionForEach =0 u. g" h u1 E$ n. m
modelActions.createFActionForEachHomogeneous$call; m8 P) o; Y. P, U" N6 s U) S
(heatbugList,$ ?7 z6 c, ^( o0 \
new FCallImpl (this, proto, sel,
/ F2 j; [) R7 O" r new FArgumentsImpl (this, sel)));
& e5 K1 M; g U% C7 @4 G2 L } catch (Exception e) {* W* o6 O# } n
e.printStackTrace (System.err);
: y* z7 l$ R* F- `" P9 \9 B }
" t- m' N. w; I& ~! I
; v b$ Z: ` Y& T( t7 o syncUpdateOrder ();
& P/ \: K! a. k+ @
$ K1 T6 i! S) S+ E try {1 I. t* N- z& ~+ Y) b/ }, `
modelActions.createActionTo$message 9 I7 g6 Q+ e+ k) J3 T
(heat, new Selector (heat.getClass (), "updateLattice", false));- ?8 @' T/ s, ~6 B% h" D
} catch (Exception e) {: a) d8 Y- M+ h8 s% T/ J/ k
System.err.println("Exception updateLattice: " + e.getMessage ());
' J1 X" j9 F* K) K6 N }7 k4 Q) e( M1 S. m5 U5 Z
{8 X# D4 f' {7 j( N3 o // Then we create a schedule that executes the' X" Q8 H! ?8 w$ w
// modelActions. modelActions is an ActionGroup, by itself it
$ o& \: G4 ?8 e: [ // has no notion of time. In order to have it executed in
5 E. _; a6 Y7 G1 F( p2 _4 L! C( A // time, we create a Schedule that says to use the6 J$ L$ }( V& g# {5 g2 ^) N
// modelActions ActionGroup at particular times. This! ]/ Q( \# u3 Q% o! ?. i
// schedule has a repeat interval of 1, it will loop every0 b9 z! k% ^/ ~1 z5 z# D+ E2 b
// time step. The action is executed at time 0 relative to: d% t. r2 @: o$ k
// the beginning of the loop.
8 Y0 V4 A! n8 m6 o
: y# s7 i$ G8 \6 |7 ~* [: ?$ S // This is a simple schedule, with only one action that is
8 F' P6 Q& m; v2 t; w // just repeated every time. See jmousetrap for more
6 S1 B3 W; G9 X: l, ? e; \. S // complicated schedules.
0 @- u5 Q8 p0 s$ s, x7 j2 p
- M0 a4 g4 T+ }% L modelSchedule = new ScheduleImpl (getZone (), 1);/ A7 C. t' B2 N. i1 K
modelSchedule.at$createAction (0, modelActions);( Z3 Z2 T! r' M, P
! c0 m2 O( X4 B! g4 O
return this;
% J8 G" q: t+ w$ t+ |2 l a. C } |