HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% a3 C3 q1 A. ?" b/ s( g
5 c$ b/ b: U5 h& f public Object buildActions () {
* s6 l/ H% h# D) q. u2 N3 @/ m super.buildActions();$ S" X8 B4 q. S) Z5 _7 t, K
+ j* l% g; ?$ A' [: q
// Create the list of simulation actions. We put these in
% u0 Y @) F3 Z9 T, D( p // an action group, because we want these actions to be
) X; s0 B& c% K // executed in a specific order, but these steps should
# e& b- l0 W. y( @ C+ u ]5 A // take no (simulated) time. The M(foo) means "The message% b5 D. w: k8 ]( E l) D/ V3 Q4 J
// called <foo>". You can send a message To a particular
4 Y. ~' a: H5 \7 _0 I$ c // object, or ForEach object in a collection.
; J- c/ r- u' {. x+ Z
$ w; i& w F: G, N // Note we update the heatspace in two phases: first run
B1 E+ E O. k! Q# k; @3 ~ // diffusion, then run "updateWorld" to actually enact the6 ~+ F6 I/ f- R9 H) e; G( j8 k
// changes the heatbugs have made. The ordering here is3 u+ {. A8 B& _7 g! `$ c- c
// significant!# }$ t) G5 r& M/ _0 r" U
/ L# a4 z( m/ v4 K( b, t; z // Note also, that with the additional# `4 C: V: m6 z0 U4 @
// `randomizeHeatbugUpdateOrder' Boolean flag we can! Z) E5 V' n9 m" n; M
// randomize the order in which the bugs actually run
+ q1 N J' w. F! ^ // their step rule. This has the effect of removing any
; k! q+ T9 r! f7 W // systematic bias in the iteration throught the heatbug
1 e5 O& P4 I# n3 I // list from timestep to timestep
2 ~2 L8 S7 h4 [; ]5 ~
& C8 ]8 w: F ~9 O: ?5 b- b // By default, all `createActionForEach' modelActions have2 P, R- v+ t" G: ^, M6 H d
// a default order of `Sequential', which means that the8 |3 b$ |: o5 m, I
// order of iteration through the `heatbugList' will be/ d% H4 s& I+ a
// identical (assuming the list order is not changed0 J9 `7 e M, I; P! {
// indirectly by some other process).
; }3 H1 i! U! L/ M
+ F' `: X& P ?3 O; w# T5 e0 X modelActions = new ActionGroupImpl (getZone ());
* A7 Y. c6 d3 _/ C7 C2 T, l+ y- D* w
try {% i4 T; \0 ]- v1 }& b% h9 D) f) u
modelActions.createActionTo$message
4 V4 R" ^& D1 y! Q( ~, `, A1 K2 N2 k (heat, new Selector (heat.getClass (), "stepRule", false));8 d% Y8 X9 Y4 O3 [) y l" n% Y
} catch (Exception e) {) k0 O( y8 \ ]; O
System.err.println ("Exception stepRule: " + e.getMessage ());
7 ~' ]. j# q% K. M; W4 ]- h8 s9 \ }
; j5 \* D/ \0 @$ v) V& q# Y" J, P
2 d& S* ^7 z \ try {) q/ {! s. w+ g8 Z
Heatbug proto = (Heatbug) heatbugList.get (0);
: O. o; Z& u; B6 ^2 }, s Selector sel =
]. S! J$ K$ D' Z* }7 j# Q new Selector (proto.getClass (), "heatbugStep", false);
( n2 z% T5 _* M, x: E1 B actionForEach =
8 L0 p( Q: Z2 Q0 M4 ? modelActions.createFActionForEachHomogeneous$call# l6 V2 C+ Z. d6 M8 F6 Y. {, |, I
(heatbugList,
- C5 P7 X! E/ m1 j new FCallImpl (this, proto, sel,
# i% ?. T; P. q y9 b' B new FArgumentsImpl (this, sel)));
, \, J9 y6 S: L0 W3 u } catch (Exception e) {
! I3 K V( I B% F4 \- @7 K e.printStackTrace (System.err);5 h& U) _/ q4 e, s. l; C
}
1 G9 C e+ S) z, W5 x, h0 l % z7 [1 E! ?* K$ B! g8 k
syncUpdateOrder ();) c; P0 m: y2 e9 C$ f' l) `
: D6 M* n3 \1 w, ?6 q0 f try {* u9 S% m7 D, Q5 o! \8 z& r* `: S
modelActions.createActionTo$message
7 J% m0 q1 F9 e: K( x/ q* k1 J. J (heat, new Selector (heat.getClass (), "updateLattice", false));
, K4 s, \ C, g1 H: i } catch (Exception e) {5 H4 ~- u: O2 p% }1 x; z- x/ M
System.err.println("Exception updateLattice: " + e.getMessage ());
& e4 _0 M; ^$ }1 [6 A6 j% A$ i }/ `; @! p( O9 M1 H0 U
/ b0 m4 H+ }0 O5 v/ {8 a
// Then we create a schedule that executes the
; @6 M( C2 I8 ]/ o // modelActions. modelActions is an ActionGroup, by itself it
( ] Z' a7 o. w* E // has no notion of time. In order to have it executed in
6 u% k( t1 o) g9 ` // time, we create a Schedule that says to use the2 ^" A4 ?4 A2 A0 m1 a
// modelActions ActionGroup at particular times. This* x7 e* s( t7 X$ u# [% r4 T
// schedule has a repeat interval of 1, it will loop every: O( d* n2 E* J) V# z3 N* y
// time step. The action is executed at time 0 relative to
! v- B3 e5 N# {. X // the beginning of the loop.2 E7 G7 V% r" M& L3 ^/ s( b
K$ h; V$ M3 q7 H+ B2 g
// This is a simple schedule, with only one action that is6 m9 R5 x( B4 w/ K% i7 }+ ^
// just repeated every time. See jmousetrap for more8 A0 S! w, X! `2 b8 O: z0 _
// complicated schedules.: ~8 [+ }. n i
& H! r H9 n8 ^5 |+ k8 C) ` modelSchedule = new ScheduleImpl (getZone (), 1);
, K" G. G( y5 p3 J0 V& Z+ [ modelSchedule.at$createAction (0, modelActions);+ `! k7 R& ~1 `# c3 ~- x
, P5 t5 Q; f, `2 e9 I; {3 e) f
return this;& o! m& [5 H. y* I
} |