HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( Y& Q( f# L, V" m9 E, o7 K6 R+ X3 X8 l( n: F
public Object buildActions () {( ]! i4 y/ q, a8 z/ q
super.buildActions();
. _4 D: Y% n" V: `" g# }6 U ; ^' d0 V' U& _
// Create the list of simulation actions. We put these in: [3 z: {# Q) i
// an action group, because we want these actions to be
( X. U: b w/ n9 b3 S; z7 w // executed in a specific order, but these steps should7 ]! S) o2 C4 R( P
// take no (simulated) time. The M(foo) means "The message* |) K9 o1 p' R5 M2 Y+ C
// called <foo>". You can send a message To a particular) F0 ]& w. ~3 q4 p% j
// object, or ForEach object in a collection.
" ~8 Y; F. h& D- _ ) J- @3 G7 C8 U
// Note we update the heatspace in two phases: first run0 C6 r! ~9 c& p/ \6 U
// diffusion, then run "updateWorld" to actually enact the
3 `7 H9 s% s) O) p" V7 V // changes the heatbugs have made. The ordering here is
8 }2 M1 u: X/ U& a% x% X // significant!
/ \* S D5 w4 \% B$ _! X 8 }- \/ O) }- b9 [9 {
// Note also, that with the additional
+ `( V @9 v% Q' V! \ // `randomizeHeatbugUpdateOrder' Boolean flag we can" P# U" g3 O0 s! [* B( a! n
// randomize the order in which the bugs actually run$ ^% g/ k. C% Y0 i
// their step rule. This has the effect of removing any6 K; K. s3 {( z4 L% q( j" V' t/ }
// systematic bias in the iteration throught the heatbug
% l* j% ?; e( M8 X l // list from timestep to timestep4 c. D3 Q( D; B1 l% r
( R2 D d' h0 r
// By default, all `createActionForEach' modelActions have9 a9 {1 Y. w* [. h! v
// a default order of `Sequential', which means that the
. v) Q- `; c& g7 Y // order of iteration through the `heatbugList' will be% d( |3 s! W- Z# a# J( N0 P5 F
// identical (assuming the list order is not changed
3 l. o! P& r9 ]# S3 ~ // indirectly by some other process).
$ v8 E T$ r* ]' ?. b/ U; L+ O
( ~9 t$ ^. l# S5 q% c! H# ` modelActions = new ActionGroupImpl (getZone ());! @: R) r- Y$ h# M' Y% p. u1 j- }
, m1 x; x' o4 C5 F: y. f try {
, h k6 e6 ^8 f1 q! a; F, C modelActions.createActionTo$message
w5 V3 @& c% Y+ k (heat, new Selector (heat.getClass (), "stepRule", false));
6 E2 X8 U& Y! r0 _" F3 m+ w } catch (Exception e) {, O, c) E1 F/ j
System.err.println ("Exception stepRule: " + e.getMessage ());
# \) ^5 w6 H' ?& u6 m2 y- r }
$ a3 Q8 r* h8 h$ I3 _# d' M$ v' D. ]/ i) }
try {# i8 U/ s, Z8 C" e6 y7 E
Heatbug proto = (Heatbug) heatbugList.get (0);6 O2 U1 V7 b4 I# b+ t
Selector sel = 0 A% B* P! j$ H: \1 Z
new Selector (proto.getClass (), "heatbugStep", false);
# k! B8 O6 A0 B% [; V6 Q actionForEach =
: G( H8 `" n: i0 q, n modelActions.createFActionForEachHomogeneous$call: F; B# f7 i* Y8 Q ?, P
(heatbugList,. J. p" m# } p8 N
new FCallImpl (this, proto, sel,. g, m3 s: H5 R; |% [$ S9 S
new FArgumentsImpl (this, sel)));
& `& M0 B" X5 V, F } catch (Exception e) {, y$ S/ F: G& y3 V
e.printStackTrace (System.err);
& E8 s d$ u# Q }
" K! \/ [+ e) Z- O+ l$ f
. O7 f* h7 N7 v4 ^) z: g- g5 Z# [ syncUpdateOrder ();
+ E. u' ^! k& \- `. X- |1 E9 z' C! v8 Z
try {
. \+ w2 z, M* U0 A" i modelActions.createActionTo$message ! k! C i* `4 W9 l! i
(heat, new Selector (heat.getClass (), "updateLattice", false));1 o0 t" ]- O( z& k$ p; V2 [8 D% E
} catch (Exception e) {
% s8 Q; Z! O7 D4 s, G0 r) C3 z System.err.println("Exception updateLattice: " + e.getMessage ());! F- u T' F& h% T0 r' M0 O
}; |5 J6 }1 ^% ]. }
: H( {$ q/ \, s( o9 m // Then we create a schedule that executes the
7 B0 q4 u. x& X; I7 T4 j // modelActions. modelActions is an ActionGroup, by itself it
1 M7 z, k& Q9 ?- ^, K5 J9 z // has no notion of time. In order to have it executed in
1 I5 U- I+ M) R b5 q b // time, we create a Schedule that says to use the
5 H# R0 S6 {' b5 o% ^: D // modelActions ActionGroup at particular times. This2 e( `& C& [0 Z- H! L6 T3 J
// schedule has a repeat interval of 1, it will loop every
9 c' g5 U" I g // time step. The action is executed at time 0 relative to
" ^6 S4 M) D9 T // the beginning of the loop.. ]& w1 K* X& n
4 {) z- ~: q" t7 {
// This is a simple schedule, with only one action that is
$ N$ u6 R5 w$ I // just repeated every time. See jmousetrap for more
/ D2 c3 o2 T" P* D // complicated schedules.( e& C3 q( @" ^# S. a k
* P6 B f' O E4 T# P ~ modelSchedule = new ScheduleImpl (getZone (), 1);* E; F3 S% C) S
modelSchedule.at$createAction (0, modelActions);. q% F/ @" g" Y3 v! g" c
( P* a7 s. ~# f2 N/ |# f return this;+ Z% f" I7 ^. v8 I0 n
} |