HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 Z- S$ b( j" _
- C; z) F' [1 p) r& f/ R3 @ public Object buildActions () {
& |+ C$ k; v( }& r super.buildActions();1 O3 D9 P& A7 i. S. Z
& j+ u% E, F w: i- ^
// Create the list of simulation actions. We put these in. D2 J; q7 y) r# Y0 R
// an action group, because we want these actions to be
/ }2 }% N: U. g& U+ h // executed in a specific order, but these steps should
1 M3 M& Y, v0 G9 u // take no (simulated) time. The M(foo) means "The message0 g1 m8 Z/ P7 v) x6 u1 j
// called <foo>". You can send a message To a particular, f$ {# D6 c4 M3 r- U- C p2 l
// object, or ForEach object in a collection.1 q! |, V& H- x" d* g7 P
# A7 P7 t) B+ F) n5 q // Note we update the heatspace in two phases: first run
. X! e$ T9 ~# M' _' d, |* O // diffusion, then run "updateWorld" to actually enact the
; y! C U1 c9 A // changes the heatbugs have made. The ordering here is% a+ R f' Y3 t# ?: E# K+ I
// significant!
; ~ F) ~5 N# r9 r T $ z$ s& B. _. K/ D% M. |
// Note also, that with the additional
9 L' N( {4 R) @7 h6 w+ l0 b5 {! ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can, S: G% M$ ]3 X7 l" q9 ^0 U- ?
// randomize the order in which the bugs actually run
1 F \1 Q4 M5 q" i% t/ f // their step rule. This has the effect of removing any
A j! ~% B, |& Z$ Z* z% w // systematic bias in the iteration throught the heatbug, I) t. C# a" ^
// list from timestep to timestep0 z3 X; i" ~8 C \; F
$ `8 n" O; Q& P/ ` // By default, all `createActionForEach' modelActions have) j5 S: Q1 y, i6 e1 d
// a default order of `Sequential', which means that the$ y* I5 O/ d/ f: v+ k
// order of iteration through the `heatbugList' will be
3 O' i$ z1 ^' e9 Z: C4 w* V3 B // identical (assuming the list order is not changed
1 w+ T$ ]/ j5 a% F I // indirectly by some other process).
# J v, Q& V9 v- g" q6 Z7 ~ ' S2 i- T9 d4 H9 g1 B/ t
modelActions = new ActionGroupImpl (getZone ());
2 p: C/ O6 w% ?5 b+ }( ]. H4 Z @6 I' T5 L- F
try {5 W. G" L( H ?1 `
modelActions.createActionTo$message
% C4 }, f% c6 N. _ (heat, new Selector (heat.getClass (), "stepRule", false));
9 q. w' S( r( r; `# x } catch (Exception e) {" {3 }- h- m2 ^* [4 M5 X
System.err.println ("Exception stepRule: " + e.getMessage ());% s8 ^ i% D7 H& w; J' Y7 \0 m
}
0 ~4 {/ H% Q6 A/ U9 [( d1 J9 k5 u5 ~. \
try {
+ Z' r/ ]3 B& u9 }% j( v1 Y( x9 U Heatbug proto = (Heatbug) heatbugList.get (0);
% ^, j1 |+ f# A2 e9 h( \/ N Selector sel =
6 Z4 d+ T: }. x$ O5 U( Q new Selector (proto.getClass (), "heatbugStep", false);
i) \8 ^/ }) E9 I+ n/ C$ O actionForEach =
1 {8 [' r/ U5 |+ j modelActions.createFActionForEachHomogeneous$call
4 i: n4 \; s3 B# a (heatbugList,
! T* v7 ?/ e$ h# Y) p x# | new FCallImpl (this, proto, sel,
# g/ V6 L4 {/ h# @" z1 _7 U new FArgumentsImpl (this, sel)));: H v8 L1 |. V# D9 c
} catch (Exception e) {
7 V" q: ?! P/ y e.printStackTrace (System.err);
S* ?9 t2 Q z }
, N/ J, V; N( l2 l0 m( Y
$ N! w! j O, |, s3 [2 A+ ? syncUpdateOrder ();
9 ?4 D# x8 j- i' s
0 b2 q/ n: ?# O3 O$ p$ {( q! H$ ? try {
: z2 P0 `; ^8 I/ M2 ?2 H" P' I modelActions.createActionTo$message
/ \- x4 }! f2 k0 m6 d (heat, new Selector (heat.getClass (), "updateLattice", false));( ~9 S; _6 y, z: m% A+ M% @
} catch (Exception e) {
" s& k: m3 p3 L9 d: W) F1 Y System.err.println("Exception updateLattice: " + e.getMessage ());3 M) i( E" o s$ a4 J- f
}& q7 W E/ Z' J+ U( W. R2 F
: e8 r- t. I( n // Then we create a schedule that executes the8 D9 Z, j% Y$ v& e4 L5 g) l
// modelActions. modelActions is an ActionGroup, by itself it
: m. r% L9 ~! T9 J // has no notion of time. In order to have it executed in0 W' @7 z; P# p! M7 ?4 a% I
// time, we create a Schedule that says to use the. l; L) b( R: Z+ z3 `8 p* O8 d& t) W
// modelActions ActionGroup at particular times. This0 K8 \6 _6 L r1 C" |3 W
// schedule has a repeat interval of 1, it will loop every
$ D% t* q) G2 D, K // time step. The action is executed at time 0 relative to
& U$ F- c4 i9 a* u; i4 B // the beginning of the loop.
8 Y( ^9 O9 R" |- c4 Y3 ^& d
* M+ X" c0 C. Z2 w9 c // This is a simple schedule, with only one action that is$ D8 a. [! T8 J/ q; v# W4 H. U
// just repeated every time. See jmousetrap for more
: y3 }) a( H6 \2 A) X // complicated schedules.: b1 x' O$ k" G' G4 v3 S9 P
( a! g, A* ^9 N modelSchedule = new ScheduleImpl (getZone (), 1);
+ m" B+ B0 R& W8 L' [ modelSchedule.at$createAction (0, modelActions);
. E, `: @4 l( }: y# d# R
, T' [3 W5 p) H, t g$ P, I7 j return this;4 A/ n. D7 C: f7 C. P7 \
} |