HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 Q& @: u& n; U
4 w! W8 q- f( q$ g3 u/ c public Object buildActions () {
2 Y- }' l4 z$ C- V super.buildActions();
# R+ T0 p& J, J7 K: s7 i
. X8 _" E9 F1 X // Create the list of simulation actions. We put these in
: \. Q+ J1 h( c3 q9 F // an action group, because we want these actions to be
* l8 s* \( d1 d8 H$ l // executed in a specific order, but these steps should# g2 \- ?4 S5 p# d
// take no (simulated) time. The M(foo) means "The message
& g) f6 H* w, t // called <foo>". You can send a message To a particular. S& f$ c$ J6 e$ p3 l7 W
// object, or ForEach object in a collection.% z: ^: ?5 K5 R* {8 i
* u- q" B" D, V5 R // Note we update the heatspace in two phases: first run
* ?: ^; k8 z- |; g // diffusion, then run "updateWorld" to actually enact the
8 [9 p, J/ P V+ q. i$ ` // changes the heatbugs have made. The ordering here is+ g: ~& M4 { S- y+ c6 W
// significant!
& x5 H" R" s' F6 B: q) f r. |) c" }% L4 w( C
// Note also, that with the additional- @. l4 L# d U$ n" {
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 z& x5 S$ n [
// randomize the order in which the bugs actually run' y4 ?+ L* @7 W g8 J
// their step rule. This has the effect of removing any# }; H0 n% w' x
// systematic bias in the iteration throught the heatbug* @" |3 F- ]" I e
// list from timestep to timestep6 c% t* p# K* m3 t! Z1 G0 r$ W
, J' D' S3 W; K- I, ?& u
// By default, all `createActionForEach' modelActions have
# a! k# \* V; d8 B // a default order of `Sequential', which means that the- ^! A5 v' l0 ^! ?! h c. Y( O
// order of iteration through the `heatbugList' will be
! Y0 X2 w! Z @. d8 v3 l; | // identical (assuming the list order is not changed- h' Y; o3 z( b( u
// indirectly by some other process).
6 g9 N v! g. x' g3 Y; O6 z* S, ^
& a: ~6 b: c* y8 Y modelActions = new ActionGroupImpl (getZone ());
# `5 l5 f) V$ [9 p
& |9 H" q9 {1 |) j: C! z; P try {
; `; ~' l# O7 f+ Y4 f8 N/ S6 X7 u modelActions.createActionTo$message
/ ]6 \7 X9 D7 X: Z. K( Y; E$ E# Y (heat, new Selector (heat.getClass (), "stepRule", false));) W. u% q7 ?/ O* u" n
} catch (Exception e) {8 B7 V" [$ M. f
System.err.println ("Exception stepRule: " + e.getMessage ());
! F* N, @& k3 s } g2 J. k& U: g1 {5 _
! v- C' l8 p" ^ try {
) ]6 l# a% B0 m% O3 T Heatbug proto = (Heatbug) heatbugList.get (0);6 C1 E% @! c2 W. R
Selector sel =
) w% H ~+ v" U# w7 C! v new Selector (proto.getClass (), "heatbugStep", false);
5 O3 ^# Z) `7 k; [7 C5 \- r. ] actionForEach =0 f; X& c# {! W2 `6 _
modelActions.createFActionForEachHomogeneous$call
/ ~" L6 E' s1 \, m X( b' o( S (heatbugList,
4 N, ]) ]; f3 w% P% r new FCallImpl (this, proto, sel,( p; r% g3 g! u! ~+ D, c3 P
new FArgumentsImpl (this, sel)));& A5 S3 P& N" ~7 Z' A2 R P3 l
} catch (Exception e) {
" @" R- h5 S6 w! w e.printStackTrace (System.err);
5 L: L7 c) A' u( i# c }
& _7 |6 a/ c; Y8 w; ]: `2 @0 d b3 X * B: ?5 P9 z% n9 s8 A5 c; f
syncUpdateOrder ();9 E# E( F6 |1 ~+ k, H0 J7 _8 I
& `" X! q' U/ q4 N2 x# o
try {, h1 m0 Z' M- L f/ X
modelActions.createActionTo$message 6 K) l* e: c( S0 v
(heat, new Selector (heat.getClass (), "updateLattice", false));
7 _, b: W) h, U, N. j c8 S. V } catch (Exception e) {) h' D' m0 ?* G/ t
System.err.println("Exception updateLattice: " + e.getMessage ());2 E1 ~6 a3 @1 }3 {& u3 r6 i% O
}/ B) U: A! Q# f4 ?
- X4 E* `7 `1 O/ ? // Then we create a schedule that executes the
9 }+ R; A* f; s3 z; I2 n6 i: H9 m // modelActions. modelActions is an ActionGroup, by itself it3 a4 L3 ^7 R% a+ c
// has no notion of time. In order to have it executed in
3 R6 ?2 u! i9 x" r6 G6 O // time, we create a Schedule that says to use the6 _, H; o |! i& m i
// modelActions ActionGroup at particular times. This& t# @. B' b% O: P4 H
// schedule has a repeat interval of 1, it will loop every5 h7 x* l* W5 P& R
// time step. The action is executed at time 0 relative to$ u. _' V6 I7 M
// the beginning of the loop. T- t% q7 Q, E! K
0 ^, e6 s: L8 F& Q3 k$ ~9 T2 N
// This is a simple schedule, with only one action that is9 o2 D3 Z- r @7 y3 K% l1 ^
// just repeated every time. See jmousetrap for more
W; ^7 o. O- o- m M A: P, W" J // complicated schedules.
( U/ `# s: T6 i
4 X2 t' f" J( h modelSchedule = new ScheduleImpl (getZone (), 1);
- R# W% X. j9 t. e modelSchedule.at$createAction (0, modelActions);8 `, r6 l/ W5 ]+ A0 h
) E& G2 R4 o* P( d+ b2 c$ q
return this;) U! h0 l2 }! a7 v" A' |
} |