HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' s% j! j$ s9 t( `
+ r w7 p( f! J public Object buildActions () {& j5 M1 q3 h0 n
super.buildActions();% T# P; T( x3 @9 N- t7 \
# H. D, [& \/ t; F0 f' k- ~
// Create the list of simulation actions. We put these in4 g' \) _$ w" {7 k8 B; l4 n4 P$ F
// an action group, because we want these actions to be
+ R4 L' q+ X1 H // executed in a specific order, but these steps should0 ?* c3 k5 m, K4 J
// take no (simulated) time. The M(foo) means "The message
$ k6 D3 F" |8 K" Y% U3 D5 a4 C // called <foo>". You can send a message To a particular
, M3 j' {! }; D! p. k0 \3 ^ // object, or ForEach object in a collection. P8 M5 D9 f6 u9 A# E
' L$ @5 ]9 Q2 M" D% B- g
// Note we update the heatspace in two phases: first run
3 ?) v1 c6 \% S4 q; O, w // diffusion, then run "updateWorld" to actually enact the
1 p* H" J/ w% S4 ]( Y! s // changes the heatbugs have made. The ordering here is
8 K" _3 u. Y% b, T // significant!
]4 D9 ?7 h' f C f( F# s o 7 g' ?- ?- `- _" Y
// Note also, that with the additional
5 J: Y- Y. X9 @- P3 I. l+ r9 J // `randomizeHeatbugUpdateOrder' Boolean flag we can- o8 s: i3 X% t- T% C+ c
// randomize the order in which the bugs actually run- S) ], X; a6 q& _3 Z
// their step rule. This has the effect of removing any
) | b6 m8 ] C7 E. d& T' P* F% I // systematic bias in the iteration throught the heatbug
5 ` ?/ s/ z5 N. _' z // list from timestep to timestep& H- T- b! y* S7 X; @
: n: B, `" r4 J* k& T) ~" X
// By default, all `createActionForEach' modelActions have
' e9 D' n( R' Y% Z( V3 l8 ]$ G. C // a default order of `Sequential', which means that the. g4 u, [0 X/ v! @8 A
// order of iteration through the `heatbugList' will be
! K" v0 O3 l! ?. K' J // identical (assuming the list order is not changed
; ?$ Y1 x' w% T: J) c; Y // indirectly by some other process).. ?: W: i; c2 T7 i4 Z
# o2 K/ f" |# s+ s* }& d7 p modelActions = new ActionGroupImpl (getZone ());
6 \( f! M- s9 a/ ], A% p+ f" p
& j/ F) {, X: W* W6 Y1 a+ r try {
& Q/ W) W6 w( @ f; [ modelActions.createActionTo$message
; X8 T) W0 h- p. ^ (heat, new Selector (heat.getClass (), "stepRule", false));
: b: ^, x' V. K# j' a; U N } catch (Exception e) {9 I: D# s. Q$ T% v
System.err.println ("Exception stepRule: " + e.getMessage ());% c7 d: E5 d( v- c
}& \+ m4 a8 w6 n- U! c
, b$ q; Y9 x5 e! U, ^: V, C5 n
try {
0 ?$ o$ b# T. j Heatbug proto = (Heatbug) heatbugList.get (0);) X, D- b/ X7 r, [! p! k
Selector sel = ' k% R1 l2 r% P2 o
new Selector (proto.getClass (), "heatbugStep", false);
8 k5 `6 P" y0 Z$ Q! k+ x/ R, l3 r actionForEach =
* c# X, c$ n O. e; O2 O modelActions.createFActionForEachHomogeneous$call, H0 X- ?% S% t* w: H
(heatbugList,9 c! J1 N' J y) W( z
new FCallImpl (this, proto, sel,
$ }) w' ]8 d" r5 k new FArgumentsImpl (this, sel))); @" a$ {5 B2 C. m5 e; p+ |1 ~
} catch (Exception e) {
7 P# K4 z) {5 w* }4 C- k2 l. r e.printStackTrace (System.err);* C* C* R4 C3 a A) {6 P/ D
}* T9 n% E" J; Q e
* O M7 V8 c, p( B9 p
syncUpdateOrder ();% V; ~& `/ d8 q+ H" T2 ^: b/ J( j
1 b- f, P. D- I+ z try {
# w6 B# [8 f! o" j9 r5 T' d modelActions.createActionTo$message
$ @% {" s8 I; W5 C1 x: H# r& ^ (heat, new Selector (heat.getClass (), "updateLattice", false));
6 {1 P( W) l4 `) \, J5 K; Y } catch (Exception e) {
: z, t8 d2 a4 ?" k6 I w System.err.println("Exception updateLattice: " + e.getMessage ());5 W( P4 W$ {. ]
}7 ^" R1 G# H6 {# |. w! a0 [
8 @3 N0 H; o n0 h4 m" q
// Then we create a schedule that executes the3 F0 @9 n1 ~- Q5 l9 y$ K
// modelActions. modelActions is an ActionGroup, by itself it W1 M9 c5 t; K" @6 _; F8 n
// has no notion of time. In order to have it executed in
! K7 N% a U; w // time, we create a Schedule that says to use the
9 [ N5 G; L' Q1 M2 P" a$ F2 g. m! \ // modelActions ActionGroup at particular times. This
! j: i, U5 O) L& {: ` // schedule has a repeat interval of 1, it will loop every Z% U) U$ G' A' I
// time step. The action is executed at time 0 relative to
) o9 ^/ V* y8 n' U* b% T b8 C' m% f // the beginning of the loop.
% M3 W1 J# k" x# ]2 {& N; o$ E0 V* `7 c6 @+ b# w7 p
// This is a simple schedule, with only one action that is
# W4 Q0 `( r- _ // just repeated every time. See jmousetrap for more
: d7 F$ p4 z, @9 x // complicated schedules.
, R( b5 k/ T. ~% W: `( }2 i# |
# g% }( t6 B: Y; e G; v, L modelSchedule = new ScheduleImpl (getZone (), 1);) b5 M4 E o4 I' q: p! l, l
modelSchedule.at$createAction (0, modelActions);
+ A3 W. C' f. b. Y, o; ^/ H1 L) [ 5 m' w. S& h# l0 `) L) J$ R. |7 z
return this;* A3 n, ]# P; |: J
} |