HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) b7 f8 z8 G2 s) ]; I
' L( J2 p) T( }5 Q/ P public Object buildActions () {+ Q+ U& |' x+ i2 z2 A; H1 l
super.buildActions();9 y$ d) K" ? k7 q
) c' Q# Y: k2 A8 A9 f* I( c" x // Create the list of simulation actions. We put these in
2 P' U! S3 w# o( m; ~- o // an action group, because we want these actions to be6 m1 k @; \1 e i5 R
// executed in a specific order, but these steps should8 V7 `+ |+ W+ c2 [5 G
// take no (simulated) time. The M(foo) means "The message; C @/ H% t5 B3 g1 h0 W" {+ q
// called <foo>". You can send a message To a particular
: V h7 }6 @. L0 u // object, or ForEach object in a collection.
0 C0 b C+ |0 @& Y1 p% K . h$ g$ g( V8 ~8 q2 G
// Note we update the heatspace in two phases: first run
* H5 }" u. |4 l& V // diffusion, then run "updateWorld" to actually enact the' p" \$ X. ^* O& |/ a
// changes the heatbugs have made. The ordering here is
/ t2 D6 N1 E! \ // significant!- o& I' Q# o# V# [
6 B- B/ S# I1 L% E5 ?3 }- S& y1 g
// Note also, that with the additional
. J# h; w8 L2 G' J // `randomizeHeatbugUpdateOrder' Boolean flag we can
% |3 C9 |0 Z" T* I7 y" f/ h) f // randomize the order in which the bugs actually run. h/ R/ P) X7 q
// their step rule. This has the effect of removing any5 J: o% j# w7 a
// systematic bias in the iteration throught the heatbug
8 P/ o [% A5 a( e // list from timestep to timestep
e; r# G& m9 m/ `- B- o W 4 N- V- N+ ]0 M" D" P
// By default, all `createActionForEach' modelActions have3 ]% T; d' V/ \
// a default order of `Sequential', which means that the9 D3 E. x$ c8 S! `! f8 k% O
// order of iteration through the `heatbugList' will be! }& m& y" [* Q% C+ D$ Y$ F. P
// identical (assuming the list order is not changed
! Q8 I( H+ ^3 e+ V: S { // indirectly by some other process).
4 _' U! ^, P/ }! A $ U4 W! o8 J: e) H# X6 [7 S
modelActions = new ActionGroupImpl (getZone ());$ c: E' p2 ?2 ]5 O; C: i
x+ c/ W8 Y2 o
try {* c; f1 R( Q+ R
modelActions.createActionTo$message
' x; H8 W$ _7 N/ g* K1 J (heat, new Selector (heat.getClass (), "stepRule", false));
& l3 j A N: j2 Y& B } catch (Exception e) {1 a: \7 W+ W+ S! G6 ]
System.err.println ("Exception stepRule: " + e.getMessage ());
6 U7 G% ]" @! N }5 y3 [3 C# T- D9 Y4 n
, m" ] j- g& C/ q+ G7 Q" |
try {
# o5 ~5 |/ s, j" l) h6 W! w8 f Heatbug proto = (Heatbug) heatbugList.get (0);* G2 \' Z! Q9 R& g3 P
Selector sel = 9 @( i9 Y( c' Q0 e& q
new Selector (proto.getClass (), "heatbugStep", false);
) j8 F9 J1 M5 }7 G( T actionForEach =6 g; a, Z7 S' Y, }$ X/ J
modelActions.createFActionForEachHomogeneous$call
5 m% \% R3 |. z$ x* ^4 ? (heatbugList, b1 {/ i9 m4 h1 A0 h! }! I
new FCallImpl (this, proto, sel,
2 _4 H l; K0 U+ s new FArgumentsImpl (this, sel)));
8 ?7 t/ {* p. f+ B' M; Y3 ? } catch (Exception e) {7 p- U9 v d+ J& `* I3 T
e.printStackTrace (System.err);
9 H9 M; N$ v; T. l }: x7 o6 Q8 B* H7 _' y) q' t
% p w& a) A. M9 H syncUpdateOrder ();) F o: G: I7 \+ L& H1 W
1 R& y4 d4 ]( B* R try {" C4 Y1 K6 ]2 m) I S, g# j
modelActions.createActionTo$message
5 Z* h; G j; g; H (heat, new Selector (heat.getClass (), "updateLattice", false));( O( e# N! i6 ^) m( k; y% m: l5 ?' {$ M
} catch (Exception e) {
( h# H0 d, N; N0 @: Q3 N System.err.println("Exception updateLattice: " + e.getMessage ());& v" U$ E, w1 z f$ o- l: v0 k0 _
}+ X5 ], P1 ]6 @7 c _/ y( _' _. U
! b, V9 g( j% G
// Then we create a schedule that executes the
% E! ^, H+ H& ~8 W // modelActions. modelActions is an ActionGroup, by itself it
% | H: I$ p n. V" N R9 u2 P // has no notion of time. In order to have it executed in
0 z+ C0 ^7 q3 T" k; | // time, we create a Schedule that says to use the
. F7 O9 S' Z6 s0 G" T/ b# {: B // modelActions ActionGroup at particular times. This* ]- A& m4 g. E( k$ p5 L# W! P
// schedule has a repeat interval of 1, it will loop every
! [; Q1 b" p& [1 R8 a8 y6 S // time step. The action is executed at time 0 relative to
2 E) C. w. q `. @ // the beginning of the loop., p( m! v S! D6 Y/ ~7 L+ j
( ~; ]' G$ \1 p/ u% W
// This is a simple schedule, with only one action that is
( R) f$ t, G8 D3 I6 _, m" { // just repeated every time. See jmousetrap for more
5 ?) x! w- v u. C; C0 W% n // complicated schedules.& N4 e. B$ Y2 F) Z( R' B
" ~0 P3 W) d; b* U
modelSchedule = new ScheduleImpl (getZone (), 1);
: ?7 d& O& o: h modelSchedule.at$createAction (0, modelActions);/ ~9 E6 L4 ~1 I% }4 Q
" o: V7 X7 Y# C) D4 w& ^/ c
return this;$ F$ q7 h: x# _2 ~' C
} |