HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" r+ k! B/ U5 l; G( ?
% W/ u) A! l% X, r9 b
public Object buildActions () {
! z! H j) u* d) p* v$ t super.buildActions();/ n0 ~6 N) Q+ q7 E
3 q9 `9 Z1 k* T
// Create the list of simulation actions. We put these in
7 ?) H2 f# k0 U% a0 `6 Q- X" ~ // an action group, because we want these actions to be4 W4 @0 l2 T: P3 ?3 V& z
// executed in a specific order, but these steps should
& [$ |3 W! M& f1 b! }* _! t# G // take no (simulated) time. The M(foo) means "The message
0 R ] d6 ~$ I/ P9 ~* @ O // called <foo>". You can send a message To a particular
: C% _8 M5 o" v8 m! z // object, or ForEach object in a collection.% O- ]" A6 O+ `1 | ]
' {1 V9 q. v" Y% O
// Note we update the heatspace in two phases: first run
/ ^/ h& e1 i1 D3 T$ {9 h J0 f // diffusion, then run "updateWorld" to actually enact the2 O. u! E+ h5 E6 k: ^
// changes the heatbugs have made. The ordering here is
1 l. Y$ Y1 x, N // significant!
# f" L+ j8 X( }+ ` * _% I7 Q1 m+ K8 B+ v Q
// Note also, that with the additional4 ~ X( a g5 _. y2 S, t
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 ~" F4 \/ p8 e9 O' i
// randomize the order in which the bugs actually run
; R" n1 L1 k; f5 B% s! Y# T // their step rule. This has the effect of removing any
4 l3 u4 e! {/ t5 ~1 _4 i z- A/ b8 I // systematic bias in the iteration throught the heatbug
/ H- ^- m3 a+ ~8 B5 h" `3 y // list from timestep to timestep
+ k2 t4 [& f( u5 c
$ b, ^6 p( E7 u# e6 ]( a // By default, all `createActionForEach' modelActions have5 ~- B+ k: w) d7 p' t* d1 {
// a default order of `Sequential', which means that the
$ h Y* H6 G; j" C+ ^( u+ t // order of iteration through the `heatbugList' will be: [7 q; I/ R$ j& l- c7 A8 z
// identical (assuming the list order is not changed
4 R3 @" `% G. q // indirectly by some other process).: [- S" V+ s7 Y: |0 `
3 {# z& l/ a) U7 @
modelActions = new ActionGroupImpl (getZone ());- H2 Q2 \2 W& }8 T( B1 g( B' _
! f. h' Y2 L; B" M4 v
try {
3 c0 c( j' o; \9 f3 s5 r modelActions.createActionTo$message
; ^' U, V" E6 f4 ~" Z4 b/ H (heat, new Selector (heat.getClass (), "stepRule", false));
' s& U# E5 a; ]3 H+ l } catch (Exception e) {
; q8 e% K7 ~; K- Z3 n System.err.println ("Exception stepRule: " + e.getMessage ());
( d# l" a) K$ ^* u W }
5 k* q6 l& L" h( l1 \5 L
F- j# U0 o! S& @3 K) O try {
/ E+ R; P& z- o" W Heatbug proto = (Heatbug) heatbugList.get (0);
7 k% a3 ^5 {, \/ U5 H9 R Selector sel = 1 k8 R: z7 q2 W# p
new Selector (proto.getClass (), "heatbugStep", false);
6 ~$ H: ]8 l. F actionForEach =% V9 [/ L+ o R' R: n
modelActions.createFActionForEachHomogeneous$call
, h* {, N' {9 m8 ?8 k (heatbugList,% d8 T" e. g2 N/ B, L
new FCallImpl (this, proto, sel,
# [9 `$ K7 W ]" ~ new FArgumentsImpl (this, sel)));
% e5 U- P) E2 h9 Z& a( @% E7 S } catch (Exception e) {
& ~5 j; W, V6 O e.printStackTrace (System.err);- C+ V6 a* @, O$ A' @0 \, o) B- A) G
}
3 D5 Z7 t3 j1 \: |
; h% ~1 t9 E( `8 C syncUpdateOrder ();
* D: r' {2 M+ @
5 C) X* T( q. C9 M try {! g; Q/ v# B. Z* {+ i$ ]
modelActions.createActionTo$message
" l O; \2 j! V) N (heat, new Selector (heat.getClass (), "updateLattice", false));* }2 C6 N% O& J: c8 y
} catch (Exception e) {/ Q0 R8 t f" M4 t
System.err.println("Exception updateLattice: " + e.getMessage ());
) d) d: _" q0 ~- T% m& l }+ |8 O' Z- `7 ~. D' W9 _
1 E. d4 j' P- B# q0 j. }2 h
// Then we create a schedule that executes the
/ Y4 W: r( G4 J" C9 Y. v0 ^$ l* _ // modelActions. modelActions is an ActionGroup, by itself it
7 J0 c* W; C; @- @8 d2 D% O // has no notion of time. In order to have it executed in9 T4 T+ R" W4 v
// time, we create a Schedule that says to use the8 {/ p# U E6 ]5 _; Y3 K
// modelActions ActionGroup at particular times. This
* }: i5 b2 \2 i- q // schedule has a repeat interval of 1, it will loop every
z# m5 [' p& y+ m% ^8 t N* F // time step. The action is executed at time 0 relative to: N x! C" }* t, G: V- F; H( ^
// the beginning of the loop.( {. e1 j$ O4 O4 p% q6 y
1 G! _+ Z- ~& _7 b+ G$ ]
// This is a simple schedule, with only one action that is
+ R9 I u8 O, w // just repeated every time. See jmousetrap for more- N6 ~! O1 d) R W: F* } ~9 W
// complicated schedules.5 k; t2 h. R9 e, O* K/ [. N( i
* n0 z& d/ f# C modelSchedule = new ScheduleImpl (getZone (), 1);9 Q n+ J/ s% V: O! v( |
modelSchedule.at$createAction (0, modelActions);* \: e. r+ n4 [+ j9 |: c
1 c3 }) T+ _3 v return this;
$ G, _" s9 s, z4 J } |