HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 a- Z( X$ z' K9 M! C
j' b# l2 G( N# l s0 M. J public Object buildActions () {8 J6 x8 c" r( X7 [' o
super.buildActions();
. ^ w8 H8 V( Q* @+ q
2 F/ ~9 _3 T# K // Create the list of simulation actions. We put these in! f8 Y* W& r! n7 {+ A
// an action group, because we want these actions to be
/ o/ W8 J1 ?' H( x // executed in a specific order, but these steps should
2 G- o* @& t% G7 r- a, a // take no (simulated) time. The M(foo) means "The message4 b' ~ V5 a r# k
// called <foo>". You can send a message To a particular
7 }4 ?0 z/ {5 g; i // object, or ForEach object in a collection.
! E6 n3 E+ q+ M3 O5 Q : ]% i5 w; K1 g- j$ n4 W0 Z
// Note we update the heatspace in two phases: first run' T* Y! \7 p- `( _' G
// diffusion, then run "updateWorld" to actually enact the
! Q# _$ l- }! A$ d' {0 ~2 p // changes the heatbugs have made. The ordering here is& F( b$ p' S7 C$ F
// significant!
* ^; y* s! X' N" }, v1 | " R8 X# f1 k4 S, p0 F8 s# n
// Note also, that with the additional
" [7 e) ^/ J- d V( O/ \+ Q$ { // `randomizeHeatbugUpdateOrder' Boolean flag we can
" {+ \0 T6 I0 v- A8 Z! e* t3 I; t // randomize the order in which the bugs actually run
) g9 u/ _3 e. B$ N/ H) T // their step rule. This has the effect of removing any
) j& f0 ~4 x0 f: `0 B // systematic bias in the iteration throught the heatbug5 E2 t# n/ i7 B2 |
// list from timestep to timestep+ t% U7 C9 T* m' r
; d- K. P! w H# h+ g, w2 a2 R
// By default, all `createActionForEach' modelActions have+ C2 \+ O& p0 J. W$ [; j: d( g
// a default order of `Sequential', which means that the f1 A: a! F* B( {, G; \
// order of iteration through the `heatbugList' will be% ^/ t9 f( i6 i5 N2 ]+ x
// identical (assuming the list order is not changed
5 D. i1 }1 {9 X4 g // indirectly by some other process).# P6 ~- ~% S! E! G/ a7 C: k! D
5 ]* p- {! e4 l+ x7 m0 R9 s modelActions = new ActionGroupImpl (getZone ());6 H+ j" [! j: p# J
% h/ _4 V' `7 q% n& f K( v5 Z6 |
try {) o ], [% |- c0 C4 G
modelActions.createActionTo$message4 I+ j" A! v2 d. f N: _+ E
(heat, new Selector (heat.getClass (), "stepRule", false));% c) T0 L% p5 ?' b
} catch (Exception e) {5 g$ a1 v0 o. T- {; }6 X
System.err.println ("Exception stepRule: " + e.getMessage ());, T! ?4 ~) |7 g3 |8 Y. x# f
}
7 Z; A4 B* h% M' Q
7 d$ z: s# H+ Y w+ U& u try {( E: Q1 q) h+ _8 `! c7 D
Heatbug proto = (Heatbug) heatbugList.get (0);6 Q* U8 \+ t+ i4 B; Y9 z
Selector sel = . N8 f! z2 f! T* i- K
new Selector (proto.getClass (), "heatbugStep", false);
# h( Y$ R# `1 C% y# y6 \ actionForEach =, t' ~) V) U! V/ E! f
modelActions.createFActionForEachHomogeneous$call
% l8 I3 U! z" x5 @) k. R0 I (heatbugList,
8 Q; S2 G/ M" {' N7 m+ b. F new FCallImpl (this, proto, sel,
' \( s# @- K3 w$ k3 r9 _ new FArgumentsImpl (this, sel)));2 g- U: W: q+ d
} catch (Exception e) {" _% T# i. W8 F9 F4 q: f$ z
e.printStackTrace (System.err);
4 g: h' h' X4 k: c }) y- m1 |1 S; }6 {
- W1 T) f' q& h' {5 S0 t3 H
syncUpdateOrder ();
. x9 U$ T8 Y9 B6 [7 @
4 L+ M, p' X! A2 S. L try {: R% o! `9 B) h; y! P* X0 d
modelActions.createActionTo$message
, `# | h/ x9 _. z% p9 F: z7 R (heat, new Selector (heat.getClass (), "updateLattice", false));* l1 j' I+ w5 I# \* j& H
} catch (Exception e) {
7 Y( {# y$ a& P System.err.println("Exception updateLattice: " + e.getMessage ());$ w, y1 K! j% y
}0 G/ b' ^: p, Y9 u6 N$ k8 D
2 x# w1 Z5 U5 h8 D- [" J. q: \5 c // Then we create a schedule that executes the
+ m: r3 d8 ?; I# ^$ ` // modelActions. modelActions is an ActionGroup, by itself it7 }9 ~: v, L6 E* Y* E
// has no notion of time. In order to have it executed in
+ d" O' ^, u! u9 `7 Z! Z: l // time, we create a Schedule that says to use the" S0 y+ c4 l, n
// modelActions ActionGroup at particular times. This( m+ N4 g, p% J/ Z n
// schedule has a repeat interval of 1, it will loop every
( B: `: [, ?. [6 p // time step. The action is executed at time 0 relative to
3 C3 `- o% q# q+ S9 |$ y- I3 j // the beginning of the loop.& d% a7 K* \0 Q- Z( T
7 C" |2 \( V) H, N, @- {+ ~, r // This is a simple schedule, with only one action that is
& s8 l u0 i4 F, e3 [% D% D // just repeated every time. See jmousetrap for more
% w, `+ ?5 U. _ // complicated schedules.
: _9 J% H. `& y+ S$ S4 i
( z) H3 T1 ~/ ?* B! m. [. Q modelSchedule = new ScheduleImpl (getZone (), 1);9 H5 `( K' U w. `0 J, [" Z1 F) q
modelSchedule.at$createAction (0, modelActions);
& y M6 e8 A9 w& ]; S# b
: a" u% t" m+ p+ I& M+ g' k return this;1 p @" h7 I/ K8 j1 I
} |