HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' E$ g3 D( {$ `
@ ^6 ~; e8 ~3 S public Object buildActions () {: z+ k1 f! i2 O9 `' t" B, Y
super.buildActions();: D! u* k9 n6 |6 d) I8 p2 }3 S, c
, y# y5 C3 Z% Z/ ~
// Create the list of simulation actions. We put these in( w" y3 L1 r j& Z! k
// an action group, because we want these actions to be
% G% n3 X3 T5 |3 z // executed in a specific order, but these steps should5 h8 x( n1 r! }8 g
// take no (simulated) time. The M(foo) means "The message; A+ m5 L3 O$ l! u4 _ u
// called <foo>". You can send a message To a particular
, P% `& v J: ~' q // object, or ForEach object in a collection.% n# h; u6 S1 F5 l
0 T: S5 I' Y3 e9 M" m7 G // Note we update the heatspace in two phases: first run$ b, `8 W0 D' B+ Z8 r4 [+ h
// diffusion, then run "updateWorld" to actually enact the# O7 [* y- {6 @8 \4 \/ T
// changes the heatbugs have made. The ordering here is+ C8 b) M. i, R3 n- F V
// significant!! C. w4 X! Q8 N8 A1 P
8 |; ~8 T( f5 r; C) p
// Note also, that with the additional3 N) ^" {; y5 v4 r! ^# K
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 c/ N, J! V l1 ?6 S // randomize the order in which the bugs actually run
% n: Q* B1 {$ ^7 P // their step rule. This has the effect of removing any
7 l/ F4 N" V. |/ u2 F // systematic bias in the iteration throught the heatbug
' a) L/ r7 {+ L/ [; [7 Z U6 W // list from timestep to timestep
0 v; I) f; j' ]8 n
5 S# z2 ]1 p; X- ? // By default, all `createActionForEach' modelActions have
F1 n8 [7 |6 ? E8 l% N // a default order of `Sequential', which means that the9 p1 v q8 L" {1 J: v
// order of iteration through the `heatbugList' will be
( L6 ~' ]' [5 V$ R, l9 H# ~) t // identical (assuming the list order is not changed
+ ?" e; q; H8 h+ m4 w# w+ Z // indirectly by some other process).+ K( `1 ^, F2 V7 L
( k9 @, l. H2 s& j T; R modelActions = new ActionGroupImpl (getZone ());
8 ]# h/ Q* s6 t6 x) s3 J+ q
3 Z1 D; _$ I5 h1 o3 W" h+ v- ~3 { try {
% T; {5 H3 r, s& ^- y5 V: I0 o! b modelActions.createActionTo$message. G2 x$ E2 n7 J2 _! r
(heat, new Selector (heat.getClass (), "stepRule", false));
: h: w( j5 ?8 |! ?# e0 W4 F- h5 v, a } catch (Exception e) {1 V) E- c1 _' W9 B: z) ^
System.err.println ("Exception stepRule: " + e.getMessage ());
" ` w+ \+ c* U5 A }
4 K* Z9 @4 B) n" k$ \* b+ M) F% j N. a1 u O1 y1 m% y5 n l
try {6 l/ R& G; m7 S9 A
Heatbug proto = (Heatbug) heatbugList.get (0);
) ^: ^. U% i9 a0 f& E& Z9 K8 q Selector sel = ! z9 f6 k! t; _7 p
new Selector (proto.getClass (), "heatbugStep", false);3 d# a" k9 r4 e4 p! v
actionForEach =# P" p/ _" ?0 I, ]( U a4 p2 a
modelActions.createFActionForEachHomogeneous$call
5 {$ U% c" r% B (heatbugList,
# N' |5 O- @: D' k x+ T8 R new FCallImpl (this, proto, sel,; I3 P/ \. c+ _/ v4 S6 ?* V# t) @
new FArgumentsImpl (this, sel)));3 A% Z8 G* g9 g; b5 ?3 [3 _
} catch (Exception e) {
/ m5 [" G9 |6 I/ O, `% D e.printStackTrace (System.err);
5 c& j. `* i/ J7 k: ^1 k$ O }
6 T `5 }- L2 [ 4 T# v' m: D% O1 D4 a
syncUpdateOrder ();
& a+ Y. w) p O/ u& Z
0 y8 |( E0 i" b) \- A% g try {
5 s. U9 @/ z; f+ i: e modelActions.createActionTo$message - D, ^$ H8 W2 o6 ?( [9 \, f
(heat, new Selector (heat.getClass (), "updateLattice", false));0 v l0 f* ^4 y' F( m. C* B/ N
} catch (Exception e) {8 r! x. Q. Y- L8 q* |
System.err.println("Exception updateLattice: " + e.getMessage ());3 [; n3 n- B& f8 I: e% g9 V# t
}: O8 f, M2 [( r/ D0 p
4 \# W; Q% T6 l" d- q- _
// Then we create a schedule that executes the
: _& K1 G# i: u6 U$ Q0 E7 \8 Z1 i# Q // modelActions. modelActions is an ActionGroup, by itself it
- A! Z3 K9 E2 p# [" f // has no notion of time. In order to have it executed in
' W3 p) V- \- _+ m7 P // time, we create a Schedule that says to use the2 Z. s- c" m1 u# W% }* x
// modelActions ActionGroup at particular times. This
* q0 Z% V4 X# F5 `& A# N3 Z // schedule has a repeat interval of 1, it will loop every
- m, @1 t& s! ]6 q; C- W" R- f // time step. The action is executed at time 0 relative to% z8 g! D3 w; t) ]9 e
// the beginning of the loop.+ B6 y7 z8 v/ Y5 d8 ~8 O
- x2 H; M8 a( G$ S& n
// This is a simple schedule, with only one action that is
. x5 z7 ]# g5 | c8 \ // just repeated every time. See jmousetrap for more0 c8 H% G- n' {" v6 M% ^ r
// complicated schedules.
/ t8 d% z0 K* ~1 w2 r! B- W% {- j 1 ?5 ^% k1 o0 E# J; }
modelSchedule = new ScheduleImpl (getZone (), 1);
) C) m* M( P/ I: @2 e2 y modelSchedule.at$createAction (0, modelActions);
$ l( P; W$ C6 ~) G, X9 b- q& P 3 I3 f3 r: ?- j9 a- m, P
return this;
# a0 e* u: K# l, i8 ~ } |