HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- P- B. c0 E) m9 A+ t; b. E+ f+ B' y
public Object buildActions () {& O, A6 _# ~. K/ M
super.buildActions();
; K; E7 j$ ?0 ] 5 H( m) C/ P% q
// Create the list of simulation actions. We put these in
8 G. y: W4 Y0 I7 r // an action group, because we want these actions to be
5 g2 B! y9 U7 T9 c- E1 ] // executed in a specific order, but these steps should
1 d9 C2 T$ U0 L, p4 z9 S, j# D // take no (simulated) time. The M(foo) means "The message) @" K& p9 |8 b' c* i
// called <foo>". You can send a message To a particular
. C9 N' [9 `$ p C! b* [) K // object, or ForEach object in a collection.% ]. U, K( E- J+ v) f1 F
3 }( m: J" A/ ^ // Note we update the heatspace in two phases: first run3 ~& M9 T% {7 D2 y' c( @# B3 n
// diffusion, then run "updateWorld" to actually enact the
7 } C, C4 F/ |9 m* a8 F // changes the heatbugs have made. The ordering here is5 R- ]4 F2 [0 T/ q6 j$ `2 g4 J
// significant!. U" W. X6 r) Q; s8 W% e3 g
( O9 n$ K8 T6 x. v' C
// Note also, that with the additional+ v& o" u5 s) P# Y& k% N8 w
// `randomizeHeatbugUpdateOrder' Boolean flag we can P- A& x* G8 @8 }1 e/ j$ O
// randomize the order in which the bugs actually run6 k5 W2 d! `' L. ?+ E
// their step rule. This has the effect of removing any
" g: s- X3 o U/ ?" c // systematic bias in the iteration throught the heatbug/ M- E/ m" `0 a
// list from timestep to timestep
4 k" y; U3 n* Q6 t
* h% E2 H1 p* z" X // By default, all `createActionForEach' modelActions have
7 A2 q% c& z3 b" }, t7 D // a default order of `Sequential', which means that the5 o7 k% u- E' T3 o+ n x
// order of iteration through the `heatbugList' will be
2 o/ t8 F% \; `" P0 I2 H. L // identical (assuming the list order is not changed
/ ]! f: F! a5 Z c // indirectly by some other process).
+ S: Z% N# d d2 Z/ t, s+ C8 p
5 Z3 O( ~- b2 m" s, S- p5 ^ modelActions = new ActionGroupImpl (getZone ());
; C0 r# V; k' n8 A, t# I9 R% Q% Q+ w3 s% B0 V
try {
( G9 q& e8 k1 x modelActions.createActionTo$message! Y' L- ^' S+ V6 [0 L
(heat, new Selector (heat.getClass (), "stepRule", false));3 Z9 w* I' p, E! ]
} catch (Exception e) {- a6 Z2 n3 U9 C0 Y7 O
System.err.println ("Exception stepRule: " + e.getMessage ());
' \8 z7 k/ @) {7 u1 X4 {5 N }
4 Z7 {$ r; J$ v/ k+ m" p' j3 g6 A9 q, e
try {+ b$ O+ a! K! G& t
Heatbug proto = (Heatbug) heatbugList.get (0);
" h# D- }9 y% E1 j5 R' [. s' @ Selector sel = 6 ]$ r( @! E0 B) u
new Selector (proto.getClass (), "heatbugStep", false);. a$ h. J8 ]) \$ m$ P
actionForEach =
) o; z* C _1 d modelActions.createFActionForEachHomogeneous$call
" H- t6 j" q4 p. E8 w (heatbugList,% ]) U/ a. G3 H
new FCallImpl (this, proto, sel,
9 {. B" N X, N$ ]- h \9 G new FArgumentsImpl (this, sel)));" Y9 d) J8 x0 B3 v3 f1 y
} catch (Exception e) {1 O" p0 O. d: V9 C1 A' o! M
e.printStackTrace (System.err);3 Y& b" `8 w2 _% a9 {
}
6 G; I4 K$ F, z) h* j# i
- \2 j# Z' e9 {4 U4 V6 W% C syncUpdateOrder ();
* `/ K" Z0 ^& V( t2 W! N% z, K7 N7 L: \8 K0 k2 l: ~; ?* |5 J
try {- e6 X2 E3 [' g* p
modelActions.createActionTo$message
/ e+ [- d3 f2 f: {. p; N0 U& ` (heat, new Selector (heat.getClass (), "updateLattice", false));; b; I# t' X7 \5 ?
} catch (Exception e) {
; j9 k* M* T, ]0 M7 z System.err.println("Exception updateLattice: " + e.getMessage ());" `# B2 h( N* I% j# n
}+ e. {# N2 u) q" t1 u
/ j/ N6 j x* P. p' D' c. b // Then we create a schedule that executes the
2 ~, }6 T; d, w9 ~* t // modelActions. modelActions is an ActionGroup, by itself it
4 y/ \/ n6 V9 Y b1 T' c6 F) `9 j // has no notion of time. In order to have it executed in, O% F+ j: |' ? A
// time, we create a Schedule that says to use the
) a+ R2 T" f4 r i R: @& H: j // modelActions ActionGroup at particular times. This7 [ {1 u, d; x0 X$ h
// schedule has a repeat interval of 1, it will loop every
6 g# M/ P0 J5 }5 C" m2 K: C // time step. The action is executed at time 0 relative to
c5 e' G- Q. [* R // the beginning of the loop.
, L+ n! }5 J& x$ m
5 ]+ ?0 C4 @& A // This is a simple schedule, with only one action that is
6 Z9 X4 J5 K6 x- F // just repeated every time. See jmousetrap for more
: e( H6 p; [0 X" \6 g // complicated schedules.
! q% p b$ v7 H8 p% k0 k
/ B9 B% N1 n7 {/ Y: h) w modelSchedule = new ScheduleImpl (getZone (), 1);
: ?5 a. \1 S: i$ F) B modelSchedule.at$createAction (0, modelActions);) a8 [8 v/ d& Q; q+ P5 @
. _% H: j! j' p
return this;" M8 V6 N& H5 o
} |