HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 l! U' [& p. K# X5 K
; n7 t4 D$ p; D- q public Object buildActions () {
* ~/ Y' [: \2 I7 Z7 E% A/ C super.buildActions();
, u! r' i; B( e- E0 J
t4 B9 B# R, h. \9 m& w // Create the list of simulation actions. We put these in
% v' ^, ^9 \/ e4 ~' ]" u* P( u5 P U // an action group, because we want these actions to be
1 F( F& c) e5 H, I // executed in a specific order, but these steps should0 T" B% W! u2 x4 v X
// take no (simulated) time. The M(foo) means "The message% y" z8 i! H3 R
// called <foo>". You can send a message To a particular7 J% c9 {6 `2 [
// object, or ForEach object in a collection.2 o* w. ?7 l! n- X
8 T1 I& L1 U4 y1 I- ]. o% ?6 N // Note we update the heatspace in two phases: first run( p! B% s9 A }0 t
// diffusion, then run "updateWorld" to actually enact the% z8 i; R0 i& C F5 H* C: p
// changes the heatbugs have made. The ordering here is3 d8 G9 R+ I' l& \
// significant!) ?2 S. O1 i2 M: k. b% ]# j/ L
4 Y: r$ C, F0 q3 C) _9 D
// Note also, that with the additional
* U6 v/ p# T1 H7 }/ y6 \ // `randomizeHeatbugUpdateOrder' Boolean flag we can4 S: I @4 B! O! L
// randomize the order in which the bugs actually run8 L# a3 u7 K$ g
// their step rule. This has the effect of removing any
/ U! B+ w3 N/ e! L- C // systematic bias in the iteration throught the heatbug6 U. P& L1 H' _; d; {2 s
// list from timestep to timestep T8 x3 M! g9 m) C7 J# R9 q
% |& B1 @3 U( U7 `4 u5 e5 a
// By default, all `createActionForEach' modelActions have
. X* O7 L0 d$ Y // a default order of `Sequential', which means that the
: s: J- U I& I$ l/ c/ ? // order of iteration through the `heatbugList' will be$ ^ w4 B8 x* S' K4 P
// identical (assuming the list order is not changed' o5 B/ O$ x' ~0 {' E
// indirectly by some other process).
6 d/ b" S( w3 C6 Z
: [: O% P4 ]3 v0 `: C* T modelActions = new ActionGroupImpl (getZone ());
& w0 c, V. H3 @& m1 c9 _" j; Z) T* g. g8 {0 D# W8 F! v
try {
+ @5 X8 v/ y0 m3 o' H modelActions.createActionTo$message; x2 j( l" S1 q/ ?
(heat, new Selector (heat.getClass (), "stepRule", false));' _+ Q1 \; e) L8 J& a3 U
} catch (Exception e) {
: i7 }2 j- q" k: w% E% L System.err.println ("Exception stepRule: " + e.getMessage ());8 z3 e: n1 J( p; V
}5 Y# U8 A/ q1 E9 L' z3 o5 @
2 T, }4 K& G& d
try {
+ c, G, b: P7 r9 d Heatbug proto = (Heatbug) heatbugList.get (0);
6 X4 G. v+ X1 v+ J4 n Selector sel = ( R7 B4 E6 e# _3 w' B4 I
new Selector (proto.getClass (), "heatbugStep", false);' V. P6 R$ i7 e5 J0 C- u
actionForEach =
+ s& m7 I/ _# S6 R, u1 ?3 F modelActions.createFActionForEachHomogeneous$call
- r3 ?7 i2 E/ Y (heatbugList,
2 _* J. c6 }: p4 g4 |' ?- T new FCallImpl (this, proto, sel,, {% Q% }) Y2 H- n6 w4 I
new FArgumentsImpl (this, sel)));
i% @7 v; b, v; ?$ M8 _2 M } catch (Exception e) {' h. v+ v+ K: `/ u1 l7 g7 P' Y
e.printStackTrace (System.err);
7 T) }1 B n6 p: ?9 Y- b6 l }& o( P& T4 j6 i
/ h9 t' G' o! n2 w8 E* v5 y
syncUpdateOrder ();
. r/ W5 I; j9 M- P l; B7 R% Q: [0 D; x) {4 _6 v7 G
try {( D' U& R( e8 J8 L5 |( w
modelActions.createActionTo$message 0 [$ }* k2 J, {) P; D
(heat, new Selector (heat.getClass (), "updateLattice", false));" H: H/ _2 w3 U, c0 K/ p
} catch (Exception e) {
9 e/ F, ^4 q+ O2 t- E System.err.println("Exception updateLattice: " + e.getMessage ());5 J* w6 o" a9 E2 a, L
}
! Z, l, @7 L+ R$ J7 j, L8 H0 _8 @; L
3 v/ g* G/ G) [3 b$ C! o6 \2 g // Then we create a schedule that executes the
/ `- W9 A6 h: U/ V0 r% o // modelActions. modelActions is an ActionGroup, by itself it
2 f1 c9 D$ U. j/ l0 w+ g // has no notion of time. In order to have it executed in" v& h3 t% }7 B9 X
// time, we create a Schedule that says to use the
, J# f# m- B \1 L# N // modelActions ActionGroup at particular times. This
8 R0 a3 h. }5 A1 g, X7 Z% p) ` // schedule has a repeat interval of 1, it will loop every
5 D6 n. A: L2 m5 d) {& i // time step. The action is executed at time 0 relative to
8 A! {- C& ^ B- m+ l // the beginning of the loop.; J8 w6 H9 c! m: L% Z: s
' w. c, g( Q, {1 Z
// This is a simple schedule, with only one action that is
" o$ u6 f8 R# w6 p3 a* a // just repeated every time. See jmousetrap for more+ N( x3 c W7 C$ y# t, D
// complicated schedules.
: q; Y) W l( H8 H: K5 g ]( W- g5 Z0 C+ h* i) s' C( k
modelSchedule = new ScheduleImpl (getZone (), 1);$ V7 J' z2 G; E1 x6 M8 p
modelSchedule.at$createAction (0, modelActions);/ X! g f, y/ H- d% @! O* X3 ]
' _4 S" B& D* D9 F6 _' \0 j return this;' r; Q" M, R! W4 A! O0 G
} |