HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" o2 a5 D/ D I* b) F& E
3 e9 K! T0 B, ?4 p( R public Object buildActions () {
7 p p7 Z2 [; P8 P: y$ j super.buildActions();
* X0 \9 q' i" s5 T8 n7 t+ u7 q 2 k& V# W5 d6 Y
// Create the list of simulation actions. We put these in
. _. ]; q3 b2 d. W" {' T$ V5 R$ G // an action group, because we want these actions to be9 q2 V/ I/ {& k6 r% O0 T( z* Q
// executed in a specific order, but these steps should
# f2 E0 k$ c9 x! S) d9 e4 J- r' j // take no (simulated) time. The M(foo) means "The message( u+ a( d# K( U9 f9 }1 u
// called <foo>". You can send a message To a particular
! s* a7 F$ u0 z1 {# i% z0 w // object, or ForEach object in a collection.2 E! c( Q; z/ k) ~
' B+ v( W5 F" M( J // Note we update the heatspace in two phases: first run
$ Z, |/ L# o2 z6 c // diffusion, then run "updateWorld" to actually enact the ?5 @( k3 _) t8 M
// changes the heatbugs have made. The ordering here is
7 N- Z# V6 L! l4 m \ // significant!
/ w) [& O# ]0 V: m R ^! l
& _+ S+ y( b8 b: ~ // Note also, that with the additional5 z5 E" N: F; _. R5 t4 B
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 f) F6 }$ M4 h. M
// randomize the order in which the bugs actually run( I! e/ l, u7 {# J# H
// their step rule. This has the effect of removing any4 c- `& ^' c% n9 e# i( M/ u% x1 Z
// systematic bias in the iteration throught the heatbug; s; h% I1 W" p# [4 U
// list from timestep to timestep
7 Y' g3 {6 ^+ X. [2 a & X I1 T; B: B6 ]. H( l
// By default, all `createActionForEach' modelActions have
- L6 u# a5 e; g0 {+ k // a default order of `Sequential', which means that the
$ d1 s5 H; j1 a' Z0 b$ t& g // order of iteration through the `heatbugList' will be, S: b" K3 W, \0 G( [
// identical (assuming the list order is not changed0 D: V5 F# t' H( Q1 w6 O
// indirectly by some other process). G+ e/ a" G# N% g4 S
" n% ^) }; Y& s+ R; Y$ u9 _ modelActions = new ActionGroupImpl (getZone ());
2 \- V* D! L9 \( i+ ^, H `% x$ a" F4 }- B+ N- N; c. r
try {$ z% P& n0 @. t' p+ |+ H
modelActions.createActionTo$message
& c* h" d: q: J* G& m5 y0 g (heat, new Selector (heat.getClass (), "stepRule", false)); N2 Q8 V' P$ @% K3 X
} catch (Exception e) {
0 L" l. J' p; R8 }7 i System.err.println ("Exception stepRule: " + e.getMessage ());
{) p8 } {8 f; P& s }
% j4 e$ F6 ^6 J" H+ V+ `1 c* Y
# x2 X: ?! P- L9 t5 o0 U try {8 C( D" R: _/ N0 u/ [' G$ {
Heatbug proto = (Heatbug) heatbugList.get (0);
" {( M, _: S* }+ z# R- x% M Selector sel =
; w: {6 [$ j6 ~; I. a. S1 ], s1 d new Selector (proto.getClass (), "heatbugStep", false);. N- y* {- {0 m# o& l& E- b
actionForEach =( B! ^; |, G0 o+ L- }6 F
modelActions.createFActionForEachHomogeneous$call3 {% I2 {. N, v @
(heatbugList,- z4 e: g7 ]4 d+ d. k4 S4 W
new FCallImpl (this, proto, sel,
& V7 b* W: I$ X: o$ k new FArgumentsImpl (this, sel)));
; N% v7 X/ F* R* Z8 d } catch (Exception e) {; Q* E3 \. T$ _4 |9 R! c; J7 r
e.printStackTrace (System.err);
2 `/ J. o0 y6 }+ ]5 ?; A }/ y0 G- y5 K+ Q- r
8 S0 }, @ r) [
syncUpdateOrder ();
7 t, y0 j, n, g/ T. W8 j8 m
7 V, a7 ^) A/ ]: V8 O2 i; \4 z& } try {/ f( K9 K4 t7 ?& j& K
modelActions.createActionTo$message
9 J3 g6 C$ H; r r3 d (heat, new Selector (heat.getClass (), "updateLattice", false));
, c" s, c& q; d+ O } catch (Exception e) {
f( G! R2 [. X+ t2 f System.err.println("Exception updateLattice: " + e.getMessage ());, b8 L' I4 w( O3 [& k J& _
}" Z" I" ?5 z, C+ g9 v1 w
) ~1 x: H6 }, n" u4 d* `- b // Then we create a schedule that executes the( M& H. x f, X- g! `1 i% q
// modelActions. modelActions is an ActionGroup, by itself it
2 a8 ]" V: e2 [- v8 W/ k // has no notion of time. In order to have it executed in
7 [5 d) e# K/ |, q! D3 M9 @5 ~ // time, we create a Schedule that says to use the! M9 S* ?6 H; B' M. V
// modelActions ActionGroup at particular times. This
- e) q( G4 }" m! r8 J // schedule has a repeat interval of 1, it will loop every
/ N1 L+ p6 c2 s% N // time step. The action is executed at time 0 relative to
0 ^, H) H9 L* m // the beginning of the loop.) k5 [# h U' }! i. q7 E& d C
, b1 D2 q0 ]! O: l2 Y
// This is a simple schedule, with only one action that is' l% p0 a) y9 V& k- ]
// just repeated every time. See jmousetrap for more
/ z, v+ G4 [. _3 X2 @5 ^ // complicated schedules.
9 X8 b1 |2 D5 t3 O, D8 \+ t- Q
1 w* c& j& d6 \ modelSchedule = new ScheduleImpl (getZone (), 1);) o9 {& I% M. q! G
modelSchedule.at$createAction (0, modelActions);, r7 e, W; T/ }9 {; P0 ^
0 ^ e6 B) l" z, k% S5 c/ ^ return this;
: T' c: m6 F: |( G' R } |