HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ ^7 t, M' _! e. j3 M' Z+ P- g: T0 |+ h
public Object buildActions () {- V: _! _/ f: K x* t- g8 m
super.buildActions();
5 G) U; q$ T2 s4 l; x X' f }; N6 S( k' c: n; ~" b" B5 J
// Create the list of simulation actions. We put these in
) n+ l6 j4 A( O+ B0 i3 t // an action group, because we want these actions to be2 N! {1 q; h" \2 d
// executed in a specific order, but these steps should! r2 m+ o7 J6 ~2 U$ P, z
// take no (simulated) time. The M(foo) means "The message
3 y: a3 S5 K, N4 m I# [! ^ // called <foo>". You can send a message To a particular
$ V% v8 l+ g' B# K S // object, or ForEach object in a collection.# p* x7 q) N3 f
+ n( ]5 _4 Z' O; s }! q // Note we update the heatspace in two phases: first run
+ i' S: ?# n- |0 s: }' I! Q // diffusion, then run "updateWorld" to actually enact the1 f' Q4 [9 d: r9 J3 n- t
// changes the heatbugs have made. The ordering here is
) v% f6 ~+ m$ H) Y // significant!3 _/ x9 J* d# |! D. q0 _
# N9 v" ?1 [. U- p- B r
// Note also, that with the additional9 M# z! n* q2 v1 E& D
// `randomizeHeatbugUpdateOrder' Boolean flag we can, ?, {" X& {) i+ G4 a
// randomize the order in which the bugs actually run8 k% W" u$ ?* ?1 @- ~0 ]0 M
// their step rule. This has the effect of removing any
! ?" ^* @/ R, a: [% Q) c; p ` // systematic bias in the iteration throught the heatbug
0 t+ W7 I3 B3 o& g* X b // list from timestep to timestep
+ P7 b+ }+ B4 w { 1 S$ S2 k9 {: @; K9 `& m9 f! G. a
// By default, all `createActionForEach' modelActions have
- Y4 `7 \& p" v( U // a default order of `Sequential', which means that the6 v0 D( |, x/ h/ s, P) R5 X9 }6 \
// order of iteration through the `heatbugList' will be, W& c: [, z9 z/ _7 S
// identical (assuming the list order is not changed* f x! ^/ Y0 h2 D
// indirectly by some other process).
& X8 \6 `6 w2 d/ j4 J! X' T/ t + \( G" t( W9 [- R
modelActions = new ActionGroupImpl (getZone ());0 i, h4 g% m# {* i* q
% i% ]6 L' S1 }+ T try {
. W g4 a1 E- P6 u* | modelActions.createActionTo$message
$ i8 T" ~7 ~( A) } (heat, new Selector (heat.getClass (), "stepRule", false));, `8 S/ O! a# [
} catch (Exception e) {
. ?( _) V2 P5 [# L6 O1 R) z System.err.println ("Exception stepRule: " + e.getMessage ());
* @2 [: k, H) q: n( B }
) \* |- Q6 `; W: X2 v
; |. b o: ~5 W+ ]9 @: R try {
F5 S9 P$ h. q4 {$ Q Heatbug proto = (Heatbug) heatbugList.get (0);$ l. c6 C) B7 o
Selector sel =
* b, |1 w% c: R* W8 T new Selector (proto.getClass (), "heatbugStep", false);2 K9 |2 ^* q& h$ `
actionForEach =* S2 R( k( c3 U. m" I6 G
modelActions.createFActionForEachHomogeneous$call5 V. D$ w M5 l0 L6 w: N- D5 Y
(heatbugList,
; {. u" Z3 g; S( n' p( H new FCallImpl (this, proto, sel,1 p. g/ D3 e V1 d: D, b* T
new FArgumentsImpl (this, sel)));
( k+ I) J9 Q. z! h F1 ]7 {1 b+ e } catch (Exception e) {, g) I/ N5 \+ w! d! S P$ W! w# ?
e.printStackTrace (System.err);
0 e5 G8 s7 G- j* n; U }
$ W' R. z9 J# F, i
( f+ r0 u G6 G- }0 m syncUpdateOrder ();
) J* t8 B! g. x7 _! s$ N
2 c9 \0 W+ j( U. t7 w) x. k try {
6 Z3 A8 \: O$ z7 Y" ]4 [ modelActions.createActionTo$message * i" c! j$ U: D9 ?' c8 d9 _
(heat, new Selector (heat.getClass (), "updateLattice", false));
m# b Z4 ]' O; r% u( z } catch (Exception e) {
4 x; a: e3 o% ^ System.err.println("Exception updateLattice: " + e.getMessage ());/ D5 a( x2 h6 `" h, c$ E {+ U
}; L9 B6 d# i4 y( F; J) R
, m L* `5 I0 [
// Then we create a schedule that executes the% Z7 o. k! S3 i: w8 o
// modelActions. modelActions is an ActionGroup, by itself it
5 `+ c# h$ n* J# p5 H- P' r // has no notion of time. In order to have it executed in$ x: y. Q' ~4 k# B
// time, we create a Schedule that says to use the
( U* [. o' s' u4 A4 D // modelActions ActionGroup at particular times. This
3 k, b9 L% f: n# f; m. a. A // schedule has a repeat interval of 1, it will loop every; O, }9 D7 L$ G, {. d% Z/ F0 b: e
// time step. The action is executed at time 0 relative to# `; \ f4 `, Z3 y* d
// the beginning of the loop.
% `) d& K5 @) B2 n
5 ]5 A/ H$ x4 P) F1 @$ ~( r- b // This is a simple schedule, with only one action that is
( l Y7 d% n8 Q+ s // just repeated every time. See jmousetrap for more
* G' Z% N' L! @. u' _$ a2 r // complicated schedules.
+ J' B: Y& B# q9 | + j( d8 z4 U7 _/ b& V
modelSchedule = new ScheduleImpl (getZone (), 1);1 \/ q, t' ]! m7 R! U
modelSchedule.at$createAction (0, modelActions);. r* [: ?+ t$ c- S. M# d
+ ^* H2 H" f7 V, s
return this;0 y5 D: }" }# r( k
} |