HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ Q) C+ x% L7 Y
$ z# x6 s* `3 }( C- J5 x' p* x9 Z9 Q public Object buildActions () {
% x6 w, [0 k, S1 H super.buildActions();
" W7 o5 |4 h( {- g5 f" B: j
4 K+ n" f" [# w9 y+ s b8 n. l // Create the list of simulation actions. We put these in( b9 i. [2 [2 i& F+ P2 c
// an action group, because we want these actions to be
& O) X! O7 A4 q! H+ g3 I& y; l6 k // executed in a specific order, but these steps should* o3 F9 B7 \2 F1 f
// take no (simulated) time. The M(foo) means "The message* l3 T1 }* M# Q9 q& j3 }% |
// called <foo>". You can send a message To a particular
# L9 ?6 L* q9 S! r, G& d; W0 H/ R // object, or ForEach object in a collection.
2 J0 b( b9 m1 C" s( n4 {0 C 1 v. ~2 T7 S/ a1 E) d; j
// Note we update the heatspace in two phases: first run6 n- `' O+ q; C% ^( h
// diffusion, then run "updateWorld" to actually enact the
: ^- U2 C/ U6 i- Q // changes the heatbugs have made. The ordering here is
" j f+ d% b5 L/ k6 b // significant!; t t( S! ?8 h. {$ O& F
. ]) ^- L) Z' Z' A# O# {' d // Note also, that with the additional3 j( p% S) f3 N5 o6 K2 U( p0 O
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 q: c" W0 `+ C% H* N // randomize the order in which the bugs actually run
0 L5 f8 v' d0 F# S) K // their step rule. This has the effect of removing any7 j; l7 V: K ~, p9 C6 ]+ b8 [
// systematic bias in the iteration throught the heatbug
8 {, c i8 `* e* Q7 `; \ // list from timestep to timestep) m) U: z. ?& g) e! b+ h; X
4 D- F, A4 K" O: Q o1 o8 v
// By default, all `createActionForEach' modelActions have
/ N! \* B _5 a# T# [2 ] // a default order of `Sequential', which means that the
; S, F$ T7 e4 g; N // order of iteration through the `heatbugList' will be+ w8 `* z# _% F% E
// identical (assuming the list order is not changed
9 T1 W( l- Y- I // indirectly by some other process).
0 ?! y8 t/ Z R; l' r9 T/ |/ X1 x
+ a2 B) J! x. j/ j4 _ modelActions = new ActionGroupImpl (getZone ());# a# @* N5 I6 x5 M/ }
& E& T! `. u# {8 ?0 O try {
# I4 d6 @ y2 C( u2 i modelActions.createActionTo$message
# L; A8 j9 l1 F: e (heat, new Selector (heat.getClass (), "stepRule", false));
p% D' s) M# ^) Y0 S, d* O" Y } catch (Exception e) {5 C+ X# n1 s) q# @1 v6 y+ G# h
System.err.println ("Exception stepRule: " + e.getMessage ());
+ h2 h z- M0 w6 M# l5 R% U }
) c" j! b# X3 Y S; Q! F. C( N7 {. B' M" p8 w
try {
7 g8 q) ]9 O' ` Heatbug proto = (Heatbug) heatbugList.get (0);
4 U8 t2 {6 N7 @# |/ q6 i Selector sel =
7 C5 B1 y% G1 f; [) h' _ new Selector (proto.getClass (), "heatbugStep", false);, @. i) l+ |/ S* c2 S
actionForEach =
+ s4 Q$ Z7 u2 T2 R modelActions.createFActionForEachHomogeneous$call
9 W+ H8 h: e" ` (heatbugList,7 y# p2 U, M X: s( ?
new FCallImpl (this, proto, sel,
/ g" W) C/ s% v4 V3 n. u# V new FArgumentsImpl (this, sel)));: z* c6 e" ?8 [, r
} catch (Exception e) {
$ d% O* ^" @6 T" d7 P: t# c v e.printStackTrace (System.err);8 s9 N U2 R$ y) y4 G# \
}9 _. _' [. T& y, T5 M2 k6 e8 \
6 P0 C3 W+ _5 T1 P- b- S syncUpdateOrder ();
, U8 J, u, a! r+ C J/ |' |7 ]0 F4 l8 _- X5 n U3 P
try {
+ p+ |) Q9 y5 c: X5 R# S6 p modelActions.createActionTo$message * s" V2 R+ }5 h& \1 M* d0 y
(heat, new Selector (heat.getClass (), "updateLattice", false));: h0 @3 ~' ^$ @$ V$ O" N1 R
} catch (Exception e) {
& t4 z( b$ c1 ?# r9 } System.err.println("Exception updateLattice: " + e.getMessage ());, b# T$ t$ S) i/ b$ V( V, s8 ~
}
% n7 `; r% J3 c2 K$ K3 v: H
5 z2 v4 W% K/ b // Then we create a schedule that executes the' @- f2 u6 K L4 o+ i' Q
// modelActions. modelActions is an ActionGroup, by itself it
5 W( B9 q- \; P // has no notion of time. In order to have it executed in* \) q% O# a' O4 Y3 T! F# ^
// time, we create a Schedule that says to use the4 S! ]( G8 `+ }1 h
// modelActions ActionGroup at particular times. This% A* H3 t% [7 k3 J
// schedule has a repeat interval of 1, it will loop every6 a: g7 l! T. g. ~3 A ~
// time step. The action is executed at time 0 relative to# L3 Y" A% [9 g9 W
// the beginning of the loop.
& Z; K1 L; J6 k4 O0 q# }/ `
7 @9 r5 |* e0 i4 v+ \4 v3 ~- G% D // This is a simple schedule, with only one action that is
/ m$ p/ ^; l" F // just repeated every time. See jmousetrap for more& V5 ?6 `/ J* n1 h' P# U
// complicated schedules.
' K2 ^! @! `# E/ \/ J) _* X( B j$ r1 C$ g2 v( U4 z) I
modelSchedule = new ScheduleImpl (getZone (), 1);2 Y0 `% j1 g+ P7 H9 k
modelSchedule.at$createAction (0, modelActions);+ @" q+ T$ N& Z
/ q$ T! W- J. L& j7 | return this;
~" s6 J- a6 V } |