HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( V4 `% ]$ @; a
1 R2 z( l! }6 b; x
public Object buildActions () {
/ t5 f# i# a# U+ }6 ^) Q1 Y super.buildActions();
$ u3 i. q, @: m0 W7 n 5 T% U5 z j$ O2 l0 F) j) m
// Create the list of simulation actions. We put these in
$ S; C3 L* T* _& f6 n, c // an action group, because we want these actions to be
9 e) i# `+ r, S) A# H( E // executed in a specific order, but these steps should
3 R S9 u6 @( }+ i" l) t' u // take no (simulated) time. The M(foo) means "The message+ L+ W* _! O+ X3 z9 q8 Z6 w
// called <foo>". You can send a message To a particular7 o! k' h8 K+ d( |0 M0 o: c+ C
// object, or ForEach object in a collection.
* x {# |. k/ n" A' u
& y0 d; P- k: p // Note we update the heatspace in two phases: first run# v$ n6 F& E/ `$ P
// diffusion, then run "updateWorld" to actually enact the9 l5 ?/ d0 t5 C) X
// changes the heatbugs have made. The ordering here is
2 c2 k' b% [6 l // significant!* q% y% e* p) g0 W
8 [# Y0 J) @, A7 d% u3 F' S // Note also, that with the additional
; E: a& ~. s' N! ^4 e; h9 I' N // `randomizeHeatbugUpdateOrder' Boolean flag we can, A3 S g* r* A# ^2 x; V0 N; S
// randomize the order in which the bugs actually run
, L s; @8 J6 p# [, Z! H // their step rule. This has the effect of removing any
& ]6 J9 B1 z, A1 ]' m w! S // systematic bias in the iteration throught the heatbug6 e; T$ W6 P2 S! t) `# N
// list from timestep to timestep& `% K$ J" R+ B
2 F8 s. v" h, B, C& c+ J8 W6 R // By default, all `createActionForEach' modelActions have
2 ]% \' D# `9 d( M // a default order of `Sequential', which means that the+ z; q$ u- S2 A$ ^1 \! p
// order of iteration through the `heatbugList' will be) Y& A* {5 {( m; B: A
// identical (assuming the list order is not changed
, F2 N5 w* ?- |+ {" j // indirectly by some other process).
" Q' P4 n( m# }0 x
$ P' `5 f- ?' M* E# y7 _/ g1 D modelActions = new ActionGroupImpl (getZone ());5 h2 N$ q# U H0 m# \" n* X8 X' m
! y' N' ^( C! n2 ?. w2 [1 Y try {
+ Y G8 Z5 b" T9 q; z \3 A* ] modelActions.createActionTo$message
. s" h& G7 f, @( ` (heat, new Selector (heat.getClass (), "stepRule", false));
+ u; h: v& D r* U3 P H* B } catch (Exception e) { X5 R% P2 i, ]7 }
System.err.println ("Exception stepRule: " + e.getMessage ());
) c. x8 y7 R. A/ _. x5 c' L }1 o3 m( f7 k1 j' K! G7 [
# o* _2 ^3 ^0 @& }+ K try {
/ l' ~ o! l+ L9 N4 e Heatbug proto = (Heatbug) heatbugList.get (0); C: e- E7 g3 Q, a
Selector sel =
0 A4 D$ B3 m3 i new Selector (proto.getClass (), "heatbugStep", false);
* r5 B% n: \* L/ O5 s actionForEach =
1 g S H7 e) b: ? modelActions.createFActionForEachHomogeneous$call7 [. Z* I0 O, q7 @* i, r
(heatbugList,
9 z* x% Z5 Q" z new FCallImpl (this, proto, sel,
$ v# p! p( l; O8 i& n7 ` new FArgumentsImpl (this, sel)));- v: z7 W1 ^; A# e1 L# M
} catch (Exception e) {7 `( s V& F- A5 y; e0 P1 ^6 ]
e.printStackTrace (System.err); a7 z2 @# B% p# a* _( E
}8 W* o; j# M r
% Y" y2 w: _0 P
syncUpdateOrder ();
% `6 j0 C" {9 o( O; }. ?4 n0 m6 [5 f2 X& M. {: ]. x
try {
n/ f7 L9 @4 r+ [% d* f" H modelActions.createActionTo$message " _+ {! s/ A0 |6 C: w$ v0 U7 q+ G
(heat, new Selector (heat.getClass (), "updateLattice", false));" f s0 R; @' R0 w y* c
} catch (Exception e) {8 N+ U0 `& b8 w7 S# U3 N* l; |
System.err.println("Exception updateLattice: " + e.getMessage ());
) `. K3 Z8 c/ K, a' l }1 r6 A. e3 h$ P0 N" g3 H: D
" ?! M' W3 ]: A, P" q, N' ]
// Then we create a schedule that executes the
! y: |2 l( b5 s6 }- S // modelActions. modelActions is an ActionGroup, by itself it
0 M/ U6 R& X& k$ o; e5 e8 y // has no notion of time. In order to have it executed in# D' m- a% P/ H8 N+ u
// time, we create a Schedule that says to use the. |8 h& @3 m, w
// modelActions ActionGroup at particular times. This& e" @9 h: l H9 m
// schedule has a repeat interval of 1, it will loop every
# G+ j3 d0 g7 W* V // time step. The action is executed at time 0 relative to
9 ^2 G% {6 L' S // the beginning of the loop.
& K, ^7 V) |: {" V5 v; x9 f9 {% Q( ~* h9 ^ S4 x" Y
// This is a simple schedule, with only one action that is
+ f. L/ [3 ?& i2 k3 }- _* _9 ^5 o4 g) s // just repeated every time. See jmousetrap for more+ _% v, Y$ P5 e3 ]$ r. T( e
// complicated schedules.
! P" `8 T, L* p$ P! ~
0 v& |7 J3 U% N( ~% K modelSchedule = new ScheduleImpl (getZone (), 1);( i/ @* R7 Z8 o4 C7 n
modelSchedule.at$createAction (0, modelActions);! T. u* u8 h. ~9 J$ \& j" f
* \8 t) A" t" N! u/ | return this;
( w! n0 Z. |2 O } |