HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# s0 C" A* a, N( i2 o' N8 X
: N A* X7 V" n+ m* P public Object buildActions () {
% k5 x/ k8 }3 S* V7 k" h# k super.buildActions();
! x- w, t y7 L" f4 J. X: L1 Q
% O% M2 i6 h9 @& s4 E* c1 m) ^ // Create the list of simulation actions. We put these in) Q! e# J! s3 y# |/ n: q7 x
// an action group, because we want these actions to be+ A: T. c" f4 b4 P; w% N! C
// executed in a specific order, but these steps should: {8 ^( J8 S" c4 G
// take no (simulated) time. The M(foo) means "The message
' y- b* Z1 T# f, U0 F // called <foo>". You can send a message To a particular
' Y# A( c- O. A: a' r$ w2 z+ f0 X // object, or ForEach object in a collection." L+ s- A0 V! N9 Y0 o; y
) d4 A$ b& o/ ^5 i, z/ ?
// Note we update the heatspace in two phases: first run
2 S9 p9 i0 p- _ d5 e* o // diffusion, then run "updateWorld" to actually enact the% l2 L" l9 I, f# M# [! T6 r# }3 \
// changes the heatbugs have made. The ordering here is
) x+ y3 o9 x! \/ A; O F7 k( [* s' Z // significant!) u3 g, Q2 h5 i& U2 y% \ V
9 J$ n Q; H. ]7 q& B // Note also, that with the additional/ b& v, g+ X! s! V" _
// `randomizeHeatbugUpdateOrder' Boolean flag we can. W5 p# }5 z; u N% M5 l
// randomize the order in which the bugs actually run1 y* Q/ Q1 w3 j) s, _8 C, S+ `
// their step rule. This has the effect of removing any
/ q% E/ I- s% c% d. \- M // systematic bias in the iteration throught the heatbug, z% ~, `0 }8 ?. r5 q/ g
// list from timestep to timestep
) \. |( y8 }# m3 t2 G
2 |! l( D' k# s$ J( q( ]( s // By default, all `createActionForEach' modelActions have
8 O; p' Q3 d) k% G( [% k // a default order of `Sequential', which means that the+ N6 |$ L+ v5 z* R* v
// order of iteration through the `heatbugList' will be+ N* r' A: h* f% j, E* b
// identical (assuming the list order is not changed* [4 U5 ^$ i ~2 u: d5 f
// indirectly by some other process). i! j9 I8 K# x8 H# H) e5 K. i8 d
1 w; F8 `% o. M0 e# u* A modelActions = new ActionGroupImpl (getZone ());# I! D" s: o$ r6 o/ w
6 `# ?5 V; B/ z+ X9 ^7 B try {( K5 A3 r3 C) O* M; _; M5 p; I+ I
modelActions.createActionTo$message' X0 J* |* ]5 ?% H# u( |
(heat, new Selector (heat.getClass (), "stepRule", false));# R, J0 r, z: C0 E# ^0 G! \
} catch (Exception e) {
- V" }2 {% _% b; T' L1 s! ~ System.err.println ("Exception stepRule: " + e.getMessage ());4 z+ x& m* u5 J3 C
}
9 [- M* f+ I) L: ?* q
% y3 e7 K9 N' X! D& b: K1 u try {' X) j ?. L, b0 \& P, u: f
Heatbug proto = (Heatbug) heatbugList.get (0);# ]9 l' W7 ~* P: {1 p
Selector sel =
8 m# y0 J* X, P& P5 e new Selector (proto.getClass (), "heatbugStep", false);
% O/ u7 i1 G1 y0 o+ c& ~ actionForEach =/ B" _( M/ S5 m1 V% |$ P
modelActions.createFActionForEachHomogeneous$call+ R, P- y" J% D; I
(heatbugList,7 @; Z$ J4 F3 z; ]* e1 N! g
new FCallImpl (this, proto, sel,
0 n; a D5 H; p/ r5 \# d new FArgumentsImpl (this, sel)));
. K1 y! `$ o" z% O } catch (Exception e) {! p4 U+ \# I* `: v
e.printStackTrace (System.err);# E* Y& n& @& t: Z, e# u" V
}7 g5 M. R$ b7 y
' |# ]9 C+ d8 [$ o6 e. a' V, P
syncUpdateOrder ();! H+ n) F: U6 m. H( n
- ?0 E, [# X* q/ g try {, ?% P, j$ G* i) G% S# V" `$ w
modelActions.createActionTo$message # H% G6 s4 S( c9 l1 ?' O
(heat, new Selector (heat.getClass (), "updateLattice", false));! ^5 g+ n! h, N4 e/ Q/ C' m
} catch (Exception e) {
) n: J2 P# P7 W1 X0 T* j* I System.err.println("Exception updateLattice: " + e.getMessage ());
: n( }( W6 d' o4 Q9 j }
( }1 K: N B! a
6 j1 A8 G# t- E6 S, ?2 b( T5 l // Then we create a schedule that executes the
& E1 j& ]2 x3 h // modelActions. modelActions is an ActionGroup, by itself it
& E" d. ~( V: g+ `, L: W // has no notion of time. In order to have it executed in
* b) V: l( D W/ v# a1 d/ J // time, we create a Schedule that says to use the
: J _/ }! T! {% E6 ]( g' g // modelActions ActionGroup at particular times. This- k; f( {2 \- F
// schedule has a repeat interval of 1, it will loop every J" Q# o6 ?! E9 X$ T1 \! m0 @
// time step. The action is executed at time 0 relative to
( `5 L$ K: M( c) @. v8 k0 U7 m // the beginning of the loop.
6 \* O& ]/ F0 ^ f: N) B8 D+ R0 a1 P( ]# t! R) I! Q1 W* N' O/ N
// This is a simple schedule, with only one action that is
5 n2 j) N' q) Y6 o( ]6 U4 G; E; b7 Z // just repeated every time. See jmousetrap for more
' u( w# e# O+ `6 R3 ]! S // complicated schedules.1 H/ S$ J+ K9 M! {. F! B' K7 T& F
, i% t; }' ]/ U b. i } modelSchedule = new ScheduleImpl (getZone (), 1);1 a6 T7 |: h. [, e$ W
modelSchedule.at$createAction (0, modelActions);
# ]+ {: y7 X: r, ~7 H" M& Y ; j4 f$ T+ K, f
return this;6 A! J% ]! c2 s2 V
} |