HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 C5 S' u* r, M* k: H w6 K0 ~6 U* o' \: U4 s: D) U
public Object buildActions () {1 n4 e' x; [8 u) M3 X3 j8 Y; w
super.buildActions();+ m" w, [8 K P. i1 |
3 C K& e6 s$ J7 k0 [, h1 W
// Create the list of simulation actions. We put these in
- N# U9 H, Q) m) L4 y // an action group, because we want these actions to be
7 {, u/ e8 N! ?5 Q" Z# H* h% ^; r // executed in a specific order, but these steps should8 Y9 y2 u2 s3 P# `
// take no (simulated) time. The M(foo) means "The message
' |: Y0 f3 j% }8 V // called <foo>". You can send a message To a particular
# `% u, d! e" A1 ?) r // object, or ForEach object in a collection.
7 w+ m0 J5 r9 v6 Z/ g, F6 U! s2 T % X6 n: d6 V* A6 g; ~4 b
// Note we update the heatspace in two phases: first run
# Q8 N0 V# [4 _2 M) d+ t // diffusion, then run "updateWorld" to actually enact the
. V7 s9 \; T% L9 z/ \; G/ S9 A // changes the heatbugs have made. The ordering here is
5 C, k+ o: T# Q6 f // significant!8 }2 r' |4 E( w: g' v7 F3 d
# {, ]" J! |2 r // Note also, that with the additional
9 o5 x0 ]+ {, F# a: S+ s. X8 q* R // `randomizeHeatbugUpdateOrder' Boolean flag we can: z- ?* a' }. N" m
// randomize the order in which the bugs actually run
' w/ o+ N9 t, s |9 J& P& b' ?" O // their step rule. This has the effect of removing any4 r7 s+ Y! M6 o# `+ z
// systematic bias in the iteration throught the heatbug
% B" a' I; p" p( g9 G( @# b9 f/ {: u // list from timestep to timestep
$ u: O$ w) F' }" z% Q) J 8 o& _4 O+ R+ O. Z7 B
// By default, all `createActionForEach' modelActions have
B% H6 |0 k& f, N! | // a default order of `Sequential', which means that the4 \* u7 i# }- z. X2 i7 ]
// order of iteration through the `heatbugList' will be# D+ ~3 ]! F9 q! n& S/ [
// identical (assuming the list order is not changed! a! i) \! W" J& f- e
// indirectly by some other process).
( R9 u7 c" D5 l4 V+ _# U/ G3 } : o% \( I1 {/ m% G2 c
modelActions = new ActionGroupImpl (getZone ());
" G; z: ]' ^ ~
3 g S6 x, r' Y* y. {, O4 y try {
+ z) B$ m4 W7 [8 C& v! V2 R modelActions.createActionTo$message
6 S9 W9 b7 [( b) \ (heat, new Selector (heat.getClass (), "stepRule", false));
1 N, w7 i* W, Y } catch (Exception e) {3 C( g! `# [* ], I# N
System.err.println ("Exception stepRule: " + e.getMessage ());$ N4 v* a5 z* G8 d7 n# m6 f
}/ [# _( `- x9 z! R, J
3 u4 K( g; p! y3 |
try {
) i C; s: S1 [7 O. n; _6 U Heatbug proto = (Heatbug) heatbugList.get (0);
( O9 Z/ D) U u0 b% k6 e Selector sel = 5 p; [% j5 ^; v# c
new Selector (proto.getClass (), "heatbugStep", false);$ `# N/ ^5 f3 ^0 \
actionForEach =9 o# L0 B) W# k2 P: O
modelActions.createFActionForEachHomogeneous$call4 k$ o" t" o; d; {* X' C% N9 p
(heatbugList,
% d0 W- h: @8 A new FCallImpl (this, proto, sel,, ^/ ^( F; c6 G9 d/ a2 G+ S
new FArgumentsImpl (this, sel)));
) l/ l* r: x6 \" Q+ f/ U8 R0 } } catch (Exception e) {
. O: m$ Z4 S% C$ Q* ` e.printStackTrace (System.err);
8 B z7 }3 R7 s }
a: [0 }9 b* Y1 N3 j9 Z + v" g; P) S% I- u3 H) c5 N+ D
syncUpdateOrder ();
. t. x+ n3 X% ?1 @2 w, I+ |5 b& n
" O; i+ J2 Y! H* |* | try {! x% n7 _ K6 e. g+ E
modelActions.createActionTo$message
3 k3 v# B+ K0 X% b7 v, w# j# A (heat, new Selector (heat.getClass (), "updateLattice", false));7 Y; h/ M$ ^, z
} catch (Exception e) {
# `3 U2 I9 v, w% O1 t3 O System.err.println("Exception updateLattice: " + e.getMessage ());& t2 s3 f" s/ B& R( O
}& ]" k7 `2 i& g: a% `
% m9 N4 v: S2 G. v# U6 C // Then we create a schedule that executes the
& b/ V8 v& v) t, _; ~; ` // modelActions. modelActions is an ActionGroup, by itself it4 F5 p. o9 T6 K0 q9 p& P, }
// has no notion of time. In order to have it executed in
# c* E% [$ H) u' Q // time, we create a Schedule that says to use the
7 @) P3 p* q; Y: \) [ // modelActions ActionGroup at particular times. This
4 \# J/ c+ x5 ~0 g // schedule has a repeat interval of 1, it will loop every: x! c- `! t/ L
// time step. The action is executed at time 0 relative to( c2 E$ A. o# Q* U/ u" @% b- K( N* b
// the beginning of the loop.
) Q8 Q4 t" G; }! f5 h8 Y0 Z
% w% T W3 r: R2 R: R" R% g // This is a simple schedule, with only one action that is2 O* k2 E- b! \/ T; A$ N# y, [
// just repeated every time. See jmousetrap for more
! o( g* j# K! B3 T+ j // complicated schedules.
+ W& @' S; i0 v! l * I# C% N2 `6 z
modelSchedule = new ScheduleImpl (getZone (), 1);) I, H$ C5 a# X. Y' n+ a7 C
modelSchedule.at$createAction (0, modelActions);) ]9 a5 |8 [+ v' K* [2 Z& [
* Y; m$ M5 ]; }" B% k" C$ X9 T
return this;
+ }* j( j8 y0 O } |