HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: n( T8 G' v: _2 V8 s1 Y
" D8 }( F3 N5 J ^" D8 \ public Object buildActions () {
0 H2 x4 p4 U; _" W% f/ h super.buildActions();
4 E- z% Y" N1 {# n6 M' Z4 r4 I2 D
* O; }+ X$ R5 b1 N+ b* t* s // Create the list of simulation actions. We put these in2 O; i' r: E! F) R0 l
// an action group, because we want these actions to be7 U7 x( F$ `' Q& E+ Q0 f
// executed in a specific order, but these steps should, }" L2 N, y& @/ y9 P4 h9 U3 J
// take no (simulated) time. The M(foo) means "The message" t# s' ^ Z8 Y6 A+ B
// called <foo>". You can send a message To a particular2 o+ |: G( z* x
// object, or ForEach object in a collection.4 @; _( a7 T+ O$ |( K# W. r' @
1 X3 j9 ?8 `4 ?% D+ J, u; v9 |
// Note we update the heatspace in two phases: first run- r6 A2 M& [4 _; z
// diffusion, then run "updateWorld" to actually enact the
8 ?4 |8 X: Z: |" \# R+ G; d // changes the heatbugs have made. The ordering here is
; V' B3 |4 T o7 a4 Q // significant!+ E/ R; Z; \7 m
$ w9 X0 l/ \0 R- [
// Note also, that with the additional$ c; I, r" |' |( \
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 [+ i# ]% i4 H. {" Q* j7 e // randomize the order in which the bugs actually run. b9 m! D$ L; x0 j% S
// their step rule. This has the effect of removing any
" [1 S0 ^9 ?6 U // systematic bias in the iteration throught the heatbug7 u$ y) {7 ?/ b) f
// list from timestep to timestep
+ {% L5 S9 c7 A1 X+ w
% }- L4 M9 U& N // By default, all `createActionForEach' modelActions have
- ^. R; h: j, y! M* |% C // a default order of `Sequential', which means that the
/ x" j8 }% l6 \2 a, q% q // order of iteration through the `heatbugList' will be- ]5 w( `) G$ [! f# W; A
// identical (assuming the list order is not changed
% N$ G& s# H# h' Y* L8 h% I // indirectly by some other process).3 Z h8 }2 t7 B Q$ r7 V
6 a q8 a8 }( K( B2 s: n5 K
modelActions = new ActionGroupImpl (getZone ());: b6 X! V5 J+ K1 \- ]
* f- }" k" a" I
try {
2 r8 e; b# h9 u) ?' y% C7 R modelActions.createActionTo$message
7 j- y1 K0 s2 r, N3 b8 o9 ? (heat, new Selector (heat.getClass (), "stepRule", false));
" g2 `8 a7 p! m9 d' Z3 s } catch (Exception e) {
9 [8 v4 n; ]$ p# B: t. P System.err.println ("Exception stepRule: " + e.getMessage ());
4 P; X' v6 d& ]; a+ S) B }# d( b' n, M, q2 m5 R
" Z+ }# Z. l+ _% K- H
try {
: J' T9 E7 l8 n6 T V' ^ Heatbug proto = (Heatbug) heatbugList.get (0);1 e2 b- N* F) x# n% e
Selector sel = + y( a* [) ?- f7 A
new Selector (proto.getClass (), "heatbugStep", false);' S7 S' M" x3 Z7 k7 m
actionForEach =
& P! p2 V, |/ u! d: T- H% i/ Y modelActions.createFActionForEachHomogeneous$call# _! G7 D9 B0 F- w( `$ Y! X
(heatbugList,( G! B$ ]+ }, L
new FCallImpl (this, proto, sel,
7 k6 e4 S& ~$ Y# Y! r/ X4 D) |" j new FArgumentsImpl (this, sel)));
3 z1 D. u" U9 f0 V1 B( f3 J } catch (Exception e) {
+ X% p8 C" b7 _9 K+ B( K e.printStackTrace (System.err);# ?- D; I2 E. x4 E- {
}: ]8 ]- V. G% t$ g, s. y
: Q- @7 y" U2 C; Y6 |6 a. f
syncUpdateOrder ();
( O8 ]: R4 c9 f4 ?5 t
4 O% I8 a) \/ p9 ?/ e try {
. x# B8 Z' v/ ?" E modelActions.createActionTo$message 4 R" i/ }; i6 x2 j! o# S
(heat, new Selector (heat.getClass (), "updateLattice", false));; P( E" K, ~8 _( T
} catch (Exception e) {
9 l* b7 |/ C0 j u3 g System.err.println("Exception updateLattice: " + e.getMessage ());
8 U, V. {0 t7 c/ f" z, s }
0 o1 {- r6 R# j+ N! Y ; j R: j7 c, q! S4 C7 d( i/ k5 y
// Then we create a schedule that executes the
' _& h& |' ^; _! X: B1 _; q/ g8 Z // modelActions. modelActions is an ActionGroup, by itself it
' V8 M6 h: W6 u" b& M+ g! i // has no notion of time. In order to have it executed in/ y5 ^2 B) w5 C6 T1 _
// time, we create a Schedule that says to use the( Z4 \: n" w$ p/ m) T- w
// modelActions ActionGroup at particular times. This S) R. A- n$ R6 ~- G4 d; i5 c# }1 n
// schedule has a repeat interval of 1, it will loop every
6 E1 ?0 V8 B! Z( s9 S- v // time step. The action is executed at time 0 relative to
& C% X$ u/ |3 |( k5 `: V1 r, q7 ]$ o // the beginning of the loop.
, C& i" B. ?+ @) H ?
, {* P8 b9 `; Y) b9 P5 U: L0 O // This is a simple schedule, with only one action that is
7 N$ [& X9 r/ W& r2 @ // just repeated every time. See jmousetrap for more6 E8 j4 Y; }) Z0 R
// complicated schedules.( D c5 V- v* ?; j+ W
& M* [2 R; R4 u! u1 \+ k5 ~, Z
modelSchedule = new ScheduleImpl (getZone (), 1);3 p/ j5 c2 B3 T
modelSchedule.at$createAction (0, modelActions);! z1 `, _4 Z, G2 e# A: w; A" [1 ^
0 o# z. ^) N J, Z
return this;% B( j, d; d p+ x3 D+ n. `2 k
} |