HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 H4 i: M4 ^" [3 @2 ~, A- z9 A
5 I1 {3 H: W3 Y8 y3 a | r public Object buildActions () {# a; f, ^, H; `/ J+ ?% W- {- |* m
super.buildActions();
: q6 z" f- h' `0 i/ S. [2 d' d 4 S9 z1 C/ y" T, T) t$ ?: M
// Create the list of simulation actions. We put these in9 m* d9 ~2 D u- n7 o2 W9 e
// an action group, because we want these actions to be! W* p5 p; n5 {0 Y/ M
// executed in a specific order, but these steps should$ _# d( [8 N2 E; w$ N8 A
// take no (simulated) time. The M(foo) means "The message
G: Y( T$ N9 L/ V1 w0 b- w // called <foo>". You can send a message To a particular
2 D4 H- P& @, _6 [% K7 p) A1 T" b" { // object, or ForEach object in a collection.
, p0 f8 Y1 K7 F) U' M
% q! `! t+ i1 Y [3 X" a // Note we update the heatspace in two phases: first run3 u) H! V! ~7 ?4 J
// diffusion, then run "updateWorld" to actually enact the
6 [% N* O7 [6 n8 p // changes the heatbugs have made. The ordering here is: {' X* e# @4 k) R
// significant!( ], q: Z7 Z6 Q3 C
5 e! q; |8 a: ~ // Note also, that with the additional5 G% n! g! P! g1 Q. k
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 p- h$ v3 M9 d' _5 G8 q // randomize the order in which the bugs actually run+ X7 r9 {: q! o4 U( j: ~
// their step rule. This has the effect of removing any3 H5 L& U% z8 }9 f! N2 @. M, Q( x
// systematic bias in the iteration throught the heatbug
2 A& l( v! v( E // list from timestep to timestep
3 U$ J* ] r2 Y7 b' [ - C, [+ l) m6 n) `/ B: J: M
// By default, all `createActionForEach' modelActions have
0 |2 \, A. {) T" v$ Z' n9 @ // a default order of `Sequential', which means that the) W% X9 c: i6 h3 x
// order of iteration through the `heatbugList' will be2 [8 Q8 g1 S/ [" d: G
// identical (assuming the list order is not changed
# ?1 k; W) A' S4 a: B- { // indirectly by some other process).
) l7 o/ ]; r# y ^( Z0 ]$ U 2 V2 @ ?3 D# P/ }1 X/ e4 k F
modelActions = new ActionGroupImpl (getZone ());
, o n, D& i: R
0 S1 X) L, P0 V. ?4 q try {
# ]4 N" a+ ?3 N% ^& S modelActions.createActionTo$message
. ^( D& S" q- w: y" q! Y (heat, new Selector (heat.getClass (), "stepRule", false));4 D3 S, s" G; g: o7 G) x
} catch (Exception e) {
( `- R% ^7 G. N System.err.println ("Exception stepRule: " + e.getMessage ());
6 J& a' D7 |$ h1 i& {* } }. Y1 E2 M* S7 U
% J8 b" f* |+ m/ T7 A5 Z- e2 E try {
- z2 e7 c1 n8 Q" {' o0 `* ~: b" s Heatbug proto = (Heatbug) heatbugList.get (0);
. G# e# p& }' X9 U Selector sel = 4 \8 U0 ?5 P6 }* f% G9 b
new Selector (proto.getClass (), "heatbugStep", false);( ]: s% e% _& p' n
actionForEach =
2 S2 }9 q: t' N) m5 ]' U modelActions.createFActionForEachHomogeneous$call8 }( v7 F7 m9 `" e* n& C2 ]
(heatbugList,
! m* O5 [0 H5 B0 x$ ]- f new FCallImpl (this, proto, sel,
E4 ~( }5 r T4 j) l0 k new FArgumentsImpl (this, sel)));1 J3 _" }/ B% ^
} catch (Exception e) {
& x2 `6 k+ T* D$ B. e5 r+ @7 S e.printStackTrace (System.err);
+ }9 |: F( [7 v# i) J( E7 S8 U }
G) R" N6 P6 `4 }2 J; K
. V3 j, i# \2 `2 M3 a syncUpdateOrder ();! o) t% N$ m. ~7 w/ r. `* Y8 G
- k% s* \( I. ^. V try {" L5 ^2 b0 ^+ z, F" h" j
modelActions.createActionTo$message
4 z$ @+ D8 x( Z4 X- M (heat, new Selector (heat.getClass (), "updateLattice", false));
4 t, o% X' e" R. k! _ } catch (Exception e) {9 Z% R2 Z4 [1 _" P4 S% t- I- T
System.err.println("Exception updateLattice: " + e.getMessage ());
& C7 m. N; b# v0 A- U( e4 R' Q1 ? }7 u# d" Y g* b' D# X8 F) n
/ u; P7 y7 o0 {( C // Then we create a schedule that executes the
* u6 V5 i' v" f8 y% r // modelActions. modelActions is an ActionGroup, by itself it
* h- C3 y1 n0 f1 B // has no notion of time. In order to have it executed in
) W+ o6 j9 I& o. s0 H" P // time, we create a Schedule that says to use the$ r1 n4 }$ F" S* a
// modelActions ActionGroup at particular times. This. c( F9 d* u0 j3 X, l# `3 @ ?
// schedule has a repeat interval of 1, it will loop every% r5 \* _! ~0 b' r
// time step. The action is executed at time 0 relative to
% R' C( {9 P- E, { // the beginning of the loop." I) E3 d" @ M3 x' R6 p
1 t B. n& X; A$ S+ N // This is a simple schedule, with only one action that is' D d1 o* Q# Y. p5 V- B
// just repeated every time. See jmousetrap for more7 J% e2 p+ S9 Z, n9 \& h) Y
// complicated schedules.4 X1 V! Z3 ?* D8 A
1 t5 u" l5 x0 ~7 X9 |( q% h modelSchedule = new ScheduleImpl (getZone (), 1);
# u9 d4 O( u+ r: w; n modelSchedule.at$createAction (0, modelActions);
. ~7 r5 B/ e& L: s* n2 J 0 n5 @" e9 y8 ]; ]
return this;5 ^$ U+ ` P7 D/ ?: W
} |