HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 b, {* [' M' M
/ s* d9 N/ E( e! Y
public Object buildActions () {3 p6 X7 P2 ]3 p, i: o, w* z
super.buildActions();
6 k: n' [' X5 p$ D } % J2 E5 O$ A" ^) J6 r! b7 g; K
// Create the list of simulation actions. We put these in
p+ K8 M! W" H // an action group, because we want these actions to be
( U$ ~/ t4 B; q; \* X6 j // executed in a specific order, but these steps should; F3 F3 {9 V$ M
// take no (simulated) time. The M(foo) means "The message
7 Q) j2 v: L! s, O, w; \* e- G // called <foo>". You can send a message To a particular
/ x- ~# M/ n/ |8 Z2 S3 \5 ]5 H8 c // object, or ForEach object in a collection.
) q0 |+ z' T4 Y( E/ s4 s' U - C) K9 }5 W. K
// Note we update the heatspace in two phases: first run! Z! V, l) M8 ]5 G9 i/ |
// diffusion, then run "updateWorld" to actually enact the+ y% T7 ^4 c2 U. F. ^
// changes the heatbugs have made. The ordering here is
. m* P3 d0 e* r6 y2 o // significant!
7 {. L5 g8 R2 y @( t3 H4 t 4 _* {* |. w( a" A" G. ~4 ~3 f
// Note also, that with the additional( q5 F2 |7 Q( M& b: S
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ [1 X$ W1 i2 |% S! j
// randomize the order in which the bugs actually run
$ A8 Y: E% Y3 s // their step rule. This has the effect of removing any
$ o7 y; s: B# i R, Y // systematic bias in the iteration throught the heatbug8 F) q3 a+ D( l+ i" y2 D
// list from timestep to timestep
8 Y4 t: U/ D$ V
' J# d, b4 u& e, c/ |" l // By default, all `createActionForEach' modelActions have
# W/ e X0 P# v2 E // a default order of `Sequential', which means that the! } J7 |% e* V1 [
// order of iteration through the `heatbugList' will be
8 m# j& l. {: m8 _8 L // identical (assuming the list order is not changed
# Y9 D' h% u5 v' O0 F // indirectly by some other process).) E* M8 h, v' v2 P- W2 k& z5 e
& O( F& f5 }1 u. d modelActions = new ActionGroupImpl (getZone ());
9 E! o0 D$ }+ B; o8 b3 p2 x* f5 {9 n- r9 T4 p" j5 x! A
try {
2 l8 A% h( Z% t: k modelActions.createActionTo$message/ N$ ?% e/ |2 ]- [) S7 U1 ]9 P
(heat, new Selector (heat.getClass (), "stepRule", false));
n5 U6 F! m5 O1 w } catch (Exception e) {7 A6 a' k/ F: c; G3 g' }
System.err.println ("Exception stepRule: " + e.getMessage ());
# n. j) Q) f, [. x3 G( F& x# o$ W* \3 { }
/ z. j& w# S G( y6 q2 Y8 B; d* o* a& i% c% ~5 H& W7 z: \
try {! F; o! g8 A9 }7 Y" C; I, H
Heatbug proto = (Heatbug) heatbugList.get (0);
7 U6 g/ G5 N* \* L( [0 K; P Selector sel =
' Y+ _8 H* ~ f. T* O7 F. ?- m+ I new Selector (proto.getClass (), "heatbugStep", false);
' e: J$ _0 Y* K actionForEach =
1 ?2 R. e9 Q3 g modelActions.createFActionForEachHomogeneous$call
3 Y5 r2 u- s% {5 w; o( B (heatbugList,* a) n& |) J6 ~/ p+ d
new FCallImpl (this, proto, sel,
. F8 X' I, E) |/ m6 N( u new FArgumentsImpl (this, sel)));2 o( w3 b! M c# v
} catch (Exception e) {; q! ^4 I1 Z$ _6 M# B. ^
e.printStackTrace (System.err);
1 T- w5 A' {$ _) n }
/ D/ ^2 [7 W0 {0 r* m
$ h: J! A5 S+ t# @* q) F( s& A% J syncUpdateOrder ();
8 I& _$ d, ?- r
/ n, t% ~+ L/ [1 q! ` try {7 h0 f& E0 f1 m. d
modelActions.createActionTo$message
# R# ^" T3 @" G$ V1 I (heat, new Selector (heat.getClass (), "updateLattice", false));0 E+ }! y6 ]' N9 i- L9 X
} catch (Exception e) {
- `$ H7 T$ I; p' |. U. r) b System.err.println("Exception updateLattice: " + e.getMessage ());
6 ?6 b; q* t( c) Z) d: K }3 i; L A7 N1 U4 i3 V
9 D F1 C1 }7 Y% X# M
// Then we create a schedule that executes the2 r9 d- x7 o# X8 J2 A; w
// modelActions. modelActions is an ActionGroup, by itself it
L( B" p% V: v2 X" ]3 G1 @ // has no notion of time. In order to have it executed in
( X9 H! N. x, S, Y/ q8 E7 H5 c2 r // time, we create a Schedule that says to use the
( r/ V% z8 M3 q' @9 n // modelActions ActionGroup at particular times. This0 V8 H3 [! u' k0 d* v" h
// schedule has a repeat interval of 1, it will loop every
* y0 |0 n- ^2 v8 B$ p* @- J // time step. The action is executed at time 0 relative to5 G6 Z U, T2 o/ T4 ^
// the beginning of the loop.! M0 Z1 R3 W6 V7 y' s
8 v1 t/ A- Y: q/ K // This is a simple schedule, with only one action that is
0 A. ]% A3 e8 V // just repeated every time. See jmousetrap for more, t& x: D4 i, k+ X3 y: |
// complicated schedules." w7 {% v3 e) }" [" s$ T t) W6 E
; D# r' g+ k5 t modelSchedule = new ScheduleImpl (getZone (), 1);
P+ g) r7 E1 G, j8 p$ }( O6 @ modelSchedule.at$createAction (0, modelActions);# A8 @+ B: h( m# v
4 U; r6 y- _ @3 ? return this;
7 J5 A+ V* E' P7 Y! q1 C } |