HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* F9 Y- A/ ~$ z- B/ a
; V: {! X( G0 F: r public Object buildActions () {
& W! d% w( h/ `' P% j super.buildActions();
( W7 H' u. H! L9 o 2 ]# D: l3 h5 w7 D
// Create the list of simulation actions. We put these in% p3 ^& i q9 m9 D9 P2 b/ ^" h
// an action group, because we want these actions to be
) |- Y7 L1 l& N# G8 W# r* ? // executed in a specific order, but these steps should
5 x9 H8 [9 c2 T+ }6 M F1 q) z // take no (simulated) time. The M(foo) means "The message$ S* J$ U' v: s j7 X0 g9 \! h
// called <foo>". You can send a message To a particular
# g3 Y. G8 S7 `) M // object, or ForEach object in a collection.
- Y( G. D% Q; a7 ] b1 |& d
x3 d) ~# ]! L3 K, g0 S // Note we update the heatspace in two phases: first run
' K6 L/ A, f3 f5 K& K$ g& S // diffusion, then run "updateWorld" to actually enact the: { u; M/ [ {; V( X# j- ?
// changes the heatbugs have made. The ordering here is
. F+ L$ \. o6 g+ N // significant!6 a u7 D6 p8 l0 R h: ]
- Y* G& }0 m7 ~8 J) i1 x# y // Note also, that with the additional
! p7 ]6 h6 |. a Y* m6 X( f // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 Q5 m: l O. ~% z3 Y' q, W // randomize the order in which the bugs actually run- p+ ]% n) H8 f$ u
// their step rule. This has the effect of removing any
8 J6 v' s2 Q$ i5 ]* f // systematic bias in the iteration throught the heatbug8 l/ C F' B! ]7 M& M8 d1 `- y
// list from timestep to timestep
$ I6 V, X$ ^0 |0 L1 i - M# b# K: U2 e; ]
// By default, all `createActionForEach' modelActions have
5 x, O1 w! g0 }7 i // a default order of `Sequential', which means that the
- {& [4 o4 X1 H% G" o // order of iteration through the `heatbugList' will be- P4 ]( y4 G; \% D6 A
// identical (assuming the list order is not changed
& K7 ^7 F% m. O1 D9 f# i2 u // indirectly by some other process).
h0 J* L1 P# p: q" B1 J3 I+ O 8 ~" O* ] z% d& s3 P
modelActions = new ActionGroupImpl (getZone ());
$ v) j% f2 Y7 V6 A& x; J
) F# |. j$ Q2 S* I) ?8 N$ H/ i- F try {
! i( G5 {9 I Z4 |- n# d' \- p+ U modelActions.createActionTo$message) t- l' B; f5 ~4 Z2 o
(heat, new Selector (heat.getClass (), "stepRule", false));( Y* f) y# \7 n( T: T5 N
} catch (Exception e) {, E0 C+ _4 C7 K. B7 i
System.err.println ("Exception stepRule: " + e.getMessage ());
: O7 P8 Y9 G6 X }, E9 |/ X6 f' D: B* v3 a
0 {/ C- \. i0 T) f4 C try {6 S+ E$ O8 G& B: n) t" U& [6 g! h
Heatbug proto = (Heatbug) heatbugList.get (0);
7 s3 F6 Q) v# _' t P& M2 o Selector sel = 8 w8 M o2 O8 B' H1 u. }; Y
new Selector (proto.getClass (), "heatbugStep", false);
% O# s) a5 H+ d, c actionForEach =. v+ R# \! Z9 J( I& R
modelActions.createFActionForEachHomogeneous$call! h* A" z. a( f" ~( ?
(heatbugList,
1 b. B; J2 T8 s, J new FCallImpl (this, proto, sel,6 C9 r O) H$ J _) f
new FArgumentsImpl (this, sel)));& p$ r: h, u* t$ B4 J
} catch (Exception e) {
7 N( ^0 D! F% M' ~4 m6 A: B e.printStackTrace (System.err);1 [9 X5 X8 I P$ n- H$ y% x O
}
' r$ X8 C. }* u# j! @8 P ! y, o$ U. Q: o; K
syncUpdateOrder ();
3 A" h: D4 e% z" l
2 @7 O+ Z$ B+ a4 }( {" y" [. f try {
6 z8 k% w* e* R5 | modelActions.createActionTo$message 3 L# Q$ F2 B; q' h$ q/ |! J
(heat, new Selector (heat.getClass (), "updateLattice", false));
- @7 C) V$ Z7 C6 m$ M5 v$ V } catch (Exception e) {
' k2 M) I) [+ H4 x# y7 T6 ?' j System.err.println("Exception updateLattice: " + e.getMessage ());
8 U: `9 `2 `& K }
, _: g# v% n& }- S# k
. N/ [/ m4 T) X0 V% E" @ // Then we create a schedule that executes the+ c6 Z8 c4 O9 L/ c- I
// modelActions. modelActions is an ActionGroup, by itself it+ Q3 |; g9 R* B! O
// has no notion of time. In order to have it executed in; P- N) s* A2 T+ d z2 E
// time, we create a Schedule that says to use the
- ^" U8 e, v+ V // modelActions ActionGroup at particular times. This
% h: z1 s! c' ~6 g! }( u // schedule has a repeat interval of 1, it will loop every
, ~! i& u; y: k! v5 K: o" i0 D- n // time step. The action is executed at time 0 relative to
' H( Q5 ?; Y, p$ | y* n2 ? // the beginning of the loop.' G$ e! u: z8 H
' E! {* o& ^& m* C/ Y
// This is a simple schedule, with only one action that is
* d* F2 U; o% W' t1 v& r E // just repeated every time. See jmousetrap for more
& ]9 w6 _5 a- b- `8 B8 C // complicated schedules.$ W( M! h/ z/ @2 C. M) T
$ P! s0 l9 O$ c& u modelSchedule = new ScheduleImpl (getZone (), 1);
- M A7 Q5 [5 r: X2 y6 C modelSchedule.at$createAction (0, modelActions);
1 }3 O! w1 `9 O+ h) `
f2 D8 C; M7 ^! k: r2 q4 l5 { return this;+ b; f/ X, u6 \- m
} |