HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 p( p% i8 Q$ W; U/ R" T1 ]
# ^% A: I2 G$ T$ C3 a
public Object buildActions () {
3 j: u9 R) ~1 [ super.buildActions();* w' r7 X; M! o! i# r( q
- o) O% w& @1 e
// Create the list of simulation actions. We put these in
1 x1 M, S+ u# a% b // an action group, because we want these actions to be2 C# x* H' Q( e8 y: n
// executed in a specific order, but these steps should. S3 q" F3 B- j6 {4 j4 |
// take no (simulated) time. The M(foo) means "The message
, D$ m& v9 z. ~9 q5 M/ Z5 } // called <foo>". You can send a message To a particular% I& K5 N1 u, h
// object, or ForEach object in a collection.
- \; v, z; k! B" ^9 T) H
: R9 Q8 v% E# b3 D7 w7 a, q // Note we update the heatspace in two phases: first run" f# W4 R, \! w2 D- J
// diffusion, then run "updateWorld" to actually enact the
1 o* b4 N! C. X# o4 V) B" I // changes the heatbugs have made. The ordering here is$ X* {2 _1 }3 r, d
// significant!
" m1 O f7 Z- l4 q* v& e8 K+ H, \ ; D* n% O: z1 ^/ H# X7 z
// Note also, that with the additional
5 v4 V" {$ K1 D/ S6 s // `randomizeHeatbugUpdateOrder' Boolean flag we can- \5 s9 ?. A- B! S8 ?
// randomize the order in which the bugs actually run4 H7 d. b. ^% p
// their step rule. This has the effect of removing any# ^7 |& K! C# G
// systematic bias in the iteration throught the heatbug! t' b0 J6 @; `) }( G7 t6 p" a" a. f
// list from timestep to timestep: A) A6 I% I: [$ ?. E! _
4 k% G* O" f9 ?: U
// By default, all `createActionForEach' modelActions have
# `7 f: j) s' j# v! M: y; p // a default order of `Sequential', which means that the
9 b1 c- Z. G3 M( r, b, c0 d // order of iteration through the `heatbugList' will be& F2 {7 g- O2 P; w; C) C- |6 T
// identical (assuming the list order is not changed
* ]- ^6 R( U( ] M // indirectly by some other process).' P) B3 N( E! h4 W1 S
) e2 x2 t1 J1 u; W modelActions = new ActionGroupImpl (getZone ());
A& l5 T# m" ^- W% z; [. _1 l4 M/ M4 h# O* a
try {
8 y* b- C6 I& z( B4 ~: G* E modelActions.createActionTo$message* u3 R* ?3 {, [
(heat, new Selector (heat.getClass (), "stepRule", false));
* G; E1 n7 a( y* e } catch (Exception e) {4 I: l1 Q2 k/ S9 `1 c6 @: v. b0 g
System.err.println ("Exception stepRule: " + e.getMessage ());
, S8 d3 ^ j3 \! s: w9 x/ K* w- D( u }
' ~- A' y" e4 ?" ]; a
6 f5 a- F- I8 y# z3 t2 z; B try {
# q+ @* }% D3 u+ r; `' A# ]* b* V Heatbug proto = (Heatbug) heatbugList.get (0);3 W& L5 h9 k! j" Y+ I( v- R
Selector sel = 1 S" g! h0 d# o9 o# F
new Selector (proto.getClass (), "heatbugStep", false);
9 T+ `. Q: h+ ~' x* T actionForEach =
3 p, [8 x( l2 e; t2 j modelActions.createFActionForEachHomogeneous$call+ o# R! y5 M9 Z3 _+ _1 C) V" D' |0 V
(heatbugList,
4 c# X: j/ B- \: \9 |; G% p new FCallImpl (this, proto, sel,5 b% |8 ?, O" D: ^3 K3 h
new FArgumentsImpl (this, sel)));
" T% a! t2 T! m; l } catch (Exception e) {- B9 H/ r) ?% Z
e.printStackTrace (System.err);
3 n; `( C, E$ b }
H9 E# R- R- ^ 9 U3 a! I s) U% L
syncUpdateOrder ();8 p; t e3 j. a! j2 ?- s
$ ^+ |* k6 C/ e: B try {8 }; G+ y( X3 Q0 |: o
modelActions.createActionTo$message * m5 [" d; }) s( G
(heat, new Selector (heat.getClass (), "updateLattice", false));6 \$ P% O; k: g# K
} catch (Exception e) {- M& n, T. E; l# }. G/ D6 r% P, @
System.err.println("Exception updateLattice: " + e.getMessage ());4 W: Q& {" J1 u5 G" Y' {6 ^
}
3 T1 s- b2 ~: H! y1 b ) b# T. U0 s6 B) o" d R8 A
// Then we create a schedule that executes the6 x" ?( V% F M* _( T- ^ A
// modelActions. modelActions is an ActionGroup, by itself it9 b* V1 u& i- R
// has no notion of time. In order to have it executed in" a3 u7 y) ^6 [' O3 F5 q
// time, we create a Schedule that says to use the7 O c+ P9 M: ], _
// modelActions ActionGroup at particular times. This s/ r/ Y" I, h9 Z [
// schedule has a repeat interval of 1, it will loop every8 K6 S4 o, `6 u7 ]5 [$ o5 u5 l! }
// time step. The action is executed at time 0 relative to- G; o8 Z1 t& M0 {: v7 ?$ h' O
// the beginning of the loop.+ r4 X3 F- ?! l$ ]6 k0 ^5 A
- h( n B0 v7 S // This is a simple schedule, with only one action that is* v: b6 O+ j3 E6 V$ G
// just repeated every time. See jmousetrap for more4 d) f; ~% S1 e) ^! u! S
// complicated schedules.% z" x0 C/ T, ?8 R3 Q/ Q# w" A
+ b% ^6 q. _) U0 `5 v; Y' T. j; J0 z
modelSchedule = new ScheduleImpl (getZone (), 1);
8 e: B$ A6 E6 X% R modelSchedule.at$createAction (0, modelActions);8 `/ q! s% R3 F* ~" t
7 ]' D+ ` m/ W" }9 L2 k& {
return this; L$ _3 i; ]& R, Y j g
} |