HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# K" ^! m+ Y- @
/ Y' J& d( p$ F- v# ]
public Object buildActions () {6 T. S: c/ U4 x s1 p. `8 D! \
super.buildActions();0 i$ Y. M8 s5 i1 c8 a
' F! V1 o$ K: G& B1 T* h! g, i // Create the list of simulation actions. We put these in' P% [% D7 Q) H g% ~
// an action group, because we want these actions to be
7 R5 u9 L$ J9 K4 C // executed in a specific order, but these steps should
2 e. q) A9 |( d% r6 v) z& W // take no (simulated) time. The M(foo) means "The message9 u( t; C+ }& U5 ~/ _: w
// called <foo>". You can send a message To a particular9 R; o( S q- L/ [/ O D+ ]7 o
// object, or ForEach object in a collection.# f$ H# _2 R, ^+ o4 p
2 [& i# D3 L, N0 _ // Note we update the heatspace in two phases: first run0 q- |! m3 g% b$ u: v6 r$ x* l1 N
// diffusion, then run "updateWorld" to actually enact the
# t) s7 J7 j, p9 d // changes the heatbugs have made. The ordering here is3 L2 t- v0 S L
// significant!4 U# R$ Y- ?$ d3 R# K z& E
# D1 @: w7 j) L5 l" x9 K7 Y' u1 v
// Note also, that with the additional5 D t2 _ j( j$ ?, s9 j- G0 a$ G
// `randomizeHeatbugUpdateOrder' Boolean flag we can- W& _0 S( }. q
// randomize the order in which the bugs actually run
7 i! p! C( |% s* q" i7 x+ a, \ // their step rule. This has the effect of removing any
1 m& D+ ~$ Q. y6 T# }2 @# v // systematic bias in the iteration throught the heatbug7 \9 Q1 _7 W3 ]& O
// list from timestep to timestep
, y$ o0 D& s; K. D- O z/ W
- M7 s% T C( p4 d // By default, all `createActionForEach' modelActions have- \% N1 E# p9 u! B+ `* E) B
// a default order of `Sequential', which means that the* i; N; D/ @% e% V# h7 C) C. a/ l
// order of iteration through the `heatbugList' will be
" M2 n) D* U/ U }/ n$ U& u' ~ // identical (assuming the list order is not changed
* G% ^6 B: ^1 I1 y // indirectly by some other process).- j* `1 } t& t! [ f. K
$ X- L% [# Z( }; E; x! R0 @
modelActions = new ActionGroupImpl (getZone ());
/ g7 H3 I/ W3 W! J
( ?& w" S. J3 ^ |$ E j1 y try {
9 z) r7 j3 M/ ~. } modelActions.createActionTo$message
# Z! _7 i5 F+ }5 S3 E (heat, new Selector (heat.getClass (), "stepRule", false));( S- X. y/ V7 r) _& D* m" X4 U
} catch (Exception e) {, n! f- ]- k& p/ i/ d% F. U
System.err.println ("Exception stepRule: " + e.getMessage ());
+ E; Y, ^: h, ^) M( M }
8 W M. \! q; @% e7 D6 ~+ c2 `1 y% v) z E
try {9 Y$ V1 F* Z* H) G: p/ r, Y' o6 V& _
Heatbug proto = (Heatbug) heatbugList.get (0);# p7 c* Z0 B0 R7 T
Selector sel = ! H3 ], k8 h! u0 x+ Q
new Selector (proto.getClass (), "heatbugStep", false);
/ h4 u( Q# F3 x) X actionForEach =" W3 L ^, N K8 U. m
modelActions.createFActionForEachHomogeneous$call; H/ r# {* v I. s
(heatbugList,# ?0 M1 o7 w+ s. ` F
new FCallImpl (this, proto, sel,' i& y8 A$ M/ O6 h5 f! o
new FArgumentsImpl (this, sel)));8 o: ?: q) H t; T8 z' A; D- ~0 t8 n
} catch (Exception e) {3 S& H# \, D. y$ Z% i3 i
e.printStackTrace (System.err);. @ F' f& k; B& ]% ~
}! H, ?; |' Y. m7 X+ {4 S1 |/ n4 y
; A* Z/ D# h% w
syncUpdateOrder ();( V( N, H( y$ d5 f2 o2 P }! O5 H2 \
& z% z$ g$ t4 J$ g5 p try {7 z& Y$ t( o7 @. @; h) X ?
modelActions.createActionTo$message
5 D. V4 u5 G9 @( a0 A (heat, new Selector (heat.getClass (), "updateLattice", false));
8 [3 C: E3 x* \" G" T& P) F } catch (Exception e) {
$ \+ T3 J3 G' v8 I. U4 A* ]3 y System.err.println("Exception updateLattice: " + e.getMessage ());
& }; i; Q7 p8 o H }
; M1 H& K- l( @) H. ^4 i: M 2 k& f- R9 \; m- G2 C" V' y
// Then we create a schedule that executes the8 D5 ?7 k% m' Q1 z' w+ J; s4 V
// modelActions. modelActions is an ActionGroup, by itself it) `$ f- C0 a d5 p& E
// has no notion of time. In order to have it executed in
3 |9 W$ v) a0 f% U R // time, we create a Schedule that says to use the3 U2 W. W' R. Y) s! O6 H
// modelActions ActionGroup at particular times. This/ b) c6 V6 R- E! c; l
// schedule has a repeat interval of 1, it will loop every3 n+ K2 v( k2 n4 r
// time step. The action is executed at time 0 relative to
' _$ A7 l/ W% J- ]3 E) N // the beginning of the loop.
0 {! p2 b, |7 M3 D% [: S
, Y( ^) _/ b' i6 w // This is a simple schedule, with only one action that is
9 w& @- E+ }3 q$ X A! J // just repeated every time. See jmousetrap for more
. m6 m h3 v y- y" F; z // complicated schedules.
# J1 Q3 U) @' L. O0 t6 }& T $ N: o' C8 S# j* X
modelSchedule = new ScheduleImpl (getZone (), 1);% ~( F4 ]+ e1 `- n+ y! r
modelSchedule.at$createAction (0, modelActions);
0 i1 i% {+ ]: e$ x0 q/ S# m
o$ b$ W1 H2 K6 |; `! E) {' U& g return this;9 U' ]7 U# i" R) z
} |