HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% B* Y* b$ M9 ?9 G% u6 Z. ]/ l9 K
7 Y1 w. J- V5 e/ { public Object buildActions () {- t" e4 l3 I1 j
super.buildActions();1 L" N" E, O( E
% Y/ f* T) Z) K/ _; n0 |( R
// Create the list of simulation actions. We put these in4 [7 R/ t% s R1 \# }5 \) B
// an action group, because we want these actions to be
% e1 a5 L9 R1 C& ^; O. k: W0 X // executed in a specific order, but these steps should9 p- z* x0 D U, }
// take no (simulated) time. The M(foo) means "The message- N( t5 ]& H( i/ |
// called <foo>". You can send a message To a particular- _: X6 A) c5 D0 c7 ^0 h( c
// object, or ForEach object in a collection.5 l) b m+ W5 b9 Z/ B; e5 h; w
8 q8 ~: u2 T5 Y; ` // Note we update the heatspace in two phases: first run( i3 b+ A6 j3 u- U6 M
// diffusion, then run "updateWorld" to actually enact the: L& t# C, }/ O3 q
// changes the heatbugs have made. The ordering here is
& o0 t3 t7 Q& m5 ~% }0 q // significant!5 ~0 w) w& H9 @+ W5 o
' N6 A0 B9 [, w- W3 G) A6 a // Note also, that with the additional
7 E' M6 G& V! F4 c# _ // `randomizeHeatbugUpdateOrder' Boolean flag we can
. I3 j1 |- l/ [9 q3 i3 E // randomize the order in which the bugs actually run
8 A; c" _* E, b$ P* ]" P, A& u/ F // their step rule. This has the effect of removing any+ Y% W8 Q8 l# u
// systematic bias in the iteration throught the heatbug
% f Y3 I% ?+ ^. B/ j/ W1 m // list from timestep to timestep
, q, y4 b3 f! G% i 9 O: Q% `: T3 S0 ^$ m0 n0 y
// By default, all `createActionForEach' modelActions have. I4 ?# ]9 o- T% k0 D) @% L( ^
// a default order of `Sequential', which means that the
- y3 h$ r) h' x/ L // order of iteration through the `heatbugList' will be5 _" X+ S& y6 B
// identical (assuming the list order is not changed8 p. c( S6 H6 x% A3 m$ y) q
// indirectly by some other process).
: M: g' C, V7 r5 b
( q" A( k4 H; p) E- G- y" P( n+ e modelActions = new ActionGroupImpl (getZone ());
M8 B$ [" p- T9 V$ \3 ?3 B+ Y+ a
try {
3 S, E# ?) A: W, {0 r f modelActions.createActionTo$message; u7 M; ~) G9 {# z6 @
(heat, new Selector (heat.getClass (), "stepRule", false));. Y0 i% M) e8 n/ x1 K, \: G+ w
} catch (Exception e) {
# b. C6 _3 F4 N, L* H1 T System.err.println ("Exception stepRule: " + e.getMessage ());
3 O" v2 a/ |5 L- J } B* B- {# T {: L H
$ H- s1 {6 y; x
try {& S1 V$ `* q V9 D0 Y2 f
Heatbug proto = (Heatbug) heatbugList.get (0);
- q' c/ X/ n1 V8 j Selector sel = 6 f! [6 ?$ o! I s6 W: T
new Selector (proto.getClass (), "heatbugStep", false);$ N( J: V5 }0 I" T# V
actionForEach =. n9 n3 I& Z0 @
modelActions.createFActionForEachHomogeneous$call
( D( C5 \4 t1 c( n R3 V `; S (heatbugList,: U8 G4 x* ? l4 ~9 w; @8 L
new FCallImpl (this, proto, sel, ~3 ^+ D5 X$ z: }: W
new FArgumentsImpl (this, sel)));
- v: ?' U8 G1 u! X. o$ A7 e } catch (Exception e) {
1 z- x8 L B% T( H e.printStackTrace (System.err);
# N; {8 x x9 b- r9 O }0 Z* a0 f b3 f1 J
2 M% u( _; x" P( v syncUpdateOrder ();1 w7 u; p! J9 b- U6 M, @
# r0 N/ z. {; z9 }1 ~ try {
& P; @& l0 B, B' D" m' R4 Y modelActions.createActionTo$message
7 S' ?* ~$ M: P; K+ z% p3 b (heat, new Selector (heat.getClass (), "updateLattice", false));
6 s6 _+ d* W! k/ S) c8 S- N } catch (Exception e) {) o3 l; G3 _- J. M# @
System.err.println("Exception updateLattice: " + e.getMessage ());
4 z3 r/ V/ d4 K* ^% |, B, w, c }
4 e7 N" c. s R, \3 A; a) i" h5 m4 c . P1 E4 B" v+ m9 a
// Then we create a schedule that executes the% D, H! e: B% s
// modelActions. modelActions is an ActionGroup, by itself it
7 P+ c. a! Q; A. ?% _ // has no notion of time. In order to have it executed in% ]! @" J; j; R( R% l/ v2 G
// time, we create a Schedule that says to use the
0 o( ^3 C4 H! @6 }$ ?6 X // modelActions ActionGroup at particular times. This
8 `1 ]$ z1 E8 T' M# `$ ~8 h // schedule has a repeat interval of 1, it will loop every. i$ k+ h2 S. k! |, B
// time step. The action is executed at time 0 relative to# w7 |8 E- T3 N' K8 H8 S- H. I; I
// the beginning of the loop.
; @7 e# H+ ?* V' u1 L. v0 F4 i% H( g9 J
// This is a simple schedule, with only one action that is
3 S( K/ N9 B+ T9 b3 {. I& S // just repeated every time. See jmousetrap for more% P4 z2 H4 \( B; V
// complicated schedules.
2 G, U o' ?4 B 0 G6 d% @ n/ b5 V0 g. x
modelSchedule = new ScheduleImpl (getZone (), 1);
: p4 }7 h1 Y. I4 ^ b5 } modelSchedule.at$createAction (0, modelActions);
/ F* K0 s3 x; Q W
4 j Y, n7 o" @/ }0 l return this;
' s, x# X, @$ n% C) V! k } |