HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 S- Y5 P& [5 c* `
5 ~; `( K7 [, h5 R: t* X- \) B
public Object buildActions () {
" ]" d7 S# S, f$ P& k5 P super.buildActions();9 l5 x I) C0 `, O
( ]6 F2 y( V3 {* M8 p // Create the list of simulation actions. We put these in
+ G! k$ A9 s5 t3 l // an action group, because we want these actions to be
( e- C' d: G. ~* a! _/ R // executed in a specific order, but these steps should
( {& b. G6 G0 ?2 _ // take no (simulated) time. The M(foo) means "The message( V* ^) K; _/ B( ?' w4 b8 ]* \, Q
// called <foo>". You can send a message To a particular$ i; T! H9 _- R. q1 ] S) Q7 n
// object, or ForEach object in a collection.
0 d4 a9 p7 n C9 ]8 P0 H1 J" S ) U5 z# t1 A, x4 k$ J( f
// Note we update the heatspace in two phases: first run4 p, A+ S6 @5 @8 h
// diffusion, then run "updateWorld" to actually enact the5 v, [+ V# P4 g: @, i
// changes the heatbugs have made. The ordering here is
+ \8 B- v( `& W9 o // significant!
( z, Q# ]8 M6 G5 M0 E% }& F & E% S' p! `6 p; V9 N# K" E
// Note also, that with the additional
& E2 K) n7 W( i$ @' ? // `randomizeHeatbugUpdateOrder' Boolean flag we can
) p4 q) v1 c& p // randomize the order in which the bugs actually run
9 U% y: x# D* b! y9 X) @ // their step rule. This has the effect of removing any; B& y+ g5 B! f! H# W. u: y
// systematic bias in the iteration throught the heatbug% N# P6 Q6 _( G8 F
// list from timestep to timestep
* Z2 v8 r0 i( [/ @9 R
9 O. E4 `) F+ \4 s0 A" H5 J B // By default, all `createActionForEach' modelActions have
: p g; m9 p. `# Q' ~ // a default order of `Sequential', which means that the
& O/ ^) r0 U$ x3 y5 v, g" P, k // order of iteration through the `heatbugList' will be
) r. C7 h1 r0 J* e* x4 R; E, W // identical (assuming the list order is not changed7 g- H3 D7 t! X/ r$ @8 [: t1 q
// indirectly by some other process).
2 S7 M6 F U( R/ A
' T7 W' |% U2 r: B* {; E modelActions = new ActionGroupImpl (getZone ());: l& F6 P9 Y2 T7 S- J
; @* n2 g' N7 J3 P3 ` try {/ B! B& m+ S$ u
modelActions.createActionTo$message
4 h! {+ B1 c+ I. v1 a- Q (heat, new Selector (heat.getClass (), "stepRule", false));3 K' E& g" j* j* m
} catch (Exception e) {
8 X- i7 {+ i& } System.err.println ("Exception stepRule: " + e.getMessage ());, D0 p) B. o1 m3 c
}' `1 c0 G% }* t, ]( M
4 U" D, M3 a" x" ]$ o try {
0 a# i% l5 d, L$ M6 w" I Heatbug proto = (Heatbug) heatbugList.get (0);
% u# \7 C6 E7 l4 E( [7 T$ {4 f Selector sel =
9 p" P- B' n1 V2 e* N new Selector (proto.getClass (), "heatbugStep", false);9 f: q6 d6 M$ K: @& C2 ~
actionForEach =
( Q: l8 [7 f4 \% Z5 P& H modelActions.createFActionForEachHomogeneous$call5 p" a e g$ Z, K9 M
(heatbugList,
7 `$ Q& u+ T# Z4 l3 p: d new FCallImpl (this, proto, sel,: v3 U7 i# K5 S5 F% R8 ]" ]7 S
new FArgumentsImpl (this, sel)));
* P/ `5 b1 f" o } catch (Exception e) {
4 v+ Z+ l- F* F7 U. v1 _: s+ k4 e e.printStackTrace (System.err);1 u# P; | F: t' V
}6 D% ^$ C8 l4 x; p- U
" U- ^! K/ b6 J5 j: V) k syncUpdateOrder ();
$ E { k7 u" d7 H: k7 {, P _# _( B
try {
9 l% k" x3 j7 J; N; l8 \9 L modelActions.createActionTo$message
9 T( Q% I+ ~4 V- ^' Q (heat, new Selector (heat.getClass (), "updateLattice", false));% s# o3 z C" T% P9 m* l4 H2 g! e, W
} catch (Exception e) {
$ z( E. O: V3 k9 |7 }6 K System.err.println("Exception updateLattice: " + e.getMessage ());
$ p' M h# ^; m }
" }- i9 i- o! W$ S- Q. ]+ _$ x& Q
1 }" d/ K- L2 W; d% b& f5 }7 B% f9 t // Then we create a schedule that executes the
6 Q9 ]4 K o. `/ d // modelActions. modelActions is an ActionGroup, by itself it
; @" L1 H7 k1 C4 L* C // has no notion of time. In order to have it executed in9 A% _# }& \# W# o! H7 T0 \
// time, we create a Schedule that says to use the% W5 T$ X. K3 c7 M
// modelActions ActionGroup at particular times. This
V* p/ c/ D/ P: y: o // schedule has a repeat interval of 1, it will loop every7 |6 R! U% U+ E! G' G/ s, {0 a
// time step. The action is executed at time 0 relative to
3 D/ _/ ?! p& g8 k% K) L1 _ // the beginning of the loop.
- A, D: b' I2 H1 }0 J
$ R& i8 B0 `9 S // This is a simple schedule, with only one action that is
; c! H) X E! ^ // just repeated every time. See jmousetrap for more
, S( S8 g L% a' S' `7 ?* q* ~ // complicated schedules.
; O7 L, [# _/ j4 j6 u/ t( K( z4 P
3 ]0 S' L1 p% n, I4 c J: } modelSchedule = new ScheduleImpl (getZone (), 1);
! @+ n" E" r* g% T( n7 w modelSchedule.at$createAction (0, modelActions); b) T3 W0 N# }- r$ @
0 j% ~" P3 V# K" l+ B7 \! q- i
return this;& g- b7 p- \3 a: X4 u: D
} |