HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% ~ Q8 ^1 {0 I8 b e% L( E \
. l- p4 L; a3 F3 r6 [7 O public Object buildActions () {; F1 p- m0 {0 p
super.buildActions();: G. R4 J6 `5 a7 }2 I1 f* S$ G
1 o3 w& X/ w$ z2 c2 Z/ Z // Create the list of simulation actions. We put these in
" h8 Q) Q% n1 k! t' M9 x& {: I // an action group, because we want these actions to be; C0 L" i6 P; a" p$ c1 U
// executed in a specific order, but these steps should
- v0 w8 N) B n$ O // take no (simulated) time. The M(foo) means "The message
# p& [. G) b2 H, D4 Q // called <foo>". You can send a message To a particular4 Z. Z- w+ `/ `* }( K5 Y
// object, or ForEach object in a collection.% M" z4 m9 I$ u+ e
: }( \: u7 \+ o6 t // Note we update the heatspace in two phases: first run- Z5 ?% i. L( \/ X. {, I$ l
// diffusion, then run "updateWorld" to actually enact the7 R2 v# i% C) `9 y; W" i2 }
// changes the heatbugs have made. The ordering here is0 V1 o- R* X, M7 a6 V0 F1 l
// significant!
2 @* C/ L5 b# \5 P* v: D ; U t1 q: U) \; m. Z: a
// Note also, that with the additional0 u0 U$ O0 s/ f+ Y, w7 y% V
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- g% v z$ z" n' A // randomize the order in which the bugs actually run
% M6 r j$ S5 z& E$ P1 s4 i // their step rule. This has the effect of removing any" F6 F7 r9 X2 @1 e" o ~8 L
// systematic bias in the iteration throught the heatbug4 K2 l D9 c; R: r1 w
// list from timestep to timestep7 H0 E/ P* l- u6 b$ {, ^$ D
: I9 ^, o* x% a- l$ W) {; P
// By default, all `createActionForEach' modelActions have
1 R5 p) b2 z6 Y% ^! u // a default order of `Sequential', which means that the. f) m& o7 [' r* ^2 ^
// order of iteration through the `heatbugList' will be9 M1 _- U5 F9 j, [: h
// identical (assuming the list order is not changed
4 E( p- u" y# ~ // indirectly by some other process).4 @5 R) E: t. ?& t% U; R. o3 m
& C* _2 i( U4 t! ~* }! b3 L1 |. g
modelActions = new ActionGroupImpl (getZone ());4 m8 r8 G$ O$ W% w! o1 F1 _' s
: A) P& r' W8 {1 p9 K+ g1 y+ c try {
8 ` M; x! G3 ~* g2 s modelActions.createActionTo$message
$ M7 v& h; C2 T$ l* i. i$ R (heat, new Selector (heat.getClass (), "stepRule", false));
5 C4 | A: j+ c7 \ } catch (Exception e) {
, u* q G) @4 s: @+ v. h Q, H& ~* c' ] System.err.println ("Exception stepRule: " + e.getMessage ());: h# R4 [2 M: I
}
/ _' w: \& Z* U' n
" S7 l: G" D; F- ?# K# b9 l try {& R3 t- A* L: Y
Heatbug proto = (Heatbug) heatbugList.get (0);
( R. F9 E7 E9 O0 E, {8 A Selector sel = ' I: i0 ]6 E( H0 U; F0 |
new Selector (proto.getClass (), "heatbugStep", false);
x' K. M I/ P" J actionForEach =
. f+ l9 r) o, b: k# | modelActions.createFActionForEachHomogeneous$call
4 ~; x+ o$ \0 c (heatbugList,3 _( M9 p; X3 W: G0 S4 |
new FCallImpl (this, proto, sel,
: I2 u8 I: M |: o& w new FArgumentsImpl (this, sel)));" E1 O+ i# m/ X( ]0 {- a f
} catch (Exception e) {. D* C# q: P9 o; I9 O
e.printStackTrace (System.err);8 {, ~* e; ~0 k" d2 O- v) S- n
}
9 x4 m$ }% z4 m+ I2 ^- w* t+ F
" k4 V: B2 A( R/ z: T% Z syncUpdateOrder ();
# K8 R G& {% u I
+ z: B- i+ P0 L+ N; y3 c) S try {3 q3 Y# Z( t0 r( K2 \2 A( }
modelActions.createActionTo$message
5 o* y0 m. N# S7 M( @" Y (heat, new Selector (heat.getClass (), "updateLattice", false));
# Z7 c! M3 Z; T/ o9 A" u" |# p } catch (Exception e) {5 I4 s, Z* m# O/ g* q/ Y. w
System.err.println("Exception updateLattice: " + e.getMessage ());
( a; }; ?/ r2 x9 P9 t7 c$ ` }
7 `4 @$ t; ?4 B3 A0 O) D& _' A * ~$ d& @: O* _! T ^# L2 C9 x
// Then we create a schedule that executes the9 N0 `' p6 @* y0 e( k
// modelActions. modelActions is an ActionGroup, by itself it
9 Q" D! u# K, @7 ^! h // has no notion of time. In order to have it executed in! @. g7 l# }) `! ]& S% d
// time, we create a Schedule that says to use the
, ^, L3 H" ~* ~8 B. N // modelActions ActionGroup at particular times. This
" }8 a- X3 x m5 Y // schedule has a repeat interval of 1, it will loop every
/ G5 P. w3 N& [7 _, F: P6 _ // time step. The action is executed at time 0 relative to" ?6 I* ?0 d1 C, C8 p
// the beginning of the loop.- p; m: x0 C1 v; w# N/ R6 m5 ?
7 I2 W/ X$ ~: e5 f! w0 n
// This is a simple schedule, with only one action that is! u# i# C5 [4 J9 H
// just repeated every time. See jmousetrap for more7 }8 ^( g: q; R& a* S% r6 \1 g
// complicated schedules.
2 y" h. g' r) {- s+ k' U5 R. \& c 7 z1 B, F# E: K
modelSchedule = new ScheduleImpl (getZone (), 1);0 E( u+ c% C V0 p
modelSchedule.at$createAction (0, modelActions);
9 b+ T! D+ W S' y: R8 e3 |% V; Z
# W/ g% \" a7 U" W return this;! O2 L/ d+ R. \
} |