HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! v- j/ Z2 _$ H1 I7 q; h; y3 B4 p- \, Q# I; X
public Object buildActions () {5 `1 f" T, ]$ m. }, o
super.buildActions();" N) ^; V: x. y- p$ q- |. w
0 B3 B6 f) w5 S' \5 `4 o // Create the list of simulation actions. We put these in
: L, h% ~6 W( n) ^9 l // an action group, because we want these actions to be
8 x$ t- y. t8 X B( Q2 K R4 ~ // executed in a specific order, but these steps should. p. V7 B# @1 ?
// take no (simulated) time. The M(foo) means "The message
+ G6 Y5 a6 F! l: w, L // called <foo>". You can send a message To a particular
- ?7 L: W! |! X# M {4 Q' o% t // object, or ForEach object in a collection.
& T# d) v" J" y# \8 o
, ?0 K& ~0 u* [- o6 t // Note we update the heatspace in two phases: first run8 l; _; D5 |: L
// diffusion, then run "updateWorld" to actually enact the
% ^( b1 p8 x" p T. Y) x3 | // changes the heatbugs have made. The ordering here is, p! G! b5 O7 ]* V
// significant!
# b7 M; {5 V- s- W7 a) {) T; a7 S 0 g7 F, e6 A' W1 A) c8 g
// Note also, that with the additional
; q7 I3 x1 x" Z( j: b- X' e+ v, q // `randomizeHeatbugUpdateOrder' Boolean flag we can6 t' [+ `8 `: ~4 ?1 k5 P: }7 I0 ^' G
// randomize the order in which the bugs actually run
; \( f6 G5 U. @( s // their step rule. This has the effect of removing any z- I" c- j% M4 t. I& k
// systematic bias in the iteration throught the heatbug
, ^3 |/ X( L) @0 u // list from timestep to timestep3 e* Q F! J2 p! k- d
* \% [' D* M8 Q+ [. L1 J8 [ // By default, all `createActionForEach' modelActions have* Q! ]0 T2 E6 x
// a default order of `Sequential', which means that the- i# p8 a; e+ j2 _7 t% ~, E
// order of iteration through the `heatbugList' will be- Q' P6 ]' Z, T' Z! g% [: m3 O
// identical (assuming the list order is not changed9 H: ~! B) z E; u- R' m
// indirectly by some other process).- L+ B5 x0 v' Z+ I; z. j" k
/ ]$ j, C, y( w; {
modelActions = new ActionGroupImpl (getZone ());
7 U: z# m$ W) A% F7 y+ F% ^- s' A8 R2 B+ }3 J
try {
/ i6 Q& S& i5 a& _: x! ?* s8 m modelActions.createActionTo$message
+ Y: Y' x* p6 Y. b (heat, new Selector (heat.getClass (), "stepRule", false));
2 f# ^% l' K+ S0 R } catch (Exception e) {
7 p2 w1 q% I. ]1 N2 H2 q System.err.println ("Exception stepRule: " + e.getMessage ());
0 W+ F4 t4 ]; u }
# W8 a1 K# g4 s. U+ l% R0 _& p# ~5 \, a
try {
, l5 ~7 R- g4 |1 C( |9 x2 G Heatbug proto = (Heatbug) heatbugList.get (0);: h; ?4 f$ L7 o; D" M+ n& W
Selector sel = + o7 F) G$ o: Z
new Selector (proto.getClass (), "heatbugStep", false);
' ]# g8 {4 Z5 b1 N actionForEach =. U! g+ \; r: @' n
modelActions.createFActionForEachHomogeneous$call
0 [; q' Y9 Y6 K/ a) a+ p (heatbugList,
" O1 P+ d# p6 H0 Q new FCallImpl (this, proto, sel,, L- J: n7 f& _& c9 J2 s7 }8 |
new FArgumentsImpl (this, sel)));* i4 @- H* I- T/ b1 I3 Q: [
} catch (Exception e) {9 C7 ?% h$ ], y* d: i
e.printStackTrace (System.err);
! Q2 h7 B& c2 ?2 h+ K2 W8 D) Y }
) \) t- f4 ^- l
" m8 R/ d7 }; |! O; S. S syncUpdateOrder ();) U; A% H: e; j8 k
( k8 f* W" Z7 N6 _7 a
try {5 }" U* W$ b3 F* l6 P* L* e
modelActions.createActionTo$message
. v2 }6 O$ y; P& F/ X: n (heat, new Selector (heat.getClass (), "updateLattice", false));
% L$ v3 b6 ]& u, T R } catch (Exception e) {. p- g( X( o" O8 R2 e- m
System.err.println("Exception updateLattice: " + e.getMessage ());
" {7 A' l( ]7 G }
8 g6 V8 a* ?) d% h. O
0 g* p' x$ s1 p% S8 e- b+ X // Then we create a schedule that executes the' ]0 |( F9 |' O+ d* w1 n
// modelActions. modelActions is an ActionGroup, by itself it
8 H( Y" u5 `$ D. C0 r" }' i Y // has no notion of time. In order to have it executed in6 P) j5 v' X6 a- J5 ~; K- ~0 S
// time, we create a Schedule that says to use the
5 r" @% S: w! ?& \! ~ // modelActions ActionGroup at particular times. This+ J4 H9 q! @0 s4 L5 B
// schedule has a repeat interval of 1, it will loop every) ]& B. _2 ^' i
// time step. The action is executed at time 0 relative to$ w9 ]& S e3 g" f0 a: A) |. f, s; j
// the beginning of the loop.
0 w) C) w! t1 b, I# x$ o5 y5 c
4 P+ k+ H% N# i* j6 W; W9 a& \) M$ v // This is a simple schedule, with only one action that is4 c( }8 f4 G+ _
// just repeated every time. See jmousetrap for more
p; f/ C/ v% x+ `! K // complicated schedules.5 d% _- T, c( y
) B- `' j3 W! I) b
modelSchedule = new ScheduleImpl (getZone (), 1);
( _( ?& h1 i- [+ X* B( V$ w4 t; L* k modelSchedule.at$createAction (0, modelActions);3 I& ]8 R2 a5 F+ x9 e
8 _" r2 E0 k# L4 D, L return this;8 `4 |8 b& r! q
} |