HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ S+ |7 Y4 J: V, a' S- O9 S
9 v$ x0 ?3 V6 w4 D3 M0 _0 F2 d public Object buildActions () {7 R$ b6 \! U. `4 g
super.buildActions();
+ Y) I3 D$ {" F2 z + t, g2 t$ X$ J8 J, T
// Create the list of simulation actions. We put these in
t' Q; I% i/ K8 @% B: G // an action group, because we want these actions to be
' R2 G4 ^/ g. E1 X // executed in a specific order, but these steps should
# |4 F! S2 S; l+ {, R // take no (simulated) time. The M(foo) means "The message- u, C$ A2 ?) R% j& _
// called <foo>". You can send a message To a particular
6 o2 m& V) F1 t- z2 O // object, or ForEach object in a collection.
6 P, R# I$ {! b# C5 q' c# v, Z
+ P$ E N8 c0 d. [ // Note we update the heatspace in two phases: first run
9 E7 {# @ L c0 }7 w r/ V // diffusion, then run "updateWorld" to actually enact the# f& q3 V2 R8 [
// changes the heatbugs have made. The ordering here is9 a4 x! e1 \# m/ T" b1 D/ V3 [
// significant!
# v4 B- ~6 q% r" k6 Y" ^5 F3 e& h
# k/ O# z; D$ X // Note also, that with the additional, c! h2 U" l' ]; J& r6 I
// `randomizeHeatbugUpdateOrder' Boolean flag we can' H+ o% ^2 D% g" c6 w/ T$ m
// randomize the order in which the bugs actually run
6 Q" o% W5 R+ |+ f- h // their step rule. This has the effect of removing any! f& l+ @6 O1 ?. D2 ~
// systematic bias in the iteration throught the heatbug/ e9 l7 p* O1 M
// list from timestep to timestep$ c9 _$ |5 O! y% S& w' r9 E7 C5 z
V3 s) `) {* H. f( y& h( t // By default, all `createActionForEach' modelActions have7 Y$ o9 z* D( g* Q5 D% d
// a default order of `Sequential', which means that the
2 N3 q" j. [2 C6 Y* G( L // order of iteration through the `heatbugList' will be
1 I* r/ K6 v8 m i7 z- h9 c // identical (assuming the list order is not changed# x6 ^. c! }5 d
// indirectly by some other process).
! |" s6 j$ f- O' X4 ^5 ?8 c& ^0 ~
! f/ ~) y9 b/ @7 J. E3 ^$ p modelActions = new ActionGroupImpl (getZone ());
5 [$ h5 p' o: ?" K/ r3 L1 D
5 L' R2 d# ^$ B7 Y" I) e6 c try {
& s% M2 a6 _ n7 ?# r! t5 J# ~ modelActions.createActionTo$message
* |. J, {& [- q% X' W% p (heat, new Selector (heat.getClass (), "stepRule", false));) O; k) `7 Q4 R$ [$ M
} catch (Exception e) {
+ k2 |1 j" B% d* u! y System.err.println ("Exception stepRule: " + e.getMessage ());
; f9 f; s0 V, Q: U0 t6 P. d3 N }1 w; I7 y$ H& @
: |, ~) ~9 F: d- b4 m$ S
try {
) G+ z z9 X" r- @ O3 s Heatbug proto = (Heatbug) heatbugList.get (0);" P% S8 ]0 G C' C8 c
Selector sel = % b$ ?9 x$ T$ m. s; w, |
new Selector (proto.getClass (), "heatbugStep", false);
- Q% O4 `" Z% B: D: \$ P5 J actionForEach =
, H! s2 p' `$ y- ]) Z! h1 t modelActions.createFActionForEachHomogeneous$call
, w$ g, Q8 k! o! s: U3 v (heatbugList,+ @1 k0 }+ m) Y/ r% L
new FCallImpl (this, proto, sel,
& n6 Y6 s7 R0 ]# U new FArgumentsImpl (this, sel)));7 f0 e% N4 I, d! [2 B0 N2 l
} catch (Exception e) {
- e2 r& l! l, w0 ?" S2 I* g e.printStackTrace (System.err);
3 _2 y/ Q( M* \* L }% y6 ~2 f* I2 n; r9 c
' e" G- ^5 f. y9 m% l" X syncUpdateOrder ();" l; s# ^5 f! M1 Y
G$ a/ j" S1 l% Z/ C& p, H0 v try { H2 v. t! u# K. I+ v, t
modelActions.createActionTo$message
3 w' ~) M9 A/ Y) I! o' M (heat, new Selector (heat.getClass (), "updateLattice", false));' C$ D7 m% d1 o8 Q9 V/ q
} catch (Exception e) {# M5 l; y+ V% |; j% g7 {& H% o" Y
System.err.println("Exception updateLattice: " + e.getMessage ());4 N; y1 }4 Y. i+ o1 A! |
}
^! Q& `% X& K( D ; D3 X j, Q7 _) i
// Then we create a schedule that executes the
% l& u8 g( Y$ h5 I% q: I // modelActions. modelActions is an ActionGroup, by itself it& K1 F/ [% d. N9 ?
// has no notion of time. In order to have it executed in
& a. D! M; L& t/ K! c8 A // time, we create a Schedule that says to use the- B: C2 _) p; m3 F
// modelActions ActionGroup at particular times. This. r+ B2 @# k f/ C
// schedule has a repeat interval of 1, it will loop every D: |3 h: U/ F3 N% W. M3 N1 E3 G
// time step. The action is executed at time 0 relative to
4 _3 h& w6 V) f! h // the beginning of the loop.4 V" R' m# @+ J
6 o8 O2 m0 s- s/ t( L // This is a simple schedule, with only one action that is
. n: D1 W) L* X. J# ^ // just repeated every time. See jmousetrap for more' w5 m# L$ w Z# G, ?5 ]% H9 |! e
// complicated schedules.! V" U) T) `% j
% y6 K9 O) @- s modelSchedule = new ScheduleImpl (getZone (), 1);( c& z3 j/ }4 \! s5 y
modelSchedule.at$createAction (0, modelActions);
7 {! Q4 D) S# X& J# b# @% k/ O & G6 L5 }& G; U+ ]
return this;
, e8 h( n( P5 y } |