HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 J1 w; y8 a7 p4 m
8 s$ l5 w% Z( _) G; n public Object buildActions () {
# p* c) O- U$ k$ b: B: I super.buildActions();$ S t2 Z0 J( C) K0 {& d
+ K2 `8 F6 S. C' |$ \/ D$ X, C* s
// Create the list of simulation actions. We put these in
% G9 B) P \: P9 h // an action group, because we want these actions to be
: C4 V l4 |$ {( A* k: s8 H // executed in a specific order, but these steps should
2 ^8 t; b- H* k9 Q+ N // take no (simulated) time. The M(foo) means "The message
9 z6 x; \7 k8 F, o- P- b* T // called <foo>". You can send a message To a particular& r7 U, V: ]# D- @& X
// object, or ForEach object in a collection.
; _! y8 A! c8 m+ B, s - ^' m" v! |) g4 U c3 A. b. \/ \
// Note we update the heatspace in two phases: first run# e$ l: x& }: ?" i" K [
// diffusion, then run "updateWorld" to actually enact the8 I! H+ c8 Z7 C5 s) q, h9 l9 W0 H" z
// changes the heatbugs have made. The ordering here is
; w# ~8 Q/ G! ^ // significant!
4 c) _/ S2 }& f. T5 L
* N! }* l7 c6 f0 \- k4 v // Note also, that with the additional. Y4 m# X# W' r a% a
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; U5 k0 {! }/ z9 w // randomize the order in which the bugs actually run
6 \: Q# Z, t2 x# ? O2 S // their step rule. This has the effect of removing any2 I; v6 K0 v. a) H8 q
// systematic bias in the iteration throught the heatbug
# h6 l8 {# H$ N# B4 Z; z+ Q8 h- V // list from timestep to timestep+ m u5 ?2 Z% O; ^, }
( o1 n- W; h7 X6 H ` // By default, all `createActionForEach' modelActions have" E. h* g, N1 q8 q. @9 C/ R% n
// a default order of `Sequential', which means that the
6 n2 k. T2 f8 M# e, V // order of iteration through the `heatbugList' will be
3 A: ]% J/ R, c$ b( T" Z# R // identical (assuming the list order is not changed- j" X. @ {) w* j" \
// indirectly by some other process).6 F: V- F7 z0 Y3 D: E/ f. W
3 m/ `) _9 Y* p1 E
modelActions = new ActionGroupImpl (getZone ());
$ D# W3 \: ?7 b
- j) N" l) _1 U9 h* }5 {- x9 c try {% e2 z% _6 _2 p
modelActions.createActionTo$message+ o2 z; d: K* `2 O$ E
(heat, new Selector (heat.getClass (), "stepRule", false));! Z4 F, c) C; y8 e
} catch (Exception e) {: X* r( d1 K+ r' C7 m( Z
System.err.println ("Exception stepRule: " + e.getMessage ());0 s3 z; d* C) L
}' D/ Y. S; W }7 U$ T% h) f
8 ?; y. H0 e5 v, R/ d try {# O T; F2 z1 f9 x
Heatbug proto = (Heatbug) heatbugList.get (0);
, N6 H' o$ W* h2 ?& L Selector sel =
- m4 `' j: B/ v& \3 A4 ?/ X new Selector (proto.getClass (), "heatbugStep", false);
* \/ l. ~- p* L8 F- B actionForEach =
( `- ?4 R6 h9 Q! ^, q# K; b modelActions.createFActionForEachHomogeneous$call* L& h7 h7 }+ s Y1 v$ n
(heatbugList,
- B- p$ p/ H z; b new FCallImpl (this, proto, sel,
& ?" ~6 ^5 c/ E, _ new FArgumentsImpl (this, sel)));: X, H9 E7 h+ _" q' T2 J% Y9 l' ]
} catch (Exception e) {+ M* d' Q+ ^/ R
e.printStackTrace (System.err);: q) [9 H. V) q& i
}
& ^; o G* N8 q- d
! {# V9 w# C: f& J2 a syncUpdateOrder ();! w" ^ |7 Z. G/ T$ L, l$ P
; x9 F4 W3 _8 d: Q try {" ^6 F* e5 ?! e* F4 _8 e; ?
modelActions.createActionTo$message
: n. \' k( g% k# w' X (heat, new Selector (heat.getClass (), "updateLattice", false));
2 {9 ^, }( w$ s6 p' U } catch (Exception e) {+ ~4 j7 ~1 j4 `( r
System.err.println("Exception updateLattice: " + e.getMessage ());
. H+ ?* n0 J* D% R: B }
9 t6 v5 u$ g9 E) A3 S ' m2 v0 H. w% G* Y1 S# X- ^5 g
// Then we create a schedule that executes the
% E, b/ q; N3 ~( e5 ? // modelActions. modelActions is an ActionGroup, by itself it6 ^' z. t& t; V! g( Q. n
// has no notion of time. In order to have it executed in6 o ?. |" v! q6 x
// time, we create a Schedule that says to use the {+ `# F: a" n1 X, m3 y- y
// modelActions ActionGroup at particular times. This
9 z! t6 b8 Z3 f1 O+ j // schedule has a repeat interval of 1, it will loop every' I0 l' n' p3 X+ a* w
// time step. The action is executed at time 0 relative to5 g/ c. T% j# \; G! {
// the beginning of the loop.0 Y- ` c5 w/ P: J9 S2 I5 R
& o9 B6 F! Q( P, r9 b: a
// This is a simple schedule, with only one action that is) V7 W- Z* m% Z, u4 H
// just repeated every time. See jmousetrap for more& i. t# {2 \8 Q, o# o
// complicated schedules." F- e+ A) Y& p( i% |: t
/ T( }, O$ E; b/ p% N0 |! H8 s
modelSchedule = new ScheduleImpl (getZone (), 1);
( f/ q: o @4 ?2 b: ? modelSchedule.at$createAction (0, modelActions);: X8 h# [$ h+ H- F+ J+ t1 y
/ ^3 ^& \1 _- {
return this;
+ T" R1 l! B1 w# G8 o } |