HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ r# y- ]5 V' S. ]* Z3 K; p) ]( n
4 z; c2 J9 y2 K# a: P Z4 c# S4 o public Object buildActions () {
8 \4 i6 s j Y9 N, l) R super.buildActions();
t0 ` t/ j f- i
( Y% i5 {; X' z4 b9 P" O& z, Z% P // Create the list of simulation actions. We put these in. l: v3 t+ @; U) `$ M
// an action group, because we want these actions to be3 i2 P) b% B# K1 C" |
// executed in a specific order, but these steps should9 S: T" i2 |. b5 m% `6 \) E \* I
// take no (simulated) time. The M(foo) means "The message; t. |8 {+ H# f! [/ e7 ?
// called <foo>". You can send a message To a particular" [3 r8 `/ C$ O1 H' n5 e9 M* U: d
// object, or ForEach object in a collection.% L; ^" K c5 G( a3 x- I
/ i# _4 P9 F9 {( y; Z // Note we update the heatspace in two phases: first run
+ K5 i/ @7 O' g* c8 k2 m8 r // diffusion, then run "updateWorld" to actually enact the
8 W6 h: ]" J: Q5 w // changes the heatbugs have made. The ordering here is2 z% z! Q5 ~6 O" |% h8 X! r0 ^
// significant!1 t% J5 d) k& Z
6 k+ _) e7 _( V2 \ // Note also, that with the additional
/ @& W: w; F' A // `randomizeHeatbugUpdateOrder' Boolean flag we can, E; W m% M2 o3 e+ q! r/ C: ^
// randomize the order in which the bugs actually run, @; u" x% s5 D
// their step rule. This has the effect of removing any
6 v9 Z F7 m$ X) Z, X // systematic bias in the iteration throught the heatbug
" [6 r- P# K+ S2 O s0 C // list from timestep to timestep
+ s0 y3 M0 z3 s' j 7 Z7 c- k9 ~6 F" C; c4 S
// By default, all `createActionForEach' modelActions have
: F/ @9 b* p* f$ p8 ? // a default order of `Sequential', which means that the$ B% S" n# N& S) K) U! e7 b
// order of iteration through the `heatbugList' will be: h/ B$ M) T8 r3 T
// identical (assuming the list order is not changed8 U) m2 O% g4 C4 X1 L1 Q4 s1 i7 {
// indirectly by some other process).
2 ^& T6 c, E! R/ Y9 x # Y: @2 f3 k u
modelActions = new ActionGroupImpl (getZone ());+ h z+ M" d" ^* \" d. ^( K, n+ O
- u" f( k+ s0 B. _. }+ Z# C. @, N try {6 X% }+ p7 ?/ p f3 G" s2 C+ m& R4 e
modelActions.createActionTo$message2 _, F2 a# L5 X! |
(heat, new Selector (heat.getClass (), "stepRule", false));
+ j/ O; [, B8 r+ I3 i7 S3 Q1 W } catch (Exception e) {
, B& b2 W0 _5 U- L0 z' X System.err.println ("Exception stepRule: " + e.getMessage ());
( H" r* |6 [& q9 T3 P; B }; u* m; O6 @( A% M8 A. ]) V
: k1 g2 E5 O0 _6 G+ a6 L try {2 Z8 F/ _: [6 u1 j1 m
Heatbug proto = (Heatbug) heatbugList.get (0);7 y' g8 B1 U4 G3 Y
Selector sel = - J! r: @ [0 R/ O$ n" ]; C
new Selector (proto.getClass (), "heatbugStep", false);
3 C. M* n0 m6 T7 z1 `# N actionForEach =. x9 ^( R. E% `- J/ o2 W. N+ E0 r3 J5 O
modelActions.createFActionForEachHomogeneous$call# H/ _9 I; h c" E/ m7 v
(heatbugList,2 e8 {2 s- ? D0 @& o7 u
new FCallImpl (this, proto, sel,) ]( y- [4 b. h9 n
new FArgumentsImpl (this, sel)));
$ K& f# U7 [ g3 `5 V, N v# J } catch (Exception e) {# o/ @7 `/ ]; C
e.printStackTrace (System.err);
' I' f9 X- ]' d" N2 m }8 B. t3 K* @% \4 ~5 P2 w8 W
B" V* k2 x8 x( b( k( T. j9 u
syncUpdateOrder ();* N. V% k8 i3 x8 x" l1 K) E
2 b! J6 s; u& T1 ^* O try {* e% @" P& @, G5 C9 F# D
modelActions.createActionTo$message
6 j8 l9 o# v; v. Q; V: X8 t (heat, new Selector (heat.getClass (), "updateLattice", false));' ^; I- n/ P" p- N! v5 ]3 l5 u
} catch (Exception e) {* V/ H4 L5 u1 h" o% ]) p# p
System.err.println("Exception updateLattice: " + e.getMessage ());8 n* T, i4 T+ V; L
}
- d& J4 C) p6 a1 |5 J" F* W' q
- r' p" I& O4 O1 M, G$ {, B // Then we create a schedule that executes the
& ]0 L. c8 x) Y, r // modelActions. modelActions is an ActionGroup, by itself it+ _( r6 t" W+ ^. z
// has no notion of time. In order to have it executed in5 Q; p; V& ~7 Z4 j
// time, we create a Schedule that says to use the
3 K; E' s, D+ d4 g // modelActions ActionGroup at particular times. This
, d1 k1 F: e0 ?6 S+ Q/ W0 a# h9 | // schedule has a repeat interval of 1, it will loop every& H$ K7 ~- r: V: Q
// time step. The action is executed at time 0 relative to
8 D) n7 `' W; ?& }' _: q; l& s! @3 Z // the beginning of the loop.& `+ I8 p4 V# f
( r4 {4 I' w) ~/ u
// This is a simple schedule, with only one action that is
! ?; b( e6 g h' }. t7 M' U // just repeated every time. See jmousetrap for more k x! E' }( S9 a X
// complicated schedules.7 w" w$ Q. Q+ \+ Y0 s$ ?
5 \% N3 C7 z: j) n/ G1 P& o modelSchedule = new ScheduleImpl (getZone (), 1);
8 I7 y8 }, t* j/ m! \8 F modelSchedule.at$createAction (0, modelActions);) c5 ?& ^4 o) [9 n
6 u; y8 r1 j8 Z. s* q return this;
/ q6 c: O( F0 Q6 z5 ~! u# ] } |