HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 e! n% R. o0 s6 U' \" S+ Q7 W& ~
: B/ w) h5 p' W! X5 ` a public Object buildActions () {( T' h& r. Q* ?" p
super.buildActions();' f# {: q% I- q) Y. z) N; w
$ T1 M. e" y" l. r! f2 l- m
// Create the list of simulation actions. We put these in' Z8 w, t- k- d
// an action group, because we want these actions to be( @2 V! F8 E$ v& b( _6 ^, B
// executed in a specific order, but these steps should! O3 o' k6 G" a& f5 |
// take no (simulated) time. The M(foo) means "The message: v( m- X9 \: e" b) }
// called <foo>". You can send a message To a particular
* Z7 ^' S, E. a; Z% d+ m0 V V // object, or ForEach object in a collection.
5 p# }3 {6 E( ]9 t4 m & W& W$ T9 p. F- ]9 B! s
// Note we update the heatspace in two phases: first run7 r% w" Q. S; ?# e
// diffusion, then run "updateWorld" to actually enact the7 |6 J% X* q* p6 I
// changes the heatbugs have made. The ordering here is
b. d; o" A1 P( K8 z6 w( f0 C# H" X // significant!% @) Y r3 C' z6 K, h0 j) p
f" ~7 F: N* _, o
// Note also, that with the additional
% J+ w8 A- E4 {- e5 k // `randomizeHeatbugUpdateOrder' Boolean flag we can
; y, l5 s' }6 c; N# o& E4 { // randomize the order in which the bugs actually run2 r. t8 C9 z* x4 I. O; y: z
// their step rule. This has the effect of removing any8 n$ B6 M% p% U5 }! W
// systematic bias in the iteration throught the heatbug0 m) W- V- N7 [, y8 f- _& P2 I
// list from timestep to timestep4 S) ?% \, O q) c* \6 B
/ G$ @. n* L) @; c- ?8 S
// By default, all `createActionForEach' modelActions have
6 i& |' p2 b) {1 D! a // a default order of `Sequential', which means that the
3 a4 {! n; Y p! d' s+ A+ P. c // order of iteration through the `heatbugList' will be# x1 E) H3 A2 Q( t: `! b( u
// identical (assuming the list order is not changed
1 s; l; A" H0 x$ A, e8 |: } // indirectly by some other process).: z: J# i- v7 X* K8 E* l; m
6 c6 g' z" }6 i; G! \) a( |2 X
modelActions = new ActionGroupImpl (getZone ());6 W7 @; Z$ w8 I
+ |9 g" G% h& E F: F3 h+ d try {& z( c' |. G" N
modelActions.createActionTo$message
( t/ q6 y) a6 V, I (heat, new Selector (heat.getClass (), "stepRule", false));
" {5 m$ X, }4 P) f! l5 d } catch (Exception e) {3 K6 P/ l0 e& \, O- P( L
System.err.println ("Exception stepRule: " + e.getMessage ());
; H2 |' f+ X0 f& { \ }
: k( f9 u8 M/ v" _% }* a* U/ d- D. p
. t Y( d( s7 l* u try {
' T$ f. L6 g/ ` Heatbug proto = (Heatbug) heatbugList.get (0);) D% E% s! V; I% ?4 |
Selector sel =
0 S; O( s" e* c# p3 [) {& V new Selector (proto.getClass (), "heatbugStep", false);
3 z/ o+ h _, A* o# f actionForEach =
3 u7 n% g: b( c modelActions.createFActionForEachHomogeneous$call
& e9 z' x9 W1 S( i3 R' m L% q- m (heatbugList,& C/ `, n! b8 y4 p2 ?
new FCallImpl (this, proto, sel,' z. y. e* P* ~& a7 N
new FArgumentsImpl (this, sel)));
9 G# |/ p b2 {6 T+ T } catch (Exception e) {
3 ?; x0 }& q' z e.printStackTrace (System.err);2 R9 l$ o6 }0 l5 D! T" A
}) o' H! t* S( T. K7 M
5 I7 Y# i- y. f
syncUpdateOrder ();
/ K3 j7 S6 z6 |3 z
9 F- j( }* a. r try {
' L8 J" \8 Z( i5 I2 Z modelActions.createActionTo$message - U4 W z) b Q! J( ]
(heat, new Selector (heat.getClass (), "updateLattice", false));
. l4 l* d V$ l5 k( m1 |$ A } catch (Exception e) {
; W: [- L3 @8 j- D) l) C# V/ V9 L) c System.err.println("Exception updateLattice: " + e.getMessage ());$ ^3 I; t: U0 B( L
}& l% ]4 B2 N* n, i- Y. H# {. w: s
( k# S6 @0 C3 J% D" W( h; O // Then we create a schedule that executes the
" A7 u! T" n& ^7 U' y // modelActions. modelActions is an ActionGroup, by itself it5 a8 w3 {4 m4 O! w. j% A9 [1 J
// has no notion of time. In order to have it executed in' v9 H- p P! E' m. |3 E" K& L _' v: h8 \
// time, we create a Schedule that says to use the& t$ G A0 P( U7 Z' Y; n v3 `
// modelActions ActionGroup at particular times. This- m! D% C+ `& J, u" T
// schedule has a repeat interval of 1, it will loop every
+ ?9 D4 A2 N/ b' {8 ?8 Z // time step. The action is executed at time 0 relative to8 `, Q3 _( U+ r- M3 g4 T5 b: _
// the beginning of the loop.8 ~& |4 @4 _5 f! b6 \
3 H8 k/ f8 |2 g" b: {9 O4 p // This is a simple schedule, with only one action that is
" ?* ? t' y7 P3 V+ y7 F // just repeated every time. See jmousetrap for more; y. |( M% V' Q: F q% J
// complicated schedules.
" ]" d- ]+ g$ b0 m {" L7 F+ K! a 8 r* G; A" U5 Q+ Q
modelSchedule = new ScheduleImpl (getZone (), 1);3 Y" Y" F. C6 k+ s j$ o7 d
modelSchedule.at$createAction (0, modelActions);
" C) R& c( ]5 m: O8 v 5 u( w8 ?% `1 h! w# p8 {6 A2 k
return this; i# B3 q, j( G7 K0 X E f5 z$ s0 ?
} |