HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ c! P5 I- _5 X- x; J% L5 ?( V0 W
- R. z4 {% j+ A. \2 d
public Object buildActions () {
. W( [' n( f, {- Q; H+ c+ W super.buildActions();
( S8 m, U7 m3 L g$ a8 x; |
4 M6 O9 p8 m9 N. u# g // Create the list of simulation actions. We put these in
) [7 q ^3 E( q8 A1 f5 a" Q // an action group, because we want these actions to be$ C6 m0 m9 q9 Z5 n; e! H
// executed in a specific order, but these steps should. K L) V V# H# ]
// take no (simulated) time. The M(foo) means "The message T9 x7 x4 I, ]1 _7 I
// called <foo>". You can send a message To a particular" O* o' m8 g; |* I6 C2 x
// object, or ForEach object in a collection.
/ _, h% ~! O5 S: G. `
5 z. Y; h" @* \! r. e% R // Note we update the heatspace in two phases: first run
# I8 p$ A9 v) A/ W; J // diffusion, then run "updateWorld" to actually enact the
1 y( O' C% c2 k7 A6 b // changes the heatbugs have made. The ordering here is
3 s& O+ j1 _/ x6 A# c // significant!
4 \# @& @: l2 S ' }( {8 q1 ]) C
// Note also, that with the additional* H$ d* |4 D/ Z. o5 f4 M: L
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 ?% b( w. E6 @/ {9 b( G* m // randomize the order in which the bugs actually run* s9 v3 r* M' g$ x
// their step rule. This has the effect of removing any3 F1 k3 E; X& p u
// systematic bias in the iteration throught the heatbug
* e8 p5 f: S% b& U j // list from timestep to timestep
" y' I$ {; d3 h) N 9 q8 f/ D8 S7 E% T
// By default, all `createActionForEach' modelActions have7 i& q# V! W f
// a default order of `Sequential', which means that the
& b) V) }" i& f! v. q& D- F // order of iteration through the `heatbugList' will be
& T7 ]& u0 t& a F // identical (assuming the list order is not changed; B6 S3 O; D6 X C" w4 U
// indirectly by some other process). k6 u0 \* p# h5 e8 r' d% S3 m$ `! E
; s! R& U) g2 q, ~$ R
modelActions = new ActionGroupImpl (getZone ());. m0 ?$ U. A: ]
# M* G8 X2 S; y, M try {- R( s; _$ y$ y/ B4 `. \% F# Y
modelActions.createActionTo$message
: `+ N% s1 x! r) L/ } (heat, new Selector (heat.getClass (), "stepRule", false));
# P6 F1 L( ~4 h } catch (Exception e) {( s0 F& g4 j! g0 g2 l5 ]) q
System.err.println ("Exception stepRule: " + e.getMessage ()); o- n5 A9 g3 ]$ ]6 y0 t5 e( Q4 }
}
! L0 m, e! }9 d* U5 S
; \ T) A. E1 w' p9 k try {' V. j7 S. c) X# K
Heatbug proto = (Heatbug) heatbugList.get (0);( A; O) o% P9 d$ I" z* _
Selector sel = 0 i3 _, i/ e* i+ g% P$ B
new Selector (proto.getClass (), "heatbugStep", false);! O- ^6 g/ y" K; K W1 \# ^
actionForEach =
) r6 V. f B2 k modelActions.createFActionForEachHomogeneous$call; Y9 r) v7 L1 Q( o* U6 x: n
(heatbugList,$ ~1 A9 Z* k: a& D5 U; N. \
new FCallImpl (this, proto, sel,
2 l& T0 M9 z) ]3 B/ P8 A new FArgumentsImpl (this, sel)));: h9 O& C9 \5 P' s8 N0 O
} catch (Exception e) {
7 F: [2 i* {9 ^2 D4 ~# y e.printStackTrace (System.err);" q( g% }& U+ y9 E
}( @6 _$ c/ E& {7 {
$ q8 R& t. M6 R6 [) k syncUpdateOrder ();
' C6 P1 L% q6 v' e7 z) z$ i# O
9 t. R$ h& F9 E3 J1 m try {
: j0 o: N0 [9 f modelActions.createActionTo$message - A- l/ C+ U! ?# ?) `- L5 Y2 O6 @ z
(heat, new Selector (heat.getClass (), "updateLattice", false));
" N2 M7 J1 X+ n' p# C5 k: |6 B } catch (Exception e) {3 F$ v1 O- l6 x0 m# P, k7 c9 R
System.err.println("Exception updateLattice: " + e.getMessage ());
; U o( l/ T0 J0 g8 [7 Z }1 L3 L4 H. _0 Z1 n
I6 t) `" b, @ // Then we create a schedule that executes the1 v6 a; V. R# ^5 Y+ I* t7 U* I! L
// modelActions. modelActions is an ActionGroup, by itself it
% H8 `( l% K; L3 A // has no notion of time. In order to have it executed in4 m9 _: n" e8 h" F0 l2 |- u
// time, we create a Schedule that says to use the
2 T8 N. x% P/ t // modelActions ActionGroup at particular times. This# y K+ A9 h0 t: M4 Z, G' Z3 _2 p
// schedule has a repeat interval of 1, it will loop every
) G# g8 n& {2 s; E/ S$ d // time step. The action is executed at time 0 relative to
% _+ L# e2 Z; u4 {' h# l1 S! T // the beginning of the loop.
: V' Z9 R/ O0 W0 e, i) N8 d; N) I5 S9 R; M! U' P
// This is a simple schedule, with only one action that is' D y5 ^! v$ o
// just repeated every time. See jmousetrap for more
, m$ ?2 ?' `6 ?& u2 }3 i // complicated schedules.* X. q( k8 S* q4 E: }
8 m6 Z5 n0 X' h
modelSchedule = new ScheduleImpl (getZone (), 1);7 Q( `4 @" s9 l0 W- V+ l
modelSchedule.at$createAction (0, modelActions);8 K. v8 }* C i! D; f
4 W3 G2 S- r8 p# f* b% t) g, Y
return this;& h- e5 _4 M" i/ ~2 {! s: C) C
} |