HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 f7 |: u. J0 K# ]2 N$ a5 y. c( M* ^0 n+ h% j
public Object buildActions () {# L# P' i( I' j9 [
super.buildActions();
3 Z8 t) m! ^% O8 C5 u! I) I @5 t1 B 6 Q6 b. t# T. e6 t) h
// Create the list of simulation actions. We put these in
# S# ~! G( v/ z" g! Q% n, R( [ // an action group, because we want these actions to be
8 k2 u9 U& L: D! _1 }# F // executed in a specific order, but these steps should
: j" p4 v! j7 n6 V0 p // take no (simulated) time. The M(foo) means "The message) M0 R# i) E2 @/ h# Z
// called <foo>". You can send a message To a particular7 ~" K9 }; O* A) ^
// object, or ForEach object in a collection.
" z8 D+ m6 b) ~( `
G; n" K8 F, E6 a* R" } // Note we update the heatspace in two phases: first run
& ^1 z1 C9 |7 C // diffusion, then run "updateWorld" to actually enact the5 R4 T0 l2 x* G( K9 {8 r( |9 \
// changes the heatbugs have made. The ordering here is. s- u; R p$ O+ b& j
// significant!6 d2 v3 K6 M3 C8 o, r! f& B+ M2 \, Y8 v
- |% e, U! a( _ Q0 i$ ~' c3 s
// Note also, that with the additional
% N% O8 i- r/ Y- a, ] // `randomizeHeatbugUpdateOrder' Boolean flag we can
. j& F {% ], s // randomize the order in which the bugs actually run% ^2 B. x) o0 D, ]2 H5 w0 J
// their step rule. This has the effect of removing any v2 C7 ]7 R) h+ q9 x" l
// systematic bias in the iteration throught the heatbug. @" l R* C( a( g& i; G
// list from timestep to timestep
1 }" _2 i/ J3 c7 r/ T2 J8 d4 d 2 X* j# l- }' O2 J# O
// By default, all `createActionForEach' modelActions have
+ t8 p& q: D2 s7 |# K9 i, f( u8 N // a default order of `Sequential', which means that the$ ?2 {# E% ^) ^* G! t
// order of iteration through the `heatbugList' will be
0 M6 s: g, @ R- \: l( y0 z! p // identical (assuming the list order is not changed
9 t: w# o3 f& l+ M // indirectly by some other process).
( E9 Z# w D* g/ [, t1 x" x( e
% Q; _2 I; Q$ \$ d8 D modelActions = new ActionGroupImpl (getZone ());' Q3 Q& J m; y. K
, Z! {# c5 H0 X( U4 e; J5 p$ V try {, U" d$ k, H# J+ ^3 O4 {
modelActions.createActionTo$message
$ V. z: z2 ~9 l% j* z8 i (heat, new Selector (heat.getClass (), "stepRule", false));/ a- p" r- n0 e% [4 W
} catch (Exception e) {# a1 ?, C! u5 r2 ?6 c2 s0 {; t
System.err.println ("Exception stepRule: " + e.getMessage ());
& U; W) s6 n/ X1 y6 w" V }
# [2 C, N$ A0 C- \
; q* ]0 W8 R$ c9 A' X try {) K1 B i/ f% Z( J; f7 i
Heatbug proto = (Heatbug) heatbugList.get (0);/ N3 X5 I x5 g" r8 |; @0 W7 Z
Selector sel =
) e% X/ H/ j1 o new Selector (proto.getClass (), "heatbugStep", false);
" \( N& t* {) B actionForEach =
" G2 O. G5 t# G modelActions.createFActionForEachHomogeneous$call
- _* s: U6 Z3 n: ?4 {) \ (heatbugList,
" r$ W* U- f* _3 B* V new FCallImpl (this, proto, sel,
5 X# B9 ~" Y" n. ? new FArgumentsImpl (this, sel)));
) V z& {2 R2 o- D9 I } catch (Exception e) {
8 j5 K8 N# L6 M0 E3 J# P e.printStackTrace (System.err);
+ x# W. f* Z& K% g5 s3 d" Y }2 R( i" J1 i4 g+ N* x' ^. ~. ^
! {& k; }; V5 p& S! C& \ syncUpdateOrder ();
1 k! V7 N& q2 o7 S4 c% i6 x( l. g) `- ?/ h' C6 E8 |$ Q' u
try {, {" s" p& X0 \3 O8 K
modelActions.createActionTo$message ) n* e. g: v' |) S
(heat, new Selector (heat.getClass (), "updateLattice", false));
' y5 C; u% l! X' K } catch (Exception e) {
# P* B. Y+ K8 o _! U- d/ S9 f System.err.println("Exception updateLattice: " + e.getMessage ());9 y1 d" s0 v; f8 a) ?7 F. b) q
}# C& I3 M7 x! X6 p$ v
# u% T7 m0 ~6 n% `4 F' q
// Then we create a schedule that executes the5 x" {( `! Y% S5 s
// modelActions. modelActions is an ActionGroup, by itself it
+ d: w. N( Q0 v4 e' e) Q" q // has no notion of time. In order to have it executed in4 |# I( Y* F8 v3 W+ o* s
// time, we create a Schedule that says to use the1 b) m! y8 K: Y6 D. N
// modelActions ActionGroup at particular times. This
' N; h/ S a: }6 h5 F8 R! Z* C // schedule has a repeat interval of 1, it will loop every
, K$ t9 n: ?, \* Z // time step. The action is executed at time 0 relative to2 b; b4 j. H9 S" h, I8 o! G
// the beginning of the loop.
7 V* D1 V. }! H4 h
# S6 t3 S* a3 Z n% ^2 |! l$ ` // This is a simple schedule, with only one action that is; g: x! J1 h b
// just repeated every time. See jmousetrap for more; i) p7 Z2 @& z
// complicated schedules.
r' `4 b( l# G k* c* [6 P2 Z% L 7 i3 ~: E* b. z9 |
modelSchedule = new ScheduleImpl (getZone (), 1);% P8 I: F4 T$ V
modelSchedule.at$createAction (0, modelActions);
& h+ r" X/ l$ B; l 5 f: `+ E5 n) b: h' ~+ T# @6 |
return this;
1 k/ M; x e# ^5 B. _% ~9 ] } |