HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; {- V6 [: a5 L# M) q0 q# T
2 t, E# O, \, e! H5 v0 J8 M6 H0 x
public Object buildActions () {( V/ r: {4 Y) n4 _, ^2 a6 ~
super.buildActions();5 q, ?% m1 T1 a1 t; H
+ L- r/ o1 x! P/ O: F // Create the list of simulation actions. We put these in
: `% {1 t. v* k // an action group, because we want these actions to be/ ?6 B- f/ K5 u( L
// executed in a specific order, but these steps should. u1 D( X- N9 c+ A
// take no (simulated) time. The M(foo) means "The message: u+ W9 `1 T0 k1 e% E% [, J( V7 F
// called <foo>". You can send a message To a particular7 [: \0 H5 F. O0 C% @0 t
// object, or ForEach object in a collection.
: q4 j7 S9 d" N# {3 K" Q
3 q& i/ e* X5 p v1 t* W$ | // Note we update the heatspace in two phases: first run! ?- H1 M( q, S% G; }2 ^
// diffusion, then run "updateWorld" to actually enact the
1 [7 ^) u% G ~! w // changes the heatbugs have made. The ordering here is a% `& ?" F# G% M% a) s' x
// significant!
8 c) g0 E, W& {( O& f+ b
4 x4 D* O7 [) |" [" ^0 Z6 w% j q // Note also, that with the additional
" D4 N' y' x) }! i, ^+ q( I# V/ o // `randomizeHeatbugUpdateOrder' Boolean flag we can, o6 i; w( @' T
// randomize the order in which the bugs actually run
: I6 w: y, @8 e/ ~5 C // their step rule. This has the effect of removing any
# x" M8 q/ Z3 |' Y& S // systematic bias in the iteration throught the heatbug# ^5 G2 i/ Z% e9 o
// list from timestep to timestep
' b8 k( ]6 F+ @1 ?3 w5 R2 d
: ~- c5 O a, W9 e) V$ ~ // By default, all `createActionForEach' modelActions have" t) `. ?! }6 e; k/ {1 V" L
// a default order of `Sequential', which means that the; q1 Z: Z' |7 z* g
// order of iteration through the `heatbugList' will be- I$ ]* S8 _; h: G" `& |
// identical (assuming the list order is not changed& f1 v( r# j3 G& \
// indirectly by some other process).# `5 ]$ V D% L$ m' _
9 V0 w' J: n5 i! y modelActions = new ActionGroupImpl (getZone ());
, X; v: R/ V' y j; Q- {! \+ M8 ^. x8 s: j; L
try {" ~8 @3 D( [% [6 m
modelActions.createActionTo$message+ `3 }9 G" r5 S9 l
(heat, new Selector (heat.getClass (), "stepRule", false));, y# a8 ~1 q1 @6 H- b! ^
} catch (Exception e) {6 U' ~4 S1 @& D# M: I
System.err.println ("Exception stepRule: " + e.getMessage ());0 M; `5 v; [7 R
}. O, @3 S/ V% T, p% M6 U; ]
- \$ \0 o. ?% e7 b- F
try {# _- w: ?$ C8 ~0 h
Heatbug proto = (Heatbug) heatbugList.get (0);; y5 |* O. ~' O; B* x
Selector sel =
7 l2 q! z' i3 m9 q: w9 l* x( b( ^ new Selector (proto.getClass (), "heatbugStep", false);! K: a, {) x8 Y
actionForEach =& i8 X8 Z8 G! S+ [$ T
modelActions.createFActionForEachHomogeneous$call! B8 Z7 Z2 r( h( |4 W
(heatbugList,2 b; g3 f# c6 I# {) @
new FCallImpl (this, proto, sel,
# g. i! ^5 \4 ^) @& J" s" O) d/ m new FArgumentsImpl (this, sel)));
+ z% ]! A: M; a c2 ` } catch (Exception e) {
" F1 i9 M' u' n+ _; T/ Z5 o e.printStackTrace (System.err); S6 w% P$ e/ Z5 K1 {. x
}# i+ R0 x0 H! D2 z. f
$ K2 ^" v7 B' w
syncUpdateOrder ();2 A! [8 C. k, T, M5 L
4 A4 _- {: g% ^( l# N9 S* L; i
try {
% ]2 a+ e5 H. ^! d( C modelActions.createActionTo$message
, b* D* l) \; { (heat, new Selector (heat.getClass (), "updateLattice", false));
! j! G# M' B! v; O: X } catch (Exception e) {
- Q$ U8 J9 v+ |! O- \ System.err.println("Exception updateLattice: " + e.getMessage ());
+ [1 M3 Z( W9 v, R Q }
( }5 V( Q/ Z# q( j0 y
+ V$ k0 ], f: S2 @ // Then we create a schedule that executes the
2 U: ^4 f. g. I/ e7 P9 p // modelActions. modelActions is an ActionGroup, by itself it
$ G( p; s9 L% L; z: Q4 q2 m$ K // has no notion of time. In order to have it executed in& w! l" s1 }# D2 ~' e
// time, we create a Schedule that says to use the5 ]1 t' M: w* |8 R4 O3 e3 i+ c
// modelActions ActionGroup at particular times. This. b% U* p8 V% ]5 e" t7 ~+ M7 ?; \
// schedule has a repeat interval of 1, it will loop every
v0 p3 V% W7 i) `6 l8 U7 ~ // time step. The action is executed at time 0 relative to; [( K" I3 p/ P& m
// the beginning of the loop.- R# m( p' O* e: v; G" M" T
" u! ]3 j" S* O( O( Z // This is a simple schedule, with only one action that is' \" }* M: q y
// just repeated every time. See jmousetrap for more
" |8 l9 A! G* g+ Y9 ]5 m6 S // complicated schedules.. K3 W( J; Q: B4 \( K
4 R, E' L* |: b6 B3 D: Q% J* f modelSchedule = new ScheduleImpl (getZone (), 1);
0 S: U, \, J* r. B modelSchedule.at$createAction (0, modelActions);
0 D y* }, X4 E/ h7 Y
* \9 |4 P8 b0 h W+ q% H return this;1 O6 }9 |# C4 B* N. X9 }
} |