HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ s; ]9 G5 V9 I/ y" X
$ ?& I7 e Q- o9 @
public Object buildActions () {$ J) ^* w: {: z7 Q1 T% a# N
super.buildActions();3 i, }2 v; E' h- Z$ [" d9 R2 H" k0 \
* J2 [2 E5 @. {$ Y- b( A: b8 e) U: |+ L // Create the list of simulation actions. We put these in
2 C% P$ t; U. y% o( ?. {/ V K- U // an action group, because we want these actions to be; t+ F0 w; B9 I" E# ]3 Y- T
// executed in a specific order, but these steps should9 _# ?' d1 p1 _- C. j3 ~
// take no (simulated) time. The M(foo) means "The message
1 X0 c# e1 D! E* W' R ] // called <foo>". You can send a message To a particular2 c( F% m- K( X8 ^& O* u# N
// object, or ForEach object in a collection.4 U6 n; D4 s: R; t% V5 p; }
* F5 a* ?( K o' q7 ^
// Note we update the heatspace in two phases: first run4 ~. t3 J% d S8 i+ R
// diffusion, then run "updateWorld" to actually enact the
F+ I% Q" N2 B' k6 _1 h) d // changes the heatbugs have made. The ordering here is. S! |, {7 t; f# i+ G" X% J
// significant!; h; _8 y( |( P% R# T
6 Z4 Y* R6 z2 Y/ h5 x$ f K
// Note also, that with the additional
* O. K, o5 }* e3 m // `randomizeHeatbugUpdateOrder' Boolean flag we can
- ]" P- D- D; y' m; U6 _; g // randomize the order in which the bugs actually run# \3 h8 i1 k/ E
// their step rule. This has the effect of removing any
, ?6 {0 b+ A* y( T8 p // systematic bias in the iteration throught the heatbug9 i8 ]9 C6 D" O, K. {& O) X$ `
// list from timestep to timestep i; n+ t/ ~2 N" ~- c0 R
8 h0 m. V& o8 ~' ^/ b // By default, all `createActionForEach' modelActions have. y' {8 ?2 F1 N, G, s8 n( M M; _
// a default order of `Sequential', which means that the
1 d q0 d& `. E. E7 e // order of iteration through the `heatbugList' will be2 j t# |% t, A, M% B% f: W& U8 h
// identical (assuming the list order is not changed+ i8 v" y( H' @4 |6 ^1 x1 A
// indirectly by some other process)./ L* S3 i$ q4 m' h Y/ u; y7 H
& K3 B, N* Z: P modelActions = new ActionGroupImpl (getZone ());2 u v: G7 l/ Z! }
# e& M8 W5 D! |+ p8 e& E/ ^7 `, \, N
try {
9 r# X w" I' c) M; [1 Z modelActions.createActionTo$message
7 f4 E: O0 q' j (heat, new Selector (heat.getClass (), "stepRule", false)); h. W, _; R2 c4 n8 l+ r: E
} catch (Exception e) {( @1 b) U0 w% U# c& t, f) m
System.err.println ("Exception stepRule: " + e.getMessage ());
" ~0 V0 E& z3 m C L1 C }* g" u1 C7 t! n0 _. j# v
- D: Z* ], W3 K% ]. O* g& V
try {! q& x, t D e4 r* Y
Heatbug proto = (Heatbug) heatbugList.get (0);
% g4 l7 N5 O$ k* s; h9 L Selector sel =
3 W _( y( x- ?, d new Selector (proto.getClass (), "heatbugStep", false);8 p3 h- P/ ~4 v5 o! g
actionForEach =
' L% d( v0 s2 _, s+ r& w! t modelActions.createFActionForEachHomogeneous$call5 ^% \4 u+ I, o1 Q. b' s7 a+ X
(heatbugList,
' b% } o; N, X" A new FCallImpl (this, proto, sel,
6 ]0 E% I/ J4 K$ u( q new FArgumentsImpl (this, sel)));
( R: a8 a5 _: {! U1 p$ v& {) p } catch (Exception e) {
6 }% P4 N# c7 H6 G4 X e.printStackTrace (System.err);
" s% W7 x9 n) T7 n6 \ }
7 p# s* Z* k0 o9 P
1 N+ A) d4 ^( Z# C9 Y# ~( ] |; u syncUpdateOrder ();* S0 v% A7 x C! S
: T: a- X' d! y9 S try {
* \+ t9 t8 h' S. h modelActions.createActionTo$message
& @1 t8 N, J5 Y- J, s (heat, new Selector (heat.getClass (), "updateLattice", false));
' ?/ C4 t4 K- j& @5 T; d } catch (Exception e) {: R5 P5 N8 C& v2 G' I; ]; H7 C
System.err.println("Exception updateLattice: " + e.getMessage ());& \; y4 z# z3 a# n/ s3 z
}! \& o: r! [% [ ~3 [& E
1 x, E( t: d. C' o // Then we create a schedule that executes the) b, |+ a% @2 M7 A: i# p
// modelActions. modelActions is an ActionGroup, by itself it: _- {" V, R) Y" G: j
// has no notion of time. In order to have it executed in
3 H. H% f; s, K! w# X- f$ L3 } // time, we create a Schedule that says to use the
8 Q6 C; m) Q I( E; ` // modelActions ActionGroup at particular times. This7 N- }* Q, N# T D' P u% B' f' q" N
// schedule has a repeat interval of 1, it will loop every
- v3 _% a. x0 b% R) I# O // time step. The action is executed at time 0 relative to, s, W5 s6 x! ~$ h
// the beginning of the loop.
1 X8 b1 f( `7 x8 @0 Z8 k; B
! ]/ X v+ P% w# w/ u0 p$ F5 h // This is a simple schedule, with only one action that is; w0 w+ D- H+ _* l8 ^5 U a
// just repeated every time. See jmousetrap for more+ p( H7 ?. ]! N
// complicated schedules.
# C, p4 q9 b1 ] X" |: @: L# d
\! z, \' V. v" o modelSchedule = new ScheduleImpl (getZone (), 1);
, Z& y! ~4 u3 c% [) E1 I modelSchedule.at$createAction (0, modelActions);/ }0 z- X% z7 |
2 p3 r' }+ T) y: `5 Y1 r
return this;
3 o) u! h0 t$ c& i7 T } |