HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' r* T% N: a% Z; b, B
* W2 a: f% B/ H9 ?: r
public Object buildActions () {/ I0 Z `5 B3 c' g2 h7 G2 g
super.buildActions();7 a: R/ U7 Y: P/ Y3 [1 ]
: ]* ]5 W* L: A0 T- T
// Create the list of simulation actions. We put these in
- v0 t0 n3 t3 q8 W) F0 f" {& [; \& l // an action group, because we want these actions to be
) Q1 I/ i: f b" [# }! u9 A // executed in a specific order, but these steps should
2 I+ s" L+ Y4 R8 G# Y7 F // take no (simulated) time. The M(foo) means "The message# @) q6 ?7 E L' X3 t4 U7 l
// called <foo>". You can send a message To a particular2 K( N: s& o/ o3 y) e
// object, or ForEach object in a collection.& [* n& `- o6 p; y: Y q. W8 w
- m* ^6 {* i, L( h
// Note we update the heatspace in two phases: first run% m* J# m6 c* i+ N
// diffusion, then run "updateWorld" to actually enact the
0 }8 _3 G* G ]6 [& o1 [+ u) o // changes the heatbugs have made. The ordering here is! ^0 ?$ A4 G& Q+ [
// significant!
: Z' F. p9 x- s- r" i1 b
8 h# A5 x# b9 `3 G, i. N; o // Note also, that with the additional
f5 H5 N7 X$ x3 e" x! o f( _ // `randomizeHeatbugUpdateOrder' Boolean flag we can' {" A8 B; @1 ~1 f8 }, j' I
// randomize the order in which the bugs actually run( v: i" |4 J; `( ^
// their step rule. This has the effect of removing any
1 ^, U, [/ {4 _5 r* c$ Q% e/ w& h // systematic bias in the iteration throught the heatbug
: e1 A$ a3 D; s: ]" G* [% n2 I9 ~4 A // list from timestep to timestep) a" G4 o& Y& }1 z
J+ V% X' ]- P8 h! {+ O' G
// By default, all `createActionForEach' modelActions have# u: u# T! x" ?& J, d+ C i0 ]
// a default order of `Sequential', which means that the# D4 R* ~$ N9 j! C9 B
// order of iteration through the `heatbugList' will be4 u% @% ^3 q5 ^6 m' X' J
// identical (assuming the list order is not changed
2 X% k0 x& i- V& p5 v E/ c // indirectly by some other process).5 C$ @; r$ K3 l6 g0 |$ w5 L! K5 Y7 R8 f
' X0 S4 s! K1 U+ s% a9 a
modelActions = new ActionGroupImpl (getZone ());
; Y7 \, \& k1 J5 {1 U5 I5 x. x% l- A; H5 z9 h" d+ X( J6 g
try {
H1 q. z0 W+ v* Y0 F modelActions.createActionTo$message. |+ A6 M- P5 S( P1 h: b1 i. R8 B
(heat, new Selector (heat.getClass (), "stepRule", false));
9 I9 x: p/ v9 r7 m- I } catch (Exception e) {
4 F7 _+ W& J/ w$ I; R. C. L( m* E System.err.println ("Exception stepRule: " + e.getMessage ());4 c8 ?/ Q3 F2 O: |5 Z
}# G$ ^3 @6 m8 [
: O, Y5 f( c! A i1 f* ^4 U7 U2 z, F try {
4 H" q8 ]* W! V$ @# _3 ^6 |( N Heatbug proto = (Heatbug) heatbugList.get (0);# x: X, ?: ~. `- A5 m$ m2 W
Selector sel =
1 l0 v2 R0 j( _ new Selector (proto.getClass (), "heatbugStep", false);
( @1 n) G7 {3 _+ j* L' p actionForEach =
: m( A& ?- g# T3 l1 l6 N! w2 Y$ V q modelActions.createFActionForEachHomogeneous$call
: J9 m! J: [$ P) X8 a7 T1 a4 }4 P (heatbugList,9 Z9 ?9 E X) O2 {) M: e2 X
new FCallImpl (this, proto, sel,/ a* w% G- B6 f$ w8 \" B' Z
new FArgumentsImpl (this, sel)));
& a% H7 {4 J6 d1 s7 c } catch (Exception e) {5 J" q% u8 T# {; V9 A+ Z8 C
e.printStackTrace (System.err);9 I: v6 I& C. G1 p* k
}% Z1 i) p9 P; ] ?. J
8 M6 H2 r* O6 } B0 _( k1 c
syncUpdateOrder ();
$ ~" z) y4 S S
1 H8 t" p9 u3 K0 J6 J) D try {
" _. ?9 P. I; K7 r5 ] modelActions.createActionTo$message & l7 q4 A( u7 ^
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 C5 V& H2 B7 Q9 y } catch (Exception e) {
' j& U8 X2 _5 P. |9 z System.err.println("Exception updateLattice: " + e.getMessage ());
: S* }1 _1 D8 K5 c% D. y. J }8 p9 w$ w! I2 p) S
8 _4 y5 F( V+ ^ // Then we create a schedule that executes the9 u; z! g& \* ?3 C4 q
// modelActions. modelActions is an ActionGroup, by itself it
; r/ m1 V6 F" n. Y) @ // has no notion of time. In order to have it executed in( M% s7 m I/ L0 D( @) k
// time, we create a Schedule that says to use the! [% U. o* I7 x. `! h% r) v
// modelActions ActionGroup at particular times. This8 h. j9 C3 e4 P4 J3 V. G
// schedule has a repeat interval of 1, it will loop every
% r0 [' ~+ I. W // time step. The action is executed at time 0 relative to
; _; z* |! w" w: O2 \ // the beginning of the loop.
$ h) Y. x! x! K
" ?% T& C/ \. Z // This is a simple schedule, with only one action that is
( ~" k8 P, ?7 X6 l // just repeated every time. See jmousetrap for more& _1 M6 x6 L! x8 z% Y7 q" T4 D& k
// complicated schedules.4 P! @2 }# G5 i8 h( x. s3 F/ Q
5 A# e0 C' V( C. i
modelSchedule = new ScheduleImpl (getZone (), 1);
# E8 f) b3 x0 s( F& ]- J modelSchedule.at$createAction (0, modelActions);, T* {7 ~1 ?1 D h; v
; [( U, J5 F2 y1 G1 p return this;+ ]9 y) c0 m- _+ d0 D5 m
} |