HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" C& F/ Y' r+ f- X; v; m5 @6 h0 ^3 l
public Object buildActions () {
# c) u9 f# C) F super.buildActions();! l! x! Q6 X- ]7 r
7 e& e! Y+ t& O+ D0 V
// Create the list of simulation actions. We put these in l) |% v5 }& T5 w6 K
// an action group, because we want these actions to be
7 Q7 a' U' I% D7 Y1 L$ P7 X3 k // executed in a specific order, but these steps should
4 G/ q2 V! z: s9 E // take no (simulated) time. The M(foo) means "The message2 n8 T0 [4 c* w" c- Z7 V' ? d
// called <foo>". You can send a message To a particular8 D& f# y {- B
// object, or ForEach object in a collection.7 D v. q5 A C2 C" U* C
( \( c# _2 z, ?' P8 I/ ~ C+ z // Note we update the heatspace in two phases: first run
6 t& x5 i$ c+ g! u1 ~ // diffusion, then run "updateWorld" to actually enact the
4 t6 Y) @6 V' Y1 V/ F1 ?1 \ // changes the heatbugs have made. The ordering here is' g3 h. `: [: e
// significant!2 V0 l& J l B' O
1 |, j; { N0 ~
// Note also, that with the additional, V* b3 a% q; _) `% R
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 s: b+ m- D: @9 i* _7 n // randomize the order in which the bugs actually run
3 c" i' I5 J0 ]! o // their step rule. This has the effect of removing any6 |. l1 p1 }' v+ M7 j# g
// systematic bias in the iteration throught the heatbug# @, u- X5 q" L+ F) d
// list from timestep to timestep3 j/ c4 i$ Z5 O
9 L7 ^! {6 A* M) G/ P
// By default, all `createActionForEach' modelActions have
, }1 G/ l3 f3 C5 i" d3 J3 J# W$ G // a default order of `Sequential', which means that the5 U- L% R- b1 m/ m7 m4 Y9 q
// order of iteration through the `heatbugList' will be
; }' L* t# v2 U5 I" l% A2 o9 O // identical (assuming the list order is not changed7 Q3 K4 u! G: X7 d
// indirectly by some other process).
3 k5 m/ D9 |1 }* D6 g3 ?$ B& C+ S
- [3 N3 g$ Y m' j1 ] e+ p7 @ modelActions = new ActionGroupImpl (getZone ());+ l, `/ Y% M, z$ K ~' A
* N+ A4 A. C, Q$ X- Q/ K try {$ Q" `5 y7 ?! I6 |6 ]
modelActions.createActionTo$message
, p+ M; m9 A, P2 V7 U( g9 Q4 q (heat, new Selector (heat.getClass (), "stepRule", false));
, A" m5 t9 g5 E$ H5 @7 X' i T7 j } catch (Exception e) {
5 f/ r) F( J* t$ E. y$ g System.err.println ("Exception stepRule: " + e.getMessage ());
7 t( \$ K2 m3 o+ M! l }
& i; v# U X) Y
7 B: J6 B3 X$ m p; ] try {
: E. O( ]* a9 b Heatbug proto = (Heatbug) heatbugList.get (0);; D9 s; @. {7 k* J9 ~3 a
Selector sel =
( z1 p% D$ C8 L4 C6 p new Selector (proto.getClass (), "heatbugStep", false);
( T& D' A# S& Z. a3 Y actionForEach =5 @# x2 r( J s9 _$ V2 j+ b# D) N
modelActions.createFActionForEachHomogeneous$call
1 h9 [2 l1 m4 Q" Z; E8 b' R) [9 t (heatbugList,
0 n0 q: C6 {! G! K2 Q- V! t5 V new FCallImpl (this, proto, sel,- j2 C* T$ r; I9 ^" [" N
new FArgumentsImpl (this, sel)));
7 g2 i/ j! h& s% e N1 i# v } catch (Exception e) {$ b" c- `4 b& \( I3 e* p: Y' {
e.printStackTrace (System.err);" T2 `8 C3 m$ Q) m% O' j7 m
}
% g& X3 G7 F, q* @3 \6 t3 Z 2 b0 {4 n- z, V' m
syncUpdateOrder ();* g8 t3 Z6 J) D
, Z% N. L5 \9 p4 l( j) v' r$ N
try {
( U. E+ i1 |9 v3 n& ~5 Q. ^: d( q0 N modelActions.createActionTo$message
W8 _2 Q8 p% e# [8 I, f) a% `/ t/ [ (heat, new Selector (heat.getClass (), "updateLattice", false));
- K4 P: C+ Y1 I6 V' _ } catch (Exception e) {; L4 p3 L, z- y1 s3 O3 I
System.err.println("Exception updateLattice: " + e.getMessage ());+ Z6 H, N+ \' f' Q% `$ }1 m# h0 J7 i1 C
}
6 h f0 Z% {8 I: t7 L9 A1 h
* w( a4 o7 d3 e% N8 M& I // Then we create a schedule that executes the* Y& Z( u! w3 T( z2 F7 J6 k* T
// modelActions. modelActions is an ActionGroup, by itself it) i4 k3 h9 t% E! f( h2 \
// has no notion of time. In order to have it executed in
* _4 o( ]. f5 k- S% a // time, we create a Schedule that says to use the0 q; U& w- p" s: z. [
// modelActions ActionGroup at particular times. This8 x. h* B* { g) v2 E5 a
// schedule has a repeat interval of 1, it will loop every4 m/ h8 p' C3 d* `3 p7 k
// time step. The action is executed at time 0 relative to' Z" m: k# }0 l t
// the beginning of the loop.
+ _4 g8 f1 [: l# j
* C, l& A' t$ r // This is a simple schedule, with only one action that is
. ?& `$ }0 ~& C // just repeated every time. See jmousetrap for more4 v3 P( c5 r, x c( M* L2 U* J6 [
// complicated schedules./ y6 m# q. `/ y" B( }
* C. W/ H" D) k! V6 w/ S( l9 E
modelSchedule = new ScheduleImpl (getZone (), 1);+ O: `+ H; @% h, l7 E8 n" i
modelSchedule.at$createAction (0, modelActions);
5 Y1 g, ^/ `" l2 s; B8 k9 x O0 W7 [% T% R: \: m8 h; \
return this;
5 x) n7 X i- Y' i8 }+ C G# p } |