HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' i; K" U+ p5 p+ s8 d6 H1 A, ^1 N$ n; y5 I2 l& d
public Object buildActions () {
/ e' @& \8 T) @3 V# r, U super.buildActions();
; M& C6 Y( H( c+ v5 n( @0 a9 ~; ~9 _
5 |$ g8 ?6 e8 N // Create the list of simulation actions. We put these in Y& D/ q* c4 }2 l1 J
// an action group, because we want these actions to be# l, z; o& p# R
// executed in a specific order, but these steps should6 U0 i; C: V, x) d! T
// take no (simulated) time. The M(foo) means "The message% [" N; [) ?8 ?
// called <foo>". You can send a message To a particular
* j2 z, h3 u) Q9 T // object, or ForEach object in a collection.9 m/ x' C4 L# C1 v. a& p% G
# f- |+ `5 J8 @( K# P6 f" a
// Note we update the heatspace in two phases: first run
! s1 u. x; I2 s7 y% W' Y5 ~; e // diffusion, then run "updateWorld" to actually enact the
0 K+ m3 q' Y" z; O2 R. ] // changes the heatbugs have made. The ordering here is
3 A3 {5 g6 j5 C& P; n8 ^ // significant!' L& n; Y5 e1 l7 M$ K# }1 F
& ` h# a& i' y3 C9 f/ a# j // Note also, that with the additional$ H% C5 q0 [, A
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. R' `. Y. l. a- M* \ // randomize the order in which the bugs actually run
5 c& w9 g$ h. u2 l( Y // their step rule. This has the effect of removing any
P5 H( @: b5 h" L$ Q4 F6 D // systematic bias in the iteration throught the heatbug, \4 E. }" H: m' j
// list from timestep to timestep! f* U+ _2 _" M
; ^5 K0 {3 u8 v+ u, U7 m( W5 r; O
// By default, all `createActionForEach' modelActions have
) |' M: f/ ^/ g, i // a default order of `Sequential', which means that the
+ M) s4 r9 q2 P+ F/ S [ _6 C j // order of iteration through the `heatbugList' will be/ K# K" [3 l% m2 v) J
// identical (assuming the list order is not changed' `% B6 T0 F$ f6 y4 l; _
// indirectly by some other process).
* u {5 X: ~( S
: @. B. g6 H$ a9 c5 H% _ modelActions = new ActionGroupImpl (getZone ());
1 H) T2 h3 u E. V+ I- [ x. r0 z" [1 G
try {) U5 p: b K- h d0 }; G+ {
modelActions.createActionTo$message
9 q* c1 v, m h/ e. _ (heat, new Selector (heat.getClass (), "stepRule", false));
W% y3 Y$ i" w0 f5 n7 G- @ } catch (Exception e) {( {& ~$ L! y% [# C
System.err.println ("Exception stepRule: " + e.getMessage ());! E* g: I# E& h. N( s( ]
}9 n2 s8 u9 T; r: r4 g
! t$ y1 ^7 C- Z2 J3 I: l: U try {; U5 V! C" T( @" [& S: M" [
Heatbug proto = (Heatbug) heatbugList.get (0);
& p0 B) K1 d3 j* t' W' q% _ Selector sel =
. g/ g% M3 R6 a" s- B3 ] new Selector (proto.getClass (), "heatbugStep", false);
- c1 L1 u' }. i' G' M" m actionForEach =
3 f& X7 l$ p1 o% W$ L6 ? modelActions.createFActionForEachHomogeneous$call" H; f3 J2 {% P$ g# P6 J" W/ t
(heatbugList,5 M! C# B) V6 u! r ^
new FCallImpl (this, proto, sel,
5 K/ D d& e& N new FArgumentsImpl (this, sel)));
! O. t2 C7 A9 c S7 j/ D } catch (Exception e) {
: T o& i1 D# F. w' g e.printStackTrace (System.err);
: U6 c4 J0 {4 _2 S0 [ }
# L5 T$ M' v1 u1 o; i( F # U# m2 M2 A2 [8 |! c9 E0 G
syncUpdateOrder ();
# P% `+ G0 Q* E1 Q- a4 R; n( o# K+ j& m, R, _$ k/ y- u' \! g
try {
+ g t& p! p+ C1 P; Q modelActions.createActionTo$message
1 ?5 [, F) |8 H6 c' v (heat, new Selector (heat.getClass (), "updateLattice", false));
5 C1 G \* ?8 Z+ B1 [ } catch (Exception e) {
$ {4 S0 g- n# R9 K6 j3 G System.err.println("Exception updateLattice: " + e.getMessage ());2 R* r8 Z; t1 H& D7 O
}/ r+ x5 h( }9 K$ Y4 D7 |# @
" s T! V. M" W // Then we create a schedule that executes the, Z& t Q: O8 C$ j! P: K
// modelActions. modelActions is an ActionGroup, by itself it" L" X- a5 U3 ?/ c! w2 a
// has no notion of time. In order to have it executed in
; e" z6 _5 G3 O/ ^& {2 U // time, we create a Schedule that says to use the
2 r/ L& k/ T1 r# }, D // modelActions ActionGroup at particular times. This. J* Z; S. J# ]
// schedule has a repeat interval of 1, it will loop every
4 Z6 z6 n7 D1 f // time step. The action is executed at time 0 relative to
* q2 n9 I1 _: m" Q // the beginning of the loop." x0 ]2 H6 ]4 e3 a% H) V/ X' L
; f! c" k$ V- H5 k t! c* O0 Y. ^
// This is a simple schedule, with only one action that is
! ~& Q, i7 Q p6 b3 {$ W- g // just repeated every time. See jmousetrap for more
6 [$ q! [9 h; {' O( c // complicated schedules.
9 u+ Y5 }* z4 c r5 c, `/ o
# @, V' v2 E7 u8 _8 e modelSchedule = new ScheduleImpl (getZone (), 1);
; p6 F7 M' h6 O& h) }/ u0 u } U modelSchedule.at$createAction (0, modelActions);5 c7 Z, R5 ?8 `9 n( x
/ I! F9 U( ^ j3 E1 R7 @" I. s: e return this;% _3 a% H. J# N# R* {2 {
} |