HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" v: g! m9 [3 L) x2 k8 A! j& r, f! q) g+ p
public Object buildActions () {! k" |8 J3 N1 {1 C5 i
super.buildActions();5 D0 R1 r: B/ E: i' z
+ g2 `4 u4 T2 f) r // Create the list of simulation actions. We put these in
' w# }5 B4 h0 v/ H // an action group, because we want these actions to be; K e8 c- T. k" O! X: W
// executed in a specific order, but these steps should
' ^, W9 U6 s; ]3 a# a // take no (simulated) time. The M(foo) means "The message9 q, b' c- k+ {2 N6 v' c
// called <foo>". You can send a message To a particular
) _* R5 e/ X. ?+ i // object, or ForEach object in a collection.9 |; t4 q) [; C+ l8 l. _# E4 F
# G3 g. ^ X0 h& U4 b4 Z // Note we update the heatspace in two phases: first run
% S! ?7 H/ y. }" G' P; k // diffusion, then run "updateWorld" to actually enact the( h4 z7 n( I/ b+ Y3 R
// changes the heatbugs have made. The ordering here is
9 Z2 G. P- g3 M1 v+ _8 A // significant!
/ Y8 x8 c2 d$ d8 G7 |
. y6 K; {" f0 g // Note also, that with the additional
$ p/ |; {: S: L6 m. H$ C // `randomizeHeatbugUpdateOrder' Boolean flag we can
# ?4 P. s7 ?6 h( \2 }3 q // randomize the order in which the bugs actually run6 E' S- i( J6 o' Z& h, {
// their step rule. This has the effect of removing any
9 E# }$ ]0 y; t% \0 o% \: F9 G4 l // systematic bias in the iteration throught the heatbug5 @ b+ Z2 j( A: C2 F: g. T0 X# ~
// list from timestep to timestep$ Z- z! P+ H/ I8 i
; i1 E% E1 l$ C. Y+ L" C+ K# O1 r // By default, all `createActionForEach' modelActions have) _: E8 K! ]$ p4 k% o r
// a default order of `Sequential', which means that the" Q+ `( E0 F- O2 N5 Y* O
// order of iteration through the `heatbugList' will be& ^" W% a; \4 ?1 O. c$ t3 e
// identical (assuming the list order is not changed( a3 d* _. u p
// indirectly by some other process).
9 r/ f4 d- P* S, ^( R3 f7 G 8 {9 a! v2 y3 @
modelActions = new ActionGroupImpl (getZone ());
( B% z- y6 K0 w$ p$ ]. G4 |! l# x% }; D0 N6 o* j+ ?+ Q' I
try {7 A b% v5 |7 v- z6 e# p, b
modelActions.createActionTo$message* i2 V8 K. [/ m V4 ?7 V7 W9 m; |
(heat, new Selector (heat.getClass (), "stepRule", false));
7 ^ z- N: J" O% _, x } catch (Exception e) {
, Z0 f* o: l" {: K, C8 W7 t) Y System.err.println ("Exception stepRule: " + e.getMessage ());
0 I3 o4 ^! u: n" ?% ~4 z }
% e- s& G3 C/ E, N
9 i! Y6 h& k1 ^$ k: C3 C try {
- V) g' \( s- x4 x7 s2 X. z Heatbug proto = (Heatbug) heatbugList.get (0);1 ?! U7 |6 M, p- e' @- H. o8 ]
Selector sel = 7 B, z0 k: ` S( A
new Selector (proto.getClass (), "heatbugStep", false);: r. U8 [8 h, O7 [3 h% ^9 w- J
actionForEach =% m4 w0 \" z/ Q+ D
modelActions.createFActionForEachHomogeneous$call
7 w; r7 R# a2 c5 R (heatbugList,& c) J3 Q( Z& Q
new FCallImpl (this, proto, sel, Y5 V& U+ _% q8 D+ P
new FArgumentsImpl (this, sel)));
! `( H, a( ^7 q+ d } catch (Exception e) {# n+ n0 g: b6 H' F8 w0 j: i
e.printStackTrace (System.err);
/ o$ f; G# f4 Z* S( S0 E }
0 E8 A+ E. V; |" W, V5 L0 D1 k 7 x- V7 Z7 D8 q F# x: D* R
syncUpdateOrder ();
D2 y6 ?" J8 A' l/ b9 V# s
, C* j8 L4 C& Z, h3 i4 U" e' |8 K$ N4 P try {
: s& Z2 u6 c5 C3 P \ modelActions.createActionTo$message
/ @# ~1 `! S" J# F. @5 h: S$ ` (heat, new Selector (heat.getClass (), "updateLattice", false));
! C1 z% j4 c! K8 ^ } catch (Exception e) {, b4 C5 ] z5 p' @$ N
System.err.println("Exception updateLattice: " + e.getMessage ());) o( a8 f3 g# z$ R
}
5 ~' ~ V( O' d) r$ g
. C+ j% W- y. m2 a* W1 i // Then we create a schedule that executes the0 ]: b; u- Q @
// modelActions. modelActions is an ActionGroup, by itself it. a- j \. N+ k% l4 a4 F
// has no notion of time. In order to have it executed in
/ X8 e- u( W: f& k& x& W; r // time, we create a Schedule that says to use the t* v9 G. [! t: g# O' W
// modelActions ActionGroup at particular times. This0 J' w4 O2 l% B1 M
// schedule has a repeat interval of 1, it will loop every' U& e% K5 Q( ?* |, L0 a* }) H
// time step. The action is executed at time 0 relative to
" o5 I Y6 E% c% ~' q' J7 t // the beginning of the loop., x [/ k( ~( u2 V1 D0 c& y9 R
/ ~8 @( R. s) u: k1 O // This is a simple schedule, with only one action that is
; \ O; W+ W( j // just repeated every time. See jmousetrap for more
9 \1 c" X/ ^4 j // complicated schedules.
% L$ Q3 @) M2 }# Q f& c* E! S + Z( c, b: q% C; A) }- [ X
modelSchedule = new ScheduleImpl (getZone (), 1);
+ ]4 y. w8 x+ W' R( ~: @* e modelSchedule.at$createAction (0, modelActions);
' j. g+ |2 ?( U& k , b8 q x: E' D+ g+ i( x/ n
return this;
7 X1 S+ l& W1 a% G } |