HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 K9 r, `/ S, `+ T* F
6 E3 |- p$ s5 N& b# G6 ` B& A
public Object buildActions () {
$ t; t. {3 F# }! p super.buildActions();
* }9 t7 \/ L5 D" h" x
$ v$ ^: F$ U4 m! r* ?% b) V // Create the list of simulation actions. We put these in
2 n+ Z$ @( X" I. {9 D- n0 ? // an action group, because we want these actions to be8 _& q+ _5 B0 K4 r& I
// executed in a specific order, but these steps should
2 ?) r; Z$ G R8 N4 q // take no (simulated) time. The M(foo) means "The message
# R5 J5 h2 P6 u+ y: U0 F: O // called <foo>". You can send a message To a particular
, h2 f* `% }0 z // object, or ForEach object in a collection.5 E5 N+ \- e+ V7 q/ K0 G3 }
/ ~! P, R1 N+ [ // Note we update the heatspace in two phases: first run
! J% V1 ]* Z' m; e5 X; ? // diffusion, then run "updateWorld" to actually enact the
* Q& O6 j& k; h: z, @ // changes the heatbugs have made. The ordering here is9 R" n) ]! i, V3 t. z) S
// significant!
8 l. J/ G7 W, ?3 ~1 ?" r( z
4 G: \3 s% @/ l% Z* } // Note also, that with the additional
' t" P! c, A1 w3 h // `randomizeHeatbugUpdateOrder' Boolean flag we can" A- l; Y5 L( a' x/ ]! m! f( w+ x
// randomize the order in which the bugs actually run
0 b8 l8 V5 V' ?8 w% A ] // their step rule. This has the effect of removing any
: e5 Z/ U1 U2 Z3 S o // systematic bias in the iteration throught the heatbug
, ?. L$ n3 a4 `/ |! x // list from timestep to timestep
% ]; W9 _1 {% }# {
' ?1 G3 B% I/ p& a" r0 D% t // By default, all `createActionForEach' modelActions have. u- p' E) @+ w; i" W' f
// a default order of `Sequential', which means that the( F M J: @. }' |9 L/ T
// order of iteration through the `heatbugList' will be
$ l7 H- U \! V: @" w K. A3 M6 f // identical (assuming the list order is not changed
1 c% C- H, l/ Q$ e // indirectly by some other process).1 T& M; S+ A8 I% H7 T1 e% ^3 b
( b, c; e3 o! j8 @" t; I modelActions = new ActionGroupImpl (getZone ());
) |. T' g$ s8 P3 M: a" U
$ z3 d: k r! I* \ try {
- Q; v* _: r' K modelActions.createActionTo$message: I$ |9 Z _$ h% T& |
(heat, new Selector (heat.getClass (), "stepRule", false));
6 T6 a/ m0 u! U4 q Q } catch (Exception e) {/ P* C# k- c; k5 P1 k( w
System.err.println ("Exception stepRule: " + e.getMessage ());0 P" C6 Z; p1 I9 v2 |" m F5 }
}. C! Q8 c" |9 K- p
, x2 D Z& i. J8 O try {
" z4 k6 s: i% R; P2 w Heatbug proto = (Heatbug) heatbugList.get (0);
$ g2 G/ E2 P* L! q- b Selector sel =
' g: y( o, l" k$ K! s0 j$ z new Selector (proto.getClass (), "heatbugStep", false);. Z; d0 M6 U* ~! Z
actionForEach =
0 n3 N6 C% i1 { modelActions.createFActionForEachHomogeneous$call0 R- v4 D+ h# r7 V5 {
(heatbugList,
6 h o% s1 o1 h4 ]% G; C new FCallImpl (this, proto, sel,
( U8 E* K8 @4 P& f new FArgumentsImpl (this, sel)));4 U3 l9 Y4 h: ?3 T! m
} catch (Exception e) {
; A9 y+ l" f; |& B5 X e.printStackTrace (System.err);
& [. j, s0 M# D' {! I! o, [ }
0 N0 `& E- L3 [8 T! K6 s7 P$ j2 Y 2 p* |, C! F+ H+ u9 [8 f
syncUpdateOrder ();6 t- t1 y3 [ K; M5 N1 x% w& x
' W! c4 h# q( q9 `3 t, T try {
. H6 Z9 {1 ?8 H0 P modelActions.createActionTo$message
/ E' ~" s# M2 j) l: `2 Q (heat, new Selector (heat.getClass (), "updateLattice", false));
1 k: J* }1 }/ h- P0 U- A } catch (Exception e) { |6 F! S- k \1 C: j
System.err.println("Exception updateLattice: " + e.getMessage ());% {3 a1 ^" {0 A2 E
}1 M: R' L* `9 `: C! U& Q' Z. t* _
8 d. B# ]1 a/ q( r! n7 g // Then we create a schedule that executes the0 B: Y5 V+ M: s- x# Z; L
// modelActions. modelActions is an ActionGroup, by itself it
7 o+ H- k2 d5 M8 d) z- p // has no notion of time. In order to have it executed in
4 o: V& J* J5 D- q4 j, P6 b6 E% v // time, we create a Schedule that says to use the
8 H5 C! _3 m& b4 U // modelActions ActionGroup at particular times. This
3 X! p6 i/ y- L1 T2 o3 W: [ // schedule has a repeat interval of 1, it will loop every
* s6 U2 j9 j7 n5 v- \ // time step. The action is executed at time 0 relative to
1 r: {6 d6 O4 U- `* r* l9 i // the beginning of the loop.
O2 Y7 D1 t) F# h5 b3 s' r/ p, ~ q: C$ o
// This is a simple schedule, with only one action that is& s8 k% A/ k% B/ v/ V6 x
// just repeated every time. See jmousetrap for more
- r5 i" p' J# h6 L8 U // complicated schedules.* b+ b; ^3 H4 {9 {1 }
8 o1 c) R% V# |9 n6 }! r
modelSchedule = new ScheduleImpl (getZone (), 1);0 v6 `; \* [. e9 _ C
modelSchedule.at$createAction (0, modelActions);. z! ~6 |0 z" A% o$ ]9 A
' S$ j G8 t4 t# U' h return this;4 v5 q8 N# p8 p8 R- N% {
} |