HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 E6 C7 _5 P1 Z: ~- }# K9 W! s/ `3 W$ i
public Object buildActions () {
0 B9 J, W! C( Q, A( m% p7 {; ~+ H super.buildActions();$ O& L6 _. C0 \' K
) D1 K1 X* Z% `; P c // Create the list of simulation actions. We put these in
2 Z. i5 v( B+ Y% c' [1 u // an action group, because we want these actions to be- Y; [2 L; A7 j/ P+ \
// executed in a specific order, but these steps should
g9 V. Q+ |6 v5 a+ b# W& T0 a // take no (simulated) time. The M(foo) means "The message8 n5 Y' S9 y/ S; ?; I: @
// called <foo>". You can send a message To a particular
8 B% r, {4 w5 U3 I, O: z% h# a // object, or ForEach object in a collection.' h* N) d) o2 \- C- f
$ T( s2 a0 ?2 M; Y // Note we update the heatspace in two phases: first run5 k( I2 X( c; f
// diffusion, then run "updateWorld" to actually enact the
! [" g, U8 f" \1 Y // changes the heatbugs have made. The ordering here is
, o9 d5 a! {) G- i // significant!( a, ?1 x5 P/ {$ ]8 i7 d
2 j0 p& J- z: N) s+ m8 ]! |
// Note also, that with the additional3 z8 N2 w+ Y( f! e) j8 a
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 Z7 T" l' E: ]2 Q a7 N
// randomize the order in which the bugs actually run
, f) ~' M1 V+ n // their step rule. This has the effect of removing any2 s, C. v8 D3 K7 M. c
// systematic bias in the iteration throught the heatbug+ N$ B8 ^8 t1 ^ W2 m. F
// list from timestep to timestep9 S8 {2 d2 M* Z& e8 I& c
( J8 u4 h8 P. E% ]5 a // By default, all `createActionForEach' modelActions have
0 S& i S, ?7 d8 T: Z% M // a default order of `Sequential', which means that the9 ]9 J+ q' k# |; i* u
// order of iteration through the `heatbugList' will be
2 z+ o) S% K6 H3 O/ @3 R // identical (assuming the list order is not changed
: |2 ]/ l' x6 m) u // indirectly by some other process).
6 g7 U& `( R( m # z$ M/ u0 w0 T' f: {1 N) ~
modelActions = new ActionGroupImpl (getZone ());& ~- t3 }; Y2 i2 O# V1 W: X! s w
. d5 v, e4 i w) b" |# f
try {* w9 Z' q4 E9 @$ f: b( X
modelActions.createActionTo$message6 \" j5 h9 Y( P- |
(heat, new Selector (heat.getClass (), "stepRule", false));) b1 P8 A2 R) ~' }; l
} catch (Exception e) {
* i* B" f! g. h# ` System.err.println ("Exception stepRule: " + e.getMessage ());8 D2 C' P+ a% R( g$ }* U
}
5 b5 t* B- l0 F; I, F' P# X0 I [6 H% g. V7 A# O
try {' Z2 M7 K# j1 x
Heatbug proto = (Heatbug) heatbugList.get (0);: u$ u" {( r. ?) o: C
Selector sel = . R* S R9 t5 \. e5 a
new Selector (proto.getClass (), "heatbugStep", false);
9 v6 x' @" M) f* T3 B8 ]3 t actionForEach =
0 @: f4 B0 j. x4 q& x% O& H e- q8 A modelActions.createFActionForEachHomogeneous$call# T9 h a* E' M1 {. a
(heatbugList,
; ~8 {- y' t5 s+ K new FCallImpl (this, proto, sel,6 t. _/ F- ?) A" q' V) y& J8 v
new FArgumentsImpl (this, sel)));8 i- y2 q- b6 Y2 r( w T
} catch (Exception e) {
, _9 _; p! b4 {# t e.printStackTrace (System.err);
( Y2 Q9 F9 z7 ?/ l }2 q, @% I7 u8 N+ B8 M# z$ R6 D
' t7 Z6 ]; g) r$ Q
syncUpdateOrder ();1 @' w# z6 P2 B2 G Q: b
3 O4 ?, t/ q1 @' m try {( ?* B- D+ c) Q7 J( q
modelActions.createActionTo$message 5 S( b' q4 ?. l$ m
(heat, new Selector (heat.getClass (), "updateLattice", false));6 a: B; i" Z# x) r5 N- r
} catch (Exception e) {7 }6 v# O- V! {8 P5 R" k: }
System.err.println("Exception updateLattice: " + e.getMessage ());
( i" w8 \" g2 v1 ` }% p9 j# G" J9 I) `
# |7 t9 h5 o* B
// Then we create a schedule that executes the
5 {& b5 j8 F0 p5 o* ]& u // modelActions. modelActions is an ActionGroup, by itself it
7 q ~" B c& S/ }$ A // has no notion of time. In order to have it executed in
. T# |, N$ {1 \ t // time, we create a Schedule that says to use the( z$ C% B9 o6 R1 o& u
// modelActions ActionGroup at particular times. This
* A* w0 f7 @5 h: @3 I* Z // schedule has a repeat interval of 1, it will loop every& L* {; j' B* D5 K
// time step. The action is executed at time 0 relative to
% \8 }# y& ^2 ]7 a& R$ m // the beginning of the loop.& x" k0 t/ X: `* [: p7 U! d9 ?
& C8 x3 B$ p3 [, G u // This is a simple schedule, with only one action that is
: Q; Y! Z9 ?: l) y8 L% [/ A! p // just repeated every time. See jmousetrap for more4 z# z) t5 P/ _/ [( ^& |! c7 ^
// complicated schedules.
( r( y5 J5 g: m% c5 x4 K7 \- S 9 I' V A0 t5 T4 p! {
modelSchedule = new ScheduleImpl (getZone (), 1);
* w! Q; |0 l" D% o: S modelSchedule.at$createAction (0, modelActions);2 y; v/ p! o$ |. j; f1 R, {: n
5 \5 \# O2 K$ c* n return this;- @- x8 w' j& [( O! e
} |