HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ Q; l4 }$ c( e' m& g& ^$ d- E
0 n/ v$ H) t d' _ public Object buildActions () {1 j% e1 e" _& g8 J
super.buildActions();9 x, y, r! v" ?, W$ m, c
% x; O4 {$ e c% @" ^1 g' f // Create the list of simulation actions. We put these in8 s$ F, k" u! }% b& W" E1 K% G
// an action group, because we want these actions to be; O! V% f9 m$ a/ Q& C, q# D
// executed in a specific order, but these steps should
. `$ r: |) `/ f // take no (simulated) time. The M(foo) means "The message1 _( J0 M0 c! C7 P
// called <foo>". You can send a message To a particular
: z5 o% `' v9 l // object, or ForEach object in a collection.0 q$ Q7 X( G6 k! S, r
3 G4 u& X' P7 ] i
// Note we update the heatspace in two phases: first run$ C. C5 J; Z8 }# e1 P1 V7 u6 j) i9 g
// diffusion, then run "updateWorld" to actually enact the, |8 J: \3 m e! E8 Y, T# R+ v$ K
// changes the heatbugs have made. The ordering here is
: l r- e7 P% s( } // significant!
* C! T6 D) l. l, E1 P: R
6 W' N# X/ w$ J8 [0 b // Note also, that with the additional7 \! a+ i% g( u1 a
// `randomizeHeatbugUpdateOrder' Boolean flag we can& h% Q7 V8 z& M( `; L( Q" k% C$ n
// randomize the order in which the bugs actually run# ] I2 d5 [8 U+ t6 o; Y) |
// their step rule. This has the effect of removing any3 V: k$ U" }& }7 q
// systematic bias in the iteration throught the heatbug( A9 v5 b7 }* c, x5 }+ W
// list from timestep to timestep5 m, h6 Q$ Z3 t9 A! k2 b
?4 E0 x$ f5 y, k
// By default, all `createActionForEach' modelActions have" {. t7 Z, {( N M* H* Z
// a default order of `Sequential', which means that the2 k+ S9 H7 L% P9 D
// order of iteration through the `heatbugList' will be8 x5 X$ A2 R' ^9 r
// identical (assuming the list order is not changed
9 T) P& ~; e8 s" O! S/ h+ X // indirectly by some other process).
" K) [. b) N6 _5 z M 2 ^" n z w* Q7 i% w
modelActions = new ActionGroupImpl (getZone ());0 E/ `0 N3 D1 T( R1 F) i
& F6 Y9 Q. S+ f* ~' C
try {
8 m5 o/ L2 G4 J modelActions.createActionTo$message( L: G( ]+ X4 @( R7 k
(heat, new Selector (heat.getClass (), "stepRule", false));
N0 C7 G- C7 Y& j' t) S2 t0 V8 n } catch (Exception e) {- o* ] q- F/ }+ S% C. B+ V
System.err.println ("Exception stepRule: " + e.getMessage ());
) q, A$ _* t" F# y; J }
8 u f" I3 I* r+ N, P
6 o8 B, G* P) e9 u; _! F5 g try {
# j. ?8 g: s. V7 y) _4 h$ u$ _ Heatbug proto = (Heatbug) heatbugList.get (0);
3 @2 Y( [1 ?. C! j Selector sel = & \* ?9 G& u& v8 h
new Selector (proto.getClass (), "heatbugStep", false);
* S- n/ o2 U( H; q0 h8 p actionForEach =
$ l6 J7 ~+ W, d! b modelActions.createFActionForEachHomogeneous$call
& d& r) f4 V2 c. a/ _0 {; K# _ (heatbugList,! H, A# W$ m3 z0 ^! O- N4 c+ U
new FCallImpl (this, proto, sel,
+ U; j% A% {, K9 W new FArgumentsImpl (this, sel)));
) Z" e; ?5 X% _& ^8 A- O; v5 y } catch (Exception e) {
+ b) J! y5 L/ N. i1 g7 Y e.printStackTrace (System.err);3 o# [9 q& W e
}
1 X7 C" ?/ i8 A' g ?$ U1 _7 W% e ) r# i$ b+ C- j" a( W2 d
syncUpdateOrder ();" G# G% Q" v0 z6 R+ G7 F
' y, ?- ~" }0 l8 M
try {% p. E& `. n' D- ~
modelActions.createActionTo$message 8 n: r- Y3 y- h" B
(heat, new Selector (heat.getClass (), "updateLattice", false)); y7 R5 c2 o0 z: o6 I
} catch (Exception e) { G a, o" o; u |0 r
System.err.println("Exception updateLattice: " + e.getMessage ());
' ]/ E$ x) F m }3 C i0 Z t4 }- Q/ q8 h) b
! s0 k+ M: O4 Z0 q // Then we create a schedule that executes the# ?+ N9 Z# q R& y* J, T; ]' L# A
// modelActions. modelActions is an ActionGroup, by itself it# Z5 C% U+ d; L7 J" n
// has no notion of time. In order to have it executed in( S; O0 u' z t7 w
// time, we create a Schedule that says to use the
1 M$ ]/ R/ S" M- ]# p8 Y X // modelActions ActionGroup at particular times. This% ?9 b/ ~4 @8 P, g i
// schedule has a repeat interval of 1, it will loop every
. S/ p' _) M( w3 ^- C& y( o( x5 `. \# i2 { // time step. The action is executed at time 0 relative to2 v4 O' D! y$ B8 N
// the beginning of the loop.
' W6 u( U4 k/ I1 m0 i' m1 J1 a, W
! @& V( q. {5 `& d4 r3 [& h // This is a simple schedule, with only one action that is7 F' N, f6 ?" [! i5 J8 G
// just repeated every time. See jmousetrap for more
5 o) c1 c! B- p V // complicated schedules.
+ ]0 O" k: @) z+ F
& {/ {8 C& \/ D4 k; J4 C$ Z modelSchedule = new ScheduleImpl (getZone (), 1);, R% N4 b' Z( ^8 p
modelSchedule.at$createAction (0, modelActions);+ A G% B* s. d+ j. {
- v0 f- H3 A2 @& _( J
return this;! a, }, Z) y$ n5 m, e2 z
} |