HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 r. d; t) l$ O6 R/ r0 {
& S* v, s2 c; u$ @& b public Object buildActions () {
" ?9 u0 O L; y3 N* o( @ super.buildActions();7 E1 a7 `6 H" I- z+ X& g
$ e7 g x. c- U1 r# c
// Create the list of simulation actions. We put these in
& I1 z; R+ D/ O x* n1 x // an action group, because we want these actions to be
0 k, s. H5 k% ^5 T3 z- e/ h$ Y // executed in a specific order, but these steps should# P5 f" n! r6 Y/ x
// take no (simulated) time. The M(foo) means "The message. l! q/ A- e- F% I& w# N, b
// called <foo>". You can send a message To a particular% y x5 e3 v$ e. C! X
// object, or ForEach object in a collection.8 G- b. G, }1 ^# O8 _
: H$ ^ A% o* N6 g( P! \ g // Note we update the heatspace in two phases: first run
; }, y( E; q3 W. A( Q7 _& t, N // diffusion, then run "updateWorld" to actually enact the0 r4 @& Z, G& Q& c2 S3 Q+ `
// changes the heatbugs have made. The ordering here is. w, @+ z& ~) y* m2 e
// significant!' A R% A% ~ ] m3 u
2 [. L+ l" t7 o: y$ [/ x; G6 J
// Note also, that with the additional1 d% h$ B" V- q$ H4 J1 H
// `randomizeHeatbugUpdateOrder' Boolean flag we can- f# e T, G2 A7 V9 k+ p
// randomize the order in which the bugs actually run
4 T% d- ?! t( s' W // their step rule. This has the effect of removing any" f8 O1 Q3 m+ E. Z; G) ]
// systematic bias in the iteration throught the heatbug# V4 R1 Q, O% Q' ?6 o2 k
// list from timestep to timestep1 ]) p, Z7 `+ i7 w* H# o* E
. o* n1 o# M9 o* `% |. ] ]/ P* t2 J
// By default, all `createActionForEach' modelActions have
s, G2 e+ M4 I2 T // a default order of `Sequential', which means that the
1 i5 X9 {* v7 A8 U& L( U // order of iteration through the `heatbugList' will be" ?1 `( }) s3 {$ l9 a" a
// identical (assuming the list order is not changed
0 @% j* E8 i% g+ n+ m4 H* v0 @ // indirectly by some other process).
2 T, ^% x9 Z3 w' ^6 P
7 a: ?( h* h; N- R; F1 S modelActions = new ActionGroupImpl (getZone ());+ H$ a5 ?6 v( `. u+ |; b
, F& b% i- Q9 e- d- g: g) U$ e$ F/ w, u
try { H# c, o( |7 \9 o
modelActions.createActionTo$message
4 E; B# ]$ n; P# @3 G (heat, new Selector (heat.getClass (), "stepRule", false));1 h+ H# b, j) @9 o9 A* j
} catch (Exception e) {
2 {" X( G, E* b9 |4 L System.err.println ("Exception stepRule: " + e.getMessage ());
% c0 I# c" P1 B2 M3 j }1 e2 g5 j) p7 x9 X
2 g6 I9 o' t. ^: I! W5 m B try {7 B) x" j6 T; o/ i
Heatbug proto = (Heatbug) heatbugList.get (0);& O9 ?; D7 K& p/ @ R
Selector sel =
9 p0 t7 w& O, T new Selector (proto.getClass (), "heatbugStep", false);
/ ~3 F, [5 E& \4 J, @ actionForEach =: _7 A" @, R4 y6 X/ F
modelActions.createFActionForEachHomogeneous$call& E1 o, ?5 _: I$ s
(heatbugList,
9 t S: `; n. F8 F) @7 I new FCallImpl (this, proto, sel,) f8 L* Z! d, N1 M% b; T
new FArgumentsImpl (this, sel)));, A/ V( e$ U+ v3 `1 s: d' b
} catch (Exception e) {
. g. u. A& X4 n: R e.printStackTrace (System.err);
3 h& t2 b5 U( m }& s- \; p/ j( w) n# L( i5 m
/ A! g3 M2 i! T2 c
syncUpdateOrder ();
; k O ]/ u' m; \; e* ]% K8 o0 P# `& h
try {
0 w4 l1 Z* Q6 E/ r5 d; N& q modelActions.createActionTo$message
+ Z# t6 M. e6 n! K0 H1 m (heat, new Selector (heat.getClass (), "updateLattice", false));
7 L0 b0 k% v }9 }% ]- f' N, U+ O: S5 k } catch (Exception e) {
: f, G- L: a$ @0 R2 W$ q/ p J [ System.err.println("Exception updateLattice: " + e.getMessage ());
0 p( Q" `! C% r X1 @! }6 v6 \ }8 \( d* `0 A% M' A2 h
9 J% a/ n9 R- o // Then we create a schedule that executes the# c8 e0 m& S3 v1 k& X
// modelActions. modelActions is an ActionGroup, by itself it$ F4 E5 o5 L, v9 M
// has no notion of time. In order to have it executed in
- N% l7 [, ?6 U' @" G: K // time, we create a Schedule that says to use the
- j" N- E% d) ?5 D // modelActions ActionGroup at particular times. This
& s7 J+ w2 M) Z1 Q8 P* O4 i // schedule has a repeat interval of 1, it will loop every
7 B" X4 @; h9 @& T8 s // time step. The action is executed at time 0 relative to
1 _) p9 k% w' S // the beginning of the loop.
$ {7 R) C& R' V- j* i
8 M2 a& @$ A3 A: y // This is a simple schedule, with only one action that is
4 x1 s2 f1 p9 Z6 F, v // just repeated every time. See jmousetrap for more
) }9 y' K* }5 k/ H$ [% d // complicated schedules.3 q: z4 Z; H( S. s3 D! k; W% W( V6 P
: l" u2 A* A. o) c7 G# x! h modelSchedule = new ScheduleImpl (getZone (), 1); ~) b9 N9 x( K8 I1 a4 @! Q
modelSchedule.at$createAction (0, modelActions);0 n; j2 W: \% O- Q
- \6 Z7 T; `5 Z! E ?4 N5 ` return this;
" r. c$ X, A3 {; y8 o' Q } |