HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 ~% D# t; B, n5 M& l
. B6 w }( x, N public Object buildActions () {
4 @; L) Q7 k1 V; ], z+ J super.buildActions();
7 K4 ^6 D( ?6 _5 ]
/ g; B7 u5 A2 b m, `" P2 N' ^" B' O // Create the list of simulation actions. We put these in8 g$ ]+ S; ^0 |" S1 D6 A" E3 _
// an action group, because we want these actions to be
3 ] k: y, ]: |9 p) U // executed in a specific order, but these steps should
$ `( Z6 Q5 }" B, y8 X // take no (simulated) time. The M(foo) means "The message
" ^2 i/ A- g0 u3 P, _7 |+ x // called <foo>". You can send a message To a particular: Y( A# ^( ^6 f
// object, or ForEach object in a collection.
) i+ r$ S0 _1 k P/ t) t % W# H: k$ |: n; q* n, _+ r
// Note we update the heatspace in two phases: first run
- X1 D7 `9 |( C* K& g1 i // diffusion, then run "updateWorld" to actually enact the
/ T' p$ h) v$ n( N' k# `) l // changes the heatbugs have made. The ordering here is; O8 j9 d. Y1 l) | D) P/ C
// significant!2 I0 b' Z& |9 \
) Y& h# M- [: }2 s/ r# m
// Note also, that with the additional
0 E! _. X/ z. i* z // `randomizeHeatbugUpdateOrder' Boolean flag we can# a. k m+ ]2 ^
// randomize the order in which the bugs actually run
9 s. [% Q5 I0 @5 ~ // their step rule. This has the effect of removing any7 V4 a2 d- B" N6 V& N
// systematic bias in the iteration throught the heatbug8 T* v2 v C1 W8 j( v' b
// list from timestep to timestep
% C2 ?; Y9 O' |. ~( W/ j% L0 K# }
5 b) ^$ E9 u& I" a$ x, V( Z x // By default, all `createActionForEach' modelActions have* |5 ]0 e1 e5 i; ^, G$ U }
// a default order of `Sequential', which means that the
# V# \! `! ^6 w' p9 p! x+ M8 h N3 } // order of iteration through the `heatbugList' will be
$ U$ w# }3 N/ u( A8 F // identical (assuming the list order is not changed: ]- J/ C% L% \
// indirectly by some other process). Z) T6 F: ^9 O& N \& y
* p4 O, x# _ v1 W5 L! J# x3 I
modelActions = new ActionGroupImpl (getZone ());
8 ~7 k, _' [6 L. Q) N. e3 x! S* l6 _! `5 W5 S4 ]( p) W! J
try {' f7 ^- a. g* ?7 q3 y6 l4 z$ |3 D) b
modelActions.createActionTo$message- _6 q. C6 t/ n4 ~# T
(heat, new Selector (heat.getClass (), "stepRule", false));
8 b6 t. f( w% Y/ ?/ h9 F8 x } catch (Exception e) {
/ `$ J$ p8 u5 j, R3 _& |6 M6 ] System.err.println ("Exception stepRule: " + e.getMessage ());
# p6 E @, v# Q0 N; w7 N }/ w8 V: x1 |8 J
3 X5 k( z2 B* z7 {+ l! b6 E: I try {
* @; ?& Z% a" R$ a* W Heatbug proto = (Heatbug) heatbugList.get (0);, h7 z! U' f/ r0 \ {
Selector sel = & d: ?& x' ^& V4 [. M4 q8 u! j
new Selector (proto.getClass (), "heatbugStep", false);" V F) M9 J# Q! |3 `+ X2 r
actionForEach =% `: ?1 V; T- T" F% j! C
modelActions.createFActionForEachHomogeneous$call) U7 i3 N4 u& w, g
(heatbugList,
' f: o1 }; k* {3 [6 j" l5 C2 l W new FCallImpl (this, proto, sel,6 L) l$ I" \. y" P- u; t
new FArgumentsImpl (this, sel)));
7 k" ]8 H/ Z8 B: n' I1 T } catch (Exception e) {
" Y( l; W. I8 `" | e.printStackTrace (System.err);. a* F3 b+ j9 Q* K2 z
}. a( N/ i7 j; e% n+ N$ h
6 o$ s, ?* `- K5 O9 X( v1 T% _ syncUpdateOrder ();% m# I$ g+ E) d" }, ~3 s9 K
7 K" E+ r' l7 }: y4 |. x try {- y8 r) X8 F. W0 o
modelActions.createActionTo$message : H o: m/ q% `; `. T" e3 d
(heat, new Selector (heat.getClass (), "updateLattice", false));
) @/ \6 Z+ x3 J4 }8 j+ k } catch (Exception e) {5 P5 I( ~; L) d
System.err.println("Exception updateLattice: " + e.getMessage ());/ a @9 X+ w4 g: r0 K* j. M
}
, {. ]8 J' B& {% M 5 W4 {. ?+ }) s6 g+ U
// Then we create a schedule that executes the: D1 @4 |! B+ _6 Y k0 M# r; J
// modelActions. modelActions is an ActionGroup, by itself it
$ G$ V$ s; d F( i: o! N& I9 I: h& ~ // has no notion of time. In order to have it executed in
0 d2 [! h; B; c" S* E ~* ?1 c // time, we create a Schedule that says to use the
2 [( \2 f+ E- T) P3 s // modelActions ActionGroup at particular times. This
4 ~/ e2 n, A7 C% l- i K/ G // schedule has a repeat interval of 1, it will loop every% f2 G/ ?3 W, \& b5 [) r" |& `
// time step. The action is executed at time 0 relative to: r3 Q. Q7 y1 K ]
// the beginning of the loop.
0 x) M2 p$ G) K, m! f! w- [. X J2 L3 s; f9 t6 Y! {
// This is a simple schedule, with only one action that is
6 M% ?3 s/ w+ N" j4 t7 ^ C // just repeated every time. See jmousetrap for more
7 g4 R6 a9 H; B // complicated schedules.! H: Y# u3 D% O2 O4 c) |
; _9 o8 m0 A9 \0 D6 d modelSchedule = new ScheduleImpl (getZone (), 1);
* p% O3 {2 v% p2 p' I/ m+ n0 ` modelSchedule.at$createAction (0, modelActions);
) _9 X- O. N% a: f' j, J* V 6 p* R0 N" a( l) g B, z1 E
return this;: K" _! x. q' N9 t( A+ j0 s* x" M
} |