HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 Q+ d E6 _' a; f
' A5 n7 y0 k: \, K public Object buildActions () {
% Y' X$ k7 \: [1 g* ` super.buildActions();4 x# ^6 h: j- p& s
. j" R' @% {% | // Create the list of simulation actions. We put these in. k, o0 } n: e
// an action group, because we want these actions to be7 r1 q& E& d% I/ K Y
// executed in a specific order, but these steps should- z+ r8 Q0 \# b; c0 }, q6 f/ t+ h y
// take no (simulated) time. The M(foo) means "The message
) l+ Z0 y i$ I // called <foo>". You can send a message To a particular
" f- v4 S* F; }- A6 w: p7 o+ U // object, or ForEach object in a collection." k. [( C6 a3 u3 |8 M* f5 Y7 F' R
# B7 _* y7 J9 N/ t6 r' b J // Note we update the heatspace in two phases: first run
' Z2 c% H$ ]9 B# N4 F) o // diffusion, then run "updateWorld" to actually enact the4 G# z H* ^/ M C! ^6 d: _) w6 v
// changes the heatbugs have made. The ordering here is
; m9 | z4 T+ l& \* P, Y // significant!
( y8 t; o0 T/ D% M" b7 Q# t0 @8 p
: f! P0 ]% ?1 P& z8 O* { // Note also, that with the additional
: r1 |9 U7 \( h4 T( K- b // `randomizeHeatbugUpdateOrder' Boolean flag we can" v& z# p- p$ z. ~) g$ E
// randomize the order in which the bugs actually run& z1 g1 h; ]4 M; a3 ?5 r
// their step rule. This has the effect of removing any. G) @+ \# ?% h! [
// systematic bias in the iteration throught the heatbug# E' V% L. u$ A2 v7 w
// list from timestep to timestep- L4 [1 I2 K+ L1 b4 P
" N8 }8 o! h) e' G" I" D) V, @2 J$ s4 O
// By default, all `createActionForEach' modelActions have
$ Q2 d C. b+ ?" \, j! { o) f# b, v // a default order of `Sequential', which means that the
9 ]( j0 A; l7 r- d // order of iteration through the `heatbugList' will be, c6 `: n+ ^$ e" z2 ^
// identical (assuming the list order is not changed
& Y" G. P! y. x E // indirectly by some other process).1 i$ v; q9 r( E# j& V# Y3 R3 R1 v
- b$ O- h" e9 G- F- L' B modelActions = new ActionGroupImpl (getZone ());; L/ ]' l" d% F% D8 W* _- [
- s9 A6 y6 q+ {3 ]
try {
5 ~, L/ p7 n" [5 Z, q' } modelActions.createActionTo$message
2 A6 ~/ T5 e7 |* |+ o (heat, new Selector (heat.getClass (), "stepRule", false));
# n" B% }. E4 B; u* G& k- ] } catch (Exception e) { ?. v+ L) G: @. z
System.err.println ("Exception stepRule: " + e.getMessage ());
: J; F6 H4 N' C a- s$ e }
! b9 F1 g6 ?7 I# J1 I1 `
5 @7 e0 h9 Q7 D) B7 q try {# h$ y& Z1 R, P5 S( \' s
Heatbug proto = (Heatbug) heatbugList.get (0);! d8 ^! |8 R% `( i# s
Selector sel = 0 v* M! _0 l/ f, {; B D
new Selector (proto.getClass (), "heatbugStep", false);
" t% y, L: H4 S actionForEach =8 r3 j$ F# t6 O) b1 Z
modelActions.createFActionForEachHomogeneous$call; T3 M- I E- v+ }; i5 |
(heatbugList,' s3 A4 i% k. v" F9 }9 \
new FCallImpl (this, proto, sel,! _: \, A$ s3 d
new FArgumentsImpl (this, sel)));) j- l9 A1 B( m, r* R2 B
} catch (Exception e) {
! z- X, O0 l& r2 h, `0 x e.printStackTrace (System.err);
- ?4 ?3 b) t4 [" O8 a }& H3 G8 g, H. o5 v6 V1 y' C; V% g
8 A* u3 p" I3 X7 {2 V
syncUpdateOrder ();
, ~9 V5 g: B& x% L* Z2 [( d$ o# ], p4 o, W8 M) Y2 v* U! L7 x" m
try {0 V K, L& l' H6 r1 U' n! R# R
modelActions.createActionTo$message
: R" K0 Q6 j3 Z O* X: _ (heat, new Selector (heat.getClass (), "updateLattice", false));4 w: c, ~# W8 S" l2 {: ?! X" j0 p0 [
} catch (Exception e) {
+ O+ Z4 h9 ?6 C. C System.err.println("Exception updateLattice: " + e.getMessage ());+ J% o7 z& |) Q2 C! \: y
}1 M [8 _3 a1 {) I- T7 u
9 ?1 L2 L1 S2 m
// Then we create a schedule that executes the
3 z+ o6 y ^! c+ n // modelActions. modelActions is an ActionGroup, by itself it
& ?2 x; W; B D$ v0 h' D8 J8 B // has no notion of time. In order to have it executed in
' g& e* Y1 s1 o" Z5 m- e // time, we create a Schedule that says to use the
2 T& `% }; ]; l/ y. b0 J; H // modelActions ActionGroup at particular times. This! w" p" L' I" d$ e/ W5 Z+ y
// schedule has a repeat interval of 1, it will loop every
% R. P* N, V9 r7 ^4 I( z* v: y1 N // time step. The action is executed at time 0 relative to
6 ]6 ^* {) u# n |3 Z0 W Y // the beginning of the loop.
; Q: p. K. M" [ K
7 m: L8 a+ C) \- G8 S2 I // This is a simple schedule, with only one action that is
/ l! B/ a! F- b6 L3 h4 ? // just repeated every time. See jmousetrap for more
9 r7 ~! t3 Q3 p( f3 S( q2 N* j* E* ` // complicated schedules.
7 ^# z0 I4 S* u/ _6 T3 e$ c , L1 S/ g o# ~, F) ^0 y3 `8 B
modelSchedule = new ScheduleImpl (getZone (), 1);
8 e5 |0 a2 ] ]! d% x modelSchedule.at$createAction (0, modelActions);
9 Q4 C6 f/ d3 L9 w6 T: Y 5 A% v; c$ q6 R W6 R# o
return this;
/ X) J5 L2 k0 ~! h Z& N: x6 p* M2 g* d } |