HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 i/ Q5 q9 N; g- z! q. j1 P+ N
5 |1 X% i+ m2 r4 @* |* |6 L$ R' P3 y; Z4 S+ m public Object buildActions () {7 V9 S8 E8 X( J, i3 ?- f
super.buildActions();) j1 f, w# \4 O1 }
: S+ q% G1 P/ ]$ U2 ^+ p // Create the list of simulation actions. We put these in
, [& n% K4 P! F7 z5 k! O // an action group, because we want these actions to be
9 o- O+ T- K% \; o& E // executed in a specific order, but these steps should3 m. [/ S8 a0 _1 [" O8 q7 L
// take no (simulated) time. The M(foo) means "The message
* e& X3 i5 `5 o e- B E // called <foo>". You can send a message To a particular+ _, W. _4 K& v
// object, or ForEach object in a collection.
: Q* F* b+ E, t5 N! S. L
, X2 g9 v3 y$ U0 z, J8 {, y // Note we update the heatspace in two phases: first run
0 P# M3 `5 n* a4 S/ Y$ l0 S // diffusion, then run "updateWorld" to actually enact the
, B* @1 b; f5 w! F // changes the heatbugs have made. The ordering here is4 p1 t; x1 V/ W- \1 \8 X1 N( V a
// significant!" u# |6 N8 N' Q2 U
1 W. c' |! \# P; d
// Note also, that with the additional) b) Z7 \4 D C, ~: W
// `randomizeHeatbugUpdateOrder' Boolean flag we can) |* C( B& U* V+ i1 Y
// randomize the order in which the bugs actually run
a9 ~- \$ L# T W* g9 p' x // their step rule. This has the effect of removing any* J, ~( K: o8 c0 o$ @$ J
// systematic bias in the iteration throught the heatbug) N& i' D: x' x& s+ p) t' P
// list from timestep to timestep
2 a' I4 q- \* R# C4 K$ | - G @4 q2 b5 g
// By default, all `createActionForEach' modelActions have8 Z6 z. S) h2 k/ Z+ g, W# i
// a default order of `Sequential', which means that the" Y% ]* P% o* E5 H/ K A& _
// order of iteration through the `heatbugList' will be
9 v- W2 M* ?% S3 K // identical (assuming the list order is not changed
' ?& v5 y2 `! u9 O2 `' Z // indirectly by some other process).
% P& B$ n6 a5 k5 z
: h( p: p3 t+ ` modelActions = new ActionGroupImpl (getZone ());
$ M, P2 ?; U9 N! H" u
, ?9 Z# V3 `! N. G# F3 b9 r try {$ k; {+ R* Z0 L$ ]" ~6 E4 Q4 g
modelActions.createActionTo$message
: a2 m2 n+ i# x8 k6 H% n, X, s (heat, new Selector (heat.getClass (), "stepRule", false));$ L. |# x" i. y' }1 C
} catch (Exception e) {
0 s+ Z7 n8 b0 N& W7 } System.err.println ("Exception stepRule: " + e.getMessage ());
8 A" J K8 X% } }4 s# z& ?* z F* c( `
1 U$ P5 A# l% ~8 o. ~+ g
try { ?; ^# M2 P) O- k1 ]$ R! D
Heatbug proto = (Heatbug) heatbugList.get (0);5 Z4 [$ \. ?0 C
Selector sel = 5 ]. J; |3 M6 C, l g+ K
new Selector (proto.getClass (), "heatbugStep", false);* Q- H$ B8 Q+ _: B2 C3 ~8 Y9 u" o
actionForEach =3 ~: j _( n9 T# B" G
modelActions.createFActionForEachHomogeneous$call
+ Q5 [6 t( H# q9 W (heatbugList,
5 B1 C& ], p& f8 j new FCallImpl (this, proto, sel," I% k; I0 v1 ]
new FArgumentsImpl (this, sel)));
4 V: l: N$ }. ~' d: N } catch (Exception e) {) q. a! J( P8 B
e.printStackTrace (System.err);
) d& q$ z2 d% k0 P9 ~7 G8 N5 J } z0 M1 ^' {! Q
* Y- r$ L. ]+ c4 a: D) q* r0 { syncUpdateOrder ();6 Y7 _. g# C/ q# X B" I9 i
1 @- r( ]* J* v try {
' h" k6 t: [6 h+ ?8 S4 N3 X modelActions.createActionTo$message ) Q4 d6 j! A- s
(heat, new Selector (heat.getClass (), "updateLattice", false));2 A# y. Y3 Z# e5 x; `
} catch (Exception e) {0 W" {! x1 n, X s* I
System.err.println("Exception updateLattice: " + e.getMessage ());% `8 p! A$ z# k! k. v1 s; N
}
' z! f/ D- l- Q" q4 e1 a
9 f; q" V4 O9 N% E // Then we create a schedule that executes the& T& w+ ]! ^8 G
// modelActions. modelActions is an ActionGroup, by itself it0 l) g6 ]- G# d; @9 H8 J
// has no notion of time. In order to have it executed in; s. `4 c' k: u" D: R8 z7 W) P
// time, we create a Schedule that says to use the' E- ]6 {# N ]9 b2 E, d* |& l
// modelActions ActionGroup at particular times. This: [5 `4 y6 _# j- T1 J o/ J Y
// schedule has a repeat interval of 1, it will loop every
; ?9 l& V7 u: V7 e! Y8 V; n // time step. The action is executed at time 0 relative to: p8 S* @! G1 {4 b4 } l( P
// the beginning of the loop.2 Z5 m Q$ Z9 O6 S
% ~- S8 w3 D/ O7 `; J
// This is a simple schedule, with only one action that is1 w& r$ m7 O( N' l, Q
// just repeated every time. See jmousetrap for more
1 Y1 v* f( a5 R) y: ~+ B+ y% N! d // complicated schedules.* e# Y/ ]/ q- ^$ F9 {" ? z# o
+ p' K& h4 }$ ]3 O# q modelSchedule = new ScheduleImpl (getZone (), 1);
. ]! u2 U2 n# S6 a. T7 [) ^4 G modelSchedule.at$createAction (0, modelActions);
' Z* L, P' o6 k- V& h
8 f% j8 E* N; L; X/ F/ r' @9 o return this;
; V0 j* d4 P8 w } |