HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 d$ Z! Z- i; j2 V, [2 _
# ^4 {" q0 w% f: u+ Z
public Object buildActions () {
/ J% d- P% o7 b) y super.buildActions();4 I- e+ `8 h4 q/ J- z9 L
; C1 ~0 ?; ?, [$ h# v# N- n9 d& A# M // Create the list of simulation actions. We put these in
9 K' [: l8 J' ?, S // an action group, because we want these actions to be
! Q2 D& {! G) W+ O% P& b7 |% e // executed in a specific order, but these steps should# x! b9 @) d8 E2 q2 c9 a
// take no (simulated) time. The M(foo) means "The message( a5 M, T# x5 P7 p, x; T
// called <foo>". You can send a message To a particular
9 f' C$ n6 v! Z) d5 I // object, or ForEach object in a collection." d5 T5 h# k% Y6 ?2 ~8 P; |5 C1 j
# g0 C# R" R+ {1 ] // Note we update the heatspace in two phases: first run
' k4 H1 O' x& ]+ q) ~0 M* u. t // diffusion, then run "updateWorld" to actually enact the
& t0 ]' l* @1 k' x. e# n% |/ l // changes the heatbugs have made. The ordering here is! j: P8 Q$ u n. w2 r0 l7 t3 f
// significant!
0 z* A; v; A1 n/ y8 t& L ) ^. C8 e4 B) {5 S" ^! R
// Note also, that with the additional
; I9 ~5 X. o) v j+ \5 T6 x% a4 z // `randomizeHeatbugUpdateOrder' Boolean flag we can @0 K& C3 h$ I3 \
// randomize the order in which the bugs actually run( H. t- Y: u5 l1 X7 n
// their step rule. This has the effect of removing any
+ d N: Q' \/ M+ C9 b& p2 k/ F) b0 D% } // systematic bias in the iteration throught the heatbug
8 Q6 y' m3 y* W4 k // list from timestep to timestep7 `; M S2 E* r* k$ k3 y7 g
& Y" p( K' F. k
// By default, all `createActionForEach' modelActions have
8 V* H0 q7 T% T$ f // a default order of `Sequential', which means that the: Q' \9 A% S! y# }& R
// order of iteration through the `heatbugList' will be
. X% J$ v" ^9 p; @ // identical (assuming the list order is not changed8 R4 p i7 g) h+ n+ c
// indirectly by some other process).
* C* e# W4 C& D% G% e/ x$ \% S7 @' N 0 s- t* d8 r H: i" O
modelActions = new ActionGroupImpl (getZone ());9 u1 M( C. E0 z
: o9 i- }! Z/ ^: j3 s1 L, A" `# W try {
U! S1 g' `* P/ r& S modelActions.createActionTo$message
& V& M+ V6 ]" @' \; r (heat, new Selector (heat.getClass (), "stepRule", false));
3 T9 J$ T3 n' R6 b' ` } catch (Exception e) {- Q' D7 @! o5 ?2 {) a- \# j
System.err.println ("Exception stepRule: " + e.getMessage ());2 u) I" B5 ]- ?0 P
}
3 S/ L8 |4 l3 H7 K# y& a! s$ o4 \* s( h
try {/ u7 U# v7 \1 `1 @3 z3 I6 [/ @
Heatbug proto = (Heatbug) heatbugList.get (0);8 l! K7 z8 t- V/ i9 m# G
Selector sel = " X7 ~) v: a( d" W& m+ l# z7 B
new Selector (proto.getClass (), "heatbugStep", false);. o% K$ E" v7 h
actionForEach =
+ Z' g! C, e9 @4 L' u9 x% T modelActions.createFActionForEachHomogeneous$call" W2 v0 C+ D! F8 B$ B( E
(heatbugList,
6 [1 U9 ?9 g! o! |, M( H1 H& v/ z0 t% c new FCallImpl (this, proto, sel,: O: i3 j, n2 U
new FArgumentsImpl (this, sel)));
2 v; F+ o: U6 }/ ]: @ } catch (Exception e) { {) c8 t4 t% f5 j
e.printStackTrace (System.err);
; F$ ^9 K/ L& |6 E }) ?% U/ _- G& U; }, M: `. o
: ^& l1 _: {! S5 I
syncUpdateOrder ();4 V- j; S1 X$ S9 ~
; M1 {, H P9 V
try {
& w6 W+ o% `9 N: B5 D5 j& o modelActions.createActionTo$message
0 F, A' E8 r9 M/ O8 x6 t (heat, new Selector (heat.getClass (), "updateLattice", false));' U1 _( r9 {* ?; k. w0 V; k$ Q
} catch (Exception e) {
& f& ?6 f. m- U5 @ System.err.println("Exception updateLattice: " + e.getMessage ());
# n( }$ `. H. W; f; Y: g1 | }- @, j* d5 v; e4 m% c% |
. Z: F- x7 j; \- k! N, b5 g
// Then we create a schedule that executes the
+ l9 a9 Q$ m9 y- ?6 v5 {; e // modelActions. modelActions is an ActionGroup, by itself it7 ^* J- ?7 Z3 v8 \/ q
// has no notion of time. In order to have it executed in
- q# t2 g0 X. |! X1 d0 P- |) |, X2 i // time, we create a Schedule that says to use the8 c0 ~1 k1 Q5 E! |% `) w
// modelActions ActionGroup at particular times. This
+ c( ^6 v) E* V9 s3 l: Q // schedule has a repeat interval of 1, it will loop every g# b" P0 Q8 x+ c- l' i+ K
// time step. The action is executed at time 0 relative to* N: F$ u& {% d4 @2 @3 d/ m
// the beginning of the loop.3 ^$ [" L: \9 g2 k
0 O5 |; U7 |9 f, X // This is a simple schedule, with only one action that is
9 e6 g! c# `7 L5 T // just repeated every time. See jmousetrap for more( y3 t; g5 p; |- _2 {# D6 p6 c
// complicated schedules.
& G( P" k: ]; j ! p$ K8 O# C A) C
modelSchedule = new ScheduleImpl (getZone (), 1);: [: o$ g( Y/ ^* Y; x7 z
modelSchedule.at$createAction (0, modelActions);
' N; U! O2 W- J
: j. I6 R% A2 N( L% o( s return this;
; ~+ H0 K4 o- Y7 b' B! c } |