HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( J+ _7 u/ G0 \2 E- L! G+ j( W1 J( [) m m- M5 f: R* w. u* \
public Object buildActions () {
$ o! N( @7 X) j; L/ V) V super.buildActions();
' d- N( q# o( a3 T; \+ ]) P ; C. x7 R2 ~, @+ r
// Create the list of simulation actions. We put these in, m5 _; }1 ~; `4 M8 r# U
// an action group, because we want these actions to be
5 [7 P5 a% c( T" r( O& k // executed in a specific order, but these steps should
4 V8 a% X, p- C // take no (simulated) time. The M(foo) means "The message
8 b5 M7 n" ]' [; n6 O( h // called <foo>". You can send a message To a particular5 F) _# s# o2 M6 L$ N2 D3 Z1 a
// object, or ForEach object in a collection.1 X! |5 M# x7 n, z; b* j
" l5 ]+ R3 q, C9 I3 R o // Note we update the heatspace in two phases: first run# e/ v7 i. y3 @
// diffusion, then run "updateWorld" to actually enact the% m( W8 ^7 |7 A7 `( Q$ ?% F
// changes the heatbugs have made. The ordering here is
: ~/ [; l4 h% E( R // significant!" V9 K; ]0 G- D" g- L( f& a. J3 R" J
1 C2 C5 O4 p6 w" X1 v8 n
// Note also, that with the additional
1 e' D! b' T3 y // `randomizeHeatbugUpdateOrder' Boolean flag we can
* d% `. Q- M1 l; l) ], R // randomize the order in which the bugs actually run
Y; o8 [6 P& _% v+ G- f // their step rule. This has the effect of removing any P& V% I, J/ \% N# y
// systematic bias in the iteration throught the heatbug9 Z0 D2 x7 f7 K' A5 Z# A3 t" J
// list from timestep to timestep
6 U$ ?' D1 T- e: N( C; f
, o! m/ i1 r! D+ {: ]6 |5 U // By default, all `createActionForEach' modelActions have
, b( R* q! }0 }8 }" Q // a default order of `Sequential', which means that the5 W8 ~* j7 V, W0 p4 p
// order of iteration through the `heatbugList' will be1 @& g( O" s# T# N, r
// identical (assuming the list order is not changed
3 y/ ?, Y/ @+ _; z // indirectly by some other process).3 l2 p9 W- c+ Y9 }# v: b
: R- W$ K! r* k+ x$ Q
modelActions = new ActionGroupImpl (getZone ());
+ `2 T+ l5 H6 L& o" D1 Z; M3 p( v0 A3 T; _2 W+ Q7 K
try {- B; l( ~. q: s9 x f! y( c* d
modelActions.createActionTo$message
: U$ y2 ?" E8 J+ n4 y7 G* j (heat, new Selector (heat.getClass (), "stepRule", false));
+ P$ i% X/ `/ d' i# k/ s } catch (Exception e) {1 ~' L+ }2 _. V# x$ `4 t0 j
System.err.println ("Exception stepRule: " + e.getMessage ());
3 a( C/ o! u* f& G" O4 B }
3 r0 K5 F& I9 J( c. n3 y
/ Y( s0 E7 y; ~ try {
3 U# j/ C5 r/ D& e5 ]* f Heatbug proto = (Heatbug) heatbugList.get (0);
( Q! P" \' o- e* S! K2 @0 P Selector sel =
, @( ~ a7 Q( U f new Selector (proto.getClass (), "heatbugStep", false);
0 z% y7 A3 [, C' w8 H: B+ Q: R actionForEach =3 @) a3 P6 f3 J0 `! ~/ H
modelActions.createFActionForEachHomogeneous$call
1 ^; g, T" n- E+ v (heatbugList,
- ~$ ]( f! U: v! l8 h5 ] new FCallImpl (this, proto, sel,
" y% I; i& R0 U* V' I3 Z new FArgumentsImpl (this, sel)));
9 K4 g* N" V0 V6 n. P" i8 M U% ] } catch (Exception e) {( B1 @" ?" Y, q) Y% k8 r% _
e.printStackTrace (System.err);# o- p1 ]" U- `; I! G
}
1 v5 P% d$ t: y
" I2 b8 L6 Z: { syncUpdateOrder ();
1 R3 x- e* a$ D3 W4 ~' N" W
! R& w1 B7 x% F# [ try {, M z: i, G! \
modelActions.createActionTo$message
6 ^3 x( A$ H9 b (heat, new Selector (heat.getClass (), "updateLattice", false));
, s; h, ^/ y2 W3 [' f; u' i8 j" \$ K } catch (Exception e) {; W, f5 [. P9 W
System.err.println("Exception updateLattice: " + e.getMessage ());
- j/ h. M5 f1 s2 A }
( n; v' e( Q8 R8 D
9 `7 R; \/ J. N8 [4 k" [6 ? // Then we create a schedule that executes the! |' ^, \0 A8 x* B! a
// modelActions. modelActions is an ActionGroup, by itself it
) [3 I3 \& X# V [" o // has no notion of time. In order to have it executed in2 [' s) d7 @8 h! u- X- Y- G
// time, we create a Schedule that says to use the
: H4 T) i) d% v8 @ I // modelActions ActionGroup at particular times. This5 ]$ u* l" E3 \. ^
// schedule has a repeat interval of 1, it will loop every; I9 z& E2 F- ~# d. a y
// time step. The action is executed at time 0 relative to
# U$ a6 I% N2 |1 C7 P6 U l // the beginning of the loop.
& e% M5 v! k3 d7 R! g& a1 O4 J! S8 b8 ?1 ^# C0 J1 y8 P9 b
// This is a simple schedule, with only one action that is
# ~' }1 t, O, `4 m, \9 ? // just repeated every time. See jmousetrap for more/ U* v% H' C4 S) M; z) F; j: Q
// complicated schedules.
/ G$ T9 z; M* K4 T 8 U G" P( A9 D! j! u! A8 m
modelSchedule = new ScheduleImpl (getZone (), 1);
( @' B) c0 B* ?2 v- v modelSchedule.at$createAction (0, modelActions);
. y4 ]1 C* m6 M1 S Y) d
' b8 R* V3 o3 Y% b- C6 s return this;% s% f* t4 L. W1 p& L% [* P
} |