HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 ]# i! q% ]7 s( j* d1 J' d7 k
) I( T6 r" n5 T, v* h; F
public Object buildActions () {
5 U( q7 S8 z _/ L super.buildActions();
u. {0 R* ]/ Z1 y $ i$ _# B% z r
// Create the list of simulation actions. We put these in% X) e* S" k( y% H3 {
// an action group, because we want these actions to be
8 K& E( r# P3 P7 c // executed in a specific order, but these steps should b8 J8 m; T& R" ^+ F! {
// take no (simulated) time. The M(foo) means "The message
0 ?& o# `4 w( s // called <foo>". You can send a message To a particular
& Y" S% B( u2 Y // object, or ForEach object in a collection.
' q' t6 n! l; J/ D1 W2 ~
P. X( b, K3 k/ Y6 n2 {4 d; i9 C // Note we update the heatspace in two phases: first run
+ b1 K. [. B. n8 \$ J/ ]" m- D // diffusion, then run "updateWorld" to actually enact the
3 q2 l% u' Z% A4 W+ R // changes the heatbugs have made. The ordering here is
. ], T4 k, r0 \3 v# b% a3 e1 _' T // significant!# m) i/ s+ |( q; V/ t
& y: r- e7 c2 M$ ]0 @) S' }
// Note also, that with the additional7 f5 \" a) ^# O h; ]
// `randomizeHeatbugUpdateOrder' Boolean flag we can
z+ V0 Q. \: x4 i9 c& b& l // randomize the order in which the bugs actually run
+ K4 E Z8 l& ]+ w6 e! | // their step rule. This has the effect of removing any
( \4 W5 Z. j, g0 V // systematic bias in the iteration throught the heatbug
2 A. |# |" g4 V; f( O: j // list from timestep to timestep7 Y8 k, n/ L" P
2 I; O% O9 y7 F( ~, h# E+ S
// By default, all `createActionForEach' modelActions have- M1 {" Y- r4 Y6 D1 C' M' f
// a default order of `Sequential', which means that the1 Z& h5 G4 u! D x8 }8 T, k
// order of iteration through the `heatbugList' will be3 k3 |! c2 U* H1 M
// identical (assuming the list order is not changed8 p) K& d% c) v
// indirectly by some other process).
* w5 K, }4 u9 d# z ' ^& |( d) V/ Z J
modelActions = new ActionGroupImpl (getZone ());- i% Q) G9 V' k) \0 t% q& N% |
6 h. ]9 u, c, j( j try {
9 p+ q" ^4 o% L+ S% ]* F modelActions.createActionTo$message. p0 j# W1 d6 k# f
(heat, new Selector (heat.getClass (), "stepRule", false));
1 J+ a% M. s- H& |7 b/ R% l6 o } catch (Exception e) {
4 `+ T9 q+ t3 x9 k+ W System.err.println ("Exception stepRule: " + e.getMessage ());# t7 u; N, {) Y2 I2 ^- L. r
}
/ R7 _( c w' c5 Q. }: |1 k7 W }4 f' z7 q8 e5 {
try {
, ?# G M3 T8 ~3 K% i Heatbug proto = (Heatbug) heatbugList.get (0);
; y* A6 u9 R/ v2 S- ^* g Selector sel = % P: e; O3 q# d! d9 T
new Selector (proto.getClass (), "heatbugStep", false);
) @9 \- r5 `" z) V actionForEach =
( Z$ b9 p1 @1 z! u7 A* v% v modelActions.createFActionForEachHomogeneous$call
8 ^ Q/ K1 q. A4 y (heatbugList,' ]3 F" g1 c3 f" l* B. u: [
new FCallImpl (this, proto, sel,9 y2 f+ q% B3 O5 }* _" F i
new FArgumentsImpl (this, sel)));
9 A. ?9 C6 f9 O$ K } catch (Exception e) {# }5 u. V$ k* U8 @* P* u
e.printStackTrace (System.err);
t; _% _! K2 [, c- D }
4 j5 U; q& P2 ^! |' a 2 O X8 P. S" T
syncUpdateOrder ();
8 d1 c0 r- f& W4 Z& i3 X- Z+ H, x3 O
try {
: \& z7 i% k6 O% C! X* F9 N: O! ]" } modelActions.createActionTo$message - [1 {3 b7 I! H$ \! {! H
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ @: J( v/ G. k2 h5 P0 h+ K } catch (Exception e) {" U! p6 q2 ]+ o2 ]7 {9 C
System.err.println("Exception updateLattice: " + e.getMessage ());9 ~0 s$ w* Z" v* ]. ?$ k& H
}
# U# V3 L1 `& u9 l7 C 6 U* C$ P* e+ ], q- f" i
// Then we create a schedule that executes the
, h; H5 i! q) @* g7 P+ ]' [ z9 C9 ~ // modelActions. modelActions is an ActionGroup, by itself it4 K% y/ L! ^4 n8 M0 A
// has no notion of time. In order to have it executed in
3 x& ?/ ^. S& r' m% f" | // time, we create a Schedule that says to use the
v* I+ V: i0 U+ O3 n // modelActions ActionGroup at particular times. This6 r, r. L4 S, Q' A& [! z$ I- J8 A
// schedule has a repeat interval of 1, it will loop every4 V8 L( Y& z4 [6 M9 d4 A
// time step. The action is executed at time 0 relative to8 K/ ^' Y( \4 v% J- j% w1 K
// the beginning of the loop.% y0 Y$ b* Y7 j, A$ k
. D4 T; S9 a' \# H" s' g // This is a simple schedule, with only one action that is& K- S: w) X8 W9 G: h
// just repeated every time. See jmousetrap for more
. O' N+ k! ^* b* c // complicated schedules.
8 Y( B) b! ]3 S6 [
6 Z- k, A: i: [# X modelSchedule = new ScheduleImpl (getZone (), 1);
6 _( l0 [4 S# n, q! n) _. | modelSchedule.at$createAction (0, modelActions);
( K! s# l8 p# `6 h
9 u) a% E. U3 ], O3 y5 v return this;4 c Z. q+ N! M1 c: o
} |