HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( L) X3 ~, w2 m2 g& }
9 `: b$ ~0 Q6 X4 W public Object buildActions () {6 }, s7 ^1 x- `5 T' h4 t I; f# x
super.buildActions();$ n' D: N2 R! R6 A' I( T% k
7 \" J, y1 S$ p3 ] ~$ D
// Create the list of simulation actions. We put these in3 g# Z6 z* b# V) D
// an action group, because we want these actions to be
/ m9 n& {% N3 f- b" }$ [ // executed in a specific order, but these steps should
6 H& |" b# Y9 o9 X, U // take no (simulated) time. The M(foo) means "The message
2 Z! A! e5 ]. V! ~& @1 q- f // called <foo>". You can send a message To a particular/ R+ w4 C' m( M, S
// object, or ForEach object in a collection.
. B4 ?1 ^9 J* l' [- h
- Q: c8 O5 W+ T/ d7 o // Note we update the heatspace in two phases: first run
/ |; j |8 m+ y9 l+ r // diffusion, then run "updateWorld" to actually enact the
+ [% j5 Y" K! a" s // changes the heatbugs have made. The ordering here is
; W# W+ P. e r' X" N0 { // significant!
3 k9 f: O/ J, k. X! R4 ?
1 q9 X- L5 G! v // Note also, that with the additional: x H/ ]9 ?$ f9 g
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# V1 u# a& T% t // randomize the order in which the bugs actually run/ } i+ _! w) Z) C! T
// their step rule. This has the effect of removing any
% ~1 W. O M9 t* S/ g // systematic bias in the iteration throught the heatbug: U6 |0 N3 h# |6 r
// list from timestep to timestep
0 s) W+ U2 \" U 1 K& @( j( L _; M- Z
// By default, all `createActionForEach' modelActions have
% v! x9 \$ R' _5 F) N // a default order of `Sequential', which means that the
: ?/ h* [0 }$ f t/ A- g9 \/ d // order of iteration through the `heatbugList' will be
% b0 K6 Y; H6 v, J/ V* M // identical (assuming the list order is not changed! a8 }* O9 r: ?4 g$ {8 Z2 W
// indirectly by some other process).
" _7 t" t2 e, e" V! e
& I2 ^4 L) ^& `; t5 Y! v modelActions = new ActionGroupImpl (getZone ());
9 v: K9 D) o: o% q4 I4 K0 D6 U, F$ G
try {) A6 ~1 [, r# k& u p
modelActions.createActionTo$message
, _ i4 p. y/ C$ e (heat, new Selector (heat.getClass (), "stepRule", false));8 q1 R' D" e* ^4 z" W O
} catch (Exception e) {
. Z* [* v* |" t System.err.println ("Exception stepRule: " + e.getMessage ());
7 u! X0 ?) q$ u2 y }
+ s3 p7 Q) |, w) u6 s! h9 L+ }! {( D
try {
) Q0 i8 P2 f3 z Heatbug proto = (Heatbug) heatbugList.get (0);
e* J& J g8 ]" Z& U" p, \) N Selector sel = 8 U6 \1 N' V) D1 a
new Selector (proto.getClass (), "heatbugStep", false);
) x# b+ H+ R6 x) R o$ g2 H/ [ actionForEach =& \1 o7 |% q5 h* A" G% l; @- ]/ p. E/ V
modelActions.createFActionForEachHomogeneous$call
, _$ h9 z+ \. W7 V: L (heatbugList,% d7 K# H! m$ f$ x
new FCallImpl (this, proto, sel,
7 h0 Q2 [7 ~9 T new FArgumentsImpl (this, sel)));
4 |5 \8 @5 i5 A2 L } catch (Exception e) {8 _6 A5 T3 [) N$ c# \
e.printStackTrace (System.err);
" A$ Q% U0 I2 W" m }- Q5 T# l* y2 y8 C4 v2 T. P( r6 h
; K6 l% Z3 }$ m+ V Y
syncUpdateOrder ();
5 r% s! X& o( `5 l6 A5 L2 N0 X1 i0 }! M# i+ y/ _, O4 t
try {# h( e: t) r5 i' P/ T
modelActions.createActionTo$message
' U3 Q' U7 P3 m" B (heat, new Selector (heat.getClass (), "updateLattice", false));
7 Z+ Q0 P2 c" n, L3 B$ z4 ]3 G } catch (Exception e) {- A" \( n8 F* _, N
System.err.println("Exception updateLattice: " + e.getMessage ());: y* N& f. [7 L1 N& g) U$ S4 b
}
0 U% {2 l! K8 w / u( N& c* y1 _
// Then we create a schedule that executes the
% ^5 Y, w0 Q+ Y; @0 I. Q- U // modelActions. modelActions is an ActionGroup, by itself it
, Y- c! T# Z* ]. b; x6 M( G // has no notion of time. In order to have it executed in
/ e; @8 G; P+ Y/ F9 y // time, we create a Schedule that says to use the. H' p( q( h" i4 h1 i
// modelActions ActionGroup at particular times. This, P2 j8 s. @+ @( y/ X
// schedule has a repeat interval of 1, it will loop every# C) I: ^' I( G
// time step. The action is executed at time 0 relative to; V N$ }$ o$ z7 Y, L1 e+ `, l, s
// the beginning of the loop. I& r5 | J* L, d4 \& l. q0 J
7 e! w" X, ^# p+ t/ _" N: a+ _8 i // This is a simple schedule, with only one action that is
4 U- M7 L/ j) H/ V& j // just repeated every time. See jmousetrap for more- T, a3 J2 @. N- F6 @2 b
// complicated schedules.
( q9 b& c+ o3 ` @" K) Q
* k0 N( ^1 M) q, E! D2 x modelSchedule = new ScheduleImpl (getZone (), 1);
6 x+ Y; Y% O' b- c7 e modelSchedule.at$createAction (0, modelActions);
; B, A; X; J/ h0 f + k: @ J m) `4 B! f) O
return this;* d. ~% X3 p% ^4 k4 U3 x
} |