HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
f" H7 B2 R' [3 c% j$ E5 x x
! i- ^1 @2 Y3 l" p* [5 c( N' r public Object buildActions () {
& K/ M4 f7 m8 w2 | super.buildActions();
, g2 s9 H! r/ H; j$ S0 W4 g
; g6 G) L" e0 K$ e9 o) T* B // Create the list of simulation actions. We put these in
: q% w9 x1 `- _! x0 O) _9 b1 s // an action group, because we want these actions to be
' j. F t1 ]. O // executed in a specific order, but these steps should: r: p1 k/ d2 \: v7 ?$ O; M+ B- z
// take no (simulated) time. The M(foo) means "The message
- v) s6 [- S! }" x! w // called <foo>". You can send a message To a particular& c8 D% i/ h& ^2 T6 t9 Y! p# L
// object, or ForEach object in a collection.
' m9 N/ c4 n- j
/ p3 G* z' {9 ~* o1 R // Note we update the heatspace in two phases: first run
+ s n# d/ s) x+ q: F // diffusion, then run "updateWorld" to actually enact the: x% z4 C7 h5 \1 E
// changes the heatbugs have made. The ordering here is8 U+ L5 E4 U. b, \, b3 g& G8 \
// significant!
5 P1 N: {& K7 y4 W% d6 { p 3 s2 l a( C; N7 W3 k
// Note also, that with the additional
1 @5 _) C6 J' G/ `3 a // `randomizeHeatbugUpdateOrder' Boolean flag we can+ l$ i0 y% q" g: w/ M0 q
// randomize the order in which the bugs actually run
4 ]! `: ]3 W- ]* _( t3 |& y // their step rule. This has the effect of removing any* N: B3 w% r, y8 q H; r
// systematic bias in the iteration throught the heatbug
/ b# s0 [ r! S4 v // list from timestep to timestep
& {: r' [ j+ F; }5 N2 @- ?
c" h/ G5 n' p- ~" a( y // By default, all `createActionForEach' modelActions have G& z7 v+ H: t" B' ^- H5 l
// a default order of `Sequential', which means that the0 }. [& } H" q, ]
// order of iteration through the `heatbugList' will be
# r# |9 Z7 y0 w# o // identical (assuming the list order is not changed3 k2 t! w8 e9 P
// indirectly by some other process).
# ?$ z5 ]$ A2 S+ \ 6 }$ w8 H* A$ k, L* k8 _
modelActions = new ActionGroupImpl (getZone ());
: t4 f, J* G4 c6 @+ R5 \/ y4 ^6 X( x
try {
' G3 b! s/ m8 p+ E9 |! o% Y modelActions.createActionTo$message! U. y) E+ t- m6 f) Q
(heat, new Selector (heat.getClass (), "stepRule", false));5 w/ u: R2 {& R0 [) S) R/ k; p
} catch (Exception e) {
e( |4 B$ H7 H3 _, `1 k ` System.err.println ("Exception stepRule: " + e.getMessage ());9 x+ V7 W% {) s1 `7 Q( d
}9 n% Q8 ?+ d! b1 V. n9 }3 y& F
' b/ v! F8 K1 s' {" d, k try {: J% w2 W! |+ J
Heatbug proto = (Heatbug) heatbugList.get (0);
* w1 G K9 L, ] F Selector sel =
; j/ X6 j& l! l8 V% t: P new Selector (proto.getClass (), "heatbugStep", false);* c9 Y4 @) y V5 @
actionForEach =
+ {4 ]/ P; k+ l2 x8 j modelActions.createFActionForEachHomogeneous$call) _; }8 w9 o2 F
(heatbugList,& T4 Z) X$ f' o1 t
new FCallImpl (this, proto, sel,
, G3 @! L6 g1 A. B2 L: ]1 X new FArgumentsImpl (this, sel)));4 N6 I6 Q1 Q" t# S2 s9 Y) W- [
} catch (Exception e) {
( ?. f8 D7 ~, Q9 ~8 c e.printStackTrace (System.err);
. s8 }# A( N: F$ `6 f }6 g0 h1 p6 Y8 Y
$ V% Q; ]; o7 E- L. N
syncUpdateOrder ();
- U, N: {& l/ H5 b# M
8 x. a5 ?8 w2 q/ g7 Y$ L- f+ J R$ u try {
8 Q9 e- e9 M- Y I modelActions.createActionTo$message ; S; h$ u9 M) y: {1 Y/ q
(heat, new Selector (heat.getClass (), "updateLattice", false));. v; c) T' i5 x0 P
} catch (Exception e) {8 l5 D# n. l& Q
System.err.println("Exception updateLattice: " + e.getMessage ());$ D4 m' u% E; O9 R; `, C9 Z
}/ d7 G5 E8 ?8 j' N# I
% o( }5 V/ _( _9 z2 H$ }: `
// Then we create a schedule that executes the, i5 U. H4 x m5 W4 x! M' c7 J
// modelActions. modelActions is an ActionGroup, by itself it4 z5 Z! w# D% i3 F T B
// has no notion of time. In order to have it executed in8 [/ w! P9 N% p
// time, we create a Schedule that says to use the3 d; H9 w W# `: a" m; ?$ ~ J% [' [% h
// modelActions ActionGroup at particular times. This# B$ U/ H2 c8 v3 q
// schedule has a repeat interval of 1, it will loop every, Q" Q7 p. V- s T
// time step. The action is executed at time 0 relative to7 G! N- f' Q' {5 C0 a1 c
// the beginning of the loop.
% F- j$ I; G0 ?; N& t; W6 f3 p- M0 P8 }. y3 ?0 V2 A
// This is a simple schedule, with only one action that is
$ m. J5 ]& t f% e8 | // just repeated every time. See jmousetrap for more
/ ]( \: H" z8 O // complicated schedules.
% H7 g% k: {6 g
& p; }1 I, e% [( { modelSchedule = new ScheduleImpl (getZone (), 1);7 ]( s2 B n0 g8 t' m
modelSchedule.at$createAction (0, modelActions);
1 R$ @4 c* K* Z6 y2 Y4 H% [) A : P! f1 P# L- {% [9 |/ O! r9 R8 R
return this;
1 M. G3 f) o0 |* E7 u } |