HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( R' k: V# B, B3 K$ E) @2 _; N% W T+ g9 N) K5 m: y
public Object buildActions () {9 W$ F7 @! Q) Z" y
super.buildActions();
: h* G5 Q2 r K% q! P" k2 L) n ' k" E" c/ f% \2 n6 [& N) I- l1 _
// Create the list of simulation actions. We put these in
$ ?( [2 R$ V+ E* ]3 p, j // an action group, because we want these actions to be
- n0 w3 A/ M" I# X/ E // executed in a specific order, but these steps should
A3 J0 l, R2 g // take no (simulated) time. The M(foo) means "The message/ T! b$ D2 g# x* } g
// called <foo>". You can send a message To a particular4 @! G" A, O* m% y" i0 G9 v
// object, or ForEach object in a collection.
2 O2 v# c+ s( f" m2 y
% `: b% h" y8 E& \ // Note we update the heatspace in two phases: first run Q/ E2 E2 H" @% D5 B
// diffusion, then run "updateWorld" to actually enact the
8 c2 }' N7 ~3 A) u7 Z& D9 ~ // changes the heatbugs have made. The ordering here is
4 }+ L F+ e. V // significant!
4 C' _8 ^, E; f# ^% d: d5 C3 O% x 2 J* @+ P' i. l" g( K
// Note also, that with the additional, b- E- F% D O
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 F/ t- o. y6 Z( G1 ] // randomize the order in which the bugs actually run! S3 U2 l& V; I, D0 s4 _
// their step rule. This has the effect of removing any6 v2 H5 ?& I1 K% b
// systematic bias in the iteration throught the heatbug& f. D9 h: N5 T, X
// list from timestep to timestep
) \8 T2 ^% [7 Z1 u# @5 z, i7 a: w / V8 t9 y2 E5 Q6 S7 d
// By default, all `createActionForEach' modelActions have
0 d I7 q4 }) _" z- J; @" [ // a default order of `Sequential', which means that the9 L7 G5 v8 U! d* l8 u
// order of iteration through the `heatbugList' will be
* r: O: F% K* h% u. h& W! l+ P1 f // identical (assuming the list order is not changed5 `$ ~0 O/ V) h- N g
// indirectly by some other process).2 N! [6 U% h# ~- i
" o b$ }/ ?2 }) H, L modelActions = new ActionGroupImpl (getZone ());
' ?6 ?0 T# I, V
" a+ i/ |) \, M# f" y5 x0 A try {
1 i8 S) \/ C4 u# n4 U5 O5 W0 F: ` modelActions.createActionTo$message
5 z( t8 y7 F2 `, g6 k, f8 T& u" F (heat, new Selector (heat.getClass (), "stepRule", false));* W+ R8 |7 C0 L5 l5 E( ]! c
} catch (Exception e) {
) p* Y: Z1 @& h' h- d3 \ System.err.println ("Exception stepRule: " + e.getMessage ());
. W# I* o3 a5 ^% ^: `* [ ^ }
+ f8 S( i8 W/ k! l6 O# D2 K" E9 ?4 L
try {
8 J( q6 i* Y& P- Z7 K Heatbug proto = (Heatbug) heatbugList.get (0);. {6 g; @ ~# t
Selector sel = % N ~: e8 Q) W: ?9 u
new Selector (proto.getClass (), "heatbugStep", false);
' t g9 x5 q+ B$ o3 b: ~) W actionForEach = L f2 S" r# q2 z1 j
modelActions.createFActionForEachHomogeneous$call0 c. J6 d G4 ]& Z
(heatbugList,% T1 K2 H) `4 r5 N; @: F
new FCallImpl (this, proto, sel,0 }% a7 t) \+ K$ ~' N( I+ E2 t a
new FArgumentsImpl (this, sel)));
/ v2 @* u) a, a1 b9 A* c. y } catch (Exception e) {
# s5 d }2 x5 z# ~6 ?% a e.printStackTrace (System.err);
3 Q' v' Q; G B6 G3 c& v2 C }1 [0 d0 A6 G: u. ]
' ^ E9 f0 [3 p' d; g& I/ Z+ X
syncUpdateOrder ();$ m7 A+ \7 F% d0 v0 W0 R
3 R2 f3 s x% r try {, A, Q" {% u* \8 O6 G; P
modelActions.createActionTo$message
) j6 ~* R& [' e1 q6 I: [" V (heat, new Selector (heat.getClass (), "updateLattice", false));
: y: E& I1 ]0 L5 X- v0 u$ z8 U } catch (Exception e) {
1 P4 l5 W* ?0 {* S' t: X# J/ s System.err.println("Exception updateLattice: " + e.getMessage ());) a+ ^7 l6 i7 k2 s2 s
}
4 m- R( U4 W& \: Z/ S# T 6 P& X% m) o7 M
// Then we create a schedule that executes the
4 I' a0 I7 O+ v; E0 o, v // modelActions. modelActions is an ActionGroup, by itself it# u; C7 Z. M3 B4 C7 ]; Y
// has no notion of time. In order to have it executed in
! D9 \+ d$ ]9 b( J$ b: V // time, we create a Schedule that says to use the* \3 B" a. M. Y! t. L
// modelActions ActionGroup at particular times. This2 |0 A% J; u! @3 \. O
// schedule has a repeat interval of 1, it will loop every( u- l# V! s5 O4 L# \
// time step. The action is executed at time 0 relative to
& T7 T ~! B" m& x0 y& G2 B // the beginning of the loop.
3 y& V: `6 ^% K q' _' t$ ^$ b
& F6 O! a* a6 }- T" ?( L9 J // This is a simple schedule, with only one action that is/ O/ I# X& D* N' \) A
// just repeated every time. See jmousetrap for more) t3 _: u2 H8 Q; x( W
// complicated schedules.
' \3 ]% U& ^( P8 D6 f7 C# P! F + `9 z w: q* y) n, ]2 W
modelSchedule = new ScheduleImpl (getZone (), 1);
' `4 A# i5 Y4 m. X modelSchedule.at$createAction (0, modelActions);# z# W; \- v) w3 |6 p2 y
\0 i+ Z J- Y$ D/ @- G" n
return this;' {3 d. z7 L6 g6 D7 l
} |