HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 f7 p$ v9 E' p2 A- K, f( R; w6 D* y2 M( N i2 k/ L8 M
public Object buildActions () {
- e& }3 E0 C/ p1 E1 X super.buildActions();
6 h3 q8 |) h% F3 Q. T4 n
0 O' Q. }; t: S: p. ^ // Create the list of simulation actions. We put these in6 L6 W) D' s2 h& k5 Q( P
// an action group, because we want these actions to be/ w$ v l. _& K8 J+ _5 x. m, i
// executed in a specific order, but these steps should
: {7 v! ?' ^ y' U3 E6 v7 O# Z4 [ // take no (simulated) time. The M(foo) means "The message
6 c5 U0 v8 j. D& X v+ \ // called <foo>". You can send a message To a particular
1 m! e# a4 \, u' K- q V5 y8 H // object, or ForEach object in a collection., ^& l5 ~0 l2 j4 O. ~( S9 Q" d
; S# w# c) N! W6 | W
// Note we update the heatspace in two phases: first run
0 |9 d ? {( R3 L# I+ R, R% U // diffusion, then run "updateWorld" to actually enact the
) ]; T: |, o* ^, t) Y1 X // changes the heatbugs have made. The ordering here is" w% P( ~6 f: a" Z7 I' n
// significant!5 D$ C3 B* ?6 e4 j( ]# G; _! S' Q( a
6 K( n* q& t9 q& v, h
// Note also, that with the additional
R6 z' F3 ]* ? // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 ^# g; A. b+ s2 L3 j+ Q' H // randomize the order in which the bugs actually run
! {1 t4 ]4 J& F+ M) h% X9 g // their step rule. This has the effect of removing any) O9 p, b" l% e- d& H
// systematic bias in the iteration throught the heatbug
/ V, b$ B1 d+ o! l' ~! R // list from timestep to timestep. N+ u: q9 P3 I% R6 z; }' K
1 A' I3 Z1 c5 M2 y
// By default, all `createActionForEach' modelActions have
( I* v0 g. t# l. | t // a default order of `Sequential', which means that the
' m2 J# L! `+ j+ V: n y1 O // order of iteration through the `heatbugList' will be
0 J! x! ^9 K! i! e8 ?$ _# u( r // identical (assuming the list order is not changed+ ?( p+ p4 ?' ~, n% H! a5 f6 R; p
// indirectly by some other process).
+ N+ w$ M1 ~4 ~6 m3 Q( n/ H5 {0 ~
, g8 l9 F3 y4 h$ B' D) a# w modelActions = new ActionGroupImpl (getZone ());; I2 f) E2 r$ y. w4 K
7 b% @6 S+ k) n$ h, V ~$ H# J6 p try {# O. w0 k# {' u! n8 Y
modelActions.createActionTo$message1 P x6 y. `9 q# |' _ ^+ ~
(heat, new Selector (heat.getClass (), "stepRule", false));
7 }1 D( F% v* R, `+ D } catch (Exception e) {8 ]% P; c: ]5 {7 A
System.err.println ("Exception stepRule: " + e.getMessage ());
3 N2 c, ?# h/ y! }/ \ }0 T C' j6 C3 a- P8 w
" Y$ P$ I+ q* u7 ~
try {
3 @1 f# E; z( b. p! w* n2 I# ^ Heatbug proto = (Heatbug) heatbugList.get (0);
, `2 k% ^. v) u+ _. G& r Selector sel =
, _* [$ ~% z+ T! z, i2 X new Selector (proto.getClass (), "heatbugStep", false);
: r* w3 Q: R& D actionForEach =
. s6 o; K1 D( |& m3 o modelActions.createFActionForEachHomogeneous$call# `+ t% u5 k5 Q- y& V5 j4 y5 M% d
(heatbugList,
/ z& k; e( R d( g: V& e. m$ j9 N' x new FCallImpl (this, proto, sel,# t" h8 f8 c+ |3 T: j$ o
new FArgumentsImpl (this, sel)));
6 i% }8 T9 `( [, `# } } catch (Exception e) {
" i+ X5 o! C0 P( q e.printStackTrace (System.err);% m% Y- b# @& w; e# M: i
}
# S6 k, J* g( I& m L8 } 2 ~, M% n+ Q- C$ a4 C& V- w2 }
syncUpdateOrder ();* S4 z! Z* c& N$ \' i/ u
; A" Z: |- s* U( R [3 W+ O
try {
+ A% d1 f2 w- A( o: c modelActions.createActionTo$message 9 q" a; i0 h8 A) k6 A2 R. c5 D2 ?
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 R+ o% [1 K9 k, O1 ?% K0 s, y. T. j. J } catch (Exception e) {$ g1 W. Q& U6 R) K+ |, g4 c
System.err.println("Exception updateLattice: " + e.getMessage ());
$ F8 K. h3 Q9 D) l r+ [1 z/ ? }
( Q/ b- V5 c0 t7 @% M! o* Z 1 p: j3 f0 }6 |( a' G }
// Then we create a schedule that executes the& F) O2 q* S: i6 o
// modelActions. modelActions is an ActionGroup, by itself it
* T: ~& p& }, @ // has no notion of time. In order to have it executed in8 a/ _& @ h7 n+ c0 d$ E- ~* U
// time, we create a Schedule that says to use the! L& o/ F: w: Y/ i3 P
// modelActions ActionGroup at particular times. This4 w4 T0 p$ \1 k' {7 [0 S1 T
// schedule has a repeat interval of 1, it will loop every4 J& K0 }& |2 d! c" A" h; Q
// time step. The action is executed at time 0 relative to
6 S: n5 c5 ^4 O0 ~) m+ z5 M // the beginning of the loop.
+ s6 a; @, w, W8 C+ ~$ H" x' p- o/ h' D S8 ^7 y5 L+ ?5 E
// This is a simple schedule, with only one action that is0 X% s" |2 g% w9 b' `. Z
// just repeated every time. See jmousetrap for more
% e1 a) ~& H4 z6 P9 Z // complicated schedules.2 A& f' U0 Y, t
1 T0 `; U- Q! c# G$ E7 W3 a, J
modelSchedule = new ScheduleImpl (getZone (), 1);
) r- @8 S5 U# r) L7 v x modelSchedule.at$createAction (0, modelActions);& Q6 a6 R1 |& A+ B4 q
- S* ~" y6 ]; M. n& U' [
return this;! k, F6 |- o* w! d
} |