HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( F1 N, J" M3 Q/ n% H4 P: M+ i
8 g. j) a/ N$ N8 E5 C+ V public Object buildActions () {% g) u1 j+ }' t. e3 R6 A# J5 p
super.buildActions();
; v" b, s/ S- [$ [# |' Y& I$ b
+ A& H5 V6 N2 `1 }- q- U7 x* S // Create the list of simulation actions. We put these in
1 Z' d: k7 ^; X/ S, H% o // an action group, because we want these actions to be e3 r! {$ ^' S; A) c1 Y8 v
// executed in a specific order, but these steps should
- Q- v7 t5 q2 e // take no (simulated) time. The M(foo) means "The message, o0 K7 R) ^% Q" A
// called <foo>". You can send a message To a particular. o7 a# K( p$ L4 A- t! b: c
// object, or ForEach object in a collection.( X1 V1 D( D4 ]" z
: K* o9 i9 B5 H4 `: s# H // Note we update the heatspace in two phases: first run
% d$ W& S3 m9 w8 L9 g // diffusion, then run "updateWorld" to actually enact the
4 o6 C5 j! j0 {7 [ // changes the heatbugs have made. The ordering here is2 G) s- W0 i5 [* i, W
// significant!
3 x$ \+ V0 a* j5 V& ^ , n! h3 A) I/ W5 Z* ^: b
// Note also, that with the additional8 w3 p$ q% G1 b) E" }
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 o5 Y- {- v, F+ V
// randomize the order in which the bugs actually run
, @, ]: ]2 E }, L: J7 b( N0 n // their step rule. This has the effect of removing any/ O. I6 X1 {0 c7 B( v
// systematic bias in the iteration throught the heatbug5 H* \ Y/ b* I( U# C+ X
// list from timestep to timestep0 v8 y. B q( Q* s9 v
; q0 d+ {/ c5 A6 c // By default, all `createActionForEach' modelActions have
9 k7 F j+ I' M4 ~0 K // a default order of `Sequential', which means that the( C$ j- `9 k D2 B1 F0 f
// order of iteration through the `heatbugList' will be; ?- d# w/ _3 Q3 X1 c9 y; g
// identical (assuming the list order is not changed
- s3 L. E8 D! Q$ D% t* s+ } // indirectly by some other process).; o- X. x! w4 z! k6 d* G% o
# S5 b) k$ F w modelActions = new ActionGroupImpl (getZone ());
d8 B+ |8 X9 d8 k3 r8 `
% i/ o- m& M7 x. @ try {! d( n+ e; v" ^9 D/ E; i, d9 A7 e1 i
modelActions.createActionTo$message
: K% f7 h, X; Z: J I5 u (heat, new Selector (heat.getClass (), "stepRule", false));
% z2 {8 \& e& L } catch (Exception e) {( B# I3 t% K+ |
System.err.println ("Exception stepRule: " + e.getMessage ());
# y+ e9 E% y$ f( \! Q. S0 |) p, @ }
! ]! Q4 `* k+ q* W4 w4 B d/ u N- s$ e7 P% J8 R, A+ }
try {
6 m! }. L7 X; ^0 ^" h Heatbug proto = (Heatbug) heatbugList.get (0);
- g/ T0 l- ^" Q3 _) w: E% x Selector sel = 0 l n4 M8 _% B9 x: A
new Selector (proto.getClass (), "heatbugStep", false);
4 c8 S: p7 G- W* t actionForEach =: E( s: ]% L$ D' Z* |
modelActions.createFActionForEachHomogeneous$call( W9 m T" H) x% b( e) t
(heatbugList,4 `4 M4 N6 P" d1 ]! ?4 F# l
new FCallImpl (this, proto, sel,
% \0 r* Z/ H! ^, T! |% o5 P new FArgumentsImpl (this, sel)));
- G! E. K: A& [, u2 @7 ^: ]) Z } catch (Exception e) {) F; I, v7 c/ ~; A. e* {- i
e.printStackTrace (System.err);
$ Q$ P1 I. m- L }
$ h$ D$ E( `% V* W0 q a9 _, Z; |" a3 ?, s, x$ o) J6 S
syncUpdateOrder ();
+ h* h; X. s3 ~! n7 @2 |( \3 E; j k1 s% L. }/ C6 s6 L
try {8 r# x1 r# r) D2 ?! y
modelActions.createActionTo$message
|2 b0 R& \* V) ?. L6 d (heat, new Selector (heat.getClass (), "updateLattice", false));
2 j* o, H. e/ S. P9 l } catch (Exception e) {4 l, U. h( H5 q
System.err.println("Exception updateLattice: " + e.getMessage ());
; v7 K n7 t. H6 T* x( S' j) ` }
/ E8 R O$ ]+ N $ s% V% l: l1 k: o; R8 E- r; W
// Then we create a schedule that executes the
D; a1 s* m9 j7 F+ u // modelActions. modelActions is an ActionGroup, by itself it
4 z6 a4 h t0 x; \" p8 w // has no notion of time. In order to have it executed in
2 B9 a& x& ^$ R0 M8 [/ A% @3 _ // time, we create a Schedule that says to use the
, T+ R5 s' `2 W7 H% ] // modelActions ActionGroup at particular times. This
9 ~( S" B6 p. o" @" A! J // schedule has a repeat interval of 1, it will loop every
/ y" M% T: j t# u // time step. The action is executed at time 0 relative to
2 Q: Q9 b! N! ~0 B F! [ // the beginning of the loop.
5 u2 p# j, y7 h4 h6 f6 K. g
* o& J+ Y: d, l1 Y. w6 E: c // This is a simple schedule, with only one action that is
3 h- s, l2 i0 G" j7 j: F3 ^1 A // just repeated every time. See jmousetrap for more$ L3 n; a, z4 v4 {9 R% y
// complicated schedules.2 S" g% V; O$ |
0 P4 C, ^& [6 n2 d! b' W U modelSchedule = new ScheduleImpl (getZone (), 1);( D* h/ w) g$ w
modelSchedule.at$createAction (0, modelActions);
+ E5 ~* s' E* W; g' k) V0 @, |4 v# S # O8 I& A8 T( J: g/ ?8 ?4 D( R2 q9 L
return this;5 a, @) o; m0 J
} |