HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; Y9 e) }. O4 z5 _" K5 y
% n$ [2 J9 w4 S3 j public Object buildActions () {0 S' O, `; N/ w7 ^
super.buildActions();
: c' T# q% a' k! n2 o$ w3 l5 \, C
8 [9 @* d. Q( U // Create the list of simulation actions. We put these in
) x% O3 k% p! P% h8 N( N6 T // an action group, because we want these actions to be4 `; b. Y& b* }) g5 d. Y! I" u
// executed in a specific order, but these steps should/ ?* o6 ^1 W* ~4 e" s
// take no (simulated) time. The M(foo) means "The message
8 U& k2 r) B0 O8 |. C; S // called <foo>". You can send a message To a particular
- d7 ?* V! Y7 P6 w // object, or ForEach object in a collection.0 R( ~2 X$ H$ X( L
9 b( B6 i" i9 W. h6 d; \2 _ // Note we update the heatspace in two phases: first run
4 _) B0 c5 Z2 q* ^6 s5 @ // diffusion, then run "updateWorld" to actually enact the+ q1 p- |/ M2 I1 {( g5 r L# H
// changes the heatbugs have made. The ordering here is
6 T$ j. A! Q4 w& S3 O2 @+ x // significant!
& F2 G. T; \+ V( ]
# `/ {+ S6 U( ^/ J% w& ?# ~! { // Note also, that with the additional
% A# D% T. C# {1 E' F // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ ]1 g. e1 Q! x8 z8 m6 f // randomize the order in which the bugs actually run
# P; z+ C8 u% d# |3 a // their step rule. This has the effect of removing any
" Q6 X# J- j# o2 S // systematic bias in the iteration throught the heatbug: A1 L+ c+ b5 m) J1 h4 K) @
// list from timestep to timestep
0 {9 ?+ C4 f; s; ? 6 s# [) U# s; V; Y* f% ?
// By default, all `createActionForEach' modelActions have
$ [/ T" {- e0 W4 b e) X // a default order of `Sequential', which means that the
$ ?1 R0 z2 t# ~1 ] // order of iteration through the `heatbugList' will be
- e5 d" t+ z% D // identical (assuming the list order is not changed. h6 a) f& \* y! W z/ Z& C0 X
// indirectly by some other process).
) [( \* M: ]0 `/ j, @$ W D4 s " D2 o6 b& U* K6 T: s8 Q& F: e
modelActions = new ActionGroupImpl (getZone ());% m2 i% I4 a" \* B" Z }
- Z }. W- F3 a: K: l
try {0 V4 p2 V8 K0 C
modelActions.createActionTo$message5 J/ N9 ~$ o( l+ R* J- a
(heat, new Selector (heat.getClass (), "stepRule", false));
! z; |7 W% G' h0 f: T5 y, g: S* ? } catch (Exception e) {4 i; H' F2 i9 ~7 O
System.err.println ("Exception stepRule: " + e.getMessage ());
( z; C( j% w9 [6 Q3 ? P }' i" A4 `1 ]7 ]4 d3 X
5 _- S& B3 i e
try {
$ L6 r4 O1 K+ R; e6 } Heatbug proto = (Heatbug) heatbugList.get (0);
- H, w; k g9 g Selector sel = ( O1 L) U; A& R9 b8 |1 V: v
new Selector (proto.getClass (), "heatbugStep", false);; p6 n. V. P. D) N! {4 ?/ L9 N
actionForEach =
# P- }8 ]6 ?1 X* S% y modelActions.createFActionForEachHomogeneous$call# |+ ]) I& f1 S& k$ l
(heatbugList,
, V- t! B; i; v4 m8 t new FCallImpl (this, proto, sel,
" D8 C _. h) ^4 N2 ^ new FArgumentsImpl (this, sel)));
- d# K) P( _; a- S# s- Q } catch (Exception e) {
9 t! ^$ U2 @4 {7 V& J$ J e.printStackTrace (System.err);
; s0 f$ g3 p/ \0 l) Q) P }
( d" e& j. r: K3 K6 j. k
% X2 _2 R$ s6 _. U syncUpdateOrder ();
$ @6 i8 D2 j$ Y0 D( _9 }* b
8 ]2 n( k) h4 G try {; |3 N0 R8 g8 L# J j( x5 ?& [
modelActions.createActionTo$message
6 Y% @. y' g- }: t (heat, new Selector (heat.getClass (), "updateLattice", false));" d% d7 r* [9 n
} catch (Exception e) {
/ d3 \6 ?4 U' q& W7 X4 o System.err.println("Exception updateLattice: " + e.getMessage ());
, g5 s. v+ N9 n5 ]0 p5 d/ t0 a) @ }
) u4 F+ ?% Y8 N; G! x: @7 g
0 b( R3 ^8 a7 b/ a" b$ y% G4 H // Then we create a schedule that executes the
w: M; [" f4 g! `+ {) `+ J3 C // modelActions. modelActions is an ActionGroup, by itself it6 s" l) ?! x& z+ v+ L |
// has no notion of time. In order to have it executed in/ \: R/ P8 Q z9 L4 E, T
// time, we create a Schedule that says to use the3 A3 s+ Z. M f) w0 ]% S/ |& v
// modelActions ActionGroup at particular times. This
" n1 Y) \3 R/ `2 A- R k8 X+ ]5 b4 | // schedule has a repeat interval of 1, it will loop every
: X) ~1 Y( a- l // time step. The action is executed at time 0 relative to+ S0 e8 P7 x4 Q5 m# `, f
// the beginning of the loop.
) u6 v5 q. R" B6 k$ ]( B. b5 m' \4 W0 I
// This is a simple schedule, with only one action that is
, q; c* A2 p) [* I' F Y4 n2 D // just repeated every time. See jmousetrap for more, R# v3 n( G4 M2 V# X: f' v
// complicated schedules.2 p& ?# i" m' P+ i% s
/ R3 i8 u% Z: a
modelSchedule = new ScheduleImpl (getZone (), 1);
1 b; Z. ^) q% U& _; C$ I3 P modelSchedule.at$createAction (0, modelActions);
; j+ |" p. ?+ r! o 2 b, e7 D$ r8 L: v0 K
return this; U% [0 O* Q# d( Y+ h! R0 d
} |