HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ R" ~" ?0 R9 ~ W3 V
% I4 }; u. q4 p+ a3 T4 u public Object buildActions () {- } T8 J! l& a8 W$ a
super.buildActions();
! m/ U8 D9 k; m, n/ m$ w
0 e/ ?6 i9 W# H1 H0 \ // Create the list of simulation actions. We put these in* l7 G/ p' J1 Z* z* {
// an action group, because we want these actions to be
* P6 v9 h& C* [ // executed in a specific order, but these steps should* J8 w* O$ W$ h6 I( h
// take no (simulated) time. The M(foo) means "The message
" i h: }# Y9 Y& a4 O8 x7 X // called <foo>". You can send a message To a particular2 e; Z) _$ k) I0 C
// object, or ForEach object in a collection.
/ T; a4 z$ s% Z# `
$ b! S: m0 J% @$ c. O0 I // Note we update the heatspace in two phases: first run
) U& H7 u( k- H4 U# H // diffusion, then run "updateWorld" to actually enact the+ k, A4 n+ u+ V9 T6 P( z
// changes the heatbugs have made. The ordering here is' A& B) K2 c3 j# P3 v& e
// significant!6 J3 ]) v5 C- B- v% e$ Z
& }6 a: S/ W) l2 {" e // Note also, that with the additional) B. ]* q" S h: k/ @. _/ i# n
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ S- x5 o5 t( v: x // randomize the order in which the bugs actually run
; ]+ F/ l2 \/ N4 O3 j4 ` // their step rule. This has the effect of removing any+ W6 e) ~8 b% X9 B+ u
// systematic bias in the iteration throught the heatbug
J* W8 C$ X5 R4 E5 L // list from timestep to timestep( k1 r5 b3 C7 V5 s- Q' \
# m9 a$ h: m D // By default, all `createActionForEach' modelActions have# N$ e& w( t/ M I
// a default order of `Sequential', which means that the
/ J3 \; l% d, B+ y( o% D7 @ // order of iteration through the `heatbugList' will be0 a+ [* v. b+ l5 [. Q: [( f
// identical (assuming the list order is not changed
2 H, R1 [2 P4 z2 } // indirectly by some other process).( e& f* A: g4 z9 v
3 W; ^4 S; L" X) C3 @6 j @7 M
modelActions = new ActionGroupImpl (getZone ());
+ N: }& l1 [' P% c+ N n- ?; u1 ?+ o: o/ f7 d1 F9 ]
try {; P P; H6 }! ~; V) @) J
modelActions.createActionTo$message
8 }/ M/ T* K0 r! u; R a, j (heat, new Selector (heat.getClass (), "stepRule", false));
" F, O, b/ n6 ` O4 q } catch (Exception e) {
3 ?+ I3 o# U5 q# y$ s System.err.println ("Exception stepRule: " + e.getMessage ());
' D5 ^4 v# N) v: A2 _5 k }
3 ~% w7 {4 O7 y8 n( y) i) j& }5 [! M6 q+ o- e
try {
$ ^+ w. F6 W- D Heatbug proto = (Heatbug) heatbugList.get (0);
& }. ?( m* f# v0 o6 v6 a3 ~3 A& X- [ o Selector sel =
( i8 T" l7 `' Q( @- [6 b new Selector (proto.getClass (), "heatbugStep", false);
s$ o# A* a W- {+ S* C actionForEach =
+ ?1 ?! Q( Y1 h# w! a. I modelActions.createFActionForEachHomogeneous$call
% _! ^2 }' [/ H! V% B: I. S (heatbugList,
8 u, Y8 \& S3 Z1 d+ U* ~) f new FCallImpl (this, proto, sel,2 c6 D; ?+ W5 R6 r9 W
new FArgumentsImpl (this, sel)));
8 r! I; G3 \: M6 R7 [& T } catch (Exception e) {# a4 c* G/ I# k
e.printStackTrace (System.err);
7 _* f& }( b! p6 F }& N$ ~6 z) u( ~, @& n
: @+ \0 e6 f M- g syncUpdateOrder ();
; \( ]+ B2 A3 _0 w2 X+ v; E7 B* C+ s) S# B, b
try {
, n3 P# u* Y+ U% H modelActions.createActionTo$message * I- Q x/ R4 `
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 \5 b1 W7 E( t: ]: }7 \5 L } catch (Exception e) {# ~- l5 q" }4 D: I' ?
System.err.println("Exception updateLattice: " + e.getMessage ());' m5 x2 V( |5 V7 e
}
8 B. l2 C# ?4 A# i3 L
7 v# G8 c2 Q; r3 E" H* U W // Then we create a schedule that executes the
7 {. D! u1 j3 a$ a# X4 i // modelActions. modelActions is an ActionGroup, by itself it
N1 v8 O% `( L4 J0 E // has no notion of time. In order to have it executed in
; `1 f, d6 y* O/ `# \& B // time, we create a Schedule that says to use the
' T# g; N: m7 n$ ]; }. P/ O p // modelActions ActionGroup at particular times. This
+ O& Z+ p6 c$ | // schedule has a repeat interval of 1, it will loop every% \0 I+ a: O9 @; w; m4 D, z( E4 e' j2 _
// time step. The action is executed at time 0 relative to6 F5 e2 A! w% ]5 y) N6 y
// the beginning of the loop.
5 L c/ o7 F, t* w# \; c: R$ h+ u' k4 S; `
// This is a simple schedule, with only one action that is
1 U1 V5 G# o# n- r // just repeated every time. See jmousetrap for more" m: { }8 _+ V4 B
// complicated schedules.
3 Y5 p- g3 M8 W8 h9 G* x2 z1 P 6 v* B. b* u; J8 |& F( k
modelSchedule = new ScheduleImpl (getZone (), 1);
3 m- c. K2 K1 N2 A9 {- s5 u5 ^, j" e modelSchedule.at$createAction (0, modelActions);2 r; @& O+ X) A- l9 h2 O) C
2 ]3 j9 z) y( G* O5 o
return this;
' _, E6 p/ h8 w: i } |