HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' g4 I4 j3 B* D0 p4 ~* G5 n
8 }0 l9 O- p* e, s( ~
public Object buildActions () {, ^4 L/ m y, `2 V1 X# B p
super.buildActions();$ {% q7 i+ z# u+ T! e1 r3 Q" o, u
" N/ B7 r& A1 y- K4 x( I // Create the list of simulation actions. We put these in6 w) l4 z; x$ ]
// an action group, because we want these actions to be- U6 M3 E: n7 {8 ^ X
// executed in a specific order, but these steps should
4 z0 T, I* m ]& Y, B4 A* u // take no (simulated) time. The M(foo) means "The message
0 N' \! @, }2 Y+ b* v // called <foo>". You can send a message To a particular
$ P0 [- L/ b+ u1 F4 Q // object, or ForEach object in a collection.
. y- g+ Q( Y+ v9 W 0 p$ s! t. J, U/ `. t
// Note we update the heatspace in two phases: first run
5 j5 L/ g) @9 L4 u3 p. q // diffusion, then run "updateWorld" to actually enact the
( [' a! ?/ i0 N5 f. h4 ]' d // changes the heatbugs have made. The ordering here is
7 [* u. S- M! j" z // significant!
4 X; j& W2 B) [- x& S9 x+ `* S ) p% M( @' I' V$ Z
// Note also, that with the additional2 N6 L. T; }* s6 I* z! D
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ o9 Y6 J3 k; L // randomize the order in which the bugs actually run% O3 p3 p6 d' q. Z
// their step rule. This has the effect of removing any' a2 u1 E, L6 R" W9 m: l
// systematic bias in the iteration throught the heatbug* x) X& ^( v; Q G/ {# n" Y) t
// list from timestep to timestep" X/ u, S; T. R0 w1 @
. e7 _; f2 ]' B8 V4 g // By default, all `createActionForEach' modelActions have
" s& V* d# Y; f' h \, v' E3 }' v // a default order of `Sequential', which means that the8 u D. }$ B6 v6 T( Q# N- G* T5 Q4 p
// order of iteration through the `heatbugList' will be
! L- g- i# B( \5 m7 L& b+ I // identical (assuming the list order is not changed+ G o5 N4 R' W, ]. l3 E
// indirectly by some other process).* u& F, P, X% H+ F1 l
* X) E6 _; C J+ s$ p! n modelActions = new ActionGroupImpl (getZone ());
2 E8 j& F9 H7 I
4 a: a/ H4 d2 Y$ W3 V try {
- Z2 O: ~. M/ w9 G: q modelActions.createActionTo$message/ U+ X$ n" }$ ~: t* F; P
(heat, new Selector (heat.getClass (), "stepRule", false));$ i0 R% K- x+ V
} catch (Exception e) {
# c5 C/ i0 Z6 n% x% M System.err.println ("Exception stepRule: " + e.getMessage ());0 u( L' K+ I- g2 ]5 _5 ?
}
( A. y/ r6 k+ d" K1 U, k2 a
) o# J% J7 D0 `; Q try {
2 s! s: L' e- c" }4 |8 L Heatbug proto = (Heatbug) heatbugList.get (0);
$ o$ N. U0 F4 s4 e0 g& h9 a Selector sel = , }% D# D- Y/ d
new Selector (proto.getClass (), "heatbugStep", false);
, H1 N2 P, w: `9 w+ v actionForEach =6 q1 Z$ _5 l# H! O( q; u9 S) E
modelActions.createFActionForEachHomogeneous$call2 i% |' p; P0 ^" C
(heatbugList,
- r u; _$ o! T new FCallImpl (this, proto, sel,' l! ]+ @8 H, C# p* ?! A
new FArgumentsImpl (this, sel)));- v e% s6 B q2 R7 @2 G
} catch (Exception e) {+ l& f9 w p9 D5 t1 D. }% W2 D
e.printStackTrace (System.err);
: O. F! ~5 n2 I: E5 O0 l) ?6 i }, a/ m0 G) ?; i% w" Y% R
! q% x7 S; ^) p/ F3 c
syncUpdateOrder ();0 i9 W: k. l- Q5 C. Z
" Y0 l; }( M z
try {7 y/ _4 p/ P& p9 p1 ^
modelActions.createActionTo$message & y$ @* a; c3 U" J
(heat, new Selector (heat.getClass (), "updateLattice", false));: s* L3 B3 B! V9 |* _5 U- @1 G4 N
} catch (Exception e) {; S' r' I9 J2 B- V1 w" v" R# s
System.err.println("Exception updateLattice: " + e.getMessage ());
% |* C8 N n& V4 K* Q; { }
2 Y* e1 o& z6 J2 c- m
+ r/ n7 ^- }! @* n! h // Then we create a schedule that executes the7 ]9 Z6 X3 ?! V) p
// modelActions. modelActions is an ActionGroup, by itself it
% A+ Z; a# i) d- T! N" g- D // has no notion of time. In order to have it executed in/ h; d, h. h8 k: L# N! y
// time, we create a Schedule that says to use the
9 B& f0 T7 J1 q. q6 z // modelActions ActionGroup at particular times. This
0 `, k$ w+ R# y1 K& R1 v // schedule has a repeat interval of 1, it will loop every
: x9 _5 i$ e& n0 I) M, C) j" g // time step. The action is executed at time 0 relative to
4 `# N" E( ]* h) j2 Q // the beginning of the loop.
0 m9 t( z! c: Y7 N, K. F7 R; J L( b: l/ W0 e7 ^7 e/ a
// This is a simple schedule, with only one action that is
3 X: w" S. @# z. D$ P+ N; v7 C // just repeated every time. See jmousetrap for more
0 F* N! k. v2 E // complicated schedules.
: B0 z) A0 X, X. |, v . B& i1 l+ T3 T( w( v
modelSchedule = new ScheduleImpl (getZone (), 1);
) s. Z6 c9 S: a1 h6 E* T& i$ g modelSchedule.at$createAction (0, modelActions);) a1 w4 H3 m) U% A+ W
, l5 L- L% q. G ^ return this;& ]* V% Z Z6 p8 B* `
} |