HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ f; x# Q( v+ P' N4 j7 R# h1 n) n2 j
public Object buildActions () {
4 o( a3 T" k; h( |0 I super.buildActions();
9 Q/ w" }+ M5 V- Q r- S* b T5 `6 E2 m6 A0 r4 U
// Create the list of simulation actions. We put these in; ^, X* A& T: o# Y& S" T
// an action group, because we want these actions to be
, U1 w: n `6 q& ]4 f# x' Q // executed in a specific order, but these steps should
8 _+ N+ S: \5 h2 [& d // take no (simulated) time. The M(foo) means "The message& M! ?1 W! y0 L3 {% D' G) @
// called <foo>". You can send a message To a particular8 N- u7 b* L: \. N
// object, or ForEach object in a collection.
$ b8 i4 ?8 U6 B" U; ]6 Z. ` # y* a4 ~# S: U& l: K
// Note we update the heatspace in two phases: first run
) _3 P) x# `$ y' q3 \3 N // diffusion, then run "updateWorld" to actually enact the
; K+ k4 ~1 K8 E6 o: n6 E // changes the heatbugs have made. The ordering here is
! @6 {/ K( D# T, h // significant!
5 \& k: s0 `5 x$ [: i
9 m* r/ P% K$ I // Note also, that with the additional
, l B) O5 d: M" W. V2 n0 g4 \ // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 u, ]: i3 ~( q) g // randomize the order in which the bugs actually run. P5 }- I) ]# g
// their step rule. This has the effect of removing any
) J- d( I& |- X* L$ S, ? // systematic bias in the iteration throught the heatbug# S2 |+ V& r A1 q5 M5 i' y% u3 r
// list from timestep to timestep
2 |; ]( Q8 Y3 G, m : D4 x% v! S2 h7 r9 h- U
// By default, all `createActionForEach' modelActions have
2 e1 P' o. ^' \ // a default order of `Sequential', which means that the
5 U L% U1 @; g N, I8 q; [6 w8 M0 s // order of iteration through the `heatbugList' will be7 ~& I5 e- U& a, D) K2 [& ?
// identical (assuming the list order is not changed
8 i4 G [4 o- ?1 Z, _* i // indirectly by some other process). \, m# M) b6 p+ S2 b* t
) d' ?. l( w0 \) k' U% H6 N( Z7 n' e
modelActions = new ActionGroupImpl (getZone ());4 a* b/ V. ?. [6 ^2 r' ?. I
7 z7 E( L: x, J0 s, f+ R# x2 O try {% n! H. G" o; q% k- [
modelActions.createActionTo$message
; G* P: G4 F* |( \0 S) m/ N (heat, new Selector (heat.getClass (), "stepRule", false));3 r5 u: n* i/ t
} catch (Exception e) {
/ x/ F8 H2 s* M Q; d: ]' C System.err.println ("Exception stepRule: " + e.getMessage ());
% |; U* L7 y8 p S0 f1 q! C }: ?: v& G- V7 [! e, X
5 S' B! O! ]$ ~, T) R
try {
) Q, V7 k5 q3 z; g- W Heatbug proto = (Heatbug) heatbugList.get (0);& J" B8 s) o- p# y8 t
Selector sel =
7 H$ I/ D7 M% x" h new Selector (proto.getClass (), "heatbugStep", false);
. I) U* ^+ b! O' Q$ P9 e5 ` actionForEach =9 I# F) m2 W, N1 O1 F. D3 m
modelActions.createFActionForEachHomogeneous$call7 a d) x- |# U7 b8 M
(heatbugList,
8 K$ _4 X& u# Y! ]$ A6 j* |0 e2 A' L new FCallImpl (this, proto, sel,% U0 e- a/ ~1 U' R0 |
new FArgumentsImpl (this, sel)));
+ m# i) }6 ~$ M/ R3 d( j } catch (Exception e) {! d0 r. w; }* ]( L4 S% @
e.printStackTrace (System.err);0 \1 a! I: [" c, w! y3 s
}* M. ^& N, Y- q
0 v+ M( o% U n' e; z, i; w! P
syncUpdateOrder ();
4 O' Z% F, o+ p% L1 Z$ V6 O0 \( s* J; H
try {! ^/ |% d. q! z
modelActions.createActionTo$message ) j8 c+ P P: ^8 s
(heat, new Selector (heat.getClass (), "updateLattice", false));
& k9 m- v3 D5 X3 b } catch (Exception e) {
" o0 m$ B9 U) H' D% T9 @ System.err.println("Exception updateLattice: " + e.getMessage ());- W; S0 z5 J* A. [& P8 g
}( f2 K9 j( H" z$ a- f' u2 V; K
' N3 `$ P* J: X0 ~/ |) o // Then we create a schedule that executes the
! m6 m1 V/ D6 v3 p' U; g // modelActions. modelActions is an ActionGroup, by itself it
* N: w: i2 Y4 F // has no notion of time. In order to have it executed in
0 n6 J7 s" E* ~ // time, we create a Schedule that says to use the | y. @4 U1 V O4 \
// modelActions ActionGroup at particular times. This8 B0 G0 `/ ?( e! y0 M5 S+ N3 E8 v5 t
// schedule has a repeat interval of 1, it will loop every
- |; h, I7 e" Q9 ?3 [: s // time step. The action is executed at time 0 relative to
# _" j* M5 {1 x // the beginning of the loop.) D, l' w5 D1 C, z* b7 j* D) ~
7 ]2 Z2 T2 k1 S) Y% y3 L) @! r
// This is a simple schedule, with only one action that is/ z2 t: B; \' Y1 P
// just repeated every time. See jmousetrap for more
* _( O* \- x0 b3 `" ~0 ~ // complicated schedules.
% f: T% \9 E9 Z2 r9 @, @& K5 X
4 L& `8 z- k5 f7 I modelSchedule = new ScheduleImpl (getZone (), 1);2 G% H# k5 R/ z, z/ o* i
modelSchedule.at$createAction (0, modelActions);1 r3 p" c1 @( p; Y# f
; C' ?! j0 u. t. q
return this;- w* q* v! I& N: S. M# z
} |