HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 F7 Z+ ~6 a* s: `# d* l V9 c% r* i/ n/ w$ _" N9 {: q
public Object buildActions () {3 U+ S$ N1 i k) E- f
super.buildActions();
2 D/ u$ a3 H7 P ^3 G3 _
( V5 X; e$ Q! } // Create the list of simulation actions. We put these in
4 K/ V% @& I0 ] k8 M // an action group, because we want these actions to be3 d* X J. W [
// executed in a specific order, but these steps should
5 W4 H% [9 T* `- h+ p: w // take no (simulated) time. The M(foo) means "The message" t! P8 m: t# h/ X- J0 \
// called <foo>". You can send a message To a particular
' u( u% R# {& c // object, or ForEach object in a collection.4 a' k5 ~. E& g) d; C2 b0 d
. K1 l2 e, Y8 _/ q" `
// Note we update the heatspace in two phases: first run
# l* Y- Z+ C( ^: M- U // diffusion, then run "updateWorld" to actually enact the, u I) B7 D4 ]- B7 N* @, v
// changes the heatbugs have made. The ordering here is
- _6 j5 G8 @: C# B" v0 J; Y o // significant!
) ~5 n5 m. J& L6 P / Q7 {& F7 R, @, U' U
// Note also, that with the additional4 P5 }3 c1 F" u" N3 [' a _( h) P
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ N$ a5 D9 l! c& g' S' \* S // randomize the order in which the bugs actually run e: F" b: W+ M8 D
// their step rule. This has the effect of removing any
6 p8 Q8 f/ K' X // systematic bias in the iteration throught the heatbug6 X# f2 d% n; B# m, W1 ~
// list from timestep to timestep: L/ L8 r6 D4 F! a
7 A" z4 c; @: P0 a6 d2 m // By default, all `createActionForEach' modelActions have* n" W+ h' M7 n: q. A0 ~% e
// a default order of `Sequential', which means that the: f6 [& j4 @" T' h
// order of iteration through the `heatbugList' will be
5 w' G5 u. p; \( | // identical (assuming the list order is not changed5 ]$ {$ S$ u0 y# g: A
// indirectly by some other process)./ N4 F" x4 g0 U7 m4 c4 C A: Z0 w7 B0 e8 M
: g, z4 b# T3 q9 R
modelActions = new ActionGroupImpl (getZone ());
, Z3 b; d: Q' A G# U6 A
2 k6 E: Z9 b f) E+ z* w+ y3 K( m try {2 P7 f4 ?. k0 A9 o6 C2 i
modelActions.createActionTo$message
2 U$ V) d8 E1 [7 f9 W (heat, new Selector (heat.getClass (), "stepRule", false));
^9 P( A. ^- U" E: X } catch (Exception e) { E# J- v E% _
System.err.println ("Exception stepRule: " + e.getMessage ());
4 U- u) C. v- l, i }
9 h, }( i* l% ~, h, i: L
$ f& ~) w" P! a) f3 L: w \ try {" @- f3 g- l3 o! Q8 d
Heatbug proto = (Heatbug) heatbugList.get (0);' ]; b$ H* t% M: Y. L8 I7 M9 e( p M
Selector sel =
( ^) g8 h$ K: Q$ ] new Selector (proto.getClass (), "heatbugStep", false);1 H: L$ y6 B4 j% C8 e @0 N8 a
actionForEach =$ n( z& M8 o5 e9 K8 u7 K4 X
modelActions.createFActionForEachHomogeneous$call
: }! I# C/ U" ~) C (heatbugList,1 W4 |8 v' i& z# n
new FCallImpl (this, proto, sel,
1 `/ G! V7 L9 ^/ M# J new FArgumentsImpl (this, sel)));
4 k6 i ~3 V r9 w# j } catch (Exception e) {( @4 k4 ~' C7 q7 T! r
e.printStackTrace (System.err);
9 F! g* G F$ O9 S4 {+ c }
& ?8 X- f% p' P 4 v; E* V" x0 z/ q1 H( Q
syncUpdateOrder ();
4 I* ~& t' \$ o0 h9 s F' ^3 _9 A0 l; A j9 A. k R
try {
* W5 I* R& ~9 C6 b X# | modelActions.createActionTo$message 8 W: E9 j6 i9 ]& o
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 S* e7 Z. A" r* k! @; x3 i } catch (Exception e) {
5 t4 q8 c. N' t0 y* C' a. J8 M( L$ G System.err.println("Exception updateLattice: " + e.getMessage ());- h# I3 a" [0 [1 k4 K
}
0 u" d, h: \* G# D ; C' G# s4 u8 D( h8 x8 J
// Then we create a schedule that executes the
! c% a& ]- H/ V // modelActions. modelActions is an ActionGroup, by itself it" e: @+ L3 v& l
// has no notion of time. In order to have it executed in, c* d% m* o' `) `1 o* i/ n# F
// time, we create a Schedule that says to use the
: h( x7 W V [2 N% V* N" z // modelActions ActionGroup at particular times. This
+ M4 e: H8 G+ O; P6 W // schedule has a repeat interval of 1, it will loop every
/ n/ h- k; h) c# Q+ U // time step. The action is executed at time 0 relative to
* y# ?5 W, Y7 q/ `) x( r // the beginning of the loop.* u( a& g A& ^. X9 x0 _
) N* A$ [5 D1 M7 ] // This is a simple schedule, with only one action that is
/ E# g/ ?8 v. Z // just repeated every time. See jmousetrap for more# z7 o( Z& W" l9 w
// complicated schedules.2 K1 D+ J1 B& @& [6 E
1 H5 j8 v" c# V$ v+ R0 f( g2 F
modelSchedule = new ScheduleImpl (getZone (), 1);
# Q3 c8 e1 J4 _: U M+ z modelSchedule.at$createAction (0, modelActions);. M q8 L: T$ Z
# m1 P9 y7 L3 }& O# }8 _" r/ S return this;
1 _( Q" ^% k P9 P( I } |