HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 ~2 a o( | [0 V G) L. o
; f7 z& e0 p+ y* _ ~, o, A public Object buildActions () {
9 `2 s( I& h7 y2 x: ?, ?4 Y super.buildActions();) v8 M! m$ p5 u2 a' h9 o' j
: X! y9 Z7 ]/ X9 f: W9 H; ], r // Create the list of simulation actions. We put these in
/ U' G/ A0 o, _* W: T, L // an action group, because we want these actions to be+ ^9 a* ?' `, w7 f6 w! E
// executed in a specific order, but these steps should, Y. B ?3 g4 x8 V
// take no (simulated) time. The M(foo) means "The message# M- A; k2 `2 ?8 ?0 Z
// called <foo>". You can send a message To a particular
4 T Z: [) g8 {, A- h1 } // object, or ForEach object in a collection.
( L- b( T$ K8 e% i . Z5 @7 B2 r0 t7 o; u; x5 b& w4 p
// Note we update the heatspace in two phases: first run
. c8 ?5 y+ E N8 `* C) D$ O' v% q // diffusion, then run "updateWorld" to actually enact the' B# d: C9 _$ G: f
// changes the heatbugs have made. The ordering here is& u: Y7 B1 _2 u- l6 o# o% ?
// significant!$ Q7 J J3 f2 {6 s
7 w$ \! T; k7 n; ]
// Note also, that with the additional, g& ?$ B0 p. u8 Q5 C
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 K2 Q1 P4 ]+ o. s2 W4 J
// randomize the order in which the bugs actually run5 N {+ Y' V$ u" B% V( \
// their step rule. This has the effect of removing any
9 ?0 n+ w1 ~1 I" T1 z$ Y // systematic bias in the iteration throught the heatbug
0 {: Z' k' D$ A% l // list from timestep to timestep2 @2 s3 i4 s0 R
. P6 z; o- R; ]9 {) K6 _ d
// By default, all `createActionForEach' modelActions have3 \5 K8 f; [% X
// a default order of `Sequential', which means that the
3 C$ |% L% H; ?" D& D# q // order of iteration through the `heatbugList' will be
0 L8 H; u, E* ]: \6 E, _ // identical (assuming the list order is not changed3 o/ n5 a9 s3 i6 t9 {, ? [
// indirectly by some other process).
2 P% h& O: w3 K2 Q + @& w( M: m9 S% X
modelActions = new ActionGroupImpl (getZone ());
& T. h5 e3 [2 U. F8 J: d6 `- b- K$ Z' m5 g1 `; |+ E
try {
9 `9 } y9 t) R" A' C9 L* Y modelActions.createActionTo$message
3 I' b, @' C' L, ]2 Z# q (heat, new Selector (heat.getClass (), "stepRule", false));) K1 x5 m' ^! s1 v/ b5 O% Q
} catch (Exception e) {. u4 \& k$ @3 Y @- ?" D( R4 i
System.err.println ("Exception stepRule: " + e.getMessage ());; ]& {6 [# [2 L' ~5 D% ? r
}
( [8 @0 J3 @0 t5 i+ V; G \& P+ Z7 o9 d
$ e2 r& H) I! C. |3 R try {* W$ v0 F L/ U7 ^
Heatbug proto = (Heatbug) heatbugList.get (0);8 U/ s9 d, t" Q" m# t @
Selector sel =
1 `( l# z+ o) R% D new Selector (proto.getClass (), "heatbugStep", false);
$ k' b- l6 l0 ]9 I actionForEach =+ f; h0 h- k3 h- v! q
modelActions.createFActionForEachHomogeneous$call, C/ u2 J! F" D# G. p# Z
(heatbugList,$ f- q m' b' Q& T
new FCallImpl (this, proto, sel,; L1 F9 S* g3 }* }) Y+ t( ^
new FArgumentsImpl (this, sel)));% { D9 x6 z7 [" V ~6 }( }: h
} catch (Exception e) {4 l4 t1 ~+ C5 i" `7 y' F
e.printStackTrace (System.err);
% L8 E; A9 T O; M }' F& i6 C8 q2 ]7 ~. ?" J
% U" {- e& U+ _2 [+ [; \3 U3 O7 I syncUpdateOrder ();% O! F9 J/ d; x
u9 z) K% i& o4 }& u try {
1 |, R" M$ l- l modelActions.createActionTo$message
1 I& y. f' P# h x& _# x (heat, new Selector (heat.getClass (), "updateLattice", false));
5 H8 I! @9 @3 R } catch (Exception e) {2 f; I( Y" }- h
System.err.println("Exception updateLattice: " + e.getMessage ());
& O" m; L) r7 V+ R. l5 Q2 O( j }2 [7 [8 q. c+ M4 C" T8 Y4 {2 t- H
( h1 R" C$ @! j4 M" s3 B* M5 `8 z
// Then we create a schedule that executes the' ~$ a0 ~7 O2 K" d: V ~
// modelActions. modelActions is an ActionGroup, by itself it
1 {6 L/ `4 w$ r4 h( O // has no notion of time. In order to have it executed in- a1 b1 S2 K+ d2 a: M4 j
// time, we create a Schedule that says to use the
0 V4 {- p. n4 e- L6 c5 c! N // modelActions ActionGroup at particular times. This
1 a% a8 L; }6 H. r. Z // schedule has a repeat interval of 1, it will loop every2 Y/ B* I3 E1 ^ n( Y! n
// time step. The action is executed at time 0 relative to
$ U' H2 F! Y7 p S% b# w // the beginning of the loop.7 t/ ~% X& D" J1 m
* r: \! K& @+ R // This is a simple schedule, with only one action that is
4 U7 ]( t) f1 ^7 q6 S6 Y& h% i' B // just repeated every time. See jmousetrap for more; i z/ W* P R3 o8 z6 U
// complicated schedules.9 z, {6 r9 y. s' B1 i! e
$ d: k+ u2 B8 Y+ _( Z& A4 } modelSchedule = new ScheduleImpl (getZone (), 1);; T# |$ d0 K2 e, H% m8 Z I9 O
modelSchedule.at$createAction (0, modelActions);
2 Y( F, p, }4 U, a/ l2 t
1 n; [0 K0 `% A7 t. v return this;
0 F) n# m; [0 l( b5 g } |