KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > cruisecontrol > RoadModel


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.cruisecontrol;
32
33 /** Models the current road and incline of the road */
34 public class RoadModel implements org.objectweb.proactive.RunActive {
35
36   // Fields
37
final static int ACTIVE = 1;
38   final static int INACTIVE = 0;
39   /** State of the Road */
40   private int state = INACTIVE;
41   /** the random time of waiting between 2 differents inclines of the road */
42   private int time = 15000;
43   /** Current inclines of the road */
44   private double alpha = 0;
45   /** Reference to the dispatcher for communication purposes */
46   Interface father;
47
48   ///////////////////////////////////////////////////
49

50   /** No-arg constructor for the ProActive package */
51   public RoadModel() {
52
53   }
54
55
56   /** constructor which intializes the father field */
57   public RoadModel(Interface m_father) {
58     this.father = m_father;
59   }
60
61
62   ////////////////////////////////////////////////////////////
63
// the engine is on
64
/** Turns on this active object in order to computes the incline */
65   public void engineOn() {
66     this.state = ACTIVE;
67   }
68
69   // the engine is Off
70
/** turns off this active object off */
71   public void engineOff() {
72     this.state = INACTIVE;
73   }
74
75
76
77
78   ///////////////////////////////////////////////////////////////
79
/** Routine which computes the new value of the incline */
80   private void calculateAlpha() {
81
82     // alpha = 0.6 * Math.random();
83
time = 45000 + (int)(30000 * Math.random());
84     father.setAlpha(alpha);
85     //System.out.println("RoadModel : New Alpha : "+alpha);
86
}
87
88
89   /**
90    * Increases the current value of the incline road
91    */

92   public void incAlpha() {
93     if (alpha < 0.6)
94       alpha += 0.09;
95     father.setAlpha(alpha);
96     //System.out.println("RoadModel : IncAlpha : Alpha >> "+ alpha);
97
//System.out.println("RoadModel : IncAlpha : state >> "+ state);
98
}
99
100
101   /**
102    * Decreases the current value of alpha
103    */

104   public void decAlpha() {
105     if (alpha > -0.6)
106       alpha -= 0.09;
107     father.setAlpha(alpha);
108     //System.out.println("RoadModel : IncAlpha : Alpha >> "+ alpha);
109
//System.out.println("RoadModel : IncAlpha : state >> "+ state);
110
}
111
112
113   ////////////////////////////////////////////////////////////
114

115   /** Routine which computes the new random incline and sleeds for a time msec */
116   public void runActivity(org.objectweb.proactive.Body body) {
117     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
118     //System.out.println ("Starts live in object RoadModel");
119
while (body.isActive()) {
120       try {
121         Thread.sleep(1000);
122         if (this.state == ACTIVE)
123           this.calculateAlpha();
124         service.serveOldest();
125       } catch (InterruptedException JavaDoc e) {
126       }
127     }
128
129   }
130
131
132   /** Not used in this release */
133   public void initialize() {
134
135   }
136
137
138   ///////////////////////////////////////////////////////////////////
139
}
140
Popular Tags