HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: f' k; m. v4 [1 g1 Y0 j" w. F
0 u! h; ~8 ]* v0 A" |6 U- {
public Object buildActions () {3 ?' N. G* _. x7 F8 h1 ?; u4 ]) W7 b
super.buildActions();& T' e9 J5 h7 M; a" n
4 Y4 f: x4 z( W* i
// Create the list of simulation actions. We put these in
. Z1 L( i8 y5 f // an action group, because we want these actions to be
1 w2 @" |6 g3 R" k% j+ W // executed in a specific order, but these steps should
$ L5 F, G% }3 a // take no (simulated) time. The M(foo) means "The message
% ]& O4 E) l7 w% j& I // called <foo>". You can send a message To a particular1 V! y& ~3 T, h) e& `* v+ W
// object, or ForEach object in a collection. r$ O/ Y" R6 I R7 Q/ u5 q
# J% ]0 G; b9 r J( V( o3 v // Note we update the heatspace in two phases: first run- ^4 P2 m# p* A4 b
// diffusion, then run "updateWorld" to actually enact the
. R" c) H" w. ~6 Q Z; e) } // changes the heatbugs have made. The ordering here is' E6 y# M4 V' ^# ^; ^
// significant!, r2 j; C$ B4 @7 J
7 O, S( K" ]8 G( r) r2 [' a2 R# J // Note also, that with the additional
8 |4 W- j0 `4 o8 J$ p! f, b // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 s' q" p* a! E3 F$ F3 r T% x, q // randomize the order in which the bugs actually run% D( b$ N, r( V: K- G3 b$ G7 i
// their step rule. This has the effect of removing any' t" Z7 ^# q/ X2 n
// systematic bias in the iteration throught the heatbug% y9 n( j6 {& T! A' A4 U
// list from timestep to timestep
4 e0 A6 t+ f: s8 g% j " l! j5 M: L( j: L
// By default, all `createActionForEach' modelActions have5 ~. F, o# Z0 G5 u" ?3 [) i
// a default order of `Sequential', which means that the x9 y, L5 ~# E I6 g! \
// order of iteration through the `heatbugList' will be
2 J, R! ^7 ^* R // identical (assuming the list order is not changed) B+ W! |) k' ]+ |2 ^6 G. P$ P
// indirectly by some other process).6 T7 H- }" l. r7 {1 ~ E" B" F
0 _, x' L D+ q5 w! t
modelActions = new ActionGroupImpl (getZone ());9 p$ S, B X1 h. h1 _# h
; _( X& K3 m. J try {1 B6 @) E( `- M% A# \! e
modelActions.createActionTo$message5 u1 a, r. A- c, b. k! V
(heat, new Selector (heat.getClass (), "stepRule", false));# d+ Z, J) W _' c( g
} catch (Exception e) {! B! k+ D' _0 k/ s9 Z9 r2 p" k" J
System.err.println ("Exception stepRule: " + e.getMessage ());
/ N1 _9 l) h4 o/ G' |4 | }
, j( a* \' F+ {* ]8 N$ y7 R$ g+ v/ [3 L/ C! S6 e9 R
try {- B9 B* L. x% Q2 f, m2 I
Heatbug proto = (Heatbug) heatbugList.get (0);
# c9 j' L2 `9 E' S" p Selector sel =
8 q# _( X! A+ r0 i4 t( d new Selector (proto.getClass (), "heatbugStep", false);
; [, G+ ~* z8 E1 a/ j9 |1 B actionForEach =! q6 t/ Z8 J, C% J+ o
modelActions.createFActionForEachHomogeneous$call: H' G8 X2 [% G8 z( g, O, o/ ]
(heatbugList,0 |" c, O3 A: k: H* ` \
new FCallImpl (this, proto, sel,
% x% W6 b+ G! u. c# v new FArgumentsImpl (this, sel)));
3 d; F. y3 W4 `+ Y g5 L# c' G% r } catch (Exception e) {3 G1 z" _8 s1 w/ E" P& Z( w
e.printStackTrace (System.err);
/ g! M# Q. W4 O }" j- [& x& j6 s" _
) y! ]7 a; m( S, X9 j' L; p) G
syncUpdateOrder ();
1 @+ c! W& t9 h1 _
- n* h6 K5 V* p# H3 Z! r' P- d try {' ?( A- `, g9 X6 v
modelActions.createActionTo$message 3 h2 N+ g2 \" h
(heat, new Selector (heat.getClass (), "updateLattice", false));$ n$ y$ ~/ e. U! p2 N
} catch (Exception e) {
; c. E% C& y! M8 a) F8 p6 n' u System.err.println("Exception updateLattice: " + e.getMessage ());
! I6 W. M# K* ? }
* A# T2 }2 }! G( `8 |4 t$ P+ N" F7 _+ ` 4 c5 }, c! ~% b8 v' M9 t
// Then we create a schedule that executes the
9 u- w* U* W- t% x9 H // modelActions. modelActions is an ActionGroup, by itself it3 L3 H/ _" B4 D6 F0 }$ D
// has no notion of time. In order to have it executed in+ U: N3 ^7 R/ R$ {; W" c8 A
// time, we create a Schedule that says to use the
6 F+ d0 F" p! C6 y8 ?; G0 Z0 [# Q // modelActions ActionGroup at particular times. This
5 T4 y$ [! U$ `& \4 N. H% d3 ~ // schedule has a repeat interval of 1, it will loop every6 S6 r- M! f* x6 e7 X
// time step. The action is executed at time 0 relative to/ n" o: Q+ V0 Z
// the beginning of the loop.4 F+ @* X6 d; B8 ^0 P) K
) e" R# t" K4 B! Z: V# x( z
// This is a simple schedule, with only one action that is, g7 S$ y8 h6 L, `' N5 t
// just repeated every time. See jmousetrap for more9 y, {4 P9 W* f# N. Q# F6 c% L
// complicated schedules.: r7 ~0 S2 K0 x/ q) P6 v2 r) Y
9 T3 K/ |: R1 N" ] modelSchedule = new ScheduleImpl (getZone (), 1);
, D. C) P! t. @9 r J modelSchedule.at$createAction (0, modelActions);
9 P- \! {: _( W6 G; h - g6 p9 ?- l+ j' c* t: H$ B& S
return this;
% C( I5 t( p p% a; I" ]5 R3 ` } |