HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 i: F; e& j; I) `0 q8 N, i
, v; Q0 X8 ~/ l2 x public Object buildActions () {
1 ` x/ V& i) C7 D2 m super.buildActions();% Y) O. N9 ^% c7 H9 k9 X, Q( H2 n
5 Z- n% {8 Z8 R6 X. b // Create the list of simulation actions. We put these in
7 p8 O( y, y" X* `' ?6 y6 Y // an action group, because we want these actions to be
3 q+ f" {* }; Y // executed in a specific order, but these steps should. o3 v+ U- G. `1 V) I
// take no (simulated) time. The M(foo) means "The message) d1 _( R" u- x8 W" c& e* A' {
// called <foo>". You can send a message To a particular
+ Y# m4 _2 K0 R, c/ ~/ F9 v // object, or ForEach object in a collection.
9 ^ _) ?5 b; `. `6 q / |6 T9 C3 \1 l# c s
// Note we update the heatspace in two phases: first run
7 ^- ?% h, d" h2 c% D // diffusion, then run "updateWorld" to actually enact the
. |1 z1 N; [: W& E" H& D // changes the heatbugs have made. The ordering here is
/ |8 E2 v- @0 v) E2 q2 K // significant!& @, d. y& v; l+ ?, F3 U3 ^) b
- B; m8 h& v2 [' _+ B# T // Note also, that with the additional
% `* w$ V* y" a+ I. C- v // `randomizeHeatbugUpdateOrder' Boolean flag we can5 T; c2 w" y5 x2 w% {' [% W
// randomize the order in which the bugs actually run! G3 J' b) K' [' W1 ~4 m' @4 F3 W
// their step rule. This has the effect of removing any# ^% u: ^! @& w! W6 B: Z) f+ o+ h
// systematic bias in the iteration throught the heatbug
6 u# u) ^0 N' B% Q // list from timestep to timestep6 q+ b/ l* i7 F4 t/ U+ V- S
5 a- w; D) \+ G: q! t0 a
// By default, all `createActionForEach' modelActions have
z3 ]" S" h8 f0 K // a default order of `Sequential', which means that the. L' T+ M O3 x2 x
// order of iteration through the `heatbugList' will be! X8 q0 L+ }4 C
// identical (assuming the list order is not changed* ^1 z4 X, w* M
// indirectly by some other process).
3 ^! I& X. Y' U. q
9 Q9 ~% v6 o7 r0 \0 \ S0 X3 a modelActions = new ActionGroupImpl (getZone ());
6 x6 r3 e0 Z: k) m
6 @: c4 I# ?! r6 R try {
2 C" d$ i3 G3 i, a& } modelActions.createActionTo$message/ E- c8 W/ T% t
(heat, new Selector (heat.getClass (), "stepRule", false));- D6 F5 a7 D5 ^. [. i2 L
} catch (Exception e) {) F7 B0 d1 i5 D3 ]9 k& J
System.err.println ("Exception stepRule: " + e.getMessage ());
! x* D" F( f% O6 ~# M }
& v' k" ^4 ?9 K1 C1 A* H& n
7 S* ` H" e( w' W- Z try {" A, Y! l/ v0 N# Z6 }; G c+ v
Heatbug proto = (Heatbug) heatbugList.get (0);) _5 k' ~7 F6 M* B% L' v
Selector sel =
- Z4 R9 S- i" X* X/ s. h% r5 y new Selector (proto.getClass (), "heatbugStep", false);
! F# N0 b I" M1 U$ J9 a actionForEach =9 _+ r9 }, ^0 O* z) H$ e
modelActions.createFActionForEachHomogeneous$call
$ |1 ?2 b; `0 ^ C' `4 f (heatbugList,1 V8 X( w/ b# a1 {: z. Q; h
new FCallImpl (this, proto, sel,
& D8 O( }9 t+ r new FArgumentsImpl (this, sel)));
0 }4 B v) q. }, ] } catch (Exception e) {& y8 _/ U! m: g0 U. W) Q0 }) g
e.printStackTrace (System.err);9 l1 r2 d8 t. e9 g: V6 c; e
}1 W% C9 Q5 r& c) i
: m" c- e( t' k* L) x9 v% o; k
syncUpdateOrder ();
! r) p+ u' g9 c
! O5 m T5 C/ G0 g9 [ try {& ?! O! N/ e& G/ | H
modelActions.createActionTo$message
- s X5 K& C/ A* l; R# p% e+ v (heat, new Selector (heat.getClass (), "updateLattice", false));
' c E( s/ `' C/ R- a x } catch (Exception e) {
1 ?5 s6 S6 B# h System.err.println("Exception updateLattice: " + e.getMessage ());" w+ o7 N# y4 m
}- Q8 n! ]2 \) w+ k8 z
S. N0 j$ U! d- L* z // Then we create a schedule that executes the( }2 v# b8 H$ E) C S
// modelActions. modelActions is an ActionGroup, by itself it
# E- P6 Z( o( I g% j; J // has no notion of time. In order to have it executed in4 f2 ?3 @6 Z1 u4 M! Y, R5 V
// time, we create a Schedule that says to use the
5 f9 }; x7 }1 A2 ?/ x // modelActions ActionGroup at particular times. This
3 X9 B( O' n; ^: f" K. t/ S- t // schedule has a repeat interval of 1, it will loop every
3 B1 V/ r1 [' w3 I. V // time step. The action is executed at time 0 relative to- t7 D4 e h+ }8 l# L
// the beginning of the loop.0 M) R3 A* j$ m
# Q2 ~& L* q+ A/ r% `0 q7 q, O8 `
// This is a simple schedule, with only one action that is1 `; b+ V2 ~. W |
// just repeated every time. See jmousetrap for more; b7 Q# n, m* s, L5 l$ D
// complicated schedules.
0 `# x( d6 r; Z6 r- r% I # s, j4 g3 @) G4 U0 O
modelSchedule = new ScheduleImpl (getZone (), 1);
, A8 K. H! O# e% M9 l modelSchedule.at$createAction (0, modelActions);& T* ?8 _$ @9 ~; d
8 Q# v6 V1 k& v+ n' _
return this;% D, o: o j( O. j3 A- J# V' X
} |