HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ v. a# @- D" @& Z6 R u
* G9 ?; J0 a1 l5 b5 A3 E public Object buildActions () {; d, R$ ^8 L" M0 [4 |$ ]( z+ Q9 h
super.buildActions();# f/ J5 Y0 l. @2 N/ f* G4 }, S1 _
( E$ b* ?/ K, a d& g
// Create the list of simulation actions. We put these in
$ S8 \9 }4 |, P. u/ e) L9 S% R! U2 c // an action group, because we want these actions to be
- g6 _7 d, w8 z7 L // executed in a specific order, but these steps should' y% [" \. u) Q+ u3 q) h/ B6 K
// take no (simulated) time. The M(foo) means "The message6 F" U. S# K9 A F$ y6 L
// called <foo>". You can send a message To a particular
) N* ]' y; }3 m$ b9 V* w // object, or ForEach object in a collection.- l7 N9 ]" j9 A9 W9 a3 L6 I
- x' }# d7 e0 J/ _+ C1 L
// Note we update the heatspace in two phases: first run
2 t8 t- Q0 i/ r3 ]; y; m) m // diffusion, then run "updateWorld" to actually enact the
4 {$ R5 m1 o( y4 {2 O! n // changes the heatbugs have made. The ordering here is& n* ?6 J$ [+ K8 ]4 u5 {
// significant!
7 Y1 s/ o( W2 K% W
$ O% ~2 U; \- a3 n // Note also, that with the additional
( M- E. q; | P8 t! {) u // `randomizeHeatbugUpdateOrder' Boolean flag we can
' C0 l0 e5 I- c2 [$ A. z // randomize the order in which the bugs actually run
: g- z0 I9 }4 B* l // their step rule. This has the effect of removing any8 `% ~4 E# r* ]7 ^
// systematic bias in the iteration throught the heatbug9 R7 S% j- \5 W# K
// list from timestep to timestep1 s1 {* k9 Q+ W% j$ y
' _" g. D# Z6 Q" A) R" f" _) O
// By default, all `createActionForEach' modelActions have( z5 J/ J) u* ?+ @6 _( p B
// a default order of `Sequential', which means that the3 X( |( {( l5 Y# i7 Y0 u4 t/ h
// order of iteration through the `heatbugList' will be1 _3 Y4 e/ ^$ m
// identical (assuming the list order is not changed
5 d6 X5 k3 u4 `2 Z2 g) A // indirectly by some other process).# D" X% C; `( z( o- q
. c) x4 e6 X" C7 Q4 K% P7 ~8 v" f0 w modelActions = new ActionGroupImpl (getZone ());
O- { K* I; ]2 r; Q/ E2 e9 h5 ?! |" a' P2 h0 X& T5 g; h
try {0 Y' }& K' n% o9 h
modelActions.createActionTo$message
3 ^2 o4 ^8 [- P" s. P7 E (heat, new Selector (heat.getClass (), "stepRule", false));- u, I* L/ N# F: @2 k, O
} catch (Exception e) { ^( U9 \2 N' C% ]( w
System.err.println ("Exception stepRule: " + e.getMessage ());, p# S; J7 }, |+ b. V& B6 A8 ^; _3 m# R
}$ V7 ^' t+ B: [
6 Y- h/ b$ t* k, K: J! N# z" y P. F* q
try {4 s3 t E/ U& K% ^5 \' [6 [' h
Heatbug proto = (Heatbug) heatbugList.get (0);
+ H" o0 y: Y- ?, ` Selector sel =
9 i# z0 a0 ]) V* W new Selector (proto.getClass (), "heatbugStep", false);1 O4 W" Y1 K. s5 x% M- N
actionForEach =0 Y' k& J+ ^5 K5 D1 E6 R
modelActions.createFActionForEachHomogeneous$call
5 ]6 y# g% R. e (heatbugList,
* o3 ]& W# l/ D1 I$ n8 K new FCallImpl (this, proto, sel, l/ @0 [5 `3 v* P* Z {2 t0 }' o
new FArgumentsImpl (this, sel)));
+ n9 Q9 S% n4 J6 @ } catch (Exception e) {
. F) d7 L* X0 R# C% }% W8 z e.printStackTrace (System.err);0 J3 I( h3 [4 }* l+ t/ o4 S
}
. n9 W X" k" A2 ? ! p4 v+ @' Y) p5 s
syncUpdateOrder ();2 H; K8 K Y( v
& J" a. T; u6 [0 }1 E9 n. e5 k! S try {
! ^" G5 N9 O1 Z0 M modelActions.createActionTo$message
+ Z& X% f' |4 V9 ` S2 L% P/ E (heat, new Selector (heat.getClass (), "updateLattice", false));5 z% O+ R& e9 ^6 v
} catch (Exception e) {
: n0 q( Y9 I% k System.err.println("Exception updateLattice: " + e.getMessage ());
6 ?; l0 _3 h3 G: @& E }2 a% ~/ I; m3 a2 G5 G
8 y) v# c8 I& P$ J // Then we create a schedule that executes the4 e4 \ n+ W8 E8 X3 ]: }, W- g
// modelActions. modelActions is an ActionGroup, by itself it q0 u8 _8 r1 ^& l2 d
// has no notion of time. In order to have it executed in& G* L! F5 u/ r: h! `
// time, we create a Schedule that says to use the- Z$ C( e6 n+ ?9 A4 o) n
// modelActions ActionGroup at particular times. This
& l+ {# M( f9 X/ o" M, I // schedule has a repeat interval of 1, it will loop every
7 Q( ~) D& T+ M- ~% y' j // time step. The action is executed at time 0 relative to8 v& ]. S) N2 R# z
// the beginning of the loop.& y1 g) M q: g: X: B
' S: F* D* Q9 j- S! w
// This is a simple schedule, with only one action that is
+ u8 ~/ o# r P5 E, E* }5 }5 \ // just repeated every time. See jmousetrap for more0 R- e% ]8 Y: D( c
// complicated schedules.6 Q3 P" r; b+ _3 n' Q
4 A) p+ _, Q, J- L
modelSchedule = new ScheduleImpl (getZone (), 1);
" S' U0 d5 \7 R# x' x modelSchedule.at$createAction (0, modelActions);
0 D/ h* {" b# U0 Z 1 i/ C$ K2 F0 e/ z2 B' l6 S
return this;
9 Y$ }2 y% h+ D8 H) a5 d2 m } |