HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: R# {% y2 M4 `" H
8 \" X) A- u! ` public Object buildActions () {
; ^' ]1 W6 W& l" H( Z1 V4 K super.buildActions();6 @' A% S9 l( M' p" S
/ S( B! I/ |. j4 i6 l5 T' X // Create the list of simulation actions. We put these in; {6 J# v, u3 P/ ?3 k: |
// an action group, because we want these actions to be
9 A$ Z, C( L8 o0 P" x4 k // executed in a specific order, but these steps should! i# Q1 Q5 R$ {$ ]0 s" m
// take no (simulated) time. The M(foo) means "The message
; d& t2 h1 q0 t/ z: u, x+ t // called <foo>". You can send a message To a particular p7 R- v2 Z; S- \ k2 t0 Q
// object, or ForEach object in a collection.& K1 `0 I* _6 N% X7 q- M) U i
) V8 j, w: C/ R! V+ p+ [
// Note we update the heatspace in two phases: first run
/ H% P" |# i. K0 \8 l // diffusion, then run "updateWorld" to actually enact the
5 u4 `7 v& k8 A& w* h$ R" S N$ S4 y // changes the heatbugs have made. The ordering here is
- B" S* n& Z8 l2 z3 f // significant!
% [5 c( k- F) Q0 m& H 9 @0 l$ m9 W% S6 p6 R; X2 I. b
// Note also, that with the additional
: m! O& U6 ?; _4 ~+ w$ d // `randomizeHeatbugUpdateOrder' Boolean flag we can
! g0 j, `' k) ?# o/ p4 r // randomize the order in which the bugs actually run
! |9 `7 i1 `% |" o, \/ ]8 J: b // their step rule. This has the effect of removing any; [- f( [: V( u! w. Z' P. ^
// systematic bias in the iteration throught the heatbug
# t- d" W* Y1 G- c% J // list from timestep to timestep
! k8 Q$ K6 U2 K6 ^ 0 C. @$ m0 a5 D& l& c0 U7 X8 |
// By default, all `createActionForEach' modelActions have4 ]8 o5 o( ~0 N6 g
// a default order of `Sequential', which means that the
( R O% r7 z3 F // order of iteration through the `heatbugList' will be
1 p' o" K% v. c# y9 A // identical (assuming the list order is not changed) O3 }4 d" L1 [- D( p* g( ?8 j
// indirectly by some other process).2 F0 U0 w3 r1 w" u
! _; b& Y9 [# X# j5 Z0 B; o2 v
modelActions = new ActionGroupImpl (getZone ());4 l) Q) B! a0 Q* x
4 W) o! H; S$ O2 G7 H try {
3 B9 w) t; q" F modelActions.createActionTo$message
5 N* S: x+ ^& q% F (heat, new Selector (heat.getClass (), "stepRule", false));
$ \# D- A9 V1 d7 K) | } catch (Exception e) {
$ U- [# `. a* n: Z+ K System.err.println ("Exception stepRule: " + e.getMessage ());
6 e2 \* H' P; X7 s3 H }
( [' x& y; n* n9 V# e
2 s x5 T# N$ U9 w8 m3 ]; A try {2 s6 D; D4 |" B
Heatbug proto = (Heatbug) heatbugList.get (0);9 }1 k3 N2 L# I5 J+ `& ^& K
Selector sel = & {- U) T" k2 f. i# @
new Selector (proto.getClass (), "heatbugStep", false);( [) p2 m1 ^; d: U; z3 L
actionForEach =4 [4 [ ?6 j5 ?! V- V) a5 F% o
modelActions.createFActionForEachHomogeneous$call3 q( |8 B- S/ A2 E9 i2 _
(heatbugList,
3 U3 T) p0 u! Y& G8 ?/ o$ V new FCallImpl (this, proto, sel,
: ?. \( I2 x8 G e new FArgumentsImpl (this, sel)));2 a" k3 h1 b0 k/ G# U2 B
} catch (Exception e) {
# s8 y" Y# O$ d0 R: C e.printStackTrace (System.err);
1 v8 x3 Y( F# J. E }! u$ f1 s" ^1 N1 [7 {
: ^+ V3 D$ W' Y8 T6 n syncUpdateOrder ();2 h) V: C; `, E/ T
$ m3 m$ O# W+ m1 k% G8 H$ Z+ H
try {6 W/ z) ]/ Q, G' h+ [' Z* m6 ^, ` d
modelActions.createActionTo$message ) d8 [! N+ I2 K( i5 \
(heat, new Selector (heat.getClass (), "updateLattice", false));# i: j& F$ r) m) C$ n
} catch (Exception e) {
5 k2 Y$ ~4 \ ^. d- u* } System.err.println("Exception updateLattice: " + e.getMessage ());5 ?; I b+ |" L" b% P
}
, [: V: Q4 E- W6 R0 W3 s 2 H; x W& d' K$ K _- A
// Then we create a schedule that executes the
1 u, E# x& v6 G0 T3 c0 I1 N, Q7 X6 s z // modelActions. modelActions is an ActionGroup, by itself it- p8 r( K3 k- C+ n' p* o* I* Y
// has no notion of time. In order to have it executed in! A5 @& p2 G, z2 Q7 [! b
// time, we create a Schedule that says to use the
P' _: a* g' c0 b6 u6 Y& l3 E // modelActions ActionGroup at particular times. This- s' B) e" [6 |$ B* m5 h2 z
// schedule has a repeat interval of 1, it will loop every/ h+ w1 p4 |+ e4 k7 j) R
// time step. The action is executed at time 0 relative to
" i0 u. J( ] R; O9 B7 o- h. a/ E- [ // the beginning of the loop.
& _$ b; F( ]; N1 c4 {1 Q; k# h) u- [% f0 X1 a8 H; s, Q
// This is a simple schedule, with only one action that is
9 l% Y: f/ P$ H0 u, ] // just repeated every time. See jmousetrap for more
' F: Y9 P1 f5 o8 |2 r // complicated schedules./ P- p) P# Q6 l6 V/ Y; c$ P* c
6 X% N/ [7 J1 t' p modelSchedule = new ScheduleImpl (getZone (), 1);
* a! p) c) R! a* a& Y2 A modelSchedule.at$createAction (0, modelActions);
7 t% b0 J3 T! ~" h # p0 d% Y. z) {3 m; e: l
return this;5 A* r* H2 E+ G* i% C. k7 z2 s9 Z. }- r/ p
} |