HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# a. o5 c+ Q% K3 M. h
+ v4 i* ?# r4 h public Object buildActions () {
' K; p7 r# X% z2 X: n( \0 w super.buildActions();
% g- L3 E2 t' ]( J: y
- o0 }1 J4 i( o+ c$ Y // Create the list of simulation actions. We put these in
- a( S. v2 y* e& E // an action group, because we want these actions to be& n2 m' g& m. W' n, @
// executed in a specific order, but these steps should
2 |4 B* l2 K2 ~, q2 \; ] // take no (simulated) time. The M(foo) means "The message- G& l3 |5 o& d6 e9 Y
// called <foo>". You can send a message To a particular9 g5 X$ e- Z! a# o
// object, or ForEach object in a collection.
" Q! N* b) \" t" I' i
# M, q" \$ f3 j# K) D7 I // Note we update the heatspace in two phases: first run
6 K5 Q3 k9 y& @) l s+ y' q& g+ n& d // diffusion, then run "updateWorld" to actually enact the
% S5 t% s+ i- k+ g8 P! m. G# P // changes the heatbugs have made. The ordering here is
& i7 W, d8 g* N/ h9 i // significant! z1 J3 g" G+ d; r/ @+ \6 E
0 D5 H( e* x% [, S6 O& J1 H // Note also, that with the additional
! }% V/ K' e0 b( z // `randomizeHeatbugUpdateOrder' Boolean flag we can# `/ S$ ^8 I& M6 e
// randomize the order in which the bugs actually run+ j8 ]) f1 ]* B/ F
// their step rule. This has the effect of removing any6 |, s- g4 H: O" T3 t
// systematic bias in the iteration throught the heatbug" w' q6 P# Y# _% o, c2 t' _3 ?% ~( b
// list from timestep to timestep
$ p8 m6 \6 W" Z 9 I( u3 P7 a9 }% u
// By default, all `createActionForEach' modelActions have- g8 M/ N3 q0 N. L4 F7 M
// a default order of `Sequential', which means that the2 A1 L5 `7 n4 z
// order of iteration through the `heatbugList' will be' z. M& h! L2 ?' q1 \+ }. a
// identical (assuming the list order is not changed
2 Z+ _% ]4 q0 I. A4 B // indirectly by some other process).
6 @# G1 Q1 ]$ B
0 ^9 e# H% d2 V" X" }7 G) {9 p% B; n modelActions = new ActionGroupImpl (getZone ());
" N, {- O! `: s4 W* h$ l5 R
! K. t9 ~9 o; z* z' x4 j try {2 f; n5 X- _) g. ~! k
modelActions.createActionTo$message
' @" J! }; E& H/ S2 _% a3 {3 S (heat, new Selector (heat.getClass (), "stepRule", false));1 T O4 ^0 [( R% j# ]+ F
} catch (Exception e) {# C3 k* a! n" g7 {7 x/ m9 f
System.err.println ("Exception stepRule: " + e.getMessage ());
* ~. Z7 _, g$ z' E }: I: i+ o& W0 D$ T
- ] D& P; \# ~0 @* X7 } try {; C1 B, l6 q- M) u& [5 L
Heatbug proto = (Heatbug) heatbugList.get (0);
. ^) k4 u# a/ a8 ?5 c3 _: b Selector sel =
7 g0 H( C) j, E4 G0 I1 Y new Selector (proto.getClass (), "heatbugStep", false);
6 i8 R# [* E$ o' z2 Q$ k actionForEach =7 D- q z# x, l
modelActions.createFActionForEachHomogeneous$call
% E1 h4 a3 m5 {* k+ @ (heatbugList,' y: t* L( n" l! |* `7 Z
new FCallImpl (this, proto, sel,
' u; [2 L% q3 j/ k new FArgumentsImpl (this, sel)));( }" N4 y/ v0 T. `7 b4 l7 Z
} catch (Exception e) {
, _% p% d; C& v; t# k e.printStackTrace (System.err);
- L' z1 b& Q+ _: A }
* b6 ~2 ^' z% q% o4 y
0 x. c1 ^( D/ g* b% x syncUpdateOrder ();
9 H% J+ g$ z1 N* b* _. Y
+ k& [4 n5 r) {# [$ @$ {4 A try {
" X& B+ ~5 g& @3 w modelActions.createActionTo$message % [5 H \0 v" i2 s" ~& p4 u2 M
(heat, new Selector (heat.getClass (), "updateLattice", false));
, `* b) w1 B- F. y* C2 [, L$ J4 m/ S } catch (Exception e) {# Q) c+ }. f, q4 I, D2 g
System.err.println("Exception updateLattice: " + e.getMessage ());
0 H0 _4 A, L4 G }; O/ b6 v- E- Z0 P
, F# b+ j; J. e, I# l
// Then we create a schedule that executes the
, G+ c' A; V* `& B- _9 f, Z) D // modelActions. modelActions is an ActionGroup, by itself it
2 N6 R0 e* C1 M/ K' S" D" u // has no notion of time. In order to have it executed in. m6 \7 Z6 f# m. K, ~- P c r; x
// time, we create a Schedule that says to use the9 n+ Q# k2 z4 r, ^8 F0 ]3 y2 q1 D& B
// modelActions ActionGroup at particular times. This! O2 ]' A* f. Z8 [
// schedule has a repeat interval of 1, it will loop every
4 i3 g$ c9 l$ T* H // time step. The action is executed at time 0 relative to U+ V* e+ ]* b( L/ ^7 J* a
// the beginning of the loop.
7 e5 g& K( p8 S
9 {) ~1 P8 J& c6 v; l: f // This is a simple schedule, with only one action that is% l7 l5 F- ~! V; U/ H5 T7 O; ?5 d$ P
// just repeated every time. See jmousetrap for more. N; t' ^1 ?/ `) {2 q# B( D6 G/ Z
// complicated schedules.
1 |, J U/ K( h0 ~6 _ 9 V1 t+ s" K- @) u
modelSchedule = new ScheduleImpl (getZone (), 1);3 K( C; K* M$ C: ?/ H5 y& X
modelSchedule.at$createAction (0, modelActions);
6 T& k+ k$ l+ S& X5 |( @
: S6 }& p+ C( e) X) w: c0 H" ~9 | return this;
9 ]0 r7 D; y& B; ]3 _" C } |