HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ C" e; O! z* ?' v6 p5 B1 d1 n6 ^. O9 j1 O' m0 A# y( ^
public Object buildActions () {
) m, D7 ?" v1 c' [# z super.buildActions();
% M6 o# l6 Z4 u: I" `/ d3 D* y " v! ^; n ^4 t
// Create the list of simulation actions. We put these in
; A7 S5 B2 J7 V7 \* ? // an action group, because we want these actions to be3 Q3 \7 a! ]! p; l- U& x
// executed in a specific order, but these steps should' V) s2 x0 b1 y v8 M: [6 q: l! ^
// take no (simulated) time. The M(foo) means "The message* n# W) \/ w+ ?, F" L: L$ ?9 k6 s$ r
// called <foo>". You can send a message To a particular. G( m# w+ t# k u7 C# k6 p
// object, or ForEach object in a collection.
/ w b$ N: i0 F+ p " W! ]+ f2 X C# g' l( \$ n" r3 R
// Note we update the heatspace in two phases: first run
- w& F) _& f5 T // diffusion, then run "updateWorld" to actually enact the7 Z# i% `5 t* U. n+ ?
// changes the heatbugs have made. The ordering here is
+ q/ t' j1 @0 T# O+ O // significant!
3 @, B _, P) d# A+ c
: i! n, S s: [- @: B2 P) H // Note also, that with the additional
7 m% I7 z; @0 k9 {* P7 o( Q# ] // `randomizeHeatbugUpdateOrder' Boolean flag we can6 m* D$ E8 K& U! W8 Y. N
// randomize the order in which the bugs actually run
" f4 n# W( `* C) L7 j; Q7 c4 s7 N5 T // their step rule. This has the effect of removing any
4 J( X+ |' R3 g- a // systematic bias in the iteration throught the heatbug" B! j' f# X# Y* P- C2 C. t
// list from timestep to timestep, n* E- B/ s _3 R
: t9 Z) x/ e- t$ e. z$ n1 X5 J // By default, all `createActionForEach' modelActions have. c$ z( S/ K& Y! F4 X/ N4 ^
// a default order of `Sequential', which means that the* @0 C- e* P: c3 W* B- w
// order of iteration through the `heatbugList' will be
1 B3 y2 a, A" w) \) E/ [ P // identical (assuming the list order is not changed
" e8 g9 `1 U9 p/ L // indirectly by some other process).1 r" Y$ P6 @, _ h6 Z, E& X2 I( p
+ o2 q5 G1 y! k6 Y+ q& J0 q0 _ modelActions = new ActionGroupImpl (getZone ());
' o+ A, d9 }. c' J. D) B- O* q0 r0 i: j% c; M5 g t% p$ k
try {9 j8 [7 h( B6 }
modelActions.createActionTo$message# Z. P6 l/ W% q# l) @5 f
(heat, new Selector (heat.getClass (), "stepRule", false));2 T& b, C( K, x! w+ {& I1 [' t0 C
} catch (Exception e) {
) [& j* i# x% v4 V- z8 |% e% k4 _ System.err.println ("Exception stepRule: " + e.getMessage ());
( H+ e- ^, r, k/ M& }* e/ _; i }% a, q: M4 Q( G, W# ~
. Y3 F) Q% u' o try {8 T5 w Z) k- d6 N/ T( X
Heatbug proto = (Heatbug) heatbugList.get (0);- u1 a, U5 l. ~# d7 r; v5 W
Selector sel = 2 O! R9 u" p! \) h
new Selector (proto.getClass (), "heatbugStep", false);8 l# `; v$ U# z# W7 g
actionForEach =
* U: z$ w, ]& l3 D; S \2 E. h modelActions.createFActionForEachHomogeneous$call
" j4 B5 [ Q. ]$ c) o, Z (heatbugList,; w( q) C$ F2 `/ S
new FCallImpl (this, proto, sel,5 b. k5 D; \0 q& C$ M
new FArgumentsImpl (this, sel)));
$ u6 u# N7 w' {+ c1 m) } } catch (Exception e) {
% l E; \1 N# }+ v6 Q e.printStackTrace (System.err);; |# a2 [1 v4 a+ q9 ~9 \. T
}
- p# R) O% h; ^ % G% S: B- h" ]% \$ O) n
syncUpdateOrder ();
8 l8 c. z% o- T5 M) y6 ?
. O8 A( h. ]* _- R( M try {! l6 n+ {8 x& y( i0 |, y+ F
modelActions.createActionTo$message 4 C' M5 f( F! w6 R/ j. B. M
(heat, new Selector (heat.getClass (), "updateLattice", false));5 f( X3 T9 b. d) {& o- `% W* c7 M
} catch (Exception e) {
& ^% k2 ?% J1 | System.err.println("Exception updateLattice: " + e.getMessage ());
/ {; F, T! Y. b$ V }1 F' J% I5 w. E$ G7 ~9 h' I$ H) A8 ^
$ m1 O$ h% r- U3 ]4 G- D // Then we create a schedule that executes the: q- X1 |2 e" t' ^7 t
// modelActions. modelActions is an ActionGroup, by itself it! j% |& Q; j/ l
// has no notion of time. In order to have it executed in1 j) M! O+ d! `5 i+ Q" N0 T
// time, we create a Schedule that says to use the* `+ F- z. ]8 z1 _+ ]5 k$ f
// modelActions ActionGroup at particular times. This
8 U! B6 I1 c9 I! S. }: s& U // schedule has a repeat interval of 1, it will loop every
$ Y# ?& l/ U: e6 w' s* I // time step. The action is executed at time 0 relative to
# m0 Z. Y0 k& j8 _$ e# _ // the beginning of the loop.
" }8 h# ?8 o9 o9 s
# G7 Z$ A* u0 O+ H, L6 f p2 f* b // This is a simple schedule, with only one action that is
# g6 ^1 l- x9 ~, r" v7 c# T/ K // just repeated every time. See jmousetrap for more
+ Y0 I' e( n5 \( |5 J // complicated schedules.% m4 o5 L) R' m$ n/ S
# T& t. D/ B5 K
modelSchedule = new ScheduleImpl (getZone (), 1);* N5 A$ X/ Q/ ?1 s% E/ H3 {
modelSchedule.at$createAction (0, modelActions);6 Y* l# K4 F; R0 z: Q; }% I8 O
3 Y/ ^! V* ]+ `/ s* v
return this;1 x5 B% h7 R! @& Y+ Y! t& Q/ o
} |