HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' i/ n/ N- |+ z. v
0 J6 l+ {+ S5 U0 E6 \# m public Object buildActions () {( Q; w J/ s% {% n% C
super.buildActions();: U- ~9 h1 y) D$ M/ Y
5 |3 ~) o6 o" C
// Create the list of simulation actions. We put these in
- d+ l J a) W$ F, E // an action group, because we want these actions to be" K# Z s9 a4 Z/ _% j
// executed in a specific order, but these steps should# R- K6 q h4 B& s" z& r; y7 ~
// take no (simulated) time. The M(foo) means "The message+ g6 W1 P. v3 e- g
// called <foo>". You can send a message To a particular
1 H5 ?+ e! B1 a! d // object, or ForEach object in a collection.
; n+ S) w" l+ d/ K( m5 J
# S0 R8 C6 E5 t2 o7 E/ o. r // Note we update the heatspace in two phases: first run+ }) f, ^& y0 k% C! A1 |
// diffusion, then run "updateWorld" to actually enact the* P& y" L1 p3 m; z% O
// changes the heatbugs have made. The ordering here is( t: V* H. W2 d0 ]: U
// significant!
4 z; I6 e7 T9 k9 k) V( K % L* t: J3 n. R
// Note also, that with the additional
N& [& Y2 T. J. T) f // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 u" P/ p5 [% \# l1 B( o, S // randomize the order in which the bugs actually run5 S$ J2 V: L$ R9 B3 [! a7 k5 w
// their step rule. This has the effect of removing any
$ {0 Z) a( e6 ^/ X* ~$ o // systematic bias in the iteration throught the heatbug) [% }0 s4 G) B. h
// list from timestep to timestep
" o0 R- i: I1 c" v/ K1 t % u* D* R" r- d; F) E
// By default, all `createActionForEach' modelActions have$ |# x+ Z% ^7 s$ c+ @% X
// a default order of `Sequential', which means that the
* g- ? ^; Z* j& R // order of iteration through the `heatbugList' will be) B- [6 F, x* C) L" R7 \7 k. p
// identical (assuming the list order is not changed+ I- Y) L8 M$ c# ?
// indirectly by some other process).
: j1 c* \% q. a% _
$ x k' A7 b1 q/ J0 v modelActions = new ActionGroupImpl (getZone ());
( h J; u4 e8 R; T" j0 S3 Z, f/ L
) x" J) v! m& @" Z try {4 c; g. f* O; k0 @+ {* E9 ^: z) i# U
modelActions.createActionTo$message7 { U$ ~8 K* Y' E& U
(heat, new Selector (heat.getClass (), "stepRule", false));" [- I7 g; v4 w) I S+ K
} catch (Exception e) {
2 c. n: R% v& V$ V7 S$ N System.err.println ("Exception stepRule: " + e.getMessage ());; ^- {) ~$ Q% R
}
/ p; D* j. ^# n; d$ @1 E& h+ q. K2 L. a- l. w7 r) A1 }+ k; F3 M" {: Y
try {
7 J8 c: A: r2 r Heatbug proto = (Heatbug) heatbugList.get (0);
: a+ R* B8 h: L# z5 T* o Selector sel = " h3 M7 Z! k: ^( h
new Selector (proto.getClass (), "heatbugStep", false);
9 k& V# Y& n2 R0 w actionForEach =; D: b! c; M- p% ?2 R L3 d
modelActions.createFActionForEachHomogeneous$call
3 x& r1 j2 a9 ]4 t5 H2 x0 ^) A (heatbugList,% \1 y v6 I I& N/ V
new FCallImpl (this, proto, sel,
& c7 [* n* ~" z5 I1 e, i' ~, F new FArgumentsImpl (this, sel)));* e& H/ N4 d# x$ V* W. S) z6 N' K
} catch (Exception e) {
) T1 p! k) T* y: q1 V* w% _. h e.printStackTrace (System.err);3 f. B6 @" @7 i* C. b
} w0 J9 I" _! Q1 \
% g% T5 w+ g8 {5 U syncUpdateOrder ();; B$ j/ j" b0 K/ `3 V6 d0 e ?
: w6 Y/ W0 T3 H/ o3 ~
try {, P( V6 o/ w: Q0 \( ?8 F6 ]- l( a
modelActions.createActionTo$message ( M4 i$ z$ Z' T1 `- Z
(heat, new Selector (heat.getClass (), "updateLattice", false));* ]* ~6 S! L3 s( s$ J8 r
} catch (Exception e) {
: f! n5 P1 {" r5 P System.err.println("Exception updateLattice: " + e.getMessage ());
. C, H3 q+ I" v& h+ R( F8 B }
W/ u3 G# F8 `, Y7 [( C/ B
6 _, |; \; a: z! Z // Then we create a schedule that executes the
. ?* _/ `7 S5 j* M9 t2 P7 F9 b& j // modelActions. modelActions is an ActionGroup, by itself it
) h' u- |* C- e+ i# E3 M4 w // has no notion of time. In order to have it executed in- Y+ o' k3 `0 K3 W D4 O
// time, we create a Schedule that says to use the
/ i' \' K: I, |5 G // modelActions ActionGroup at particular times. This
4 q* [* b2 y# Y // schedule has a repeat interval of 1, it will loop every
& s9 ^9 Y9 R$ u6 c/ w0 {1 m // time step. The action is executed at time 0 relative to9 q- {6 c% U- B. n5 O0 O0 F* A3 K
// the beginning of the loop.
1 [4 d; J% M& E: T- S( I
) I4 i& w i) F1 W+ F f8 A; I // This is a simple schedule, with only one action that is- }# s. A7 U5 l
// just repeated every time. See jmousetrap for more
4 L. z. J2 e; [5 ~) F // complicated schedules.6 \0 O2 p6 W: h- i" _, ?3 Q! X7 V
3 ?; Q+ Y% F3 p9 b5 p
modelSchedule = new ScheduleImpl (getZone (), 1);
6 a6 _# n8 B" @6 o( h$ s% D% u modelSchedule.at$createAction (0, modelActions);
1 f1 ^0 R7 T7 v. a8 a# _6 ?1 {! T
/ Q1 r: @* K! J$ d. M" a& l1 ] return this;
" G" `( C( u2 S5 F; ~ } |