HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% j6 k1 w4 F5 }/ h- }) X4 k1 T
! n2 c, M' ^8 D) l5 h public Object buildActions () {
6 m" k+ u& G% H super.buildActions();6 h/ f* {: ^0 C3 z
0 @. F* @) R w& F // Create the list of simulation actions. We put these in
$ k2 K5 v" m* w' r // an action group, because we want these actions to be7 m x u, G$ f' R. U8 ~2 j2 S5 @
// executed in a specific order, but these steps should
; w, U& Q5 n0 J$ z U/ A // take no (simulated) time. The M(foo) means "The message
) ?: d2 m& J( h$ \' P9 g // called <foo>". You can send a message To a particular
9 d( ]0 }% W8 Q- C: U2 U // object, or ForEach object in a collection.% n- F" z) x' M0 R' H+ g
* `. G3 _+ {7 C3 u% a0 t6 p* F
// Note we update the heatspace in two phases: first run
$ B* a3 o* c1 y- Q/ S // diffusion, then run "updateWorld" to actually enact the4 h3 w! b* B' M9 z
// changes the heatbugs have made. The ordering here is
; U \) v/ ]2 ^; e* u // significant!6 s4 E9 q$ W" U* j, K2 V+ G) a
7 y6 B2 L, f. C$ B // Note also, that with the additional
( o& @( i) l, G0 t# w // `randomizeHeatbugUpdateOrder' Boolean flag we can `% s* @. Y% i5 Z
// randomize the order in which the bugs actually run: J% X- ~+ T, P: e* T( |
// their step rule. This has the effect of removing any( J- M# u" r; @" K, `
// systematic bias in the iteration throught the heatbug
6 \ s6 G2 W/ T2 P // list from timestep to timestep( f; S* V+ J6 h4 t
' E) j' F% s1 O d
// By default, all `createActionForEach' modelActions have
& D& o' x: U( N D // a default order of `Sequential', which means that the8 U2 j4 F) e5 V0 m
// order of iteration through the `heatbugList' will be- e2 z5 A; _4 F. \+ |7 X. g4 U$ H/ ?
// identical (assuming the list order is not changed
- ^6 r" U$ o5 a8 r) [ // indirectly by some other process).* Y3 O& [2 f" c7 d
& M1 P, {3 Q- m2 v; x! @ modelActions = new ActionGroupImpl (getZone ());% s% [" J* W3 c5 U8 H5 m% D6 f
. r" j0 X- Y3 X+ x0 l6 u try {: p, @, X7 U6 v5 K- ~
modelActions.createActionTo$message
7 \- U7 A7 m; w H& S, r! d (heat, new Selector (heat.getClass (), "stepRule", false));
2 R2 t( D9 f& i" @. w } catch (Exception e) {9 `3 p% N9 {* A
System.err.println ("Exception stepRule: " + e.getMessage ());
; E6 C' K. s' P }8 g; a! h0 K7 B% z& g; K
, I4 P) k+ x) b( h8 m5 X2 j9 p# M8 a; o! v try {- {6 X' J3 ]- b0 \
Heatbug proto = (Heatbug) heatbugList.get (0);- J2 R2 {) A8 c/ j" }: J* g: h
Selector sel = / \: [; k3 y0 n9 {: o$ ~
new Selector (proto.getClass (), "heatbugStep", false);
- T) z t9 b/ F8 i' G1 n actionForEach =0 q0 \% \( ~( ^8 D( {# d- a
modelActions.createFActionForEachHomogeneous$call
# k( n9 N d$ K& U (heatbugList,& ^" P! t# J6 G. f6 G0 Z K- g
new FCallImpl (this, proto, sel,
9 \& B# _/ t1 L; J% ?9 q new FArgumentsImpl (this, sel)));/ f: D) ~+ ?3 @2 f& G% ~- u3 K! Z
} catch (Exception e) {
; k$ N# u9 q' e2 _" |8 W e.printStackTrace (System.err);
! Q! E" n2 v# x6 @7 V1 j$ u }' B; H1 Q$ L. g8 E; h$ K& r
6 b2 z% E3 A8 M: z9 w8 E syncUpdateOrder ();# X. ?! ^5 ?: V( D; ~2 R2 y3 U; J
7 I5 N# x" U8 t, ]) B C5 h/ I try {
. S( W0 C: Y y* P) C$ p" s* h: E modelActions.createActionTo$message ' t" P s w J/ e9 a
(heat, new Selector (heat.getClass (), "updateLattice", false));" n h+ c* t- R, |
} catch (Exception e) {7 a" s; H% Z- M2 {! C! b
System.err.println("Exception updateLattice: " + e.getMessage ());2 L6 m* H: k0 h! p. X5 X$ w8 r6 ^
}
! o# z# G0 d/ o9 A; K, q ~" j
) n1 W, z6 \# ?' _ // Then we create a schedule that executes the
1 O0 ?: L; a9 X" i% q" E! C // modelActions. modelActions is an ActionGroup, by itself it
, h2 X! i1 Z& b' j. b // has no notion of time. In order to have it executed in+ S! d1 P' [7 _! O
// time, we create a Schedule that says to use the
0 z0 z: H9 [# U // modelActions ActionGroup at particular times. This
3 F7 `, p* T% t7 B // schedule has a repeat interval of 1, it will loop every P4 v0 o, n6 w$ t
// time step. The action is executed at time 0 relative to- R- @2 y' D6 R% q! B" W, l
// the beginning of the loop.3 }; D& R7 X/ x; C/ f
2 u& o8 O( y; C
// This is a simple schedule, with only one action that is( C" G+ P: T" A# G) D( @
// just repeated every time. See jmousetrap for more8 ]5 f/ r; g, v1 E: p$ Z
// complicated schedules.
' G1 @8 P9 m0 w7 t4 V 3 t9 r* x8 {& }: f9 _* |3 Q
modelSchedule = new ScheduleImpl (getZone (), 1);
0 Q# D/ ~3 H! c1 D# u. I! [; _, K: Z modelSchedule.at$createAction (0, modelActions);
9 { r$ b6 W8 | K8 ?. Z
# w. B& \$ k8 V# G# J8 D return this;7 P9 {9 ~6 m n6 ?, [2 G o
} |