HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: Q5 b4 I4 @4 Q
8 x: n8 Y) f$ a1 c5 n, t; Y& k: i public Object buildActions () {8 J; @/ Z5 c4 j8 G9 l/ Q+ Q
super.buildActions();+ }2 M+ t4 U K& g+ f; E# m" x3 D
5 [' ?+ n, f- d5 p/ h // Create the list of simulation actions. We put these in2 Q; b6 Z* ]. S% Q o: @2 J
// an action group, because we want these actions to be
, E/ m5 L4 W( V6 [ // executed in a specific order, but these steps should5 ]- D8 m" a1 l% j5 K" I0 @4 X) w' W
// take no (simulated) time. The M(foo) means "The message
7 ]% e* @7 Y6 `2 { v7 e& K // called <foo>". You can send a message To a particular2 E/ L! [' P) b V; u
// object, or ForEach object in a collection.1 u" Y2 t. b" ~" u* \# J
, h5 X/ P- i2 I+ a) U" A
// Note we update the heatspace in two phases: first run
2 l0 \; C5 o. e' C. |: y- J0 U // diffusion, then run "updateWorld" to actually enact the
9 g0 P- h* ~, i( K& y0 \ // changes the heatbugs have made. The ordering here is6 t* {8 S, p# Y* B2 E) m" i0 t
// significant!
) i# ^1 i7 u1 B8 g* z* R7 n ) @1 S$ r# x2 r
// Note also, that with the additional
- V8 G, j* Q3 o5 F8 F. a2 C$ R# j // `randomizeHeatbugUpdateOrder' Boolean flag we can
" h: O$ e' M; f# N; I // randomize the order in which the bugs actually run
% x+ {9 q9 J. E4 g // their step rule. This has the effect of removing any
6 l- l' h! ?( K4 i! K // systematic bias in the iteration throught the heatbug4 D: `, s' m4 V5 {6 s
// list from timestep to timestep5 x$ R4 h# }6 ~! Y# w
# G( A4 Z8 D1 |8 U- w // By default, all `createActionForEach' modelActions have
! Q3 X6 p4 ]( P, S // a default order of `Sequential', which means that the! H5 @2 q+ b1 W) L6 m
// order of iteration through the `heatbugList' will be
2 E3 j1 L6 E+ u: x/ d0 s2 } // identical (assuming the list order is not changed
8 D$ L( b- m4 H9 F( m6 ] // indirectly by some other process).2 d0 D! t5 c+ o
, I1 M: h6 s8 q9 `
modelActions = new ActionGroupImpl (getZone ());5 P5 \: O! }/ \8 U% ^/ p
7 ?. u1 @6 {7 o) d" o' P
try {3 z! ~1 v7 S1 w- M+ r. C
modelActions.createActionTo$message9 K% m8 T, H" e8 n) ^5 V* h
(heat, new Selector (heat.getClass (), "stepRule", false));: y0 `. v1 N: L2 v# d4 B
} catch (Exception e) {% }# _3 k% @6 n9 G% w4 ~6 }
System.err.println ("Exception stepRule: " + e.getMessage ());
; E3 [& ]! H9 G! o: I( p }5 m* A* N8 F% \+ w* r$ {4 L, t
% v2 t0 e, D9 B k; O( Q
try {
7 Q O6 j, Z' [3 z: |, } Heatbug proto = (Heatbug) heatbugList.get (0);; H* i$ _ T9 j% k2 O
Selector sel =
9 Y y( N, X8 r. I$ \ new Selector (proto.getClass (), "heatbugStep", false);) I: j* K0 i5 ]) s
actionForEach =
2 `; Y1 z3 {, ^. S0 I modelActions.createFActionForEachHomogeneous$call
5 C; B O0 ~- w! Q* H (heatbugList,
6 A% k3 C- t! t* B4 A: N new FCallImpl (this, proto, sel,
' z) B# h; U! B5 ]% [4 u new FArgumentsImpl (this, sel)));& S7 a. e& ]6 a2 u T1 @7 ?4 E
} catch (Exception e) {' d( r! O1 z/ m, i& z+ u
e.printStackTrace (System.err);
) {9 i6 M4 r7 E" y }) z' M0 R' I/ Y" }
0 \) t& M* Y; {* ]& _, U" N syncUpdateOrder ();
1 I' X9 o- P. ^5 B- u& [; ]% ~; s# R& v% |; K1 C1 }9 Q( X
try {
8 \1 K. b1 O; D3 T8 {7 m9 v modelActions.createActionTo$message
x, k" |; j: h (heat, new Selector (heat.getClass (), "updateLattice", false));
6 N3 x2 n/ A7 K6 A" }+ P } catch (Exception e) {- ]; M# V5 O$ ], p, }" E
System.err.println("Exception updateLattice: " + e.getMessage ());4 L1 o5 F. A0 |5 d' P) P3 j& N
}
0 M8 |5 N3 g$ X9 M1 ^ o
}) z; Y% `; N4 N0 c" [ // Then we create a schedule that executes the! g9 L) f$ ~* I9 s
// modelActions. modelActions is an ActionGroup, by itself it, Q: @# z7 F. u( z4 { `
// has no notion of time. In order to have it executed in }2 x/ k: ?9 o0 W) w
// time, we create a Schedule that says to use the
2 i. [/ A% H8 Q) q9 S# U5 n // modelActions ActionGroup at particular times. This3 y! z# v8 Y6 g
// schedule has a repeat interval of 1, it will loop every
" J; @7 G0 y4 d) V // time step. The action is executed at time 0 relative to( |) i" |7 X* l( ^4 g/ s
// the beginning of the loop./ Q) d9 d5 u" l4 E8 \) w5 s
! D/ q" J7 {" l) K* c1 d
// This is a simple schedule, with only one action that is0 y) w. o% Z5 \1 d# [
// just repeated every time. See jmousetrap for more& y+ K6 h% C3 g% G
// complicated schedules.5 T, e+ O9 K- C5 g
! _% s/ F" g0 h( P0 O9 g: T modelSchedule = new ScheduleImpl (getZone (), 1);7 X5 D0 Z4 z# Y
modelSchedule.at$createAction (0, modelActions);7 k) \2 J4 \: m# D# a5 F
) J) f7 x7 z1 M
return this;
' a# e o5 I( N9 \4 J \' O) ? } |