HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 M% b! f( v* c J+ p1 p2 h% g0 |1 R7 D: ^
public Object buildActions () {1 V4 r% t5 k7 e) v; p+ a( C0 x
super.buildActions();
$ i' B0 X2 r, l. U) ] |3 h/ A. m+ e/ \/ K) w( S
// Create the list of simulation actions. We put these in
u) {2 i* o$ W8 ?8 I* V" Z8 t' B // an action group, because we want these actions to be3 T" [8 I% r( W8 Y/ j4 f
// executed in a specific order, but these steps should
, O, k) {& ?, O( I // take no (simulated) time. The M(foo) means "The message
. K" o( L8 f7 F6 { // called <foo>". You can send a message To a particular, o3 h z7 O6 ^3 \" n
// object, or ForEach object in a collection.7 M7 {" Q* |7 y2 q% T# K. I0 t1 W
2 k/ V# A7 v/ V2 W* Z
// Note we update the heatspace in two phases: first run
0 N6 p5 Q E" o) e: Q // diffusion, then run "updateWorld" to actually enact the) }$ V+ Y$ Z1 \9 e
// changes the heatbugs have made. The ordering here is: q% T4 V3 {; H) G: n V
// significant!
3 B. d6 K/ g( n' @. U5 u , c/ [, m- N& h8 r& a2 T" h
// Note also, that with the additional
! k u" @! Q5 B% _6 k // `randomizeHeatbugUpdateOrder' Boolean flag we can
" T! i( j: D% z( f7 H0 i* | // randomize the order in which the bugs actually run3 Z) z1 G+ Z( `2 f# {4 D% L
// their step rule. This has the effect of removing any
. v d: p2 b5 i% ^# P% ] // systematic bias in the iteration throught the heatbug
- v& e: K% L; P7 l% H. F- R$ [4 \ // list from timestep to timestep! _3 K, z+ F7 V! @0 o- R( i4 n
7 D; j% ^% l* h1 z0 t4 ~1 M // By default, all `createActionForEach' modelActions have
% u6 g4 F; i; F+ z5 g" O0 h // a default order of `Sequential', which means that the
$ m3 O. m, u2 C // order of iteration through the `heatbugList' will be
0 E" ~2 h- B- a9 {. [' F6 ] // identical (assuming the list order is not changed$ @. T+ l( r9 i" \
// indirectly by some other process).. r# H6 m" N! S4 ^
7 l! Y( f& y: M, x: `
modelActions = new ActionGroupImpl (getZone ());
9 c, V* n$ a4 c# |
2 t- e" i' W* l6 s' ?; Y/ N! `/ h try {
9 z+ j- s2 v9 _# o, E6 ? modelActions.createActionTo$message& Q+ S6 ~1 s: q; |) H
(heat, new Selector (heat.getClass (), "stepRule", false));( h9 b' l }, [5 P
} catch (Exception e) {
2 @4 y5 q8 f6 G& D& U6 Y System.err.println ("Exception stepRule: " + e.getMessage ());& C$ s T5 ?9 Y, r( E) b9 V. n
}
/ g" \% L/ o& a6 t3 z3 z4 n! l: ^( O& v2 \
try {
* z0 e. z& B0 q( }+ H Heatbug proto = (Heatbug) heatbugList.get (0);9 ~4 l; z; @# W" z% y7 {% F3 o+ u# W
Selector sel = 4 b$ b( B+ `% E) [ j
new Selector (proto.getClass (), "heatbugStep", false);' v+ a8 A) B1 @# b
actionForEach =
?/ f& |* E8 G+ P modelActions.createFActionForEachHomogeneous$call8 H/ R- b ^9 E+ Y
(heatbugList,
0 T# f- E8 b3 ^+ a new FCallImpl (this, proto, sel,
$ s- [ p; q* R. h2 k! u new FArgumentsImpl (this, sel)));
6 v5 c4 @7 B6 t3 F5 x% D/ H4 j } catch (Exception e) {) n `* w# v+ s# A) \1 a+ |
e.printStackTrace (System.err); c+ F1 c+ H0 \: G
}' U& |& m2 V y) \
( {% h8 [# G- e- d5 |; y' \
syncUpdateOrder ();
) k2 c5 ` E8 I6 O0 f* w5 w% V( {. R& F4 Q+ W c' ^2 i
try {
1 c u# r, N) Y! R8 e+ k modelActions.createActionTo$message
1 ?. L: b4 b: z; ~: e (heat, new Selector (heat.getClass (), "updateLattice", false));; J' U" O* x) n0 @% p
} catch (Exception e) {
. C, c. P! s4 L" `+ f$ k System.err.println("Exception updateLattice: " + e.getMessage ());& t _7 R N5 p% p: P
}) B3 \; M! ]4 I
# G! r7 L5 S$ t% e+ l8 C' k' h
// Then we create a schedule that executes the
4 z4 ~) T- R5 a A8 G // modelActions. modelActions is an ActionGroup, by itself it! F0 B( o/ _3 ?9 P5 m- Q2 Q
// has no notion of time. In order to have it executed in
9 ]* X! d1 m. L/ t# x4 h, E. ^2 g // time, we create a Schedule that says to use the
1 J4 Z' v2 R& x // modelActions ActionGroup at particular times. This/ O0 v" z, R" z2 Q8 U
// schedule has a repeat interval of 1, it will loop every5 r) K; c# t3 r3 D; `
// time step. The action is executed at time 0 relative to
5 ], W4 F7 p! [ // the beginning of the loop.
# M3 X) O$ W$ o& s1 u: [2 g
* y4 ?/ I( B+ I6 w, `# F5 D // This is a simple schedule, with only one action that is
) D; E8 A; @" s* f, m) q) O // just repeated every time. See jmousetrap for more
0 M+ ~+ S3 F) O6 T6 y // complicated schedules.
- Z% v. F8 S3 f" w% g# f " E# z8 }/ K# U+ J$ y3 ?2 `2 i
modelSchedule = new ScheduleImpl (getZone (), 1);1 m# E: [8 {9 E/ T7 N s+ N
modelSchedule.at$createAction (0, modelActions);. O8 c2 a" ], u4 }0 D& `* @0 a( X
: w+ T: F: |+ J f c4 U
return this;
3 L+ B/ n5 y1 {( F, }( G } |