HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 T8 `2 b$ ?$ W' e) k. ^
" K% \+ O7 n& p$ [% k public Object buildActions () {
+ P$ L. M+ j4 P$ N" u" K super.buildActions();5 d4 G! [1 y1 K! U$ o$ i
$ W6 p7 d ^% d, I4 ~9 ~2 n // Create the list of simulation actions. We put these in
; A6 j; F- T" i N0 W5 H // an action group, because we want these actions to be
6 r* A+ c. p! [4 W; ~ // executed in a specific order, but these steps should4 i1 H2 x* @3 k! a/ _: w- b2 a
// take no (simulated) time. The M(foo) means "The message
5 \% o. ~- y6 o) h // called <foo>". You can send a message To a particular6 [) T* n2 J. ]1 C2 `3 W6 ?
// object, or ForEach object in a collection.; b% J) O7 M5 V4 c
* {1 y) t- _8 v1 Y: U% J
// Note we update the heatspace in two phases: first run
0 [6 ^0 o/ F1 I; C; ? // diffusion, then run "updateWorld" to actually enact the6 F8 O, t$ W# G* N& Y7 x6 M
// changes the heatbugs have made. The ordering here is" ^1 U" p5 A0 y2 d
// significant!
* J* s q: y# v4 y& ^* R 8 e. B1 ]5 r+ V$ ]
// Note also, that with the additional, G# T& F' l; i- q o
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' L Z( p6 l& ~4 E7 \3 `% r5 s& y // randomize the order in which the bugs actually run
! l1 [( e( y; `" V // their step rule. This has the effect of removing any
3 f. q, X. K a+ v0 ~2 ]* {3 @6 I // systematic bias in the iteration throught the heatbug3 ? _' C2 f: g# Y1 j
// list from timestep to timestep
+ ?2 Q7 n) l4 ?8 b( C + H0 q& d8 |* a: A- x* Q
// By default, all `createActionForEach' modelActions have
7 P8 L) X* m8 ~; R! h3 A2 w // a default order of `Sequential', which means that the
/ k& W3 O" L& x. r0 b. E // order of iteration through the `heatbugList' will be
Y& J0 _0 f; h2 P/ T! A [ // identical (assuming the list order is not changed- `0 H0 B/ `( i' ] I R
// indirectly by some other process).
) c' P- n7 d3 H- V" \6 w ! q& m2 O; r, T5 X' r( X' e
modelActions = new ActionGroupImpl (getZone ());
) n$ I" Z1 Z5 E2 E
5 j2 x9 G0 h* T( S5 v try {; E3 o4 k# i' z
modelActions.createActionTo$message9 V1 y$ {, m3 q! m1 o" \: v$ Z' p
(heat, new Selector (heat.getClass (), "stepRule", false));
& K$ c1 p! S. L) ` o" @ } catch (Exception e) {/ W5 H ^& A7 t/ W4 e1 e( y0 p
System.err.println ("Exception stepRule: " + e.getMessage ());1 u, H: [ ?( Z" c/ u& a3 J
}& }' B/ ?! P. _% r
9 J3 ^6 T3 T s& Z try {9 Q, z3 h( Y/ ?+ I
Heatbug proto = (Heatbug) heatbugList.get (0);" x. H4 \9 I* u- d6 v
Selector sel = % [' Q6 { \5 p4 D( v# C* f
new Selector (proto.getClass (), "heatbugStep", false);, x3 Q! J/ ^7 ]$ j
actionForEach =& ?/ X, }1 g+ H( C
modelActions.createFActionForEachHomogeneous$call
0 [4 w. l9 c: f/ Z2 c (heatbugList,: h9 c( o! I2 q# W% G
new FCallImpl (this, proto, sel,0 p3 h6 w* R- A# J& D0 A8 s' ]- F
new FArgumentsImpl (this, sel)));
( n2 M( a) m( |4 ^- o/ g D1 f K } catch (Exception e) {% n# l s1 H ?. Y8 Q
e.printStackTrace (System.err);& ?2 D: L$ _, k" s$ n! Q% X; r8 G
}. A7 }% S1 h0 k
* @2 L- r8 r( W
syncUpdateOrder ();
, E' |& X9 w4 N# Q7 ]% Q) x0 o, {" A, n$ m4 v% z) M' }
try {: z* t, {. V" B( a7 U: f9 ^2 b
modelActions.createActionTo$message 1 ^; {' E v' {4 c% n' g1 V
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 }& A" T" y: ~0 u$ _. L/ ~ } catch (Exception e) {
. [9 {2 w+ K2 x7 ]" U o# {5 w System.err.println("Exception updateLattice: " + e.getMessage ());
2 E2 @/ l/ H$ g2 M4 Q }) \ Q9 P3 Z6 u( K
# y2 v! J- T# @5 A) S
// Then we create a schedule that executes the3 y' t3 V3 m4 _: p/ q
// modelActions. modelActions is an ActionGroup, by itself it) }! {+ b/ B/ L5 W: _; q
// has no notion of time. In order to have it executed in5 F! }9 `4 i9 R) k
// time, we create a Schedule that says to use the
8 Y9 q9 p- X: Y# J6 B& w // modelActions ActionGroup at particular times. This: Z ~, c* T7 c+ D: J
// schedule has a repeat interval of 1, it will loop every) d* M: r. J, D7 d4 Y. T
// time step. The action is executed at time 0 relative to
- S# D+ Z1 ]9 H0 \. F) h // the beginning of the loop.
% t% z. A4 c* Y- V# \6 L" Z1 L( I: _- `7 X0 P$ r
// This is a simple schedule, with only one action that is$ K( \% \3 b, n1 A$ W7 I1 B
// just repeated every time. See jmousetrap for more% C" F- R2 n6 ^2 N
// complicated schedules.
3 { K* G7 q# j( F' D; M9 G8 E ' j2 a& q0 Q7 K" O5 ^
modelSchedule = new ScheduleImpl (getZone (), 1);( ]/ y4 F: ~' P( j9 J
modelSchedule.at$createAction (0, modelActions);. r# Q( |$ H" q) d1 r
: e2 a ~8 r$ d4 U* I- E return this;2 x7 Z' _: w5 C8 L
} |