HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ }8 R2 L, M8 v8 H7 v3 @ ?
3 _: ?- J8 m$ a1 N7 e7 r
public Object buildActions () {
& _& V1 }2 Z9 f3 i& b4 a" _" _ super.buildActions();
3 r \* f, h6 ^5 a2 [# I ' b1 b- D; x1 m' n- @( n$ p
// Create the list of simulation actions. We put these in% x- p+ o9 [- X& `' a; ]
// an action group, because we want these actions to be
# O/ E9 }: C Z) p5 I3 _ // executed in a specific order, but these steps should; K* S) S) n- l' M8 U" z5 Q
// take no (simulated) time. The M(foo) means "The message
3 [( \3 l, i( D! D2 d5 O // called <foo>". You can send a message To a particular4 V" A% \. @0 a7 Y; O& D2 }: ?
// object, or ForEach object in a collection.' p5 Q; d: a" k4 I
b* c# g0 s4 R! {, @( ?
// Note we update the heatspace in two phases: first run
1 [1 e. b' H$ H( A6 E/ @1 W // diffusion, then run "updateWorld" to actually enact the5 x8 B$ b2 `5 \ _( s1 A
// changes the heatbugs have made. The ordering here is( y( n1 v L/ x$ l1 g; h# k% o2 w1 Z
// significant!
* `) F5 N @3 z0 X0 a ! m: {( s1 i# B- |" K- S4 q O
// Note also, that with the additional
2 o0 |. y1 E7 h3 a8 s2 D // `randomizeHeatbugUpdateOrder' Boolean flag we can1 Q2 Q0 f( s s4 f
// randomize the order in which the bugs actually run
1 r$ u3 {' Q7 E) L+ {/ e // their step rule. This has the effect of removing any
7 r" |' w% f! ? // systematic bias in the iteration throught the heatbug
1 }1 v- G' c$ ]2 H, F // list from timestep to timestep
+ x O& K1 p, V0 {" O$ O" c
8 A7 R) B/ v/ s/ m // By default, all `createActionForEach' modelActions have. I4 a) U5 N( I9 k
// a default order of `Sequential', which means that the1 [& e' I: j# } u
// order of iteration through the `heatbugList' will be
$ k" ^0 [5 r9 c // identical (assuming the list order is not changed, y! T/ U* @& a/ n& W4 z0 s4 N6 W
// indirectly by some other process).- x6 k5 b! A; L! W
+ ]" x0 {) X& X
modelActions = new ActionGroupImpl (getZone ());9 M% o* u7 K( |, Y* k$ z1 V
' N" b* H" f' D4 q @8 I ]- C try {8 A7 R9 U6 ]5 g; a7 w
modelActions.createActionTo$message9 g4 G4 M. S" H2 G+ a) u
(heat, new Selector (heat.getClass (), "stepRule", false));
1 B/ E4 ^; P0 Z8 S0 U8 ? } catch (Exception e) {7 }6 L/ Z% I% @% @9 o7 c( N) A
System.err.println ("Exception stepRule: " + e.getMessage ());
: |3 O: s. O. W: e! B }
2 `: M, q0 n8 g* i4 r$ L' X5 y6 q! K; P
try {8 v1 d% q- h7 n+ h* R6 V
Heatbug proto = (Heatbug) heatbugList.get (0);- H) H$ _6 u8 t; l4 E! q
Selector sel = ; o5 R$ d: u9 N. O: c( }
new Selector (proto.getClass (), "heatbugStep", false);) H3 n3 x- i; U% d' u" r' A; N' C2 m
actionForEach =
1 N; J6 H$ {5 r modelActions.createFActionForEachHomogeneous$call
2 z: C8 r2 J4 g3 R3 ] (heatbugList,6 o, O4 P' g! s
new FCallImpl (this, proto, sel,
% Y: D: a6 ?, Z) d0 q new FArgumentsImpl (this, sel)));
: e/ {' g1 d/ Q; m. Y" N/ G% b } catch (Exception e) {* J1 M8 H; m5 F& C
e.printStackTrace (System.err);
. @4 N+ p2 [0 f8 ?8 m4 U }1 b4 R: b8 I' I8 }, r3 x4 n! q( I
6 m) ~' o5 B5 H, J6 W
syncUpdateOrder ();5 `) j) x% v. n# x1 ?- v
8 L3 e( ]7 X- U, J1 n try {
5 N2 u: f8 {6 x6 } modelActions.createActionTo$message
! T7 g+ a% K o; @ (heat, new Selector (heat.getClass (), "updateLattice", false));
* e7 j, \. g5 B5 V% y# ?' c } catch (Exception e) {! F9 _, ~* n& F/ i/ r5 p/ o! Z
System.err.println("Exception updateLattice: " + e.getMessage ());
, o+ V1 n% B' E* g; K6 z; c }
# z. h! X; p6 H' y# l; a & w) O- k5 e' Z5 B9 F% g: I
// Then we create a schedule that executes the
- W5 a! {8 G. W2 f: k8 r* V6 [$ K# C // modelActions. modelActions is an ActionGroup, by itself it
& i6 _2 i' `% c1 h8 j // has no notion of time. In order to have it executed in" W# T/ l) @7 `( t- E0 Y5 s; u' ^
// time, we create a Schedule that says to use the
, C! p4 R) O% C // modelActions ActionGroup at particular times. This
W, _; f9 K; W: |9 D6 ? // schedule has a repeat interval of 1, it will loop every
2 v+ e% @% S2 e% r! I // time step. The action is executed at time 0 relative to* s6 Y+ h! Q4 j% t; U U
// the beginning of the loop.
. C6 V+ {4 k6 Q% r( _, v
8 R+ ?" N. z- B$ Z# Y2 q9 m // This is a simple schedule, with only one action that is
# Q0 ^8 J% K' ]1 j; h0 b) W6 H // just repeated every time. See jmousetrap for more
# Y$ l3 |7 ], L4 T& k' i) i* L$ } // complicated schedules.
! x }/ ^/ a3 y# k! i$ ? , K. ~) M" ~. f+ ]& R2 P
modelSchedule = new ScheduleImpl (getZone (), 1);
5 Q! K: n7 h9 {3 c X' ] ]0 l modelSchedule.at$createAction (0, modelActions);
# Z, m/ x5 k" y& z2 D , K) M+ a8 j8 O8 X% J1 v4 V/ Y4 E
return this;+ A& f* S4 G7 l" y3 Q8 o) Q4 t& ^
} |