HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 n/ x. L: n, ]
2 W: G3 [& ~+ G M+ P
public Object buildActions () {# ]) }0 C! x' N& F& S" S# c! P0 t
super.buildActions();
4 i$ @8 B/ H0 v' E. J% d7 a . m% G# x0 ]4 }* i8 Q; k
// Create the list of simulation actions. We put these in
! r( t( H7 M( V! ]" `% ^+ r // an action group, because we want these actions to be
8 p2 _- ~! M' |7 p& g2 g // executed in a specific order, but these steps should2 w, r9 Z$ d$ G4 f8 ^, b5 p
// take no (simulated) time. The M(foo) means "The message
# M" C8 Y$ X! n9 j1 { // called <foo>". You can send a message To a particular
- u2 J: H; } i3 ]6 g // object, or ForEach object in a collection.1 S( y5 g# v. w5 x/ C$ |
2 G0 T# Q9 J2 b
// Note we update the heatspace in two phases: first run
! g. _/ |+ q7 a' Q2 Z2 l2 h // diffusion, then run "updateWorld" to actually enact the) {; U) j/ D) Y5 ?5 r: R# q
// changes the heatbugs have made. The ordering here is
& p$ E- Q a2 e& O0 ~" H. D( V // significant!3 B# u5 y: ]* f* T% ^& ^
9 ~' @ R$ W% J! P1 T; A6 E // Note also, that with the additional7 G, k$ }. z4 D1 Y. l" q$ a
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, y4 d7 T5 t% l4 L( r* d5 z! a // randomize the order in which the bugs actually run
* v- x+ K, }- V; Y; D- ]8 @ // their step rule. This has the effect of removing any
: {; k( d. l. F4 b // systematic bias in the iteration throught the heatbug
4 d4 y; W9 K( [: g3 k // list from timestep to timestep
: P+ M( s+ i0 |) e5 ^
9 }% L5 G) S' y* e // By default, all `createActionForEach' modelActions have' ?! A: V9 _' U/ Q' O" T4 n1 h
// a default order of `Sequential', which means that the+ q8 {, D4 ~7 v% T& y
// order of iteration through the `heatbugList' will be
7 B0 N8 |$ l- Z0 m6 l# t8 c3 Y$ @ // identical (assuming the list order is not changed
7 ~+ P* O* N1 N6 L // indirectly by some other process).% w T& L, r/ c0 d# J7 \7 F" s
) b6 O- l" o0 }; M! _2 @, U0 o modelActions = new ActionGroupImpl (getZone ());
1 U; T& Z: y7 a: z
3 [% p& B) |& z$ M. r try {) J* [5 {2 _+ C: y$ [5 E
modelActions.createActionTo$message
* e" h- P$ T7 M; F (heat, new Selector (heat.getClass (), "stepRule", false));* I" F6 f6 H5 F* I8 f: {
} catch (Exception e) {
# B }0 f( C5 u8 l System.err.println ("Exception stepRule: " + e.getMessage ());
/ W& ~8 y+ t/ a2 t }
. y9 J! f5 P7 @, h6 v1 r9 b5 \' p& I2 p
try {" a% ?% K/ x1 p# }7 E8 P4 j- ^
Heatbug proto = (Heatbug) heatbugList.get (0);$ }9 Y3 l4 u, N' k
Selector sel = 1 H# J" H9 I$ |& H! e" z! e. e
new Selector (proto.getClass (), "heatbugStep", false);
& h+ {2 C8 N; {& m6 t actionForEach =
4 J a: E1 ?3 a9 w/ z. n) e modelActions.createFActionForEachHomogeneous$call
- L; h- @/ e7 e7 r( ` (heatbugList,: H9 r6 G Y, p, x
new FCallImpl (this, proto, sel,
& O! D. u# y2 Z5 e, y. G new FArgumentsImpl (this, sel)));( U O9 ]- s: R# ^
} catch (Exception e) {- e1 g4 y* d& I
e.printStackTrace (System.err); U6 ^. q+ {( L4 E1 W! z- d6 y
}
& I. D/ q& V; B V4 Z, a ' u2 \: n# Y' T, `! Y) s7 |
syncUpdateOrder ();
' I! H8 u9 D% g& n+ K5 Y
$ D( l9 k1 e& s) E. w i8 R1 j/ @ try {7 w: R9 G1 N0 @8 H2 i
modelActions.createActionTo$message / q- L% E; L0 e
(heat, new Selector (heat.getClass (), "updateLattice", false));# L( X; D6 K- G7 h
} catch (Exception e) {& ]1 c% h! n( w. ]3 w q
System.err.println("Exception updateLattice: " + e.getMessage ());5 r5 V$ O. N2 p# i: y ~
}
) F5 c/ Q. a# W5 k6 O 2 }8 T! O3 [7 R/ t
// Then we create a schedule that executes the
3 u. j! {! q$ c( R1 u // modelActions. modelActions is an ActionGroup, by itself it
N3 s! l ~; D0 F/ U& T7 [ // has no notion of time. In order to have it executed in$ p0 v& v! e/ B3 @5 m. ?3 v; Z
// time, we create a Schedule that says to use the! @# e: p5 x" t$ r! T
// modelActions ActionGroup at particular times. This
0 Z" F3 `9 g1 s: C+ y* `- C // schedule has a repeat interval of 1, it will loop every4 X: w" f) g+ o" Q0 B
// time step. The action is executed at time 0 relative to. ~- B0 `9 l9 B1 j% E. Q) h
// the beginning of the loop.
7 v7 r0 X; d1 Z5 F# u2 r o! o1 Z- T# r2 ?' u
// This is a simple schedule, with only one action that is- t, l# {& V5 q3 u' h3 n" u, B7 m
// just repeated every time. See jmousetrap for more" ~8 t( M* S% M( W/ C
// complicated schedules. ^- ?% Z; u# Y6 n# V; S0 G% c$ f
" A/ {# M9 z3 a. R5 p! A
modelSchedule = new ScheduleImpl (getZone (), 1);
I5 [5 l/ Q7 }8 h modelSchedule.at$createAction (0, modelActions);
% `* I) Z" U( e5 @ - L6 u; A$ c) A- s+ A3 d2 a" P! R
return this;+ c. s$ i( |5 Y' ^
} |