HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 q/ t% V6 ]/ I* b! O- q
5 G7 f, i: C) x+ l% L/ R% h public Object buildActions () { _: G5 q* `0 g) E4 H/ o
super.buildActions();" d0 W; m4 F& S
+ k, I1 X% `4 g2 ]6 P- g
// Create the list of simulation actions. We put these in3 I. A3 W! c$ Q. X0 o. U* T: c% d9 Y
// an action group, because we want these actions to be( H: q+ u6 O4 q2 a* Y' _: y2 a8 T: H% ^$ P
// executed in a specific order, but these steps should
. f" }% M9 |. s // take no (simulated) time. The M(foo) means "The message2 b; N: ]# N( H! T2 @
// called <foo>". You can send a message To a particular
, c- E# C# k6 _0 l. \. J // object, or ForEach object in a collection.7 {5 H/ B! S" y, O/ r7 y6 y
1 Y' P& f8 o) q# F
// Note we update the heatspace in two phases: first run
9 ?. J! Z" A& |( B5 L, s // diffusion, then run "updateWorld" to actually enact the w! \, T: r! n! h
// changes the heatbugs have made. The ordering here is; a/ n3 H0 r1 L
// significant!5 c$ u4 x. m* @3 m
* L- S; e0 p' I9 J8 q! r
// Note also, that with the additional5 M6 k* z, Y$ R$ _2 c6 g' e
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ E' C0 M W7 w+ Y // randomize the order in which the bugs actually run
0 n5 a8 v" S4 g // their step rule. This has the effect of removing any% `. ?7 F) j! s4 `- _( ?6 C A
// systematic bias in the iteration throught the heatbug
5 q" ~9 a- r" h9 y8 ^& O3 N1 E // list from timestep to timestep
( B$ q: X& h6 E$ ?5 a8 n, \ 2 n: m" j4 p# v! Q9 F N# K
// By default, all `createActionForEach' modelActions have
6 K3 ?/ R# E# o& ` ^ // a default order of `Sequential', which means that the
) K# ^1 V" G) o- Y0 `3 }5 d // order of iteration through the `heatbugList' will be
2 c2 o5 [* v k. L // identical (assuming the list order is not changed
& H: B* D6 O! b, e5 O // indirectly by some other process).
1 G7 j5 y) t) w( R- a ) U( B( u0 `8 q, @
modelActions = new ActionGroupImpl (getZone ());
( ?& ^' R9 }3 T# _; B" }
3 b5 ?* K* y+ T+ k: N7 M+ ]( { try {
X/ w) }# Y: k: J( i modelActions.createActionTo$message
* S! v6 G) B3 F, C (heat, new Selector (heat.getClass (), "stepRule", false));' l+ V, T! j" t3 X
} catch (Exception e) {
' l2 M, p( r1 e+ {/ I$ I System.err.println ("Exception stepRule: " + e.getMessage ());
/ G5 \& L( y1 z5 J) O, G, | }
" G0 W5 q& L; P t$ U0 t& l& g" p' ?) p4 O
try {! N' p' s- _5 r5 H2 `+ K
Heatbug proto = (Heatbug) heatbugList.get (0);
; p/ m$ H! W) F2 k) C Selector sel =
) }% h( J/ y' ]1 ~ new Selector (proto.getClass (), "heatbugStep", false);3 }# }$ Z* O7 f" U8 z
actionForEach =; f3 O$ O* g# @* K8 w0 s+ s1 g
modelActions.createFActionForEachHomogeneous$call
4 e+ h' Y, e) d8 _" _; V3 e (heatbugList,
/ Z: s/ Z+ C" a) S7 J( L" W new FCallImpl (this, proto, sel,
- L; ~# X4 O3 W* @4 X new FArgumentsImpl (this, sel)));
6 }* U/ a. R2 ^, n8 e8 w: T } catch (Exception e) {8 O3 i( O3 v ]4 _
e.printStackTrace (System.err);) N- o. M0 j' d0 M) m( G
}$ U( k% P5 F1 g% c
, F( B! P8 B9 y! A: e7 F& N( B syncUpdateOrder ();
* E- z, c7 o. i5 [
2 B. f/ l5 [5 Y( d2 j try {
: }0 S( v2 i; i modelActions.createActionTo$message
8 s+ E. b e7 C1 w9 }4 \ (heat, new Selector (heat.getClass (), "updateLattice", false));
2 s8 Q0 [& X' t+ m. K0 P: a, C8 G } catch (Exception e) {# v! {5 d y! S8 r
System.err.println("Exception updateLattice: " + e.getMessage ());
2 \" ?$ n I6 F1 X" s }- u1 Y1 p+ `0 C5 a7 W
; ?8 B) O: D$ y' a' Z
// Then we create a schedule that executes the& h* X' V( `% B
// modelActions. modelActions is an ActionGroup, by itself it0 w; ~8 K ^ E$ C) l
// has no notion of time. In order to have it executed in
- R [/ {' E* k$ n1 p, c, I; j // time, we create a Schedule that says to use the5 `# r3 m# @; h( x1 c) m( e" ~, w, ~
// modelActions ActionGroup at particular times. This a; _5 F. `- T5 }) T$ @
// schedule has a repeat interval of 1, it will loop every3 w" z5 f; W! V1 ?% @7 o- S4 d
// time step. The action is executed at time 0 relative to
" Z! g4 A. y" w$ }2 W; J8 Q# O // the beginning of the loop.
+ }: T) }* h$ y0 I- ?3 [
+ k7 I! p+ Z: G* M // This is a simple schedule, with only one action that is7 i+ A K8 w6 @! w9 L4 p5 K- Q
// just repeated every time. See jmousetrap for more
: F% ]; B8 ~: M( g // complicated schedules.
! }$ Z# [# [/ E2 o3 R. J! L" [& t# M 2 B7 R% K' r% I) L
modelSchedule = new ScheduleImpl (getZone (), 1);
5 V) H% o4 R- _$ N modelSchedule.at$createAction (0, modelActions);1 i$ h" y9 O1 o$ }" S6 i
: A: q0 R9 a( l) H9 O
return this;
X- ?+ ^! z3 _+ X6 @ } |