HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# y8 ]) J6 V2 J z7 y& D' ]8 f6 M* _- F d
public Object buildActions () {
# a8 X% w7 O1 {9 c1 q7 I super.buildActions();5 y0 U& r8 z% e3 g9 v5 q n( G! E; M
* y4 L9 a. d, i1 K, m7 X7 H. @! {$ k // Create the list of simulation actions. We put these in
" ?& c/ ~- u2 e, ]$ Y# e6 g // an action group, because we want these actions to be5 P6 b( A% ]: t2 K a
// executed in a specific order, but these steps should/ K/ R, d( D9 }; t1 I
// take no (simulated) time. The M(foo) means "The message7 J. S; J0 b1 U# e$ P# c: D% U
// called <foo>". You can send a message To a particular8 i( C+ J$ D! ?+ g( E4 \
// object, or ForEach object in a collection.
0 g4 G" G4 e4 v8 n# r- U4 C! r
9 x/ H& d" b, \5 y/ s* }! h# j+ W // Note we update the heatspace in two phases: first run
8 ^( B4 t/ m+ B( f, w: L3 L // diffusion, then run "updateWorld" to actually enact the
& i4 i3 D5 `. w0 E' W+ q // changes the heatbugs have made. The ordering here is& r1 Y X/ T6 G8 @: N
// significant!9 T8 O: }9 I: M" N# b* Z$ T
+ h2 M5 ]! [" c1 l' U% ]! A // Note also, that with the additional0 y! P# D7 }7 ^- p
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) |' e- v7 Y8 b6 } b6 R // randomize the order in which the bugs actually run
0 `# o$ |1 ]1 L2 b% @ // their step rule. This has the effect of removing any
8 A6 P; r/ ~- a9 S4 k) T // systematic bias in the iteration throught the heatbug
8 T7 ~9 j a9 w5 L) i // list from timestep to timestep. e( \5 Q& f3 }8 ~; I
, X2 z2 F% b2 \" A7 q. k; h // By default, all `createActionForEach' modelActions have
* k. u+ ?" w! Q" B3 _1 o // a default order of `Sequential', which means that the
! s2 d+ ^& l' p3 r- t // order of iteration through the `heatbugList' will be
2 d, k- |; \/ H, [% { // identical (assuming the list order is not changed7 }8 p! F& ]) l4 B8 s; A
// indirectly by some other process).$ W1 i3 s! b4 S& J$ s2 s
. o* z& b- _3 n, H( \
modelActions = new ActionGroupImpl (getZone ());4 F& W4 c" H3 o
# L3 `$ c6 P6 H9 y
try {
' ?2 V3 C2 ?/ \- |, q; i7 K7 g; D modelActions.createActionTo$message
; P! x. a/ A& B% i* H: W (heat, new Selector (heat.getClass (), "stepRule", false));, X$ |% v/ _* P" Z) E1 w
} catch (Exception e) {2 l+ V' y3 r. R: X4 S7 |
System.err.println ("Exception stepRule: " + e.getMessage ());
1 A- Y0 `& h" \2 W }
6 P; O* i. [) Q& e/ @; T! I$ m0 i- E6 |0 B) P0 Y3 D
try {
/ j) p r( a! p2 u Heatbug proto = (Heatbug) heatbugList.get (0);
! G: ^4 Q* t; v) V$ @4 i Selector sel =
# M" R- T. d+ B4 W8 [ new Selector (proto.getClass (), "heatbugStep", false);6 B+ O, A+ _- B3 }: }' G
actionForEach =0 z3 \7 L, P) U
modelActions.createFActionForEachHomogeneous$call
4 D. f+ |0 j$ y (heatbugList,, z8 I1 r& ?* p# C7 k3 n" p
new FCallImpl (this, proto, sel,
3 p1 P5 N5 J" Z5 x8 M6 g; K new FArgumentsImpl (this, sel)));
% _, b, d% R( h3 N/ C% Z } catch (Exception e) {
) j% [! c# A1 ^8 l e.printStackTrace (System.err);0 l7 M( d+ E: X9 @/ m2 ^8 [
}4 T/ F% B! F5 Y/ B; G( u
; ]% K, \0 G) e0 e; x syncUpdateOrder ();
+ p2 c. r* E0 L& ^6 g9 f3 W: }! w; q& r
try {
3 F5 \0 [) {& }7 k1 V5 _ modelActions.createActionTo$message
7 s& J! t1 T1 b+ `6 Q' i (heat, new Selector (heat.getClass (), "updateLattice", false));
$ a- \+ s4 G$ t. V4 p; o } catch (Exception e) {+ n! b# }: \2 a- P! g
System.err.println("Exception updateLattice: " + e.getMessage ());' b7 N4 ]- G$ k7 F
}
, b7 W u$ @4 g& g) c
6 u% W q# \% i s# o8 T: F* r1 V // Then we create a schedule that executes the
0 I9 t7 E' @6 u7 [2 n // modelActions. modelActions is an ActionGroup, by itself it
. `2 J' g+ Y% h& I // has no notion of time. In order to have it executed in
5 Z" Z* U1 x n // time, we create a Schedule that says to use the4 W1 G O2 z$ c! A- x" B5 w5 ]2 Z
// modelActions ActionGroup at particular times. This- P& B! A& y+ y! D
// schedule has a repeat interval of 1, it will loop every. N( G" C0 K& N; F3 |% i5 c
// time step. The action is executed at time 0 relative to
5 y; e: W Y. G // the beginning of the loop.+ t t( ~. ^# F) I. |
& @9 T( v l7 Q* ~1 E1 U. A9 T: f
// This is a simple schedule, with only one action that is
$ k/ J, o. d1 q- u // just repeated every time. See jmousetrap for more. N& S+ }: J$ J
// complicated schedules.
6 A' A, i- G- j2 p . V; X' @, O3 \2 d- _
modelSchedule = new ScheduleImpl (getZone (), 1);, U& i* V1 }3 c8 s) |% `& k g
modelSchedule.at$createAction (0, modelActions);9 f7 D8 E. H- q' Q* Q4 l7 U
1 @" B' `: ^. H: Q
return this;
; D* h# r, W8 q- ~8 e } |