HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 ?( I" n& r. P6 _/ C/ A$ h
2 T7 _" r7 U; P' L. Y public Object buildActions () {% B' A: A8 h. Q! I2 a
super.buildActions();1 T; p% H( ~3 _) `
+ K$ v3 y4 B* S
// Create the list of simulation actions. We put these in g& g3 N. V/ W5 H! F$ m6 _: J; h0 \" E
// an action group, because we want these actions to be$ T% a n' u' V2 J$ F
// executed in a specific order, but these steps should/ C9 _( N1 ?4 F: H2 G5 I
// take no (simulated) time. The M(foo) means "The message' e# i( _1 Y% M8 B2 B
// called <foo>". You can send a message To a particular
1 V2 _: o8 d C1 D: e // object, or ForEach object in a collection.
7 f& }$ C7 |$ J: w 5 }, X2 Z5 w8 A0 a9 A
// Note we update the heatspace in two phases: first run" r) I; ]& `: e0 I- ?' ]; B4 E
// diffusion, then run "updateWorld" to actually enact the' C. _8 I# p( ?* p/ D
// changes the heatbugs have made. The ordering here is
' u# g Y- b: D% z* L // significant!1 v" f6 m- Z9 h5 u
" C e" {0 k5 U: {- x1 A2 H+ U // Note also, that with the additional# G1 ?( o- }2 a, `# O6 l6 }
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# ^) ^$ ~ m. E5 h" c // randomize the order in which the bugs actually run$ H- y, f7 W* p5 b |+ e: g
// their step rule. This has the effect of removing any1 x/ |) q1 _' G
// systematic bias in the iteration throught the heatbug
! n5 l" ~' u# e: ]7 x# U/ w/ w& I- s // list from timestep to timestep
% _& g0 k3 l1 i' K0 l6 Z 6 I p+ F, P% y7 J; u9 E( K
// By default, all `createActionForEach' modelActions have( K3 O) a! B( r+ W3 o; _
// a default order of `Sequential', which means that the
4 c V1 u' F# P1 o // order of iteration through the `heatbugList' will be, h% X, {8 d4 D% P: D2 q9 u
// identical (assuming the list order is not changed
9 o ?! `# F% M9 f( M8 v% D // indirectly by some other process).% J5 u7 U* Y/ s. X7 E0 O
! m9 O8 ?% K1 c) M2 C* n) ?
modelActions = new ActionGroupImpl (getZone ());0 C, Z7 @# i) P( x; n6 z
; n! d2 _- k: }) U' D8 j try {$ n! X r' k2 J0 }! f
modelActions.createActionTo$message
1 ]- s U' Q% Y& E+ w' x4 q (heat, new Selector (heat.getClass (), "stepRule", false));
+ m" U9 }% c! N( {3 ?: I4 l$ d } catch (Exception e) {
* G% w1 ^( T- L4 [- i- E. j System.err.println ("Exception stepRule: " + e.getMessage ());1 G5 ^& }7 L8 q0 L9 x- O; y
}
5 E, d5 q3 x' W) o: t; N
( [. H2 C9 j1 i' K3 R6 W! A try {" O$ N/ t+ u4 t
Heatbug proto = (Heatbug) heatbugList.get (0);9 P9 [7 W/ n, ^7 P3 H6 W7 W
Selector sel = ( p6 s& ~# a1 O! r8 {
new Selector (proto.getClass (), "heatbugStep", false);
1 i* v/ N/ i6 @0 Q. a/ V actionForEach =
- t, D: x6 _/ g% _' d* H modelActions.createFActionForEachHomogeneous$call
- N8 q: L" Z) w4 G: o5 |, X; C& t (heatbugList,
: R! u4 j$ m8 k( _( ~- ], Q ] new FCallImpl (this, proto, sel,
; S9 W' B2 l* E& ^% M4 K new FArgumentsImpl (this, sel)));/ x5 U, {& F/ l
} catch (Exception e) {* @0 L- ^* [ D6 q* _
e.printStackTrace (System.err);
7 O8 ? t4 r/ v d3 O7 S }
& a+ l% O; m$ c" B" {* c& c 0 b2 Z/ x. B7 C3 X/ g9 Y
syncUpdateOrder ();
# O( E3 w3 s* Y9 t" ?0 H$ n1 m5 P+ }% g2 [
try {
9 u5 U, n0 y; H$ f4 P modelActions.createActionTo$message " U8 B7 z! ]4 ^9 G; `, O
(heat, new Selector (heat.getClass (), "updateLattice", false));7 Q+ \' l- a0 ?$ x# q8 s
} catch (Exception e) {' w7 ^8 L, w" E& s( x( M% u& Y
System.err.println("Exception updateLattice: " + e.getMessage ());
& \, C Q) G2 |$ ^ }) _$ B* ^2 D+ f3 L% M) e* N
4 M9 u5 o+ F( T8 ? // Then we create a schedule that executes the
/ [0 [/ ?6 A/ s, T // modelActions. modelActions is an ActionGroup, by itself it# }! h, [0 M. [3 T# e# f% h. g
// has no notion of time. In order to have it executed in
/ s6 D5 a' M# q4 |; K. d! g // time, we create a Schedule that says to use the) C+ f; J: T; K0 N4 b) n
// modelActions ActionGroup at particular times. This! g& y, v q( P- P
// schedule has a repeat interval of 1, it will loop every; l6 Y# f" O2 H
// time step. The action is executed at time 0 relative to
- V4 `, \% R ^& l // the beginning of the loop.
" x$ k4 v" J1 O+ K; C |- o( z% I/ z0 I/ b! K& o7 B
// This is a simple schedule, with only one action that is" Z1 n7 F5 |2 h5 O
// just repeated every time. See jmousetrap for more
& h" t. W) N, M8 H& K // complicated schedules.
, ~ m. V" l, x1 C2 s3 M
( g1 Z b2 V2 i2 e% _8 \% t modelSchedule = new ScheduleImpl (getZone (), 1);
$ l i5 i' w; W modelSchedule.at$createAction (0, modelActions);
, H1 F% q" Z4 E* f" v# } A9 @
$ v! L8 R) c4 ~: i. B7 m% I# o return this;4 I' A& J1 l3 R J, j
} |