HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 w9 _* P& e3 r2 h& B7 U! f7 ~
% \/ A; b) M* [2 @( i public Object buildActions () {& z; V' c- ^4 F( E' j
super.buildActions();
1 P. Q1 m# E7 z) i# k
3 q6 p0 y$ M& y // Create the list of simulation actions. We put these in
4 E* `) R+ F, H4 W // an action group, because we want these actions to be
( x; H6 ~! @& F6 l2 E- W5 J. h // executed in a specific order, but these steps should1 J o& [: T4 V$ y' J# }4 d5 |" o
// take no (simulated) time. The M(foo) means "The message
+ U. C, Z2 v$ T& U // called <foo>". You can send a message To a particular
$ h# o3 U- ]+ a/ t // object, or ForEach object in a collection.) F; M* H( U }
0 M3 ^' S$ ]# A4 I+ b; Q- E
// Note we update the heatspace in two phases: first run
5 A0 d, g+ s; Y& |: Y0 Q // diffusion, then run "updateWorld" to actually enact the+ K3 K' Z# L3 J, B5 g
// changes the heatbugs have made. The ordering here is
+ c. R3 \6 Y( ~ // significant!
# K/ t0 `6 v9 B$ Q, u6 u ]2 C 1 a# q; \& k! U
// Note also, that with the additional
0 Y; N. X' r8 v // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 O1 @7 K' w/ ?) I // randomize the order in which the bugs actually run, z( }1 h, c! Y4 J6 ]
// their step rule. This has the effect of removing any
4 V6 k) G, h$ }# Y; { // systematic bias in the iteration throught the heatbug
6 Q* B# f# U3 P$ X+ F // list from timestep to timestep) E7 f" R- |# i! B1 O6 ?/ X
* j! @, N* j; H: e // By default, all `createActionForEach' modelActions have
! c5 B4 \2 Q. w9 c, {8 i# a, ^4 | // a default order of `Sequential', which means that the3 h. d* {, i4 m$ _1 B
// order of iteration through the `heatbugList' will be9 f2 h# a, A# x2 h3 m
// identical (assuming the list order is not changed. Y1 j) t1 w$ n% n9 j; e) X
// indirectly by some other process).
8 a! K0 z, D( M. a2 H
) @0 _0 m! W0 x2 j. J modelActions = new ActionGroupImpl (getZone ());0 D; |" h: c' e2 j/ \
# W, q8 _4 ~( P6 g) r8 n6 h try {9 b8 V4 S% B2 s: ^/ W
modelActions.createActionTo$message
$ S5 h. M# ]- C2 P4 I& K. k (heat, new Selector (heat.getClass (), "stepRule", false));- \9 N9 I B4 O& s/ n
} catch (Exception e) {- |, J! V: n7 }4 h' @
System.err.println ("Exception stepRule: " + e.getMessage ());! i, T+ V4 \- F: s e
}
2 A2 D! p5 x v' I8 [) U6 G6 y {. m* X/ R+ C% j% Q4 N
try {
# j1 | u2 M m8 B% B7 d Heatbug proto = (Heatbug) heatbugList.get (0);
: w1 y7 k v9 a' d Selector sel =
2 p B: {- \7 Z8 w. D4 } new Selector (proto.getClass (), "heatbugStep", false);5 v5 H0 N9 W% d2 F, w
actionForEach =: O4 k4 v, M- g3 m* q3 S9 B
modelActions.createFActionForEachHomogeneous$call# u- e4 e* N. o; x6 b
(heatbugList,
8 c/ {- h( S, X: |2 f, d new FCallImpl (this, proto, sel,2 g4 i' L/ M4 y1 j( Y
new FArgumentsImpl (this, sel)));
- E7 h' g) \( R( L4 \ } catch (Exception e) {& o" ~3 O! m U$ Q! Y2 v
e.printStackTrace (System.err);1 B, n& Q( E; [/ B0 d. D% q* \/ _
}
* u1 r7 E; X" Z6 [( h % n8 }. [4 ~% `& m o6 ^1 ?
syncUpdateOrder ();0 ^7 I( z/ |$ _9 P
2 ^9 @+ {" c3 s7 z/ \ try {
e+ I6 _+ k$ D3 _9 U3 z modelActions.createActionTo$message ! d R' z, s& A4 J! b, @
(heat, new Selector (heat.getClass (), "updateLattice", false));3 s X( L0 ` r5 N' ?
} catch (Exception e) {. v& W' M2 r- {: p
System.err.println("Exception updateLattice: " + e.getMessage ());
: w Q1 e5 u7 K' h/ n0 q }! h6 |" ~) t/ o5 F
: _( w8 E) L8 ?' @$ G7 o // Then we create a schedule that executes the
% T7 S) W4 h# N2 w // modelActions. modelActions is an ActionGroup, by itself it
+ d6 ~9 ]1 K$ b- _0 @% v5 p7 e. }1 w // has no notion of time. In order to have it executed in
" O4 d# x0 G) R+ p2 g0 e/ k // time, we create a Schedule that says to use the
3 T- s/ ?9 C8 J" N // modelActions ActionGroup at particular times. This
( F/ d: h4 N8 l2 {* v* [ // schedule has a repeat interval of 1, it will loop every/ \6 F9 p2 W, ]; V2 U& P
// time step. The action is executed at time 0 relative to4 ] h4 M4 @1 }" `
// the beginning of the loop." S8 Y, F! X$ e- U
+ m* G# {1 a5 d) b. V: a // This is a simple schedule, with only one action that is
1 S" i% m d5 T // just repeated every time. See jmousetrap for more
9 ~% z+ p8 D1 E# W3 _ // complicated schedules.; l0 l# Q1 `$ E$ A% q
. s! ?3 `/ i4 f0 b- j
modelSchedule = new ScheduleImpl (getZone (), 1);
3 T0 M& P' E6 s. n. S9 o5 k9 ^' { modelSchedule.at$createAction (0, modelActions); F. D0 a% Z1 Y) O
* o8 [( X) w$ O7 H( S: A return this;9 S* _9 j) W- A- m% m
} |