HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' i+ r! Z3 E" }7 B9 u
$ T) i7 @, l3 J0 o2 e! B7 Q public Object buildActions () {
8 _' ~. a' G% r* h- P6 B super.buildActions();' h" r9 b4 \" e: m# \# m
3 ~- j L$ n- L. A, J // Create the list of simulation actions. We put these in9 _+ ~& j4 Q+ s: B( x& W: a. D/ a
// an action group, because we want these actions to be3 E( \% U9 ^, W* o* w
// executed in a specific order, but these steps should
0 u- E* a: I/ d1 C4 m! u // take no (simulated) time. The M(foo) means "The message- B& \8 O; f" e- v
// called <foo>". You can send a message To a particular
7 K4 m& Z1 C$ I* X: D // object, or ForEach object in a collection.- Y5 L$ n5 B& l8 [; c/ c
! F" @9 O4 y# M5 F1 \
// Note we update the heatspace in two phases: first run. V, W7 r G+ x5 S
// diffusion, then run "updateWorld" to actually enact the9 y, w, f2 d! D8 ?; v( w
// changes the heatbugs have made. The ordering here is
9 C& y/ @$ T s" V // significant!
. S( ?5 N- o' S' ^
! a9 A" W0 w, C% O6 s8 a0 H. ~1 B // Note also, that with the additional: x9 \" A! ~1 b9 h8 [2 A7 A6 E
// `randomizeHeatbugUpdateOrder' Boolean flag we can# \9 [( n0 ]7 q5 ]$ W, K8 d
// randomize the order in which the bugs actually run
1 ^+ C/ Q4 W) ~/ P# { // their step rule. This has the effect of removing any- ]' U; W% ^3 j) q# z F! S
// systematic bias in the iteration throught the heatbug% D% l+ C1 x3 a4 [5 }) g: E
// list from timestep to timestep! Z7 }4 n* Y/ f! E1 O
8 J; G) X1 ]1 _+ s' K' Y8 k5 ^! {& x5 b // By default, all `createActionForEach' modelActions have l( ]2 i; ~. T- t$ }
// a default order of `Sequential', which means that the
: O% X* M ^5 r. j // order of iteration through the `heatbugList' will be
5 e* k- N6 Y0 V // identical (assuming the list order is not changed$ B; C: w% E% x M- g# q& W
// indirectly by some other process).
]/ o( T0 C# A- i9 m * b7 G; f6 L& M% b1 x- }9 r( _
modelActions = new ActionGroupImpl (getZone ());
# ]9 y+ ?1 }) x# A& T' t3 G+ Q, k# p* {) d0 |' h* _. m
try {
/ o! h. q7 F5 w) ^ modelActions.createActionTo$message
4 P8 _9 H' A+ d- h (heat, new Selector (heat.getClass (), "stepRule", false));) `# X1 t- E# v( N6 p
} catch (Exception e) {6 j# `( k) l# Q3 u2 u
System.err.println ("Exception stepRule: " + e.getMessage ());
$ t+ n7 c! T: A& z+ X8 o }3 u7 r, X4 U; ^: D( O
3 n1 Z7 Y" t0 ]! G6 y3 U
try {
4 x$ j8 B2 R% O7 B Heatbug proto = (Heatbug) heatbugList.get (0);. p' P2 Y$ l4 f2 ?
Selector sel = 9 S0 A8 }: i# {% u% B
new Selector (proto.getClass (), "heatbugStep", false);) m3 Q% t \0 r
actionForEach =
, S, `, f! i: H modelActions.createFActionForEachHomogeneous$call8 ]' |4 X* e+ u" S9 z3 B" H( \ H: m
(heatbugList," q) v! m; o% P6 c; O
new FCallImpl (this, proto, sel,
! E1 V# g- G8 e e$ D! S5 t# l' P0 P new FArgumentsImpl (this, sel)));
9 R3 ?2 Z% |5 \: H3 K( C } catch (Exception e) {
1 \" p" J# r p3 U e.printStackTrace (System.err);: P2 k5 \1 j2 n* B3 R" y" u" B/ P7 v
}/ m+ c# e# V! N1 J! w! e
4 n R" @+ ]& K4 I. F syncUpdateOrder ();
- @! C4 w9 n2 c& } V
/ w* \+ ?% b+ e8 Y2 F9 W9 q0 w try {
3 ]) X! ^' j, _8 m4 }3 n3 }# p modelActions.createActionTo$message
5 S1 N" x( g) i (heat, new Selector (heat.getClass (), "updateLattice", false));6 x. S; q$ L9 o- ~& I" q% N
} catch (Exception e) {
6 P' S! E1 A5 ^ System.err.println("Exception updateLattice: " + e.getMessage ());/ q W2 b- x/ @) F: ]/ @
}# C: Z4 @, ]' n4 s; H! I
% U" k* i% K+ |' _- U9 x" _: l // Then we create a schedule that executes the* Z# m" I8 D; ^% k3 i
// modelActions. modelActions is an ActionGroup, by itself it
8 m" w* R6 ]6 l7 ^2 h // has no notion of time. In order to have it executed in
/ S) p& b8 H" J3 i% |/ [ // time, we create a Schedule that says to use the
2 i$ j% X( O. O) }% X // modelActions ActionGroup at particular times. This5 |& x6 I0 r, E/ i$ a
// schedule has a repeat interval of 1, it will loop every; C7 Q! }# o# N; [( [0 A1 D2 N1 L
// time step. The action is executed at time 0 relative to" J o# x' d- q: A% _4 C$ N
// the beginning of the loop.
/ a, ~" x& ~5 J, v
2 e `# ?* z; |) R& t // This is a simple schedule, with only one action that is
' L& p1 i' Q$ n& I4 w# p // just repeated every time. See jmousetrap for more8 X1 C+ I" ~0 i( ]- C/ D: ]
// complicated schedules.8 z. k Y0 p* N4 i
3 k2 e0 {3 P0 M modelSchedule = new ScheduleImpl (getZone (), 1);3 ~$ v0 H1 p5 H" m6 G7 f
modelSchedule.at$createAction (0, modelActions);9 q7 i1 O# J G+ j
9 p5 ]6 A$ {0 D0 e' ?
return this;7 g k3 ?( m" l, g
} |