HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; r7 S" [' r! f# B
) f7 a/ }* o& b1 Y K public Object buildActions () {
8 @6 Z+ y0 C$ V0 Z super.buildActions();: g/ \9 X; N+ L* L8 Q( @
' x, N" P; [, m5 S8 @4 l // Create the list of simulation actions. We put these in6 n( Z b: W0 X( {
// an action group, because we want these actions to be" K7 x$ \0 P% n. b: i
// executed in a specific order, but these steps should
, [0 m; c! q. T- k' q# r+ T8 Y // take no (simulated) time. The M(foo) means "The message
) j' M9 r. V# u2 ~- h // called <foo>". You can send a message To a particular
, q4 C6 I5 |* i( }7 k // object, or ForEach object in a collection.
/ _. C, e$ t7 s : S8 R1 _% T- O4 z" n& K [9 C' A6 h; f
// Note we update the heatspace in two phases: first run8 s. T) M! f( J( |1 R5 q
// diffusion, then run "updateWorld" to actually enact the7 b. s. b" p) v4 Q! j
// changes the heatbugs have made. The ordering here is
5 }% e6 X- I6 `4 q, P // significant!
% ^ E% l. H5 B9 z! X$ K( p( x5 {4 s # |% H0 r E0 k5 k( L! c
// Note also, that with the additional9 W9 x' C! f1 C0 Y( X
// `randomizeHeatbugUpdateOrder' Boolean flag we can
8 n3 b; q) p# |3 E' | // randomize the order in which the bugs actually run
& `6 r5 ~, Z. g( B0 y6 J7 P // their step rule. This has the effect of removing any
L3 K$ Q5 N1 D5 a) t6 i: z // systematic bias in the iteration throught the heatbug
4 W V! Y! n# ^2 P- b$ m f // list from timestep to timestep; a4 k# C: w: `' I' r Z! ]
. o$ q ]$ W4 U% c0 b3 A // By default, all `createActionForEach' modelActions have
+ }5 n" `( w. q: L: O // a default order of `Sequential', which means that the1 y0 ~$ G' l# O, E, S: q0 U
// order of iteration through the `heatbugList' will be
) T) o/ Q1 t7 u1 j // identical (assuming the list order is not changed
% H) T [; M3 I7 _9 n6 P8 O2 H // indirectly by some other process).
4 g5 Y* `5 F% y- {8 H5 i) f9 @ + H) w7 L! J& ?0 T
modelActions = new ActionGroupImpl (getZone ());
/ D8 H+ F. x) l& f# v1 I) b7 ]& ]2 |7 K2 Z& e9 N8 q6 m
try {
+ y2 c5 }1 d8 Q; v modelActions.createActionTo$message
4 S* F% L: ?$ v6 } (heat, new Selector (heat.getClass (), "stepRule", false));! ^( R `; s: G, E/ H
} catch (Exception e) {
0 p! }9 [, b3 ~ H System.err.println ("Exception stepRule: " + e.getMessage ());
: m( I5 U. U$ V. m3 ] }$ h( z. F, H0 n# Q0 @
; b3 ?* }! R3 G try {# H6 O' c, D( O- y) t
Heatbug proto = (Heatbug) heatbugList.get (0);
& `/ t6 P* [' O3 ^9 R Selector sel =
. ~ _3 M* L z0 @6 O w$ { new Selector (proto.getClass (), "heatbugStep", false);$ c' h0 h( i! Z
actionForEach =
* n% B* i! B7 H' U modelActions.createFActionForEachHomogeneous$call4 j+ p( G; E$ I# T" C
(heatbugList,
) ]8 z8 F9 c7 B' } new FCallImpl (this, proto, sel,
& Z: `" i! L* U/ w, W3 D: ]. g new FArgumentsImpl (this, sel)));
6 ?" C. B: m+ i9 @ } catch (Exception e) {
) z ^: ~$ x6 D e.printStackTrace (System.err);
8 B( M6 V p& g, e& A }9 x8 o- B3 _' m* d m# F* t
+ b: |! W$ ?& Z9 t% ~
syncUpdateOrder ();" u v6 \3 E' x
$ g( c4 N% s) p try {
/ N* d; Z3 ]: c1 j5 y& g9 Y* m7 A modelActions.createActionTo$message # w2 G* c" q* p9 K" z* `3 d
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 z m/ R. R$ C+ b* v( r5 J4 {2 L } catch (Exception e) {' O! }) F& Z/ a, U, z# U8 m4 W
System.err.println("Exception updateLattice: " + e.getMessage ());
& Y; h! y: r/ ?6 e& _ }
! W' W4 q4 z3 i4 s7 I% b
- t7 X6 P+ c3 g // Then we create a schedule that executes the7 _ ~7 f: L6 t
// modelActions. modelActions is an ActionGroup, by itself it' b, {0 |9 l" k/ z9 N! o" D
// has no notion of time. In order to have it executed in! P' T4 r( w# e0 N3 R5 i9 k
// time, we create a Schedule that says to use the
5 t4 c3 u q, o# C. E' G$ k // modelActions ActionGroup at particular times. This6 i7 v7 e! o* {, |! m! P: v
// schedule has a repeat interval of 1, it will loop every2 `/ b' T6 E( u4 {9 [1 Q
// time step. The action is executed at time 0 relative to9 I; {6 l8 r0 p
// the beginning of the loop.
8 M7 E& Q+ D: T, N) a: ?
) p3 [* U% o! Q4 e" o' Q; ]8 b // This is a simple schedule, with only one action that is5 E" c# o) |1 Y7 Y E/ N
// just repeated every time. See jmousetrap for more% `4 l8 Q- C$ q' _$ p
// complicated schedules.
& \! A) o8 l4 Q( P9 p/ M
: W2 z U+ C, S4 K0 ]0 O modelSchedule = new ScheduleImpl (getZone (), 1);' c2 \0 b d; d
modelSchedule.at$createAction (0, modelActions);* \$ S# j0 D d3 {
* F+ h5 O% F( \; d ?: P- `5 }! z: [6 Q
return this;) [5 o8 e Q6 u3 c7 Z
} |