HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! K$ J& ?/ H* U
& C# o" m- V, @8 _
public Object buildActions () {
' R% G8 b9 z, Y) y5 D6 y( o2 t: _6 V$ v super.buildActions();
* l6 w" V6 G1 B8 y0 o+ |! T2 x# Y ! u$ a0 g3 U* w5 C
// Create the list of simulation actions. We put these in0 K A$ p- [$ o
// an action group, because we want these actions to be
$ H9 j5 B7 ~: G" q' o4 t- L // executed in a specific order, but these steps should
& Q7 r! i" J7 U$ p% x, h9 z$ H // take no (simulated) time. The M(foo) means "The message; [$ q+ b3 @0 F) l
// called <foo>". You can send a message To a particular
3 L" F1 h1 h$ i* ~4 \8 R // object, or ForEach object in a collection.
7 n$ @* D* @( L) g9 H/ \
$ P; q: z w" b8 e // Note we update the heatspace in two phases: first run
. W6 E& k" h0 {0 H8 |+ w- b; z // diffusion, then run "updateWorld" to actually enact the
: V g5 V z4 f6 x6 i6 z // changes the heatbugs have made. The ordering here is; c! [1 k( J! g; M; ]
// significant!7 O5 L# _ O+ @4 _
. u( Y: o, G* g
// Note also, that with the additional a7 ~& h! _: {5 S3 F! N3 Z% X$ ?& j
// `randomizeHeatbugUpdateOrder' Boolean flag we can. [7 B4 m+ v; b1 ~6 x$ n
// randomize the order in which the bugs actually run% R Y1 V( Q1 |& s
// their step rule. This has the effect of removing any
; U. \ |: v9 b! o- D- ? // systematic bias in the iteration throught the heatbug: x8 x( J& e; g3 `* o
// list from timestep to timestep
1 d* a' ^7 X4 Q# N( e5 m) r
, R8 l/ v8 @2 r // By default, all `createActionForEach' modelActions have& {2 l/ X2 ^5 U6 o b4 A" I# r
// a default order of `Sequential', which means that the9 s3 T ^$ y: X
// order of iteration through the `heatbugList' will be
# P/ T& w% I' j5 O+ Y0 t // identical (assuming the list order is not changed
" [! m5 U( h6 } b( |6 r // indirectly by some other process).
1 t- `8 D6 Q) e$ X- ?& V/ Z) ~ 5 Z1 `# ^) p$ e% M" b
modelActions = new ActionGroupImpl (getZone ());
' X% ?0 H. S( H+ c7 t% v& o6 e4 p, s0 }2 V" J
try {. M" I- o; V; {* N! h) Y: r: S
modelActions.createActionTo$message
6 Y' m, I" U c. u4 T" i (heat, new Selector (heat.getClass (), "stepRule", false));
& y3 |- `. H7 x6 c6 w" ^8 f } catch (Exception e) {
, x' b; U8 N0 i$ } System.err.println ("Exception stepRule: " + e.getMessage ());5 y# e. E9 C" p% D* ^
}
a6 t0 L2 H. @. C0 c3 h/ J. Q M+ b: U) [2 b3 u+ V
try {& n+ n# m' Z) I- _" W
Heatbug proto = (Heatbug) heatbugList.get (0);: @* j% u! \% U' e& Z) R" b+ b
Selector sel =
+ k0 z( _2 x* J- U( F" b6 w7 [ new Selector (proto.getClass (), "heatbugStep", false);# d2 c8 ^% A3 A. y* q
actionForEach =
9 v J) u, ^) c. d modelActions.createFActionForEachHomogeneous$call8 K# ~- z9 J3 l9 q/ ^! `
(heatbugList,
' h- o7 W4 S& J) Z% U& z2 o new FCallImpl (this, proto, sel,1 k2 y9 E5 V' i. f$ M7 c4 h
new FArgumentsImpl (this, sel)));
' Z2 P9 L: | `) p& V } catch (Exception e) {
* Y$ n' M1 z$ D- k- o e.printStackTrace (System.err);
/ E- a0 q* ~6 f4 ] }
: o- [: _9 J/ e P/ o0 X! P
' P2 D4 v$ G3 y* b& _ syncUpdateOrder ();/ a+ Z: _2 Q9 I5 w
1 l- T9 Z1 r' m
try {4 i5 Q$ L. O1 S T- G
modelActions.createActionTo$message % E4 Q$ e, W, Z! V
(heat, new Selector (heat.getClass (), "updateLattice", false));: M, C& M7 c6 g
} catch (Exception e) {0 |' X4 q" c( `5 K! R6 @1 t& E
System.err.println("Exception updateLattice: " + e.getMessage ());
0 H% E; C& V8 _5 M/ H0 I3 w }
+ [, a! F- k3 p- ? |1 N: {3 [
0 `* D% s- K' p+ p- d0 |7 n // Then we create a schedule that executes the0 |3 w+ | @# y# ~8 W) f
// modelActions. modelActions is an ActionGroup, by itself it
. \9 E0 E }6 E/ N4 D; \ // has no notion of time. In order to have it executed in
( ^- H8 x( L8 ?; u7 f/ A // time, we create a Schedule that says to use the
0 H) c( d* i( |" c; G A // modelActions ActionGroup at particular times. This+ v% ]6 M7 l. H4 a3 i9 s, y+ _' o
// schedule has a repeat interval of 1, it will loop every9 H6 N+ |$ m( x* X) c e+ J* b" L
// time step. The action is executed at time 0 relative to
2 s* M; P% n% S/ C+ Z. M // the beginning of the loop.8 n! g( [' m5 G/ s2 ]' p0 B0 l) f
7 \8 r0 j4 |2 K! G
// This is a simple schedule, with only one action that is
" a9 |5 F2 {- I9 v // just repeated every time. See jmousetrap for more
% R8 }$ k. s/ T; u/ d" L) Q( p // complicated schedules.
3 z: u3 F$ U( ~
5 N! U4 A% D2 i modelSchedule = new ScheduleImpl (getZone (), 1);& P# c5 n6 x! g
modelSchedule.at$createAction (0, modelActions);
( A8 b6 B- k* L+ x) J ( T9 z: Z- O; {. y
return this;
; ^( D) h% j- V8 G3 m } |