HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ X( ^: }& \% V$ V- q7 C4 P
5 \9 m& J4 s! v2 x1 { public Object buildActions () {
9 D ]# a9 N' c* Y* O" M0 p super.buildActions();
: L b9 J7 o7 k$ u! e4 d , R, c' w L1 A& r2 p
// Create the list of simulation actions. We put these in
/ x* Q! Z) p* D t& d // an action group, because we want these actions to be
& z" ]( j4 c: _ \) H4 d) k- g0 Y9 t // executed in a specific order, but these steps should
# m* _" O P5 {, _ // take no (simulated) time. The M(foo) means "The message% Y. T+ [$ v1 m9 A: L! b7 o5 F
// called <foo>". You can send a message To a particular5 ^& f0 F' ~/ T) A0 E& Q2 u
// object, or ForEach object in a collection.
! Q6 c+ O' C# F0 i8 j , Z2 e# d* ]. `& R, d
// Note we update the heatspace in two phases: first run, z+ v- U) C- ~$ }
// diffusion, then run "updateWorld" to actually enact the
2 ?# e2 v. w! j. ?# M8 z // changes the heatbugs have made. The ordering here is
: D8 s Y3 R7 p+ J8 O3 ] // significant!# \/ R' ^) W& o- N
2 T+ m6 k+ ~! i, H
// Note also, that with the additional2 m% o* q8 i0 x
// `randomizeHeatbugUpdateOrder' Boolean flag we can- N9 e8 c; ^) v, t% ~7 w5 q, T
// randomize the order in which the bugs actually run8 v+ e! i8 w/ L9 k8 V0 m
// their step rule. This has the effect of removing any- M# L+ k$ v: w, ~
// systematic bias in the iteration throught the heatbug! b- p0 c/ u; ^9 M0 z% {/ y
// list from timestep to timestep( J/ Q" \; c1 @; h& p
9 w+ c5 z! v$ B H, U3 E // By default, all `createActionForEach' modelActions have
" r$ E9 a" W; ~- A+ n // a default order of `Sequential', which means that the. H4 x8 x7 m' `! }( K s$ T
// order of iteration through the `heatbugList' will be
' i/ }4 { p! ^" e7 O& R* N9 w // identical (assuming the list order is not changed5 p' j' N/ p- G) p
// indirectly by some other process).
0 H1 U+ i# B" @. ~3 Z/ K( l ! i9 r8 s9 P) H, p( o
modelActions = new ActionGroupImpl (getZone ());! O2 ?+ s" Q! R5 p; u* B
' H: B6 e% S$ g& [
try {# D x" p& R8 ?) V: T5 `
modelActions.createActionTo$message
+ c+ M8 l; _. W. [7 @ (heat, new Selector (heat.getClass (), "stepRule", false));( u) b4 Q+ U: s! P
} catch (Exception e) {
3 z0 J# I5 ~! i; [: T System.err.println ("Exception stepRule: " + e.getMessage ());/ Q% j( Z" `4 l8 g4 H
}$ r" n" I/ n. E+ t/ o
$ t& u* [( G* s5 i. @
try {
% N0 V" l2 U, v% t. ]$ \7 { T2 u7 Y Heatbug proto = (Heatbug) heatbugList.get (0);, w+ Y$ _" Z6 O# X6 b( O
Selector sel = 8 @7 `: Q; s( d2 F. q% V
new Selector (proto.getClass (), "heatbugStep", false);
, k. h6 Z& h7 c8 V( _ actionForEach =) g( h7 N' _" s! l/ W" G& X5 t2 w
modelActions.createFActionForEachHomogeneous$call* e: l0 x, `+ k% A. \0 s
(heatbugList,2 h6 n h7 ?7 q7 @' q; y9 q
new FCallImpl (this, proto, sel,/ [( e; d5 f+ F# g) C( k) {4 ^ v
new FArgumentsImpl (this, sel)));. R. H0 f& n/ q
} catch (Exception e) {3 G$ Q. a. g/ L0 ]
e.printStackTrace (System.err);
2 D9 s' T' W: ?8 x }3 Z) Q+ L4 \( K) |4 o- n% k
9 V2 S; S! y( x! `6 [
syncUpdateOrder ();/ L7 E0 D$ C8 Z
# C' g1 ^4 V ?6 ]2 { try {
! L% u* h4 r; F# t8 S2 ` modelActions.createActionTo$message
* J8 }" a$ N9 c/ f3 q) A/ K (heat, new Selector (heat.getClass (), "updateLattice", false));
) \- P. }( @% L4 I+ B9 u6 Q } catch (Exception e) {
9 b# `- ~5 W$ R+ U System.err.println("Exception updateLattice: " + e.getMessage ());
7 p% j6 H- X( W% h3 ~! A. [! M }1 s* C. P- N0 H ]# a3 q# D( X, x
. r5 i8 i/ k0 Z j$ w- n* X! \9 v
// Then we create a schedule that executes the
, G2 \" Y8 x/ g/ O1 E5 D // modelActions. modelActions is an ActionGroup, by itself it
- a& b4 a9 g- x, c. @5 \7 B1 o // has no notion of time. In order to have it executed in6 n" x9 a, u3 [1 y6 H
// time, we create a Schedule that says to use the
- v8 Y s- ^" X8 c // modelActions ActionGroup at particular times. This
% @8 i1 i9 a7 `7 x3 } e- o // schedule has a repeat interval of 1, it will loop every
6 ^7 h2 w9 z/ b- E& [ // time step. The action is executed at time 0 relative to }. D; f, D2 O9 y
// the beginning of the loop.
6 g' G0 J5 h) c+ E
* u0 v- c# y9 A0 j/ L // This is a simple schedule, with only one action that is
, _+ } Z: b5 B: _ // just repeated every time. See jmousetrap for more: r& Z. S. N5 u( s; U# }
// complicated schedules.% M6 d7 A) f" c7 v) ^* C
7 V7 W0 n& m5 d3 _) b7 }8 d b
modelSchedule = new ScheduleImpl (getZone (), 1);
3 `# E# V1 q1 H# y. A modelSchedule.at$createAction (0, modelActions);
) H9 m% q; ~9 g7 C: O8 X - y4 V6 {+ X+ o2 M) }
return this;
, n8 e( e1 p: H9 Z7 Y } |