HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ c% U W. L, [. ?. A2 T
+ D3 d3 I! W" q* y! S
public Object buildActions () {
% b7 m% q) F0 `. W3 N$ u5 r n super.buildActions();
! d/ p- D- i* s) d/ E* A$ h) E7 t 0 y- p; B }5 k# k+ r
// Create the list of simulation actions. We put these in3 o3 V/ V* w O7 ~3 O
// an action group, because we want these actions to be
" n3 ~* M& U5 ^; G // executed in a specific order, but these steps should
% B; ~, c! L# X( o) ] // take no (simulated) time. The M(foo) means "The message
8 C" {& \. e+ O5 z. ? p // called <foo>". You can send a message To a particular
( ?8 P# d( Y; y m // object, or ForEach object in a collection.6 g- e4 e* g2 O! b
( c, }7 h: d# i) d, U( X- F
// Note we update the heatspace in two phases: first run
4 V- W5 |. S6 Z5 O$ l, D // diffusion, then run "updateWorld" to actually enact the' n3 K* Y4 U- M5 v/ Y) E3 d# O
// changes the heatbugs have made. The ordering here is
1 {. m& ^7 F# s y7 } // significant!
0 S5 G1 @2 h0 m2 Q0 e + D& c5 j, G& S7 O2 M
// Note also, that with the additional
/ }+ W4 Z: \! @& e2 Q- U // `randomizeHeatbugUpdateOrder' Boolean flag we can; w3 J @5 @ B) k G/ q+ A
// randomize the order in which the bugs actually run
8 u: k8 i' B2 O& v& ~# D! L U! V! ] // their step rule. This has the effect of removing any) f9 z8 V7 q6 b# R; M0 C0 P
// systematic bias in the iteration throught the heatbug
: j1 [+ S) W$ o% m% b ^ // list from timestep to timestep
w9 g0 p# q. P( m0 f C, d 0 |) k0 e$ `! A/ n. Z9 Q
// By default, all `createActionForEach' modelActions have% R, o! u, j- S% P! k$ P2 b
// a default order of `Sequential', which means that the
1 y$ x) a# O) A# L3 z8 h // order of iteration through the `heatbugList' will be) ~% m9 P4 G3 r7 c
// identical (assuming the list order is not changed4 W$ ~6 o1 v! O6 a
// indirectly by some other process).
; b+ _& c) ~9 h! o
/ O. V1 U( z, e* c4 T* F; H# h l modelActions = new ActionGroupImpl (getZone ());
& A' P6 X. T5 b, q6 Q Z# j+ O
# T/ w+ o; K* c7 ~/ q" u try {
9 R2 G4 l5 X$ V9 a# {$ F2 a$ t modelActions.createActionTo$message: m! `# V3 g% |7 ~
(heat, new Selector (heat.getClass (), "stepRule", false));4 u* [ D, X9 D% |( e; h
} catch (Exception e) {
; q- t+ f! v5 } System.err.println ("Exception stepRule: " + e.getMessage ());
+ I4 a1 `- d& z: \" Y( a- V1 h }
9 w \! c( Y4 p; e
, F0 H) g: W6 g- [. a# B. o try {
: I a6 _, B: j Heatbug proto = (Heatbug) heatbugList.get (0);
; q4 w1 D! P% I3 p8 l Selector sel =
! r! Y1 j+ p; ?3 A+ f$ z, R- } new Selector (proto.getClass (), "heatbugStep", false);
- f* \) g2 P A$ @9 F! k actionForEach =
, E" s8 c6 v& J. R# C5 |' a modelActions.createFActionForEachHomogeneous$call" F. d' h' g4 B0 L9 X: P6 M
(heatbugList,
H* v5 @3 {# h( F& N new FCallImpl (this, proto, sel,
C" @, q3 O* @2 c# R8 E4 B new FArgumentsImpl (this, sel))); R9 s9 d9 X! y& K
} catch (Exception e) {) z. }* S4 M$ e9 V% A
e.printStackTrace (System.err);
/ P1 c+ @: o$ d; [ }. }* V) E& j7 F; }$ u# `
$ V1 u- y; A( R% U! Y" w
syncUpdateOrder ();! }- H X! E- R
1 g, K6 {/ B W& J3 K try {
7 M! f4 F* i* f" X4 ^, s7 a+ O modelActions.createActionTo$message 0 j q: r( T# f- M
(heat, new Selector (heat.getClass (), "updateLattice", false));: Q0 }2 _! ^6 A5 `& M4 V
} catch (Exception e) {! d. {8 R1 H; ]8 j' n
System.err.println("Exception updateLattice: " + e.getMessage ());5 x$ S8 P/ W/ I: E7 M8 T
}
, q: g- @$ g+ X6 t2 T 0 ^' m+ G1 h3 s* f* g
// Then we create a schedule that executes the6 p* F, w0 b6 a6 g1 g
// modelActions. modelActions is an ActionGroup, by itself it, s# ^, d* }3 M, I6 W/ b
// has no notion of time. In order to have it executed in3 H, W1 n) E% k- t$ k
// time, we create a Schedule that says to use the
: T' D% p* w5 s. [5 g // modelActions ActionGroup at particular times. This7 I) h3 q- Y$ O, |- Q3 o* N: y! `
// schedule has a repeat interval of 1, it will loop every
& o9 {# M3 @5 v3 k! k/ b& P // time step. The action is executed at time 0 relative to
! P% W7 w( }9 d7 i // the beginning of the loop.! j9 S* E- n; Y0 B0 A5 s C& m
' e# j, ]& R" X+ @, B // This is a simple schedule, with only one action that is7 D8 f: A' m2 L& u
// just repeated every time. See jmousetrap for more) X" {# G4 m: M, y: E; E2 n
// complicated schedules., H) U* X9 F4 ]" Y; D
: \, ]5 J W9 o0 e5 j4 U
modelSchedule = new ScheduleImpl (getZone (), 1);& l7 B6 t4 p% h4 t$ l- d7 R! C$ G; T
modelSchedule.at$createAction (0, modelActions);
7 T: _" Y: I- i7 k, b6 G
& X; }, S A' H6 M3 |% f0 r return this;+ |: l6 y% m/ `$ p% n; _
} |