HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. _& ^) f1 P/ L3 \4 N( O7 J+ Y
# Q" P$ g8 z! D X* W2 U public Object buildActions () {0 g; s9 m0 {* [! w, h, z& m& Z9 k
super.buildActions();2 q x. \. a0 L5 g- [% z
% a0 r4 B" |/ L( N/ a
// Create the list of simulation actions. We put these in5 `1 Z6 Q' i; u* ?7 `9 C- s0 ?
// an action group, because we want these actions to be! I7 m+ f5 H2 [! X
// executed in a specific order, but these steps should; o$ q; R! M" f& Q8 V- i4 C
// take no (simulated) time. The M(foo) means "The message
; k1 A6 o0 G8 s, b // called <foo>". You can send a message To a particular7 s& A; O5 z6 ]; ^! `0 U& w
// object, or ForEach object in a collection." E$ }, f- n% E2 V$ n- b
6 ?2 F5 U% _& T g // Note we update the heatspace in two phases: first run a* S2 j+ B2 R4 o
// diffusion, then run "updateWorld" to actually enact the8 m1 h/ @" C! P3 ^6 ?# c, u; l
// changes the heatbugs have made. The ordering here is
) Y1 p2 a& b. x" Z3 B // significant!# n1 p+ K& A7 @: G, B
! f4 ~* ]! T* S) w" J0 [$ A3 p // Note also, that with the additional
: o3 `9 s: }$ q, q& G$ {, V // `randomizeHeatbugUpdateOrder' Boolean flag we can" i% b2 r! b2 R3 A! s
// randomize the order in which the bugs actually run
0 z) Z2 J0 C. T; g- U // their step rule. This has the effect of removing any. _, F/ t9 ?+ j9 y& B" W
// systematic bias in the iteration throught the heatbug
% Q1 C8 F* h) _! h // list from timestep to timestep
( M t8 Z( S5 X8 b) J
: C% H7 ?$ Y+ }0 R; @9 W // By default, all `createActionForEach' modelActions have
# E+ }0 c' a3 D: {) [ // a default order of `Sequential', which means that the
' A9 V8 L5 q- p: g# |) F // order of iteration through the `heatbugList' will be
& r! E6 X: }$ B; f: ]( I$ { // identical (assuming the list order is not changed4 r/ U( p* B9 b9 Q2 ~
// indirectly by some other process).2 Y8 \/ J- D0 ~! b! b
6 C( M. Y( D) I% Y% b modelActions = new ActionGroupImpl (getZone ());8 O6 ~0 x. U" W) e% A d% }! T
0 O8 R/ N# h- P- s: V
try {+ H/ f2 E( ?4 e4 r
modelActions.createActionTo$message8 M) v3 q* {( l
(heat, new Selector (heat.getClass (), "stepRule", false));) Z( c0 P, \$ ?0 \4 G+ [
} catch (Exception e) {
9 J2 I) M5 u- d& y* w System.err.println ("Exception stepRule: " + e.getMessage ());
- j# Q" g) h# r! F4 R; ]% Q( F }$ F7 C. b8 K% E* ?; ]
A& j/ K$ ^, E* W
try {3 R- q8 [2 ~) { b! D1 Q0 D
Heatbug proto = (Heatbug) heatbugList.get (0);3 i, U6 T0 E1 x6 D9 s
Selector sel = 9 |9 w9 L$ v& F4 Y
new Selector (proto.getClass (), "heatbugStep", false);
0 t: e! w9 D" B% z; J6 M" |1 b0 L actionForEach =
0 i1 @9 [8 `7 m modelActions.createFActionForEachHomogeneous$call
/ m" H8 f! `/ K# G1 ^( v y B' G( g (heatbugList,: N. G7 ^' o+ q
new FCallImpl (this, proto, sel,# U+ k5 Q b& _. R, Y4 U8 T
new FArgumentsImpl (this, sel)));6 l* J6 l, N1 ~6 V1 P! I
} catch (Exception e) {& U: ^% [. \" u u" D5 X& a' D4 D
e.printStackTrace (System.err);- B9 j0 T z" T" n- p _, D7 l9 n" H
}4 q; \' `: H# C* x: x
# s0 | ^( X& K7 T
syncUpdateOrder ();: a: X, w% Z2 U) G) r$ a( f' g
5 n9 @: ~. o C, V" K. h5 T try {9 z2 B& s I( E8 x' D |! K9 @
modelActions.createActionTo$message , B: c# Z. y5 @, G1 T
(heat, new Selector (heat.getClass (), "updateLattice", false));6 ^4 l6 ]/ A# L
} catch (Exception e) {7 i- ^" t4 a3 w9 j' U
System.err.println("Exception updateLattice: " + e.getMessage ()); V5 u" Z; h6 k' `) r `
}
, R5 Q8 ?: z1 Q, A) m4 W t8 ~( s6 B5 p* e
// Then we create a schedule that executes the
# w- s8 _8 z6 g1 D, o // modelActions. modelActions is an ActionGroup, by itself it
5 {6 a) K) e1 Z // has no notion of time. In order to have it executed in4 \: s$ Y7 @4 ~
// time, we create a Schedule that says to use the
6 t( x0 ~, s$ N# t# _9 X0 y: D // modelActions ActionGroup at particular times. This, u! G9 @5 y5 y1 ? ~
// schedule has a repeat interval of 1, it will loop every
5 e1 z t) C+ W0 O8 O& ~& w8 \9 Q1 C // time step. The action is executed at time 0 relative to
/ j3 M$ d+ c' c6 l% a" e // the beginning of the loop.
3 r/ _' l$ W- r4 t# p- F2 M4 j2 v- A+ \* N* w: S' j/ h: D2 G
// This is a simple schedule, with only one action that is) R- h2 U4 c$ }" i
// just repeated every time. See jmousetrap for more
: k) w3 r* i% A9 t0 T // complicated schedules.
7 t& @, L- [, ~6 _ + q5 _& Z6 `) i+ z9 e
modelSchedule = new ScheduleImpl (getZone (), 1);- Z9 k6 e5 m6 w+ h' j: Q3 z
modelSchedule.at$createAction (0, modelActions);
% G! s! e) w* a! ^, O( }! Y
: z. R% z: X. ~% a. R9 L" l return this;
/ o3 u6 X4 E w) s# ?( c } |