HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) h* ]/ w" d i* X0 M# i. H/ i( E! M
5 c0 Z% ]( X0 v6 H- I) I* a
public Object buildActions () {: `0 m# G3 T6 w; U$ `( X4 _8 @8 @0 P& V
super.buildActions();
[' J8 w- g% d$ J& ] ; ]* L. H P# x/ k/ Y' z; Z
// Create the list of simulation actions. We put these in' V: v4 c& V5 |$ f
// an action group, because we want these actions to be
/ ` u5 J5 K0 K: t+ T* S- ], R // executed in a specific order, but these steps should
( A7 u/ H7 ^8 g( X% r: Y1 h6 { // take no (simulated) time. The M(foo) means "The message/ g" d+ w1 y$ d
// called <foo>". You can send a message To a particular0 ]) l: d$ z" r0 c+ Y, k' [1 q0 a
// object, or ForEach object in a collection.
5 _3 {8 e" t1 o) x) w* f
+ |8 Y1 I4 H6 S6 S S/ K G // Note we update the heatspace in two phases: first run
2 Z( |" i+ {- \ L3 W // diffusion, then run "updateWorld" to actually enact the$ _+ [! Q; X3 D7 z: o
// changes the heatbugs have made. The ordering here is2 E+ m, S! I7 b$ z
// significant!! \! X/ O+ d: y5 q
2 R6 s0 s4 y. C! w // Note also, that with the additional
4 ^. k" D# F w- I6 r$ U // `randomizeHeatbugUpdateOrder' Boolean flag we can. Y4 X3 l! C' r1 d) X
// randomize the order in which the bugs actually run: V g8 \- N& N3 ]7 H# C0 F
// their step rule. This has the effect of removing any
+ v) O- ~- B- m5 s' Q$ Y // systematic bias in the iteration throught the heatbug
% d. i- B- w/ ~$ F. h; k( R // list from timestep to timestep& i* ^& g3 ~ r1 @* y
0 B) R7 P" F2 P- h- } // By default, all `createActionForEach' modelActions have! S+ `( j7 S' A/ G# f% v9 C" B
// a default order of `Sequential', which means that the9 M2 g4 W* J" l6 L, \
// order of iteration through the `heatbugList' will be, @' G" [& c V- D9 `, z
// identical (assuming the list order is not changed
2 t" a) r' u$ w/ ~. |2 U \ // indirectly by some other process).
Z4 J" H/ R& _, w5 d, u
. [; d" X! U3 W# W modelActions = new ActionGroupImpl (getZone ());
2 M' f _9 P" H7 y6 K
: h+ R9 F4 T, e1 @; i7 g' G# T8 G try { L6 E5 G2 L8 K# \/ |
modelActions.createActionTo$message
7 V6 {: d: f" ? (heat, new Selector (heat.getClass (), "stepRule", false));$ m& @* y6 _# x% f* H: u
} catch (Exception e) {6 t' k+ w$ C. O, H w2 L5 Y
System.err.println ("Exception stepRule: " + e.getMessage ());, @( Z. G$ B( ~5 p0 D8 {# {
}
d! w8 d, @* O5 _
# ?% G& a1 _' ^5 U! p3 V; |' u try {
) l( l* i+ i3 K5 s Heatbug proto = (Heatbug) heatbugList.get (0);
. Y' A" o S: h: S Selector sel =
5 o! {5 |5 l$ w t new Selector (proto.getClass (), "heatbugStep", false);
3 d7 E3 B6 V! Q- X. \ actionForEach =
2 P4 u7 c2 E3 L, \& g modelActions.createFActionForEachHomogeneous$call
) _( R. b2 |+ E3 H (heatbugList,1 X0 i. Z& [: ^2 ?) A; U, }2 Z
new FCallImpl (this, proto, sel,4 k3 M1 e+ s0 N- U9 F
new FArgumentsImpl (this, sel)));" y+ r& v* u _$ O- G
} catch (Exception e) {
* Y. P6 ~( W ~0 d6 f3 s# y e.printStackTrace (System.err);
' s* G2 K# y. k; K }
9 U, l; @4 m0 R! l: P % n2 }* @. Z' w; ~0 e. _
syncUpdateOrder ();0 ~: Z& m4 N" m
& \9 y7 D1 K, ~$ N
try {
& p: E9 u G' p" n/ K0 v$ [6 A7 E6 Q modelActions.createActionTo$message ) F/ n" a! I; U4 S' w3 ?8 F9 w5 G
(heat, new Selector (heat.getClass (), "updateLattice", false));
: U2 c* d1 m2 u& x } catch (Exception e) {
$ S, F }' P5 R System.err.println("Exception updateLattice: " + e.getMessage ());1 H- _& Z( }) V3 a. B ~7 m) H
}. n) F. X6 |! j+ g* Q* s# k/ P) o9 e# }
$ l/ M- ~& _4 [5 ] // Then we create a schedule that executes the
% _" Z7 A0 E+ x // modelActions. modelActions is an ActionGroup, by itself it
, {( ?/ F9 Y7 f6 l. N5 d. N' z // has no notion of time. In order to have it executed in$ M1 q7 y# m |8 Q# z
// time, we create a Schedule that says to use the: Q8 x" E$ h% c" k
// modelActions ActionGroup at particular times. This
8 Z( `% ?2 W% A. w // schedule has a repeat interval of 1, it will loop every2 w' O5 ]! L' X2 v# W; G/ k# N
// time step. The action is executed at time 0 relative to
: V$ V) z* K6 M, _( p8 e // the beginning of the loop.3 N* ^, X# K& e) J0 J
/ ^% e- S r; ^5 i // This is a simple schedule, with only one action that is
# W" }$ S) b7 P# H# k5 Z // just repeated every time. See jmousetrap for more
& t( _* U" }$ {& n/ n // complicated schedules.! [& h p4 }% C5 P. U
' I1 ~2 B4 ?/ `2 T2 S0 b/ _
modelSchedule = new ScheduleImpl (getZone (), 1);! M7 I6 u: H8 ~& o
modelSchedule.at$createAction (0, modelActions);- d( }) U$ ?* B, l, d' u
! n W0 K: K1 M return this;) W( T$ L* Y, [/ l
} |