HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ V8 k' j2 p0 D
) Y4 p/ y+ k& z0 g" p! B, X
public Object buildActions () {: j: `: @5 M9 }' c& C9 Q, r: |
super.buildActions();" o! c3 C" Y8 |9 S5 t c
& E# L# W$ |8 @4 {, J
// Create the list of simulation actions. We put these in
% s5 t9 ^. K* ?9 n+ R; m5 G/ U // an action group, because we want these actions to be1 N. f( C! B& i/ V* Q
// executed in a specific order, but these steps should1 J- T& B, N' B3 V- n# k6 o0 \
// take no (simulated) time. The M(foo) means "The message
% ?$ p8 `* {7 p/ c; v" i# J6 u% F // called <foo>". You can send a message To a particular& O( f/ w; n' q4 o. R' M6 e: a' J
// object, or ForEach object in a collection.
\0 ?. b1 G* ^! k% |& `/ d+ v
* ]+ q/ ]. \7 q3 s; {& r9 s0 l6 b // Note we update the heatspace in two phases: first run
H0 g0 G1 c3 \ b. F7 I // diffusion, then run "updateWorld" to actually enact the/ j; @3 d: o! R* k1 `
// changes the heatbugs have made. The ordering here is) D, L+ S1 r9 |8 r
// significant!
+ E' D) K6 C6 s! {+ f + b' @: I: ~% j) i
// Note also, that with the additional, \9 y, H1 }! N
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 y! H% v3 P6 o // randomize the order in which the bugs actually run
' F% f Q; _5 s // their step rule. This has the effect of removing any3 _8 r: ^+ |; v; O
// systematic bias in the iteration throught the heatbug2 E% `5 d! s& P8 L- Y
// list from timestep to timestep' K9 f; t, w0 U9 _/ r0 P6 K8 N
* p* L- k8 G/ @/ V9 y3 o/ m // By default, all `createActionForEach' modelActions have
1 y/ A1 u. \% n( Q& Y& `' N // a default order of `Sequential', which means that the
! _' X* F. }1 `2 X0 F // order of iteration through the `heatbugList' will be
& B1 A- M+ l. l3 ^% V9 x // identical (assuming the list order is not changed+ A/ j3 y( l* r; s. N5 W; f" O% I
// indirectly by some other process). {5 S4 V( v2 O7 }7 M
: u, s E9 f; r0 m+ k9 C# n0 B
modelActions = new ActionGroupImpl (getZone ());
; y; s' ?+ m* ^7 r( K1 ~1 X% M2 K: o. q, R
try {8 a/ D' w) _7 f7 s9 F& i
modelActions.createActionTo$message
$ E. \* B# W( H, v! B8 C6 W (heat, new Selector (heat.getClass (), "stepRule", false));
" x5 H( `. V4 F2 C1 e* n } catch (Exception e) {
# ^9 n' c h" G) @ System.err.println ("Exception stepRule: " + e.getMessage ());% L) `$ ]+ K5 ?( p
}6 Y( f0 S3 |3 C/ Z4 B( x. ^. I9 V
' C$ T" w* b: N. Z9 Y5 \
try {
4 U2 H0 ~) j& z' t, | T Heatbug proto = (Heatbug) heatbugList.get (0);0 u* Y- Z* y6 u" X0 }9 l! I
Selector sel =
* x J( I6 ? b+ N' |( O new Selector (proto.getClass (), "heatbugStep", false);
* q/ B8 }7 q: `0 z# C, L2 R actionForEach =
' `( G+ s* \. N# W# k modelActions.createFActionForEachHomogeneous$call4 p* |1 n& d$ m/ P9 O/ w
(heatbugList,
" ~8 |4 T2 r5 p0 |9 @/ u# l$ ~, x new FCallImpl (this, proto, sel,4 j0 e$ v) I5 B; n: M# V/ y
new FArgumentsImpl (this, sel)));3 M; D. F6 y: l z% C8 G& h
} catch (Exception e) { P) E2 k0 u6 I. \9 H9 p
e.printStackTrace (System.err);
! D- ]) i0 x: ?$ v% d# ?6 L6 k }7 S2 v0 w" H) a6 g) E! Q
/ v5 P. _8 g9 K) m# g/ g, o" v6 r syncUpdateOrder ();
! g) o8 u% ~* v' q. k- I- R" E0 m1 ]/ f T2 x0 W
try {) u) d* o6 N0 ~0 u# Z
modelActions.createActionTo$message
! s* b5 y" @. f3 ^) {$ `# U (heat, new Selector (heat.getClass (), "updateLattice", false));
4 c, L' W% Q% i% B) W: V% h, Q } catch (Exception e) {
3 b2 O# P0 G9 J, J7 T4 ?! W4 c5 b System.err.println("Exception updateLattice: " + e.getMessage ());0 k, }$ ?" d" _, Y" C
}
& G% n% t' ]0 R' Q2 n/ }( ] 4 M- V! `8 m# X" q
// Then we create a schedule that executes the
. n- L, T) W; q# t1 f0 _" x // modelActions. modelActions is an ActionGroup, by itself it
, V( I5 O# L* w( `) ~3 P0 F // has no notion of time. In order to have it executed in2 W% D$ e- O/ M6 }: ]1 Z
// time, we create a Schedule that says to use the: K* j9 T; X: o1 F0 U! J- U
// modelActions ActionGroup at particular times. This, ]7 j0 ?, R: L) k3 M* u% A+ r$ B1 P# U
// schedule has a repeat interval of 1, it will loop every0 J1 @( y; y1 ~
// time step. The action is executed at time 0 relative to
3 u& y! I/ G- x4 j9 A. B9 r // the beginning of the loop.
# x# ^' T/ _" W4 Y
9 n0 z% k. P, p( ~9 S // This is a simple schedule, with only one action that is4 Q& L6 P1 X8 i+ o" x2 W! o4 F4 h
// just repeated every time. See jmousetrap for more6 E7 }9 M% `' b! Q
// complicated schedules.! Y% f3 i; V0 f6 m
' ^3 q; s& G( k1 i ^8 t modelSchedule = new ScheduleImpl (getZone (), 1); t: ]5 N/ o/ R+ f/ ^% q
modelSchedule.at$createAction (0, modelActions);2 Z! F0 C- _/ j1 a
# h. \* A% C4 w! B& E
return this;4 j# a: T& x% ^2 u
} |