PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Examples/org/objectweb/proactive/examples/cruisecontrol/CarModel.java

https://bitbucket.org/lp/programming-multiactivities
Java | 208 lines | 87 code | 35 blank | 86 comment | 18 complexity | 458b32bca509526513f6542a8af10326 MD5 | raw file
  1. /*
  2. * ################################################################
  3. *
  4. * ProActive Parallel Suite(TM): The Java(TM) library for
  5. * Parallel, Distributed, Multi-Core Computing for
  6. * Enterprise Grids & Clouds
  7. *
  8. * Copyright (C) 1997-2012 INRIA/University of
  9. * Nice-Sophia Antipolis/ActiveEon
  10. * Contact: proactive@ow2.org or contact@activeeon.com
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Affero General Public License
  14. * as published by the Free Software Foundation; version 3 of
  15. * the License.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  25. * USA
  26. *
  27. * If needed, contact us to obtain a release under GPL Version 2 or 3
  28. * or a different license than the AGPL.
  29. *
  30. * Initial developer(s): The ProActive Team
  31. * http://proactive.inria.fr/team_members.htm
  32. * Contributor(s):
  33. *
  34. * ################################################################
  35. * $$PROACTIVE_INITIAL_DEV$$
  36. */
  37. package org.objectweb.proactive.examples.cruisecontrol;
  38. import org.objectweb.proactive.extensions.annotation.ActiveObject;
  39. /**
  40. * The class representing the current road
  41. */
  42. @ActiveObject
  43. public class CarModel implements org.objectweb.proactive.RunActive {
  44. /** constants representing the ACTIVE state of the car */
  45. final static int ACTIVE = 1;
  46. /** constants representing the INACTIVE state of the car */
  47. final static int INACTIVE = 0;
  48. /** the range of time for the physics equation of the car */
  49. final static double deltaT = 1;
  50. /** the G acceleration */
  51. final static double g = 9.8;
  52. /** the weight of the car */
  53. final static int m = 1000;
  54. /** the state of the car depending of the user interaction */
  55. private int state = INACTIVE;
  56. /** the current acceleration needed for the speed */
  57. private double acc = 0;
  58. /** the current speed of the car */
  59. private double speed = 0;
  60. /** the current distance */
  61. private double distance = 0;
  62. /** constants for the */
  63. private static double coeff = 0.31;
  64. /** the current incline of the road */
  65. private double alpha = 0;
  66. /** Reference onto the dispatcher */
  67. private Interface father;
  68. /** No arg-constructor */
  69. public CarModel() {
  70. }
  71. /** Initializes the car with a reference to the Interface Dispatcher : father */
  72. public CarModel(Interface m_father) {
  73. father = m_father;
  74. }
  75. ////////////////////////////////////////////////////////////
  76. /** Changes the state of the car to ACTIVE */
  77. public void engineOn() {
  78. //father.displayMessage("CarModel : EngineOn");
  79. if (state == INACTIVE) {
  80. state = ACTIVE;
  81. //father.displayMessage("Speed Changed "+speed);
  82. }
  83. }
  84. /** Changes the state of the car to INACTIVE
  85. * and notifies to the Interface to set the father speed to Null
  86. */
  87. public void engineOff() {
  88. state = INACTIVE;
  89. father.setSpeed(0);
  90. speed = 0;
  91. acc = 0;
  92. }
  93. ////////////////////////////////////////////////////////////
  94. /** Sets the new speed of the car */
  95. public void setSpeed(double m_speed) {
  96. this.speed = m_speed;
  97. }
  98. /** Returns the current speed of the car */
  99. public Double getSpeed() {
  100. return new Double(this.speed);
  101. }
  102. /////////////////////////////////////////////////////////////
  103. /**
  104. * Computes the new speed and the new distance of the car
  105. */
  106. public void calculateSpeed(double m_newAcc) {
  107. if (this.state == ACTIVE) {
  108. // this.acc = father.getAcceleration().doubleValue();
  109. if ((m_newAcc <= 50) && (m_newAcc >= 0)) {
  110. speed = (speed + (m_newAcc * deltaT)) - (coeff * speed * deltaT) -
  111. (Math.sin(alpha) * 4 * g * deltaT);
  112. if (this.speed < 0.005) {
  113. this.speed = 0;
  114. }
  115. }
  116. distance += ((speed * deltaT) / 3600);
  117. father.setSpeed(speed);
  118. father.setDistance(distance);
  119. //father.displayMessage("Speed : "+speed);
  120. //father.displayMessage("Distance : "+ this.distance);
  121. }
  122. //father.displayMessage("CarModel : calculateSpeed : Speed >> "+speed);
  123. //father.displayMessage("CarModel : calculateSpeed : CURRENT ACCELERATION >> "+ this.acc);
  124. }
  125. /////////////////////////////////////////////////////////////////
  126. /** Increase the acceleration of the car */
  127. public void incAcceleration(double m_acc) {
  128. if (this.state == ACTIVE) {
  129. if (((this.acc < 50) && (m_acc > 0)) || ((this.acc > 0) && (m_acc < 0))) {
  130. this.acc += m_acc;
  131. }
  132. //father.displayMessage("Acceleration : "+this.acc);
  133. }
  134. // else
  135. // father.displayMessage("Engine Off, can not accelerate more");
  136. }
  137. /** Applies the new acceleration */
  138. public void setAcceleration(double m_acc) {
  139. //father.displayMessage("CarModel : setAcceleration >> M_ACC : "+ m_acc);
  140. this.acc = m_acc;
  141. //father.displayMessage("CarModel : setAcceleration >> THIS.ACC : "+ this.acc);
  142. //father.displayMessage("Engine Accelerating or Decelerating");
  143. }
  144. /** Puts the acceleration to null */
  145. public void brake() {
  146. acc = 0;
  147. if (speed > 0.01) {
  148. } else {
  149. speed = 0;
  150. }
  151. }
  152. ////////////////////////////////////////////////////////////
  153. /** sets the new value of the incline */
  154. public void setAlpha(double m_alpha) {
  155. alpha = m_alpha;
  156. }
  157. /** the current policy of the active object */
  158. public void runActivity(org.objectweb.proactive.Body body) {
  159. //father.displayMessage ("Starts live in object ActiveSpeed");
  160. org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
  161. while (body.isActive()) {
  162. try {
  163. Thread.sleep(500);
  164. } catch (InterruptedException e) {
  165. }
  166. if (this.state == ACTIVE) {
  167. this.calculateSpeed(acc);
  168. }
  169. service.flushingServeYoungest("brake");
  170. service.flushingServeYoungest();
  171. }
  172. }
  173. }