HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. E. ?( d* [6 r4 E2 i
2 N- _% N0 I) Y# o& c$ _ public Object buildActions () {
: y# a; `; {# I# A7 a6 F2 j% z super.buildActions();
' P; c: Y& N( v8 a
3 w# \8 z( O0 Z1 Z$ u7 [/ [ // Create the list of simulation actions. We put these in
/ I4 F, o, v% Y2 M3 W5 x // an action group, because we want these actions to be! p: _' V5 \, A( ?
// executed in a specific order, but these steps should
& f4 X6 y; l0 d$ r4 L // take no (simulated) time. The M(foo) means "The message
6 B3 j1 ?. h4 ?) U4 a) B; h& B // called <foo>". You can send a message To a particular* U X8 f1 V% z% Z" j
// object, or ForEach object in a collection.6 `2 B5 U1 F% |, `7 s. t% J9 p
1 H: W7 s6 t l+ q- L9 ^- k
// Note we update the heatspace in two phases: first run; }9 N6 }% U- @$ j
// diffusion, then run "updateWorld" to actually enact the* `/ Q' D7 J# ~# t h8 [
// changes the heatbugs have made. The ordering here is
, ^" u) p% }5 T3 i. D1 p9 ^) ~, N // significant!: S# [* H/ A0 _
$ z7 g9 h1 A0 x# c- w+ D4 u
// Note also, that with the additional
4 E! X) S+ ^* d W* V% D // `randomizeHeatbugUpdateOrder' Boolean flag we can- B5 p( v& m: ?5 X* O
// randomize the order in which the bugs actually run
( ^0 s+ B& J* ?0 q6 M // their step rule. This has the effect of removing any+ {8 I, V |2 I; E a" `" a
// systematic bias in the iteration throught the heatbug
& m, z2 ^" d( @' |+ z& K1 e // list from timestep to timestep4 {3 C6 b* \9 C& ~! A3 K
% m; s6 T& u$ z7 z // By default, all `createActionForEach' modelActions have( s( v3 f4 P( Z0 s
// a default order of `Sequential', which means that the
7 l0 H& w4 u( }3 p/ @* s# x$ ?& | // order of iteration through the `heatbugList' will be3 V4 O) n8 E, ?" W& {/ H
// identical (assuming the list order is not changed
# F$ g9 I) r# G; ^1 c // indirectly by some other process).
- N/ T# K4 W( B
$ n) T1 J) L% G1 J modelActions = new ActionGroupImpl (getZone ());6 z ~& K2 J' D% w. i* j
& _! Q Q V2 P8 e; g3 q try {; ^/ {$ n# B# m* U& a5 ?# h
modelActions.createActionTo$message
! C7 e; L4 _# l (heat, new Selector (heat.getClass (), "stepRule", false));) ~" \) O0 X5 I6 ?; {, E2 t5 t
} catch (Exception e) {9 `7 [' c; g, M) g; x( |. h6 z
System.err.println ("Exception stepRule: " + e.getMessage ());- I- ]+ X3 O" }4 N/ b
}
2 n. Q8 k1 j4 C/ d' a% b3 G$ D. g" W5 R; E2 O
try {7 y6 K# x- a9 h0 l5 q2 F$ z
Heatbug proto = (Heatbug) heatbugList.get (0);/ Q* Z) V6 J# n6 `1 h; h. T
Selector sel =
3 Z+ K" f# l$ w9 I/ }) o% m7 h! K7 Y new Selector (proto.getClass (), "heatbugStep", false);
. x Q( w. |& y" W% i actionForEach =
7 c) l3 G: X' P9 b modelActions.createFActionForEachHomogeneous$call
! h' Z. e5 t" Q6 R7 ~ (heatbugList,
' x! o) S/ B) P) d new FCallImpl (this, proto, sel,
$ x V$ M$ m! Y. J. F- L5 g new FArgumentsImpl (this, sel)));7 w" W% L/ s; h% q& ~/ d ]
} catch (Exception e) {
. _/ b. R1 F: i* u9 s+ l% ? e.printStackTrace (System.err);
0 m* U+ _& k" H0 |7 n9 h/ M }# E, o( q) q& ~: S: E* ~4 E; g* w
7 V P3 X& ]& k) u syncUpdateOrder ();
* M, [* u; y" J% F) i) r
5 g# u' k1 c O7 {0 _3 l try {- l( A$ o3 {2 [ r# J
modelActions.createActionTo$message . S- P( z, n- h3 X+ ^' J
(heat, new Selector (heat.getClass (), "updateLattice", false));5 [7 Q2 }+ p3 ?0 ?
} catch (Exception e) {
: \! Z$ u* |& a! D) P# n1 z System.err.println("Exception updateLattice: " + e.getMessage ());3 u7 N* `) B/ @: ]% S8 A/ j
}
4 X' _& v% L) S& S1 m 5 b+ {8 \- s3 r& A2 y. }; E4 X
// Then we create a schedule that executes the- ~0 {! _- b1 r9 T! e
// modelActions. modelActions is an ActionGroup, by itself it
: R/ [9 Y! l4 A: l& | // has no notion of time. In order to have it executed in
% d# l' G. G" y3 {' \) R7 F // time, we create a Schedule that says to use the
6 `: y' ^$ g/ | // modelActions ActionGroup at particular times. This5 w& g' W6 N: C& z6 K6 B
// schedule has a repeat interval of 1, it will loop every0 G* {$ L0 c% M2 u
// time step. The action is executed at time 0 relative to
. G' l. _3 c4 o; Z: i7 X' a6 B // the beginning of the loop.
! Y$ j, }* y( m* E1 S2 c
% t7 O! _# k' [6 }, i# D // This is a simple schedule, with only one action that is5 k2 q0 ~ P3 N, u
// just repeated every time. See jmousetrap for more2 d; C, y% j$ y: J6 S. a+ l, S
// complicated schedules.
5 `8 ?* p" I6 J/ h6 w 0 ~/ c- J9 Z* s. \5 {
modelSchedule = new ScheduleImpl (getZone (), 1);, |! X8 w/ Y, ~8 d: o& w; K# c M! A
modelSchedule.at$createAction (0, modelActions);
# S. p( L7 `9 q- q
' m) C$ z; x; C+ P3 L. I return this;
; P c( S; g- q: q1 V( F } |