HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ c8 Y9 I/ l2 K6 _) g/ \, t j- H5 ]6 k
public Object buildActions () {
& s' P* `4 [# C, d l4 E super.buildActions();
8 t2 N0 k4 z4 X9 u
6 I' Z" B! V) z1 s, K // Create the list of simulation actions. We put these in
0 t3 U) n0 T* k1 d5 _9 S2 O" q* ? // an action group, because we want these actions to be1 j* R4 ~5 a( `) h3 h: p
// executed in a specific order, but these steps should
7 D8 f: C+ W- {: G // take no (simulated) time. The M(foo) means "The message4 s8 v/ y! p$ V, |* c* F! k+ l* u
// called <foo>". You can send a message To a particular1 `; s. n, I0 @0 m" D6 n
// object, or ForEach object in a collection.4 E% o0 J2 L6 q3 R% o: c, v9 d
* {$ v% c6 o m( a8 @ // Note we update the heatspace in two phases: first run
/ M5 a6 k5 V9 D+ A9 O // diffusion, then run "updateWorld" to actually enact the1 [( d, k0 @ Q" W. ^
// changes the heatbugs have made. The ordering here is
( a, T6 ] k: |4 k // significant!
7 Z$ M/ O$ _ M & m; d; j0 S: j9 q
// Note also, that with the additional: Q' [' O0 t1 x1 ]& ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 C7 q1 u& G6 {; R! E7 j
// randomize the order in which the bugs actually run1 Q+ s; t7 q2 |. R4 T
// their step rule. This has the effect of removing any
7 n/ @7 ~2 [1 T2 |7 H% H // systematic bias in the iteration throught the heatbug
# u+ F9 P% g9 ^* ~( Y6 [+ @1 I& M // list from timestep to timestep/ c4 p4 q/ h, I0 e
! s9 J- F4 S& { // By default, all `createActionForEach' modelActions have
$ |/ ?' p, o& h; A // a default order of `Sequential', which means that the
; N# t% v u0 m. a; N+ Z3 S // order of iteration through the `heatbugList' will be; _( M! {0 ]$ c2 O, d( m. n0 ?
// identical (assuming the list order is not changed2 T. U$ E" V; V; d
// indirectly by some other process).
* v8 h& R) W- k6 w! d" U
6 ?/ s+ a7 H; {% w7 \% \ modelActions = new ActionGroupImpl (getZone ());
7 m. ]: j, ^/ n) x& r5 G; F1 P# }- ^% [+ D- }
try {( r* a, F! }1 q j7 R
modelActions.createActionTo$message
7 {6 z9 |0 a% R! O# P5 P (heat, new Selector (heat.getClass (), "stepRule", false));
! ~) T$ a4 k n, z } catch (Exception e) {
3 V0 J2 G( m" A1 G/ q* \% ]8 c System.err.println ("Exception stepRule: " + e.getMessage ());
* w( N# u* Z, A1 ]5 T! c! e& w6 u }; C3 s* P1 k( Z- E2 M! C6 U! I$ M
) r# }( d4 e: a1 c
try {6 L8 p+ m! f% x! j
Heatbug proto = (Heatbug) heatbugList.get (0);
" `4 }7 L' o% V: E8 S( V# w Selector sel = 4 U) w& D% P" N0 v. H# T
new Selector (proto.getClass (), "heatbugStep", false);
N( m5 @: [; p actionForEach = h# }; m1 P0 |$ ?. H% N! U% c
modelActions.createFActionForEachHomogeneous$call
! K5 Y6 E" O* j3 E3 s4 k& g' x (heatbugList,
2 _1 |: W% F, @4 o- D# a2 J* ]( p# R new FCallImpl (this, proto, sel,# Z# `( s8 H: G5 m6 f. p7 d7 E
new FArgumentsImpl (this, sel)));5 l; y0 q6 G1 @" i4 [9 }) R
} catch (Exception e) {
9 |! w" `! _: g6 R! z: D3 S e.printStackTrace (System.err);
i. P3 F: d) a }' v9 u: }1 C3 s# M4 D1 S0 g* ~+ B
. a# n1 d! x/ B' U- G, V syncUpdateOrder ();
9 W" C+ j" K/ C: ?4 ?+ X" d* R) z) ^
try {3 Q" \/ j3 n8 V. v+ I. p
modelActions.createActionTo$message
3 W* g! n2 Z1 y l8 Q3 m (heat, new Selector (heat.getClass (), "updateLattice", false));/ a. |. a6 M. P% F
} catch (Exception e) {: E7 I2 {. X6 @. t; Z; {
System.err.println("Exception updateLattice: " + e.getMessage ());# p& R+ f5 o8 L6 J( U4 a6 h
}
# ?: \6 m( n' ~# M3 u: {) ~
+ U3 W& {; o Y // Then we create a schedule that executes the5 V( m% S; L1 D4 @
// modelActions. modelActions is an ActionGroup, by itself it
7 M' Y7 U: U X // has no notion of time. In order to have it executed in
0 A1 X/ w, _' U/ p/ Z$ x // time, we create a Schedule that says to use the, a# q1 V6 }/ m o
// modelActions ActionGroup at particular times. This q+ z# c- \. `5 X7 R5 z
// schedule has a repeat interval of 1, it will loop every* a$ a D# M+ Y( p( [0 \. \
// time step. The action is executed at time 0 relative to
6 G7 r$ K4 S) y; l // the beginning of the loop.
+ u9 I9 s5 B0 [2 l0 z" ] E1 a' h
// This is a simple schedule, with only one action that is4 j- ~* [& g" N
// just repeated every time. See jmousetrap for more
8 T8 F3 V& S. I* k // complicated schedules.' b2 w6 B3 F k0 j/ y; w% C3 ^3 G* f
3 U! t! D7 h3 P7 E$ b5 [5 Y modelSchedule = new ScheduleImpl (getZone (), 1);
' Q! H- Q3 G' w modelSchedule.at$createAction (0, modelActions);6 L, S1 t3 Y9 T
$ V0 e( g5 K/ P2 k; _ return this;4 C1 ?+ |5 p) Q% ?
} |