HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! h/ X. n0 L& }9 d. p" t& L; c3 u( p0 [
public Object buildActions () {
. ^% Y: k/ h# l super.buildActions();
% V6 t! `. _& H8 z1 a6 `- U# v0 L
3 }9 F$ |- B, X. d! Q // Create the list of simulation actions. We put these in) e5 W- g: H: n& C9 h4 W4 _$ x1 l
// an action group, because we want these actions to be
( }0 e9 B+ d) A$ h* H9 x- L8 d // executed in a specific order, but these steps should1 l: J B5 o( L4 I
// take no (simulated) time. The M(foo) means "The message( I/ t7 ^" O- A" I
// called <foo>". You can send a message To a particular
: {% ^: e3 T! d // object, or ForEach object in a collection.5 W9 E& z# T& [4 `# p& W' t. |" G( F
$ D3 _! D0 d" J
// Note we update the heatspace in two phases: first run7 c* _# @. G' X8 |. ]
// diffusion, then run "updateWorld" to actually enact the, {0 h: n. ]) H! r
// changes the heatbugs have made. The ordering here is
; `% U% S: R5 C4 X S* G+ d // significant!
) P$ q D# [$ U) a% E+ X / N) Z# [( e \0 K l% B, |
// Note also, that with the additional
3 r8 H l+ f2 O1 S @ // `randomizeHeatbugUpdateOrder' Boolean flag we can1 O. G# l; m; w" v7 i# Y
// randomize the order in which the bugs actually run" u( y7 _8 r7 n& ?
// their step rule. This has the effect of removing any1 l& l' \) z. v
// systematic bias in the iteration throught the heatbug
; ?0 L! M! \5 f // list from timestep to timestep
: D* c: E* h8 W0 \6 B% A 4 E2 s2 Q( v( }2 f" m
// By default, all `createActionForEach' modelActions have( s4 o+ N6 V. t$ c, ^/ Q$ M) g
// a default order of `Sequential', which means that the
/ x1 y/ T" w1 e2 E" l$ S // order of iteration through the `heatbugList' will be
& ?% l' m3 C/ i( C/ j; Y // identical (assuming the list order is not changed1 i# `. x+ ]* t; V
// indirectly by some other process).
; z. f: r2 k2 k! I1 V/ N, D V! t
2 q5 H8 }* [) \ modelActions = new ActionGroupImpl (getZone ());8 I+ |9 t+ K8 j9 [" f0 N
2 {0 P' }/ D3 K( F! [: |
try {# W0 b* F: G8 Z, L1 W+ P+ {
modelActions.createActionTo$message d" ]- x( {! P7 n! }
(heat, new Selector (heat.getClass (), "stepRule", false));8 W4 I8 s3 N6 M0 e
} catch (Exception e) {$ N; m& ^2 w6 \9 g _" i
System.err.println ("Exception stepRule: " + e.getMessage ());
7 z8 v8 U$ P5 p7 } }
9 `2 S6 W0 B7 `) m5 `9 G& `* Q9 R2 }7 ~9 e) c5 Z
try {
5 [0 g( B; x1 @8 {, O8 K, v Heatbug proto = (Heatbug) heatbugList.get (0);
) O8 }; L2 a, @5 M$ B9 a1 b Selector sel = % U9 o" I: x$ @0 E) P9 ~
new Selector (proto.getClass (), "heatbugStep", false);
, ^! C( {" v% e6 b5 }# @- Z1 A actionForEach =3 @: b$ X. G- ]6 M( j- `
modelActions.createFActionForEachHomogeneous$call
( X5 o, H( L. y (heatbugList,
' s- a; M1 {9 h new FCallImpl (this, proto, sel,
2 K' O+ N; f; C( k" e& _- t new FArgumentsImpl (this, sel)));% O+ v5 e3 S* ^% t. [. Y5 A/ \. u# o
} catch (Exception e) {
' t# c4 n! h) i! F ^) l! c1 E/ g" d- ` e.printStackTrace (System.err);+ N/ O7 K y! B0 Q- Y. m
}
% d- ~) d; F$ | $ M* W0 ~0 C. o/ r7 ?6 U( z7 T
syncUpdateOrder ();
7 r4 P9 _( ~6 v* L) S
! s' q, M& `: r$ H( ? try {# i% ?; h6 c$ M* G, A6 a
modelActions.createActionTo$message ; P/ v: ~5 w' D! }( ?
(heat, new Selector (heat.getClass (), "updateLattice", false));+ }1 B. H0 X) z" _
} catch (Exception e) {
$ s5 B+ X: _+ s+ z, S System.err.println("Exception updateLattice: " + e.getMessage ());
7 F% @. \0 A. M( x7 |& [ c2 Q }
: @6 I. v5 o6 g9 [7 M
2 V/ t9 ~: y v! m( A4 n // Then we create a schedule that executes the
, f6 b7 T$ Z7 `3 t // modelActions. modelActions is an ActionGroup, by itself it
/ m) K* L" ?) {( c* z // has no notion of time. In order to have it executed in
7 d) Q, G3 f7 W6 O // time, we create a Schedule that says to use the( T) `, G* |2 W
// modelActions ActionGroup at particular times. This6 b" [( Z; C' w O
// schedule has a repeat interval of 1, it will loop every
) K! m( U' b, j) v1 D4 f& w // time step. The action is executed at time 0 relative to
- i+ r0 m4 B& q // the beginning of the loop.) t/ u/ E8 y, w# ?
% m+ ?; D' j# H, ^6 ?: {
// This is a simple schedule, with only one action that is- I7 S' h) M2 F
// just repeated every time. See jmousetrap for more
1 m2 F/ X" d& c* b2 d& C2 X // complicated schedules.! a9 S" V8 i* m' P$ A/ `
1 f: s% v$ {* T$ M# | modelSchedule = new ScheduleImpl (getZone (), 1);
7 e5 V* D1 ]* n9 Y/ E2 ~2 n- z modelSchedule.at$createAction (0, modelActions);
l* f/ W$ I3 d2 L2 A : H- A; e2 R. ?( E5 _$ N
return this;
, g2 j) A+ V" l& q) P- y } |