HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 W$ l( L" V) v$ I. _! M: ]
$ d' D- x- K* Z* d6 k" C! w
public Object buildActions () {
9 N. U5 u1 Z4 O3 t! ~/ a- w super.buildActions();
! |- y4 o [8 ` o4 g $ y6 n# H% Z, y' ^
// Create the list of simulation actions. We put these in
: w, {* H( P; K n6 z // an action group, because we want these actions to be
2 a6 U" z5 C0 w // executed in a specific order, but these steps should( I& _# U3 `- n$ n+ s
// take no (simulated) time. The M(foo) means "The message5 S8 X7 h- s. v5 L
// called <foo>". You can send a message To a particular+ ^1 E7 N0 ~1 u
// object, or ForEach object in a collection.' ] @4 M6 ?( {) Y0 z5 K* l
. Z0 J( B* ?- I6 M1 l // Note we update the heatspace in two phases: first run7 d4 v+ o1 H: l1 e. F3 g
// diffusion, then run "updateWorld" to actually enact the
# p" ]: X- ^, D6 | // changes the heatbugs have made. The ordering here is
4 C5 B2 O* A7 K" P // significant!
) R! k% @" ]3 m+ n/ D' N ( \% @8 I7 \, ~& r
// Note also, that with the additional7 R6 o5 q j+ s! Q/ R6 m% Y
// `randomizeHeatbugUpdateOrder' Boolean flag we can
M' @9 L9 `$ {5 F7 g' L. m // randomize the order in which the bugs actually run1 `; L& `' f* x' l( s3 U
// their step rule. This has the effect of removing any
6 W) P, Y% C* m! r9 E( R# ` // systematic bias in the iteration throught the heatbug) r$ g: ]7 s' W$ y( ?& \
// list from timestep to timestep5 O L& j* H' D
1 n z1 d4 k% }' C8 E2 N
// By default, all `createActionForEach' modelActions have s6 L4 V+ ^5 G# g6 V4 ?7 }/ H
// a default order of `Sequential', which means that the/ f- [! O( `$ p5 I$ T
// order of iteration through the `heatbugList' will be0 A# }' `7 ]- _2 F
// identical (assuming the list order is not changed
6 k) |5 P O* ~% p // indirectly by some other process).
0 Y# u' \2 G- |5 W8 d# R6 B' F
z- o( q7 x) o& T* q, P modelActions = new ActionGroupImpl (getZone ());
) K- F# X8 t7 ?8 a2 A, b- h
+ O+ Z: W; o7 J0 |" y: L try { h# k- H. E0 K" W/ ~4 V) g8 ]
modelActions.createActionTo$message
7 \* d5 U! u* f4 z: k (heat, new Selector (heat.getClass (), "stepRule", false));
. U- Q- v' k' P. p% r }& Q } catch (Exception e) {; t3 P9 }# M+ ?1 U. \( W2 G* x1 x
System.err.println ("Exception stepRule: " + e.getMessage ());
3 R$ a3 \$ Q" v. y }2 D1 F0 t/ @$ c/ t7 b
4 K4 P% }4 {, X( k# C
try {
# {: A8 i: s7 m; o$ _ Heatbug proto = (Heatbug) heatbugList.get (0);! C; c; G: }5 R" `9 I8 C
Selector sel =
$ _1 x* f9 ~! n9 T8 e+ k new Selector (proto.getClass (), "heatbugStep", false);/ U! {8 b" E8 f% j& i
actionForEach =) Q8 L ~$ Z6 ~3 e
modelActions.createFActionForEachHomogeneous$call1 L' [3 l' g5 v; {# Z7 Z/ w
(heatbugList,
4 ^1 W$ c7 R1 b7 `; { new FCallImpl (this, proto, sel,4 ^) Q: ~ |, i m+ y8 |
new FArgumentsImpl (this, sel)));/ {5 G6 U0 L1 Z
} catch (Exception e) {
& `2 [' u- f9 {. ?( u! D: ]/ t! Y' g* Y. A% Y e.printStackTrace (System.err);
. I1 Z" B* W* \! |3 } }
7 I6 s6 L. C( ]! C / a, O" S8 O7 U1 [+ W
syncUpdateOrder ();6 R- u- n T3 q
( |: t- Q- d7 K% v4 k5 R try {
; b; e" K7 \; Y5 Q modelActions.createActionTo$message 8 h& `1 v: d" S% u. h" f
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ V& ~9 V- M7 J } catch (Exception e) {
6 n( O7 o" ^( t" l2 _9 Y6 [ System.err.println("Exception updateLattice: " + e.getMessage ());
2 `7 b3 B/ T d }1 X" y0 N- `2 T
( n @' T* K2 {
// Then we create a schedule that executes the
3 G( }0 R$ T/ \$ ^2 c8 V- r // modelActions. modelActions is an ActionGroup, by itself it
! ]& y* |8 K, n9 y2 S$ C // has no notion of time. In order to have it executed in
0 M: T, a8 ^% A& z( W/ S) l. |( x' X: d4 E // time, we create a Schedule that says to use the
# x3 T0 Z: X' L/ Q& L // modelActions ActionGroup at particular times. This, t- H7 {9 h3 x9 y
// schedule has a repeat interval of 1, it will loop every, T A4 X% G3 m# P6 n/ _
// time step. The action is executed at time 0 relative to1 V+ ^ c/ G/ r2 _: e
// the beginning of the loop.3 T0 a+ L5 j/ ` R$ H) M( X$ T
( u: {7 ?1 K/ p // This is a simple schedule, with only one action that is
2 O2 I' H; S% W" X, u! T: S( ^ // just repeated every time. See jmousetrap for more$ N3 ]% {& ~5 Z( @! k
// complicated schedules.$ ~1 h, O: F' U' [- a# ^1 v# u
% I+ y! @& |6 \ modelSchedule = new ScheduleImpl (getZone (), 1);
) j9 E2 u1 P" I modelSchedule.at$createAction (0, modelActions);, a a5 Q, v. B, [
7 K0 u# d; H* O& {2 A" J1 h" A- a+ t
return this;: x3 U9 m: ^2 o s( G" X! d
} |