HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& z! R5 s. ]" O/ W$ L
5 @0 Q3 N, a: }# [7 c, X# [ public Object buildActions () {2 J, K0 U4 z( x
super.buildActions();
1 G! _; G$ l/ O# t4 n2 C8 R" I# Z6 D- b; W 3 T m9 _( \$ O# K
// Create the list of simulation actions. We put these in$ `) x# Z+ g& w2 t' ^* Y9 ~
// an action group, because we want these actions to be$ D% P2 o' j1 O: T3 F
// executed in a specific order, but these steps should
8 Z, q4 p" v/ e" ` f2 ~# y# F // take no (simulated) time. The M(foo) means "The message
& [' Z2 g( Y+ U0 W0 j // called <foo>". You can send a message To a particular
- q8 n) s1 }& U. b% K // object, or ForEach object in a collection.
D3 ?& i3 R V+ P6 B3 | 2 _- p+ C* i q
// Note we update the heatspace in two phases: first run
: {+ d+ X' W8 O // diffusion, then run "updateWorld" to actually enact the1 D+ A: K$ M& n, r/ }- A2 P
// changes the heatbugs have made. The ordering here is
! X0 u- ?* Q3 W3 a2 \/ t1 K // significant!, s8 {5 d* H, b; ]7 Q
5 t, [( K$ _& A2 C; }0 |
// Note also, that with the additional
- r$ \ ?5 Y3 c# l& A Z. W // `randomizeHeatbugUpdateOrder' Boolean flag we can
3 B# g/ [( M7 |) H( Y# K8 @4 ?1 N3 P // randomize the order in which the bugs actually run( k- e' c+ C& e! W- [; f6 G* F
// their step rule. This has the effect of removing any
' P$ O# p9 T, c d3 v" K9 l // systematic bias in the iteration throught the heatbug
$ h7 L6 C5 G6 I |5 c+ C // list from timestep to timestep
* Y3 `, z! m7 g
4 R% c* q5 W1 {& s$ O // By default, all `createActionForEach' modelActions have+ `( s! X9 E$ O( R. ?: V* ^* Q
// a default order of `Sequential', which means that the0 b! C( T/ ]' E% D5 M
// order of iteration through the `heatbugList' will be* `; ^! t; l5 E/ a0 `! b* N
// identical (assuming the list order is not changed0 }7 |7 I8 _$ n0 N! [' V
// indirectly by some other process).3 ^' W: I; @- s
% q! D* f- X% p* p; M# I5 |- u6 x( k modelActions = new ActionGroupImpl (getZone ());7 s6 z" [1 O" u1 ?9 a
! j6 _8 U! Y5 M2 y6 g0 o( n try {+ _, E- k: h. _" z6 y9 g) u
modelActions.createActionTo$message
7 z& a4 F& R D: F (heat, new Selector (heat.getClass (), "stepRule", false));
$ X4 L- ?9 D- }& Z' y' J } catch (Exception e) {# z8 K' L, X+ B
System.err.println ("Exception stepRule: " + e.getMessage ());+ H& q, C8 I' c1 K
}# Y: p& |. x2 W% Z$ v* Q5 H
% t j# H# X# C- I- _) q try {
8 ?1 k5 @2 \, z" b8 g ~7 K Heatbug proto = (Heatbug) heatbugList.get (0);
8 K; a8 C9 W! A4 ]. W6 Y) ?' F- b: l# ?0 x Selector sel =
& D; K( u; ^% y2 i$ D' ?2 I6 q new Selector (proto.getClass (), "heatbugStep", false);+ v+ T0 ]0 m" Q6 P2 u1 X$ b
actionForEach =
' o3 V* P& y! c8 z, }1 L/ Q1 h modelActions.createFActionForEachHomogeneous$call/ @* x, v/ ^2 f% ]9 w: Q
(heatbugList,
- f- @, C V1 S2 _: ~7 ]5 P8 T$ L new FCallImpl (this, proto, sel,2 z' b0 A0 u. A! f+ ?8 d
new FArgumentsImpl (this, sel)));1 x2 B4 t5 T- f. E
} catch (Exception e) {
6 q; [' ~) v5 l e.printStackTrace (System.err);1 n6 t, v* f/ O& W# f
}
) m8 y O( E2 X) L& v. _+ w ; B! B3 ^) ~ E1 \
syncUpdateOrder ();
% `# Y/ @+ s) i' [
0 _ U, p- A2 O, @5 m9 h2 V try {
}* j; u; E- L. b; n. e* q modelActions.createActionTo$message
/ A1 ~2 Q3 m9 k& y% y6 D (heat, new Selector (heat.getClass (), "updateLattice", false));
2 ]$ C# Q0 K! s8 y } catch (Exception e) {
; ?9 n% l! [0 I7 H; N System.err.println("Exception updateLattice: " + e.getMessage ());
# k) E& a6 ?3 y4 e- D5 s9 l }3 R; f6 @$ \+ A. ^
% N5 V5 I& J/ J, ^: U1 V // Then we create a schedule that executes the5 c) i7 [ s {% Y
// modelActions. modelActions is an ActionGroup, by itself it
8 }! i R f$ W! N0 [5 [' v8 S // has no notion of time. In order to have it executed in
+ d9 Y- S1 D* v* q // time, we create a Schedule that says to use the
8 n) d. A% x1 c/ a: l4 w2 h' g // modelActions ActionGroup at particular times. This
1 \/ S; G& F7 b* N // schedule has a repeat interval of 1, it will loop every
2 n+ n( h4 a! l9 i# ?8 C // time step. The action is executed at time 0 relative to+ L" ^5 ~) h* b
// the beginning of the loop.
9 A2 j# S4 {8 s9 X/ v0 j$ y1 E
2 [6 w0 D: T3 d // This is a simple schedule, with only one action that is
9 q7 r4 U3 x( V: J // just repeated every time. See jmousetrap for more# f! l$ C2 b' m# ]3 A
// complicated schedules.
& U: I% R/ ^+ U" Y
1 R/ q# u( Z% z6 n7 c, ^4 i modelSchedule = new ScheduleImpl (getZone (), 1);
: K. r) e: G _4 W# m modelSchedule.at$createAction (0, modelActions);
i( |$ ^5 U* s, E/ [& v 0 f8 g, t) _) s6 A: V( r3 z0 i
return this; M# k% o) K- p
} |