HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ i2 D, h8 S) }+ O D9 j8 { g3 B+ |3 b( ]* ]5 w! b! J
public Object buildActions () {" K) M/ z6 j8 j- x: ]0 b# y; @
super.buildActions();: `8 t0 s* N, \3 ]# v7 w
+ _+ V/ `0 T9 b$ w/ o2 {
// Create the list of simulation actions. We put these in
2 X: y5 r1 y& k" j! t9 T // an action group, because we want these actions to be
, d5 l2 Z) i& g2 N0 R // executed in a specific order, but these steps should' f5 u: S b- v1 e! `0 ]
// take no (simulated) time. The M(foo) means "The message5 {( v' C" o1 p1 b
// called <foo>". You can send a message To a particular
! N( K2 v5 |# ?- Z# p; R // object, or ForEach object in a collection.) k$ W7 }/ O3 U# X
# m% k+ N- A) ~2 n, j // Note we update the heatspace in two phases: first run: C7 n; z; T5 A7 k) _* I" \
// diffusion, then run "updateWorld" to actually enact the7 H- r! j5 l4 @
// changes the heatbugs have made. The ordering here is
. M( W2 `( [9 m5 g7 h) z; I+ X // significant!7 d# q$ ]5 r5 q, d
/ b r. W# M3 T% v9 v7 j b' k3 V
// Note also, that with the additional- I& P* ]4 l6 q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: D6 Z S/ S3 W0 f/ b5 z! [- F" i1 [ // randomize the order in which the bugs actually run
5 j4 O n4 H; r // their step rule. This has the effect of removing any
6 X" T8 |) J6 k // systematic bias in the iteration throught the heatbug
- [ Q3 [. `( d // list from timestep to timestep
" I6 ~2 I: G% ?0 U! O7 ~ # Q: C( A1 P P/ G; B& d$ G
// By default, all `createActionForEach' modelActions have
2 P' |3 `; g7 D/ x. H" V // a default order of `Sequential', which means that the
' w& e4 s( G+ Q* D( s$ _ // order of iteration through the `heatbugList' will be9 N- x# @( U& }% P6 @
// identical (assuming the list order is not changed3 F0 x8 Q4 D# b+ I' u; t8 {* c+ x$ N
// indirectly by some other process).! L0 K6 r/ n% T6 i! E- z
4 H3 E* j+ V1 ]! C4 F, s
modelActions = new ActionGroupImpl (getZone ());
; L* u- \& @9 f6 ?& `/ g9 g
4 ^6 k0 ^0 C$ t try {) M7 B! B2 B. Y1 w
modelActions.createActionTo$message
d6 C! e, t9 ^: H1 d (heat, new Selector (heat.getClass (), "stepRule", false));
2 p" G1 h; k( x } catch (Exception e) {, ^2 ~7 d3 N; x- Y) o2 h$ T% w f5 ?
System.err.println ("Exception stepRule: " + e.getMessage ());4 M9 l0 c2 ^" k" Y# p
}
! n0 o9 J3 Y6 G0 V. `% V/ f! ~+ b4 _/ [1 i
try {$ D5 b* x u" N; n6 G: y
Heatbug proto = (Heatbug) heatbugList.get (0); M% B' s( V+ Z4 f+ E* P
Selector sel = 3 k% T6 g1 W; y$ [/ G8 G
new Selector (proto.getClass (), "heatbugStep", false);& D( a3 s) G: p. b. e/ |
actionForEach =
. x( o! d! w9 }% V. P V% R modelActions.createFActionForEachHomogeneous$call! \# X& A' J/ O. f$ R7 p, M& \, g
(heatbugList,( V7 v% R7 R0 \0 U2 @
new FCallImpl (this, proto, sel,
+ y7 k4 E: `" z7 j3 y/ l- e4 P } new FArgumentsImpl (this, sel)));
& U) h4 ?. |0 |8 ] } catch (Exception e) {* g) O4 K6 g% ?& w* d9 I, c* s
e.printStackTrace (System.err);
: p' b- n3 B' k* J, t; Z' c9 O }5 \( w3 T, b1 W# e3 h/ p
8 a1 Q1 y, ?+ B1 R) T
syncUpdateOrder ();8 r3 s9 D n4 g9 X. |
0 B) F2 V+ Z4 X* [8 {
try {* u- Y0 V* I, \4 |; g
modelActions.createActionTo$message
- C) X8 V. K- [& O# y (heat, new Selector (heat.getClass (), "updateLattice", false));+ z/ B3 Q: Y- |5 V- w
} catch (Exception e) {
1 @! o# u( g' j; W/ L- ` E System.err.println("Exception updateLattice: " + e.getMessage ());
/ O; i7 ^8 }# ~: R }' o/ t# u: G" u5 T
& ^( S, e+ q- @ t) g' E* [) y
// Then we create a schedule that executes the
4 G& v/ {0 i% _; m // modelActions. modelActions is an ActionGroup, by itself it4 j9 u9 I' o1 P
// has no notion of time. In order to have it executed in
: m- E8 {2 A2 e, k/ s // time, we create a Schedule that says to use the q0 f% g# @$ V* y6 X
// modelActions ActionGroup at particular times. This
" A# q2 A3 K9 v // schedule has a repeat interval of 1, it will loop every
" O8 N( S) ?- l% t* B4 G9 ] // time step. The action is executed at time 0 relative to* R3 C+ }% _$ o0 w
// the beginning of the loop.) @, a! s) @- T9 x
: k1 J. ]8 x* O // This is a simple schedule, with only one action that is8 o" e. {+ A6 L' S
// just repeated every time. See jmousetrap for more# o z, a7 E+ D% P4 n
// complicated schedules.
' j- d1 f* s& k1 ~* s7 }0 ]0 F2 l 1 j& h. ~$ s5 N8 ^' J3 B, j
modelSchedule = new ScheduleImpl (getZone (), 1);7 }# T+ {) ?0 t
modelSchedule.at$createAction (0, modelActions);5 d7 Z' F/ F* \
* Q1 p# l z! F: F% C- i& [7 Z) V
return this;
, k9 S$ |2 O& t. d. | } |