HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ t0 c% z0 q( `+ \) Z. L
1 _' L3 ?% N8 b, f) {2 c* u public Object buildActions () {; r/ |- {8 Z6 v' y
super.buildActions();( Y4 l- h! l) p. K4 J N
; W6 B5 Y" S8 y5 G* K# L // Create the list of simulation actions. We put these in/ K6 m. O! K- F* D7 `7 J1 O& F
// an action group, because we want these actions to be
/ O2 i6 `4 n2 q2 @* X/ ^& _ // executed in a specific order, but these steps should9 P. O6 b' {+ q2 Y& h
// take no (simulated) time. The M(foo) means "The message
' g9 x3 G6 p7 d( b& R5 @0 A' v a // called <foo>". You can send a message To a particular. c/ L* M* ?' H3 ~: \
// object, or ForEach object in a collection.$ S9 w; H. R8 ~- C8 d7 Z# @6 r
4 V! O3 v: k( _ // Note we update the heatspace in two phases: first run# ~8 _4 ~8 g. i" n4 O
// diffusion, then run "updateWorld" to actually enact the5 Y; N$ m" Q6 f8 M5 I, ?
// changes the heatbugs have made. The ordering here is
a* |; m5 ~9 \( j // significant!$ i w- o' ?) n6 F8 r+ _- c# b
, g+ V4 W F2 z/ @) s // Note also, that with the additional
# S2 Q& }/ H& j4 L U( u7 ^& r L // `randomizeHeatbugUpdateOrder' Boolean flag we can5 P. W1 \" Z1 `8 t: `8 \
// randomize the order in which the bugs actually run# O3 G: L6 U( c1 F7 ?
// their step rule. This has the effect of removing any5 Z- T. K r0 x8 ^, p0 H' f( B* d
// systematic bias in the iteration throught the heatbug: r& [# M' f0 }: C/ _
// list from timestep to timestep9 N2 T) e+ b+ ]: _ V; z C
+ D+ G2 J8 @6 j" y8 r // By default, all `createActionForEach' modelActions have& r- g6 V. S, B W$ q+ L
// a default order of `Sequential', which means that the% d. a/ p, C8 |! U* S% R1 P
// order of iteration through the `heatbugList' will be" B, V8 v# F9 A
// identical (assuming the list order is not changed; }, N, @5 {; l; q5 @7 Q
// indirectly by some other process).
E$ t3 _# f6 U4 M9 E* m& `
1 Q1 g0 W, b1 E* E6 Y modelActions = new ActionGroupImpl (getZone ());
' u1 S: e( l1 h' W7 f. ~, F6 v/ M7 q
try {
+ D* J; J) U$ g: I! g5 I! A; s modelActions.createActionTo$message# i, I n; t7 r( g8 R; `
(heat, new Selector (heat.getClass (), "stepRule", false));$ A- Z, w6 Z& K7 y+ v: }. G
} catch (Exception e) {: |7 L. m: y l1 R' E! I
System.err.println ("Exception stepRule: " + e.getMessage ());
! S0 e$ C f6 E; q! J }
4 `& G- T6 Y8 w7 p6 N5 i2 w7 U7 C
; v7 I2 Z0 |) } C" e& d, G" A try {
5 z7 E0 k9 S* z9 [7 K Heatbug proto = (Heatbug) heatbugList.get (0);
* n+ o: g t- W( W Selector sel =
6 T) [* r% f" [& N" b new Selector (proto.getClass (), "heatbugStep", false);
& |: s* X$ p O* ], q actionForEach =
6 A. d4 ?, i8 |6 b- s modelActions.createFActionForEachHomogeneous$call4 ]. a! S& W; _, o" @1 H" L5 y, A
(heatbugList,
d V3 S$ Z2 Z6 C! X8 `7 V* G new FCallImpl (this, proto, sel,
' F. x7 h7 l6 ^) s0 o# d/ Z new FArgumentsImpl (this, sel)));
[1 w1 k3 `$ c" Q( E% | } catch (Exception e) {1 V) R& v/ f) u B& ^ U4 q
e.printStackTrace (System.err);( v5 z! X* s1 k2 y
}: H( K2 }8 f% t9 o
$ @; k5 B( g8 m. B syncUpdateOrder ();
$ |, G3 j% X2 R& _3 E4 S
, F+ a' @. `1 u$ e X try {: ~( q3 d/ \3 [& p' N' I; x7 p
modelActions.createActionTo$message
& Q. w2 C. o" Z# _& R4 r) l4 b (heat, new Selector (heat.getClass (), "updateLattice", false));5 h- Z( q* C2 X
} catch (Exception e) {8 \1 x0 c; W+ A- e/ l
System.err.println("Exception updateLattice: " + e.getMessage ());) X+ [; R& }5 M* g C
}( `) W5 i4 A1 x) b; l
. W. g9 H J9 \% I: h
// Then we create a schedule that executes the; a4 G) o4 C* f! N" Q5 a& m6 w
// modelActions. modelActions is an ActionGroup, by itself it
! h+ w8 }0 y) @: X7 | // has no notion of time. In order to have it executed in4 B) O0 y3 L0 l' Z8 v
// time, we create a Schedule that says to use the4 b! L2 _! V8 \" r# a( S( M
// modelActions ActionGroup at particular times. This
, v+ R+ _% t) ]# S // schedule has a repeat interval of 1, it will loop every. \2 @* A j' N0 i
// time step. The action is executed at time 0 relative to
5 o* F7 q, D7 j* ~ // the beginning of the loop.
& U2 a9 F) A9 h. C
( }7 V1 U7 v- C+ s // This is a simple schedule, with only one action that is8 |1 q7 k5 I+ \, ?- n8 p
// just repeated every time. See jmousetrap for more
5 M! c4 M& D) I; m n# x0 O2 _ // complicated schedules.& L5 Q5 B8 j0 {- N9 K( A2 M* ^
) }, R# r9 G$ w" w
modelSchedule = new ScheduleImpl (getZone (), 1);5 U, W; W! T) f7 ]' b( J3 l
modelSchedule.at$createAction (0, modelActions);. y# l3 B- @6 S8 M H
. ~# B" l/ m) H, ]* B C. ~0 X return this;% F& B$ G7 I5 G/ i* u' N
} |