HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! ]0 G) ^6 f2 n* @9 B. R
; O' B& D) R& Z2 b' D/ U! c public Object buildActions () {% t: _! g" t6 l0 O' D: \
super.buildActions();
8 i. `; N+ H7 V* s
7 [; f8 f9 t3 @ // Create the list of simulation actions. We put these in% P" G2 x# b+ q1 F
// an action group, because we want these actions to be
2 U* S1 G+ E8 y+ @( E8 E6 Z' `: Q // executed in a specific order, but these steps should, p9 E& `% Z/ C4 }
// take no (simulated) time. The M(foo) means "The message" D3 J; W7 ]3 M; S$ N/ ^, ~
// called <foo>". You can send a message To a particular
9 j/ T& E# G( V% R // object, or ForEach object in a collection., k6 ?5 I8 M6 J0 T
8 C. @3 y. t+ r9 i4 I# G
// Note we update the heatspace in two phases: first run
8 L9 k$ F& i8 C7 U% w0 p // diffusion, then run "updateWorld" to actually enact the, X7 j/ @6 x6 A' o) U
// changes the heatbugs have made. The ordering here is
1 P: }% c1 |: J; I // significant!
5 Y+ ^/ L. N. \, ]* i
7 G4 Z+ F* {" ] // Note also, that with the additional! D: `# U* b" N0 ^( Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 s0 M: ?8 O( h) W( t
// randomize the order in which the bugs actually run+ T! f* Z8 n) P! E! p
// their step rule. This has the effect of removing any
# w' r/ A6 m- D- [: j // systematic bias in the iteration throught the heatbug
0 I( A$ ]6 M8 ` // list from timestep to timestep8 [/ w+ `; F% z& l6 f+ _2 J6 u/ I, o
( l# ~# b; z( v( K
// By default, all `createActionForEach' modelActions have
$ k, W; E1 r0 G8 i* X // a default order of `Sequential', which means that the4 M- q A" K" Q F) W2 ^
// order of iteration through the `heatbugList' will be
1 |$ h( Z' J* C // identical (assuming the list order is not changed
' M+ I3 u, a% W // indirectly by some other process).
" b0 ^9 a: J K$ Z
% [( v9 J, M2 P, l( I modelActions = new ActionGroupImpl (getZone ());
- o& z& J6 u( h3 h3 R9 R/ u9 c* Y! G, n6 |, D
try {% c9 U/ G; O. ] M* q3 @
modelActions.createActionTo$message1 w& r# E1 N3 y' K
(heat, new Selector (heat.getClass (), "stepRule", false));
- K) k2 n) \/ I' y" u: [$ T } catch (Exception e) {. u4 L, [* A5 `( T+ z+ r. v
System.err.println ("Exception stepRule: " + e.getMessage ());5 A0 K4 Q1 h* \9 V! ^0 x: ?0 a9 a
}
: W. g8 ]1 O9 p q1 f+ g" F/ M$ s% O( ]. Y. f5 L1 h
try {* @; W3 @2 x, ?/ @6 f
Heatbug proto = (Heatbug) heatbugList.get (0);
7 O9 O5 {! Z# d$ D6 D Selector sel = $ L+ v0 x! s$ l$ l4 W# [2 W1 ~
new Selector (proto.getClass (), "heatbugStep", false);
: G& W4 q: k* d3 z6 G actionForEach =: y9 d5 Z2 k) k- ] I
modelActions.createFActionForEachHomogeneous$call9 n7 D/ D$ f3 Z7 n1 K
(heatbugList,
' B$ ]. z, V, y1 ?$ ^$ L new FCallImpl (this, proto, sel,
2 F# ]6 h/ G- }" l new FArgumentsImpl (this, sel)));# n: c3 C0 b9 O# {4 A7 Z$ ?+ c* k
} catch (Exception e) {0 [' ~! G) x# R, E/ z
e.printStackTrace (System.err);8 i [. c$ B; N7 h1 l9 O, s& g3 e
}
$ B: L+ O1 C2 P 5 m! z L/ D$ g9 F/ [+ U0 o
syncUpdateOrder ();
2 s4 a2 B6 @3 Y( H) k" H4 {5 a; M- F1 |0 k" K
try {5 r. G) W) l/ T, f! C
modelActions.createActionTo$message - S B& J* G7 J+ X! _# {% F
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 |0 X- m3 A* @! H } catch (Exception e) {1 R; g {( f2 G$ l: K+ P f* _3 x% u
System.err.println("Exception updateLattice: " + e.getMessage ());/ Q, p8 z4 Y0 X( E3 `. b9 e
}0 j; D# P6 K0 n+ x
# @ u' s3 p' c // Then we create a schedule that executes the4 N" m n* W) |7 n
// modelActions. modelActions is an ActionGroup, by itself it
* A z0 e8 l: @$ G2 D( O // has no notion of time. In order to have it executed in X' n/ x$ s1 K( R, S
// time, we create a Schedule that says to use the. N$ i& _' P& x+ B# r
// modelActions ActionGroup at particular times. This
1 {6 p7 G- P; a; ? // schedule has a repeat interval of 1, it will loop every
' {$ ?3 X- V* O% Y$ J' C // time step. The action is executed at time 0 relative to3 G2 }5 F8 M Q7 f) t
// the beginning of the loop.# {9 C; g" A# N9 t# C* G3 ^$ b% t- D
8 |+ B$ v2 l6 R1 l; q' W
// This is a simple schedule, with only one action that is
6 r6 m1 ~$ N- X$ ]5 g9 K( j( w4 e t // just repeated every time. See jmousetrap for more/ R- ~2 J$ l ^( m
// complicated schedules.
% d2 `0 B( w; a' ?* T2 G$ j & u7 ]4 H4 b2 ]
modelSchedule = new ScheduleImpl (getZone (), 1);
! i1 r4 `. v( H! K modelSchedule.at$createAction (0, modelActions);
, U6 T* {; ]8 i5 r6 q* {
: \; N$ {4 z: |1 b/ c return this;* N: ^5 g0 J1 |! p p3 l
} |