HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 Q6 p) f$ t5 e! Q9 j6 S0 w( f' O2 f: X. V( Z
public Object buildActions () {7 `) w: P/ B, E- J8 N* V+ A% I
super.buildActions();
' d* x7 A& C8 y+ `) ~) k" c
2 r' [' Q, J; \, \8 L0 N/ Z // Create the list of simulation actions. We put these in
, l0 d% Z, o2 ~ // an action group, because we want these actions to be
9 t7 z; X9 S' P; }/ j2 X( b // executed in a specific order, but these steps should
0 N: |* x9 ], J' y' J" ^' L' V- L' D // take no (simulated) time. The M(foo) means "The message
0 A# M" p! Z/ W9 M& t) q. d // called <foo>". You can send a message To a particular* r, U X& x% h& u" G
// object, or ForEach object in a collection.( x- @* A3 m! Z1 _. N7 x- [4 m
U2 e" F L4 m6 }: q3 Q // Note we update the heatspace in two phases: first run: o7 @! N7 g$ r" a+ c
// diffusion, then run "updateWorld" to actually enact the
! N" u; |( e8 g6 W // changes the heatbugs have made. The ordering here is
/ g! t5 w# V* e+ r1 L // significant!
# C8 R& U" `& R! Y' N' N
. T& Y6 k0 Z( X' k, _0 a7 v // Note also, that with the additional7 H/ `* J5 Z. Y: j6 U. n" \
// `randomizeHeatbugUpdateOrder' Boolean flag we can) Y( ~& _! G/ m' @/ a! h
// randomize the order in which the bugs actually run5 r* U7 l6 X4 O. G. Y6 G
// their step rule. This has the effect of removing any
. }! ~* {. J0 H! u% T& { // systematic bias in the iteration throught the heatbug# y9 v9 A/ I6 C) O& B* T% F5 U; \+ p7 }
// list from timestep to timestep
1 V$ h. k$ x4 B `/ D " e7 R6 L$ c- U4 w# H' L
// By default, all `createActionForEach' modelActions have4 N; z1 ~1 _: X7 H/ B! l1 ?
// a default order of `Sequential', which means that the) E+ F4 t: D7 S
// order of iteration through the `heatbugList' will be& l! V* P4 I" M: E2 I' b) K
// identical (assuming the list order is not changed3 P5 {! K3 ? N- P( X
// indirectly by some other process).
1 ?/ `) _3 N9 j$ ]( ^ : p) _- C& X, y, D0 L0 W) E0 j
modelActions = new ActionGroupImpl (getZone ());: s0 L- w: i s' N( l4 ]
7 |/ U9 z/ t: V$ f' I: ?% J0 |+ {; G
try {
5 q! ^4 a/ k* [1 S2 B modelActions.createActionTo$message
3 {0 a! R6 K' Q( Y (heat, new Selector (heat.getClass (), "stepRule", false)); X a- X1 w% T
} catch (Exception e) {
" ?1 F' V' R) v w' C3 n System.err.println ("Exception stepRule: " + e.getMessage ());
7 Y+ \; f% B8 A5 [ }- Y; \, r5 ?$ w! x; o' M* B
/ r5 v% L8 L! I# x
try {$ g) {7 E; _7 g; P
Heatbug proto = (Heatbug) heatbugList.get (0);, i8 R) I" A! ]. B8 ~8 }
Selector sel =
7 @; H% @- C3 h- F: R new Selector (proto.getClass (), "heatbugStep", false);
& V" q) w' H: C, z" e" ?+ T& A actionForEach =5 l4 W% H! [/ X9 B. y7 ^7 _
modelActions.createFActionForEachHomogeneous$call
* |) J9 Y/ _ f$ w! [ (heatbugList,! k2 A- @ A2 J4 O* e$ O6 B' ]( l! ]
new FCallImpl (this, proto, sel,
, I& a1 j9 f! r" T0 O! n new FArgumentsImpl (this, sel)));
D$ T i) p% o) U: p } catch (Exception e) {8 }0 v3 `8 F$ l' [' O: w/ u* A
e.printStackTrace (System.err);
! y, D% \3 d: b6 ?. n }
- Q# G+ z3 ~1 h
1 @$ d- q* X3 T9 n8 p$ W syncUpdateOrder ();( O6 w9 e. ~" k' O d
8 V" p3 T+ |/ {* i$ P- o
try {7 g: O" O8 c" k, h! ?) ?
modelActions.createActionTo$message
1 f7 T/ n) M; R (heat, new Selector (heat.getClass (), "updateLattice", false));2 L: \; b$ a; k' @- C: j7 y# e
} catch (Exception e) {+ W: t5 u) M; x/ _& ?5 f! Z
System.err.println("Exception updateLattice: " + e.getMessage ());. Z1 b$ A$ ^/ Y' e+ @
}
1 U$ X/ T, J; n# g F; p! ?; }
+ n0 Y- f. V$ ^" I) t6 d // Then we create a schedule that executes the
5 v1 P& O+ o9 j6 t // modelActions. modelActions is an ActionGroup, by itself it
; x$ x5 l/ w% M& D/ D% X // has no notion of time. In order to have it executed in) [7 B0 x6 k' ?5 @) Q
// time, we create a Schedule that says to use the+ w4 m2 _/ P* v4 O8 K4 I7 j* E
// modelActions ActionGroup at particular times. This
N9 u# B" t3 C; M // schedule has a repeat interval of 1, it will loop every4 U- b8 v4 O7 `& A" K( ~3 [: a
// time step. The action is executed at time 0 relative to/ A k+ y0 z- i
// the beginning of the loop.9 d U5 k" P# v4 T4 L
+ B( p- z9 [) z4 Q! y4 H
// This is a simple schedule, with only one action that is G7 |& K1 s8 h6 D( \9 O% X2 }. Z# F
// just repeated every time. See jmousetrap for more
1 Q2 S3 O6 A3 C( z // complicated schedules.3 o2 v8 b( Q2 \2 I
- d, x& f5 C2 X, s" |7 q5 r& v
modelSchedule = new ScheduleImpl (getZone (), 1);
2 r z# ?. I; |: Y, ?( U" Z1 W modelSchedule.at$createAction (0, modelActions);
5 l& t4 C( P( j: k6 T: Y3 T
1 e. ^" p% y4 E5 f5 ~% t+ [ return this;
4 h0 }5 ?0 x1 w1 m, _ } |