HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ R: E. W) @1 `+ G# s3 ~3 ]3 e' ]+ z
public Object buildActions () {7 C+ c5 P" d7 ~& Z7 [, q
super.buildActions();8 R8 H3 w2 G$ n, ~
4 [7 F2 x1 `, q( u( E1 ?4 r
// Create the list of simulation actions. We put these in) @" @" P# i- W! P4 c
// an action group, because we want these actions to be: R$ v' i6 ^& a5 k4 q( ]& W3 L2 n
// executed in a specific order, but these steps should6 J( j% D- Q7 Z" h9 A
// take no (simulated) time. The M(foo) means "The message
" ^7 R1 E0 N8 U // called <foo>". You can send a message To a particular; B3 T/ t' A$ ]' C3 C
// object, or ForEach object in a collection.4 R; V7 d5 Z2 q! B
& S1 z, X4 b) d. `0 q. t4 k5 T
// Note we update the heatspace in two phases: first run' J6 ?: Q$ ?, w1 n+ t! S6 ?# q. Y
// diffusion, then run "updateWorld" to actually enact the( N2 G. v2 ~9 i8 b6 j- j
// changes the heatbugs have made. The ordering here is
: ~: [6 t$ ?( Y/ x // significant!
' w% {( b# D h Z1 B, @- ~6 u 7 }$ O+ m4 I$ N
// Note also, that with the additional
9 f- J# y5 |( d [6 F+ H: w // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ K9 A- s& E/ t8 K // randomize the order in which the bugs actually run
6 I' Y1 }3 a% a9 ?! a+ y // their step rule. This has the effect of removing any9 D2 a0 d+ T9 |6 q I
// systematic bias in the iteration throught the heatbug# `6 h6 ?" X! y5 }
// list from timestep to timestep
; W. _- A0 n4 t& F6 _1 A. B4 Q
# m$ ^- P( z6 V/ \: m+ A, e& K // By default, all `createActionForEach' modelActions have# Q. |! g4 Y3 _) [$ n! K: {
// a default order of `Sequential', which means that the6 k6 L% v( T9 n m C! u
// order of iteration through the `heatbugList' will be
. @3 ]: a$ D! }! ?% _7 j" a8 L // identical (assuming the list order is not changed
: b# v$ W3 h. [' ~' ^2 ^+ I // indirectly by some other process).4 P$ [! I, c2 R* K* H6 V
* F& w6 y$ A* ?4 n: r/ `; I modelActions = new ActionGroupImpl (getZone ());' A9 a8 y: v) u$ \
& N1 R9 B; z, C" c0 H
try {
4 m/ q; X: B" u! s2 ^ modelActions.createActionTo$message
, B+ X! W% x# _1 g (heat, new Selector (heat.getClass (), "stepRule", false));
7 w& I: ~( P3 v, M$ V# f } catch (Exception e) {3 {/ ^% y- |/ W
System.err.println ("Exception stepRule: " + e.getMessage ());0 B ]: V, ?* N
}$ O: b( T/ R7 p" L$ E$ b6 F
( D! D% L ~* D; y5 K8 M3 t
try {) h- f- I# H2 f+ q, L
Heatbug proto = (Heatbug) heatbugList.get (0);1 c1 O- s. g K2 l: R f- L1 z9 q
Selector sel = 0 N9 J# [6 s5 U$ q) \
new Selector (proto.getClass (), "heatbugStep", false);; A* m0 s) N6 `' r2 I: L6 ]. s- `
actionForEach =6 Q, L/ D) N) ^' a: x, @
modelActions.createFActionForEachHomogeneous$call$ J6 r; {) R' _$ |, A0 t
(heatbugList,4 x/ _: q4 {+ z2 k* J1 U# p* x
new FCallImpl (this, proto, sel,% b2 ]( M" x* V9 C
new FArgumentsImpl (this, sel)));
1 w* _& v5 V4 j! h0 i: ?. F } catch (Exception e) {
( a) R7 `3 E$ q& `& u e.printStackTrace (System.err);
/ ~& U, B3 [7 C: Q( C8 H. l1 V }2 N! U" k& _! s& h* r9 c. ]
! b4 p) C4 S) h# c syncUpdateOrder ();
: j4 z6 `# J$ Z$ L
3 C) f( z+ c, I6 O try {, T+ X& q7 |0 {3 I5 |5 Z
modelActions.createActionTo$message / G3 U/ W3 b+ L; i; B7 M
(heat, new Selector (heat.getClass (), "updateLattice", false));
" |, T N* e6 u. {8 h+ C } catch (Exception e) {& p4 `2 [$ t ]/ u$ A% P
System.err.println("Exception updateLattice: " + e.getMessage ());, h6 l* H; z8 f S
}) D* A' X+ z+ H& [6 X5 i
% f1 C5 o( q, c5 Z" x
// Then we create a schedule that executes the h7 J( S7 P+ {2 A3 k* O" H
// modelActions. modelActions is an ActionGroup, by itself it8 U6 g* E+ }9 T' T& ]
// has no notion of time. In order to have it executed in8 N* v5 \* s( ?! C1 O& _) m; X
// time, we create a Schedule that says to use the
- O7 o( g) [/ b N( r // modelActions ActionGroup at particular times. This
0 ~% e& t! [6 e$ B) X; g' `$ d6 Z // schedule has a repeat interval of 1, it will loop every
. B5 w* p0 S: [6 n // time step. The action is executed at time 0 relative to
4 l, ^6 @. A6 x: N // the beginning of the loop.
( D. m2 g4 n1 R
" c- G; ?, W I% P$ o // This is a simple schedule, with only one action that is
/ q Y1 b+ x4 ~! A2 e% b // just repeated every time. See jmousetrap for more' d' N0 r2 I6 y
// complicated schedules.. L, R* T: L( p& P- c6 e$ u' G
- [3 b8 I7 h% w) J0 \4 R% x1 _ modelSchedule = new ScheduleImpl (getZone (), 1);
' u6 X2 C) L i3 e modelSchedule.at$createAction (0, modelActions);* a0 q. ?6 ^& f) w" X
. I1 d2 m; [4 x" ]4 }
return this;* V# t9 ^% m/ y+ O$ D
} |