KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
34  * The class representing the current road
35  */

36 public class CarModel implements org.objectweb.proactive.RunActive {
37
38   /** constants representing the ACTIVE state of the car */
39   final static int ACTIVE = 1;
40   /** constants representing the INACTIVE state of the car */
41   final static int INACTIVE = 0;
42   /** the range of time for the physics equation of the car */
43   final static double deltaT = 1;
44   /** the G acceleration */
45   final static double g = 9.8;
46   /** the weight of the car */
47   final static int m = 1000;
48   /** the state of the car depending of the user interaction */
49   public int state = INACTIVE;
50   /** the current acceleration needed for the speed */
51   double acc = 0;
52   /** the current speed of the car */
53   double speed = 0;
54   /** the current distance */
55   double distance = 0;
56   /** constants for the */
57   private static double coeff = 0.31;
58   /** the current incline of the road */
59   private double alpha = 0;
60   /** Reference onto the dispatcher */
61   Interface father;
62
63
64   /** No arg-constructor */
65   public CarModel() {
66
67   }
68
69
70   /** Initializes the car with a reference to the Interface Dispatcher : father */
71   public CarModel(Interface m_father) {
72     father = m_father;
73   }
74
75
76   ////////////////////////////////////////////////////////////
77

78   /** Changes the state of the car to ACTIVE */
79   public void engineOn() {
80     //father.displayMessage("CarModel : EngineOn");
81
if (state == INACTIVE) {
82       state = ACTIVE;
83       //father.displayMessage("Speed Changed "+speed);
84
}
85   }
86
87
88   /** Changes the state of the car to INACTIVE
89    * and notifies to the Interface to set the father speed to Null
90    */

91   public void engineOff() {
92     state = INACTIVE;
93     father.setSpeed(0);
94     speed = 0;
95     acc = 0;
96   }
97
98
99
100
101   ////////////////////////////////////////////////////////////
102

103   /** Sets the new speed of the car */
104   public void setSpeed(double m_speed) {
105     this.speed = m_speed;
106   }
107
108
109   /** Returns the current speed of the car */
110   public Double JavaDoc getSpeed() {
111     return new Double JavaDoc(this.speed);
112   }
113
114
115   /////////////////////////////////////////////////////////////
116

117   /**
118    * Computes the new speed and the new distance of the car
119    */

120   public void calculateSpeed(double m_newAcc) {
121     if (this.state == ACTIVE) {
122       // this.acc = father.getAcceleration().doubleValue();
123
if ((m_newAcc <= 50) && (m_newAcc >= 0)) {
124         speed = speed + m_newAcc * deltaT - coeff * speed * deltaT - Math.sin(alpha) * 4 * g * deltaT;
125         if (this.speed < 0.005)
126           this.speed = 0;
127
128       }
129       distance += speed * deltaT / 3600;
130       father.setSpeed(speed);
131       father.setDistance(distance);
132       //father.displayMessage("Speed : "+speed);
133
//father.displayMessage("Distance : "+ this.distance);
134

135     }
136     //father.displayMessage("CarModel : calculateSpeed : Speed >> "+speed);
137
//father.displayMessage("CarModel : calculateSpeed : CURRENT ACCELERATION >> "+ this.acc);
138

139   }
140
141
142   /////////////////////////////////////////////////////////////////
143

144   /** Increase the acceleration of the car */
145   public void incAcceleration(double m_acc) {
146     if (this.state == ACTIVE) {
147       if (((this.acc < 50) && (m_acc > 0)) || ((this.acc > 0) && (m_acc < 0)))
148         this.acc += m_acc;
149       //father.displayMessage("Acceleration : "+this.acc);
150
}
151     // else
152
// father.displayMessage("Engine Off, can not accelerate more");
153

154   }
155
156
157   /** Applies the new acceleration */
158   public void setAcceleration(double m_acc) {
159     //father.displayMessage("CarModel : setAcceleration >> M_ACC : "+ m_acc);
160
this.acc = m_acc;
161     //father.displayMessage("CarModel : setAcceleration >> THIS.ACC : "+ this.acc);
162
//father.displayMessage("Engine Accelerating or Decelerating");
163
}
164
165
166   /** Puts the acceleration to null */
167   public void brake() {
168     acc = 0;
169     if (speed > 0.01) {
170     } else {
171       speed = 0;
172     }
173   }
174
175
176
177   ////////////////////////////////////////////////////////////
178
/** sets the new value of the incline */
179   public void setAlpha(double m_alpha) {
180     alpha = m_alpha;
181   }
182   
183   
184
185   /** the current policy of the active object */
186   public void runActivity(org.objectweb.proactive.Body body) {
187     //father.displayMessage ("Starts live in object ActiveSpeed");
188
org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
189     while (body.isActive()) {
190       try {
191         Thread.sleep(500);
192       } catch (InterruptedException JavaDoc e) {}
193       if (this.state == ACTIVE)
194         this.calculateSpeed(acc);
195       service.flushingServeYoungest("brake");
196       service.flushingServeYoungest();
197     }
198   }
199 }
Popular Tags