HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# b% y( }; e4 M: ^
+ M% r: P4 @, @: i7 ?2 v public Object buildActions () {
' J) G0 X6 I3 C! T super.buildActions();
: F: g; E: i, i6 p t . d u- }! h$ m' k
// Create the list of simulation actions. We put these in
4 s+ V9 ?7 i9 d; v! f4 Z4 D // an action group, because we want these actions to be. h; y! x$ I6 T! D; E( U
// executed in a specific order, but these steps should
: S' X! `' W4 M // take no (simulated) time. The M(foo) means "The message+ U- a" |6 @+ V! \
// called <foo>". You can send a message To a particular4 S1 L2 K' R, Y: G( L5 m
// object, or ForEach object in a collection.
% ?: \; T: D; T) c 9 r# F8 Z& B r$ h
// Note we update the heatspace in two phases: first run1 O A5 e1 g9 c3 ^& {8 D2 ?
// diffusion, then run "updateWorld" to actually enact the8 ~+ C0 d' |4 n% }) {0 L
// changes the heatbugs have made. The ordering here is
/ q* D6 ^2 x" D // significant!
, B9 N5 e9 s1 y* C6 D/ Q) S
6 @& l" v* U# F- H // Note also, that with the additional ]9 h6 H! T7 U4 J2 ~( i8 G& n
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% n& u& w2 Y9 A w6 U // randomize the order in which the bugs actually run# V: ^& ^1 k4 p$ h
// their step rule. This has the effect of removing any& F4 Y }" e+ t: ^1 V6 Q' W
// systematic bias in the iteration throught the heatbug
8 Y0 J8 \' R) |) ] // list from timestep to timestep
7 q2 _* o% I$ y8 e( x
" S6 ]. } U; M8 W7 f+ @ // By default, all `createActionForEach' modelActions have
& ~! L% O7 [% I t! f' ^" d // a default order of `Sequential', which means that the1 c% @! Y- t. g/ [' ?$ Z3 T* e
// order of iteration through the `heatbugList' will be* \# L1 i; `' \7 y5 Z
// identical (assuming the list order is not changed1 _: i* I, ~( m. n9 o
// indirectly by some other process).
* [5 ^3 g9 t% t& c F+ d
; i, k8 X# T2 |' `* A modelActions = new ActionGroupImpl (getZone ());
% p% Q+ l* x3 [: s& i) N3 l% q
4 k5 a6 J: o7 }6 p! q try {
) g6 X% R2 T5 D' B modelActions.createActionTo$message: w- ^: \5 {- {* e3 D) h* G5 f' p
(heat, new Selector (heat.getClass (), "stepRule", false));
: [0 ?( B" n6 K6 `" r5 O } catch (Exception e) {
2 ?: F d _0 q" Q! p/ X6 n; Z System.err.println ("Exception stepRule: " + e.getMessage ());# t) E! V4 G* y7 B! ?) s/ s
}+ R/ N: u' C% Q8 j$ Y
+ X# X. g3 D9 r# z3 e
try {' f$ x7 V/ v) I( l4 A
Heatbug proto = (Heatbug) heatbugList.get (0);/ q% x5 W5 \% k% W2 W9 W
Selector sel = 2 P/ `$ V; ^+ ]7 u
new Selector (proto.getClass (), "heatbugStep", false);
l8 Y' l; r+ a5 B# R actionForEach =
4 Y. q [# P8 d0 E. J4 o" O2 P: D modelActions.createFActionForEachHomogeneous$call
P: E3 g8 L; s8 L# Z (heatbugList,' M% q, d d, X+ [: b
new FCallImpl (this, proto, sel,' A' N- _5 b5 Z2 s
new FArgumentsImpl (this, sel)));& V6 Z( G( G4 f1 l! C
} catch (Exception e) {
6 X4 V; l3 E) B0 V e.printStackTrace (System.err);
; J& c( M. I" {( e }
9 ~8 ^- M$ {: S ' u% T8 G! v# j5 I2 u" g% K
syncUpdateOrder ();. F" H; X( V% I7 Y7 D
) X9 p) l& V4 E0 }2 |! D2 X2 E
try {1 A; i8 w7 h# G* M4 J
modelActions.createActionTo$message . X2 e1 K# b/ c0 Y% s4 i7 V/ H8 m. |9 [
(heat, new Selector (heat.getClass (), "updateLattice", false));: c6 k1 g3 S( p4 k8 w
} catch (Exception e) {
% O# D9 V! q3 B5 d$ K& u; k System.err.println("Exception updateLattice: " + e.getMessage ());
& b9 B5 @: H U5 ` }3 N+ J4 c0 ~% O# {' n
- Q7 P1 j" |2 c5 m/ x5 ^
// Then we create a schedule that executes the, {' ?$ Z. K0 F2 p8 f6 C8 ?7 M
// modelActions. modelActions is an ActionGroup, by itself it
2 ~' ?1 k S% _5 b ]+ O // has no notion of time. In order to have it executed in
6 {- r) E6 h( ?1 K+ @* `, ^) d3 A8 t // time, we create a Schedule that says to use the7 E% h: I. W- o o
// modelActions ActionGroup at particular times. This n: f. v/ D3 m4 k' j# x5 Z
// schedule has a repeat interval of 1, it will loop every
, x: M9 X5 g, V t/ { // time step. The action is executed at time 0 relative to
5 {5 d9 E- Y+ f // the beginning of the loop.
% M6 a: P! m4 \- [6 D2 p# y; h! C; e" x/ }
// This is a simple schedule, with only one action that is! r- K5 ^' X( F8 A! N* r
// just repeated every time. See jmousetrap for more7 l6 H; |, j0 x4 ]; G) |
// complicated schedules.! N {0 j, R; F
: o8 e/ B. i$ G8 H+ M modelSchedule = new ScheduleImpl (getZone (), 1);
; @# l3 y! u% ?# T. M4 G& l9 W9 \ modelSchedule.at$createAction (0, modelActions);
. Q$ O2 b" r6 M7 y2 C" p
0 i0 B/ c; B$ l6 U7 ~" y5 V return this;2 I5 b: M/ K- C0 z1 Y$ w+ `& J4 r
} |