HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; S* `" K* d" C- B* ^9 I/ f) _- ]! Y
6 }& e; ~4 j, e: C- t0 K6 [
public Object buildActions () {
# G: j1 J2 e5 o# ~3 M' m8 B super.buildActions();
+ [& I+ n" |- N2 Z0 b0 F ' |, I" B$ ^! \+ K/ D
// Create the list of simulation actions. We put these in) x- o$ H0 d% G( `
// an action group, because we want these actions to be8 u1 p( W- r/ L* P. u. P
// executed in a specific order, but these steps should3 m6 B7 m0 y2 F! @4 Q, M* V1 x3 ]
// take no (simulated) time. The M(foo) means "The message7 H5 E) U- \& U
// called <foo>". You can send a message To a particular( F& a/ v: C! F! Q$ L* W
// object, or ForEach object in a collection.
; k3 G' `( u6 ]
! J1 c+ p: U" G$ G3 H // Note we update the heatspace in two phases: first run( U3 I; }7 @+ b( ]* l( m
// diffusion, then run "updateWorld" to actually enact the$ C; k8 t1 T( ~
// changes the heatbugs have made. The ordering here is& t" z& v6 \* q# z
// significant!0 Y8 ~4 x6 z- T, s3 y- ^2 u' t
8 e: {' j( \: u // Note also, that with the additional
. G' u. }2 A& u+ l+ Q5 d" t6 \' d // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 q+ y F( Q r // randomize the order in which the bugs actually run
$ D& I! m3 d a% |; p' ~ // their step rule. This has the effect of removing any5 u4 e& F, a% Y
// systematic bias in the iteration throught the heatbug
" f( r4 R; t" X5 g // list from timestep to timestep
/ R* l2 H/ K( D, s
& B9 ~" M- S5 F! r$ Y6 @2 E4 L // By default, all `createActionForEach' modelActions have+ ]2 s- Z6 x+ h) l: k1 G
// a default order of `Sequential', which means that the
/ U- n1 ^& o* B // order of iteration through the `heatbugList' will be
+ K2 K. o* P, T3 @5 j# @0 }4 Q // identical (assuming the list order is not changed
9 l9 Y) E2 K/ f6 P1 U5 o! J // indirectly by some other process).
3 ~9 W9 E* x) n
3 W/ d' Y; [% k1 U& P modelActions = new ActionGroupImpl (getZone ());
5 N5 k* y! |2 F1 ]: w6 L9 X. ]2 i" R4 G! x- ^$ N
try {% k5 E$ F1 Y& U, G7 V+ ^( g1 y
modelActions.createActionTo$message
- N; F2 X! [+ n4 U3 H9 y$ Q# y/ `% k (heat, new Selector (heat.getClass (), "stepRule", false));6 S8 b+ Q3 n2 K1 r: z
} catch (Exception e) {
( c u% c$ b9 v: o( ]' p System.err.println ("Exception stepRule: " + e.getMessage ());
8 b5 ^8 s8 B) h- O( n/ u( R6 I }
: |" g/ i' n. E4 U& a( w3 W& w# e. k2 K+ Y, V5 l
try {' Z8 E/ V" v* M, H* c/ r5 n
Heatbug proto = (Heatbug) heatbugList.get (0);
8 u/ O4 `" ]5 _4 P! c) Q) |( _, P Selector sel = , s% J. B5 x5 z9 r
new Selector (proto.getClass (), "heatbugStep", false);9 @9 ]4 X$ |% t+ G2 e' i1 @' b* N
actionForEach =
$ O& j! G* B4 t5 f" Y2 Q3 ? modelActions.createFActionForEachHomogeneous$call
& ]$ f) y5 Z+ S3 Y (heatbugList,
3 o0 d& V$ X2 L new FCallImpl (this, proto, sel,! l0 H8 {& R1 |4 ]2 d3 L
new FArgumentsImpl (this, sel)));/ t4 k8 {0 L3 ~) J' j! j4 d) [
} catch (Exception e) {
( U) j2 M8 Q8 y F: }( [; J e.printStackTrace (System.err);! Q% T$ ^! B3 p+ M1 x" \
}
2 Z4 {; t5 H8 p 9 N6 i; ^7 v7 @
syncUpdateOrder ();( J7 _" e" i6 k' U
8 _+ t/ M3 ]3 v) L/ u
try {
0 H7 M( T& `- R3 n- U9 ]4 } modelActions.createActionTo$message , I; q U ]% r; H) i
(heat, new Selector (heat.getClass (), "updateLattice", false));0 u7 \# L! p4 P4 q3 T: ~ c b
} catch (Exception e) {
: F, w7 @+ c% `. | System.err.println("Exception updateLattice: " + e.getMessage ());
X( m3 k6 _& A }3 M, @" U; v/ X) o" {, l6 B7 R. Q
7 f6 D1 `8 J5 {6 }
// Then we create a schedule that executes the4 I* N% o. e8 S! d" A
// modelActions. modelActions is an ActionGroup, by itself it9 B. S8 H M7 R% y9 L5 ?
// has no notion of time. In order to have it executed in
& ~5 O! I' X! m- ~+ w* L // time, we create a Schedule that says to use the, _) {8 X3 `/ H- M: r
// modelActions ActionGroup at particular times. This6 V4 h) M8 j# F; c5 Y5 l z
// schedule has a repeat interval of 1, it will loop every- S- S; A7 _4 O: O& F& u4 N
// time step. The action is executed at time 0 relative to" A# k1 s: f0 Q, T: m
// the beginning of the loop.2 v+ }9 D1 I& b* V6 e1 z
- P X9 k9 d" P S
// This is a simple schedule, with only one action that is+ G5 P5 V- _9 C1 c$ r% {8 e2 f
// just repeated every time. See jmousetrap for more
, ]& D3 u0 C5 N# ` // complicated schedules. f- o, _' U* a( d) G. n
6 c; |% V# X& `/ N& G" ] modelSchedule = new ScheduleImpl (getZone (), 1);) Q' G, B; N: c7 G# J9 ^3 e# l0 n
modelSchedule.at$createAction (0, modelActions);- ^* j# t" Z6 v. `$ o" S
% p9 ^7 f7 d; t' b return this;
* D7 X, @( z1 p! u" u W, m& | } |