HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# w3 M( |) x, m. @1 R/ o
F4 j) N6 z* E8 p% ~' R4 N9 Q public Object buildActions () {
" i" K/ R* O1 s$ {$ R super.buildActions();3 D7 [/ U8 n5 H" \" i* Y0 K7 Y9 f
) Q( Z- @3 o& x6 ^0 Y* o# b // Create the list of simulation actions. We put these in1 A% @, H" U# R! @
// an action group, because we want these actions to be
) }+ {9 ?" d9 n, S' _8 }" {# t( P // executed in a specific order, but these steps should
8 m' P0 k8 b* G' A // take no (simulated) time. The M(foo) means "The message. b4 b; V2 E- F' W( Q7 ~3 L# b* b
// called <foo>". You can send a message To a particular
. p: ^( ~: C) L% b, d // object, or ForEach object in a collection.
/ W( F n- r! M' Q/ K% l 6 J1 Y: V a; Q L2 B0 {2 P( b
// Note we update the heatspace in two phases: first run. B4 J4 c* Z. q+ ~
// diffusion, then run "updateWorld" to actually enact the
4 l& |3 ]. d8 ]+ H // changes the heatbugs have made. The ordering here is
7 G" E9 J9 B1 U7 }. [ X$ \ Z // significant!9 F% U/ [2 |4 a9 G5 Z2 u
% P5 w% c, |* {2 f6 ~* R
// Note also, that with the additional
9 M) G2 G/ `3 f // `randomizeHeatbugUpdateOrder' Boolean flag we can$ }) _6 a) h- c
// randomize the order in which the bugs actually run" f5 G, r5 J# T. ?% a$ R
// their step rule. This has the effect of removing any
7 G9 f+ M8 y. X5 M4 {$ _5 R // systematic bias in the iteration throught the heatbug6 Q* |: z+ ]0 c) y F' @. D) ?
// list from timestep to timestep* p+ H& ~% a3 }0 f" H- z
7 e; P3 q/ M6 A* q, g
// By default, all `createActionForEach' modelActions have
+ R \. _' c' M7 G // a default order of `Sequential', which means that the8 ~& s" X$ Q( |* V3 m! c1 {
// order of iteration through the `heatbugList' will be& l$ p: D0 ?9 [% j4 m& k* [7 S
// identical (assuming the list order is not changed4 t/ m+ v3 ~4 _& ]1 W
// indirectly by some other process).
- ^+ u! A* W; E/ Z' D; D $ p, O4 ]7 B, d$ l3 x* E* V
modelActions = new ActionGroupImpl (getZone ());* f! A) \1 o- }
( F/ A3 Z6 u* J: r: d) }
try {& _- o6 A& N0 K! {! H* F7 B
modelActions.createActionTo$message: O8 {7 h% S# D$ u; |( O
(heat, new Selector (heat.getClass (), "stepRule", false));8 m# G5 e$ W; G
} catch (Exception e) {
5 C; t5 a7 J4 \/ |! N! h System.err.println ("Exception stepRule: " + e.getMessage ());) w' W0 X! m; P) h
}! p6 g% z# k6 L0 s" d0 R
% Y: A0 b* X$ K8 h, P3 x2 I/ Q! P3 Y
try {
; C4 ]2 S; b: P8 H9 A% T8 z Heatbug proto = (Heatbug) heatbugList.get (0);
" Q! O0 h [( F" a. ` Selector sel =
' X, k9 g# P7 \2 }# G5 p new Selector (proto.getClass (), "heatbugStep", false);
) q0 u% |( |; U% o. P! Y actionForEach =* o4 k; P- B8 D$ ^6 R
modelActions.createFActionForEachHomogeneous$call' i, Y" S0 P7 \: {
(heatbugList,
7 j4 F# f1 ?) | new FCallImpl (this, proto, sel,
3 U* ^8 i k- z9 E9 |6 l new FArgumentsImpl (this, sel)));
, n; _) _9 s" J+ O$ }+ t- ]7 o$ G1 v } catch (Exception e) {( z" C4 w- y T6 q+ Y7 o4 l* m
e.printStackTrace (System.err);
, N! R* G8 Q- C- q }% u& [6 p( e7 x& I2 A+ L
0 Q4 [0 T6 P0 Y/ U. I! s syncUpdateOrder ();+ W- R; S& P# [8 g9 D
5 p- \* P$ j1 _ c4 C' {6 K6 Q try {4 V3 F# Z4 G V K5 h
modelActions.createActionTo$message 0 b( f3 d; J7 S0 O" h: R
(heat, new Selector (heat.getClass (), "updateLattice", false));% `/ F* U I/ ?: n# \7 w
} catch (Exception e) {
' G% }! x: n2 M2 I% S# {5 @ System.err.println("Exception updateLattice: " + e.getMessage ());! }; J, Y6 |* x& W; l. s l6 p2 Y
}
n# Q9 d, d' C; w3 S/ M( K3 ]9 o T2 N9 ?3 O `; o5 [& Q
// Then we create a schedule that executes the- _# |, ]" I5 P- \" d: ^% I+ J# R
// modelActions. modelActions is an ActionGroup, by itself it Z& B/ a9 H( ^6 t5 @: S5 L# x
// has no notion of time. In order to have it executed in' F* y- v7 ^/ n; _
// time, we create a Schedule that says to use the- t5 c, r/ y6 b+ A
// modelActions ActionGroup at particular times. This! |, R8 |5 x/ O
// schedule has a repeat interval of 1, it will loop every
9 p6 F) T0 }& S, r; Y // time step. The action is executed at time 0 relative to
7 B: S9 }+ p! H // the beginning of the loop.
M& s* s1 A9 y7 x! i X4 D y; s# v- T
// This is a simple schedule, with only one action that is5 d2 x2 d* K e. {
// just repeated every time. See jmousetrap for more K7 S6 U! }9 U1 `
// complicated schedules.
+ Q/ ?9 P: a1 N* a! b( z3 | Y1 k& N) A T! W# v# }1 v
modelSchedule = new ScheduleImpl (getZone (), 1);
) Q& x: N: k) e5 n1 @ modelSchedule.at$createAction (0, modelActions);3 y) L6 B3 }' G$ H% `4 ?0 Q* i
' Y. F8 F7 Z" o: j6 N" Z return this;1 n3 A# e2 m/ i2 X" M: k% i& ?
} |