HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: E! }1 h0 S3 [, F0 l4 l" v C% V2 ?3 \ H' m3 D$ ]
public Object buildActions () {0 A) p9 Y, ` D# O% i* t/ ?) p
super.buildActions();/ W) M! N0 M+ t! q1 C: R; r
+ x5 F- B; s* R# L5 @; F& i
// Create the list of simulation actions. We put these in
8 u6 u) ` y4 \- u+ t- q, @ // an action group, because we want these actions to be
6 L) s& r" f/ n0 i% \: m) h // executed in a specific order, but these steps should" L+ X" l; k$ O7 h
// take no (simulated) time. The M(foo) means "The message
+ N/ B; Q- N# K // called <foo>". You can send a message To a particular# ], p, Q: l) ^2 ]$ i& I% E# g
// object, or ForEach object in a collection.
$ c# d" F- F; @! p7 ` 4 B2 V" C8 z( p5 I* y
// Note we update the heatspace in two phases: first run
+ q. r" N/ j, U$ F# S // diffusion, then run "updateWorld" to actually enact the( P ~- p4 v6 i9 }8 e
// changes the heatbugs have made. The ordering here is8 V# `! z1 w6 T* ^7 \. ~
// significant!" j8 }% o4 ?/ L: e, b( r
5 r& X# c( f4 C6 w; h // Note also, that with the additional
& ~ ^/ i" K# _4 Z# W // `randomizeHeatbugUpdateOrder' Boolean flag we can/ T5 @- M) b5 l% X Q# }, Q
// randomize the order in which the bugs actually run- B, N! q9 i8 q) ?' D0 U! `$ O
// their step rule. This has the effect of removing any
: s) v3 p/ F( c // systematic bias in the iteration throught the heatbug. m* Q5 _& k# F5 }! m1 ]& h
// list from timestep to timestep, _3 T3 ~) q7 d$ }& S: F& `
+ [/ Q2 Y' |0 k
// By default, all `createActionForEach' modelActions have: t& B+ m& w/ |; u
// a default order of `Sequential', which means that the* o! s* K; G2 q4 G' q/ M
// order of iteration through the `heatbugList' will be
6 D9 I7 i$ {6 n4 ^8 y ] // identical (assuming the list order is not changed
1 _6 }7 S' F1 }' z; N, y6 B3 v) g // indirectly by some other process).) G. |8 Z( e! D& }" W8 ?* F
2 W8 g5 ^: H! Y0 a; f5 c
modelActions = new ActionGroupImpl (getZone ());
4 t: ~: x" r b/ }
/ X( c( |/ {- X try {" c' i) C6 v4 u
modelActions.createActionTo$message
! l1 {: R# R; i) M: X( p+ A (heat, new Selector (heat.getClass (), "stepRule", false));
# C! Y- P( L* ^ } catch (Exception e) {! ~2 Y/ x& z' q9 Z" A& r
System.err.println ("Exception stepRule: " + e.getMessage ());
/ x7 I3 I+ x/ i; r) a* O6 l }: b( t+ s8 M$ s3 x
: d, G" k c1 x# ^( t' M7 @ try {5 P$ i; W+ s% k
Heatbug proto = (Heatbug) heatbugList.get (0);
7 h" v% a- I% d" d Selector sel =
" s) u! Q- T3 D/ | {0 L new Selector (proto.getClass (), "heatbugStep", false);3 }7 d6 h* g' ]8 p6 q% x
actionForEach =$ e+ A( W+ K. i- E5 [! t, m5 j, A a
modelActions.createFActionForEachHomogeneous$call
N, c1 C" V8 W2 a: F (heatbugList,
6 Y( g( d E0 v& J new FCallImpl (this, proto, sel,1 V5 d" ]/ }' a! G7 q M$ L
new FArgumentsImpl (this, sel)));0 i+ u1 C" Q; m3 R# @) W- E
} catch (Exception e) {
* n1 [' b' f7 P e.printStackTrace (System.err);
, u7 o Y6 j3 y, M7 s }
4 x0 @/ y4 E% v/ [$ {, ^ 2 k. Z1 X7 M9 I6 E2 ~
syncUpdateOrder ();+ E$ Q, _% ^. n/ O3 g
3 ]. {. _! o( W3 r0 T
try {
( K6 [5 C, v" Z6 X9 ~) ` modelActions.createActionTo$message 0 G' { F1 F4 E4 [$ K9 O
(heat, new Selector (heat.getClass (), "updateLattice", false));- u+ a6 j2 D2 c$ q8 L @7 C
} catch (Exception e) {% t8 o7 ]) x' m) t9 O1 v
System.err.println("Exception updateLattice: " + e.getMessage ());3 ~9 F, g/ _& K# L( j: t3 w" O' T
}
) p7 y1 ^% |( y2 `: D; H
. T6 v* ?6 Q. C! w" h) [4 i // Then we create a schedule that executes the
( K3 l# _/ f) I1 b1 w- E7 U0 X // modelActions. modelActions is an ActionGroup, by itself it
2 ~( L& N, l) T! s // has no notion of time. In order to have it executed in
, e1 e% c& S4 l4 t // time, we create a Schedule that says to use the1 C) e! ^' `3 \9 t7 d1 t# z
// modelActions ActionGroup at particular times. This
$ D% x' ^1 G# q' o: F' D- l // schedule has a repeat interval of 1, it will loop every
- T6 D0 X; {' ?. t/ u K. J // time step. The action is executed at time 0 relative to# k ]- X+ W! ^% r3 b+ a8 m
// the beginning of the loop.' H) Z/ Q* g2 V5 Q0 Z
+ i6 @% S! Q$ N W! q# j8 T
// This is a simple schedule, with only one action that is( a# X/ v+ m$ X7 g. X; a6 N4 D
// just repeated every time. See jmousetrap for more
) t" N2 t( E% m+ Y( v, h // complicated schedules.
5 o& h( g) `3 Y* B9 }( [ % x9 G" z+ }# p! s2 O6 G
modelSchedule = new ScheduleImpl (getZone (), 1);) E# p% X/ C2 d) y
modelSchedule.at$createAction (0, modelActions);+ l+ K# C+ e, R0 S u& s; s7 W
- q& Z8 U6 z. i8 I
return this;
4 q/ ^7 v6 W) d( G } |