HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- H i/ B# f% E, ^2 ?. p1 Z( c
9 r9 [9 A: [. ]. K4 N W1 N public Object buildActions () {
, g3 H* }: h P+ j- @: C, w9 y: Q super.buildActions(); g4 m1 R5 p$ ~9 D$ t& ], g' {( C C3 v
w b, d, O+ f3 e // Create the list of simulation actions. We put these in
7 L' {# g; H8 B3 y2 _ // an action group, because we want these actions to be0 C6 q% w0 Z3 H/ x- u* \
// executed in a specific order, but these steps should
! E4 n+ h6 E( q5 f // take no (simulated) time. The M(foo) means "The message6 v! {/ W5 f3 s0 v# V7 Q4 E# Z
// called <foo>". You can send a message To a particular! x, [; ~/ p3 n# c" h5 e6 G2 P
// object, or ForEach object in a collection.
# ~3 _' R4 j3 _
1 ?" F2 l( s& |: m+ X Y& j0 I" x // Note we update the heatspace in two phases: first run6 `! s4 z9 a; Q7 H+ _1 b
// diffusion, then run "updateWorld" to actually enact the
5 c" [" d: ]- |% F5 g: Q // changes the heatbugs have made. The ordering here is
: t8 P3 n% k' q. j1 Q7 Q // significant!
. X$ ?' F9 o* z9 H
* z/ q `: K+ p4 S* g9 K/ h4 ] // Note also, that with the additional2 V- D/ y% \8 B( T( ^% `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 r. E: C! R$ U# J1 B# m3 Y // randomize the order in which the bugs actually run6 S/ A8 _$ g* B S* I
// their step rule. This has the effect of removing any
' w3 R, | O+ y: W" ~% M3 z // systematic bias in the iteration throught the heatbug4 l/ P: ]# ^2 R$ u( u
// list from timestep to timestep6 w/ `* T4 e% s; v! E1 ~" p V6 \
& |! |& t- p4 r
// By default, all `createActionForEach' modelActions have
) l1 U/ Y8 P3 R% p2 J2 `5 |1 D% v // a default order of `Sequential', which means that the4 y: p- i( u3 y& R
// order of iteration through the `heatbugList' will be
" k. X1 P. D* a9 w8 E: f2 _ // identical (assuming the list order is not changed
9 s# Z$ f7 T" ^$ v( s // indirectly by some other process).
( O3 \# W4 v }+ R- B3 ? : r0 @6 R9 d% d+ K
modelActions = new ActionGroupImpl (getZone ());$ n. `. X, y' k( |% R4 i
$ [; P! Q" h2 ?$ i# G: Y try {1 ?/ H% t+ n( q P7 o( Z
modelActions.createActionTo$message8 Q; @2 _3 J1 B4 d
(heat, new Selector (heat.getClass (), "stepRule", false));. `/ l# O- P o, W- h
} catch (Exception e) {; ]9 K" \$ |4 r' {3 i( d9 p
System.err.println ("Exception stepRule: " + e.getMessage ());4 S' \7 @7 T9 `! X& D* e) O+ T+ Q
}% m) Q4 @, d5 E& f7 v! u+ K/ E
! k5 @, i" L+ d try {
~5 D0 M3 [& K: b. p Heatbug proto = (Heatbug) heatbugList.get (0);8 P/ n8 w! M/ _" b
Selector sel =
7 g. O2 H- t& P new Selector (proto.getClass (), "heatbugStep", false);
5 |' T3 G5 U- u" E3 n; E actionForEach =
{4 s9 \2 |8 s" n% _ modelActions.createFActionForEachHomogeneous$call
8 B2 u0 f1 j2 b* O; P3 h! g5 E+ _ (heatbugList,8 C' z8 ^: Y0 M$ e1 M2 ]0 f5 t
new FCallImpl (this, proto, sel,
3 y7 l; V- _4 ^4 Z& ^1 T. P7 Y new FArgumentsImpl (this, sel)));5 I8 A1 y( {% h0 s; {1 y# c
} catch (Exception e) {
% ^1 M; `. G: d6 g" B9 u e.printStackTrace (System.err);$ n' Y$ i3 o! P- K
}
' X* E/ \0 h5 h. [5 A) V6 b G7 ?. M/ @) H: S- q& }# B6 H' B
syncUpdateOrder ();/ n: A# N2 F3 a9 p4 ?
8 u" G) c7 u: q3 y: \) M. [, ~; F2 ?
try {9 y. `. g6 ]3 `% x" ^, Q
modelActions.createActionTo$message
% S; K- G. S/ ~- V- G) [ (heat, new Selector (heat.getClass (), "updateLattice", false));
% L& a6 w' q/ G& J/ \ } catch (Exception e) {3 k) b; W$ n# w* n0 k3 W4 u7 N8 |4 a
System.err.println("Exception updateLattice: " + e.getMessage ());
3 g, N, H J+ P* X. t& x3 } }
' p: H0 l; P! P% X
/ h `; ]1 L7 M: d // Then we create a schedule that executes the
1 ]- z2 t3 \3 E2 A0 @ // modelActions. modelActions is an ActionGroup, by itself it5 ?4 Y, a3 E6 a; }" H2 v: h1 l6 X
// has no notion of time. In order to have it executed in1 }: `1 s% n) V# A+ J
// time, we create a Schedule that says to use the
' Y$ _ {4 B, w; T& G- n3 T/ N$ N0 P // modelActions ActionGroup at particular times. This
" W/ j4 c& _: H2 Q5 k // schedule has a repeat interval of 1, it will loop every) N! Y; k U# Z! N+ N. x% n
// time step. The action is executed at time 0 relative to3 t% w4 t, V6 i: T# Y# T0 A
// the beginning of the loop.
0 w& N# R8 h1 G- u- `( @% u5 H1 ?; r" i
// This is a simple schedule, with only one action that is
; ~2 l$ @% B5 b/ o: M6 U // just repeated every time. See jmousetrap for more/ e6 \9 s' x) K% n8 ~: q
// complicated schedules.
& I4 ]; W* F% u" w, j+ U : w( F2 F- n7 ~ k8 |/ g. `
modelSchedule = new ScheduleImpl (getZone (), 1);
, ~0 n) ^4 s9 H+ v modelSchedule.at$createAction (0, modelActions);9 e; e9 k8 w" K& g$ S. f% }( t
" s, }, g% b& t
return this; J) h) V7 q- j( d4 `- X
} |