PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/org.mwc.asset.legacy/src/ASSET/Models/Decision/Tactical/SSKRecharge.java

https://bitbucket.org/haris_peco/debrief
Java | 483 lines | 232 code | 66 blank | 185 comment | 13 complexity | 9641759f89e6d72ed47daba0228f86ff MD5 | raw file
  1. package ASSET.Models.Decision.Tactical;
  2. import ASSET.ParticipantType;
  3. import ASSET.Models.Decision.CoreDecision;
  4. import ASSET.Models.Decision.TargetType;
  5. import ASSET.Models.Detection.DetectionEvent;
  6. import ASSET.Models.Detection.DetectionList;
  7. import ASSET.Models.Movement.SimpleDemandedStatus;
  8. import ASSET.Participants.Category;
  9. import ASSET.Participants.CoreParticipant;
  10. import ASSET.Participants.DemandedStatus;
  11. import ASSET.Participants.Status;
  12. import ASSET.Util.SupportTesting;
  13. import MWC.GUI.Editable;
  14. import MWC.GenericData.WorldLocation;
  15. import MWC.GenericData.WorldSpeed;
  16. /**
  17. * Title:
  18. * Description:
  19. * Copyright: Copyright (c) 2001
  20. * Company:
  21. *
  22. * @author Ian Mayo
  23. * @version 1.0
  24. */
  25. public class SSKRecharge extends CoreDecision implements java.io.Serializable
  26. {
  27. //////////////////////////////////////////////////////////////////////
  28. // member variables
  29. //////////////////////////////////////////////////////////////////////
  30. /**
  31. *
  32. */
  33. private static final long serialVersionUID = 1L;
  34. /**
  35. * the maximum level we let the battery reach before we recharge
  36. */
  37. private double _minLevel = 20;
  38. /**
  39. * the minimum we let the battery charge to before we stop
  40. */
  41. private double _safeLevel = 100;
  42. /**
  43. * whether we are currently charging or not
  44. */
  45. private boolean _recharging = false;
  46. /**
  47. * the speed we snort at (kts)
  48. */
  49. private double _snortSpeed = 4.0;
  50. /**
  51. * a local copy of our editable object
  52. */
  53. private MWC.GUI.Editable.EditorType _myEditor = null;
  54. /**
  55. * types of target we avoid when snorting
  56. */
  57. private TargetType _evadeThese = null;
  58. //////////////////////////////////////////////////////////////////////
  59. // constructor
  60. //////////////////////////////////////////////////////////////////////
  61. public SSKRecharge()
  62. {
  63. super("SSK Recharge");
  64. }
  65. //////////////////////////////////////////////////
  66. // member variables
  67. //////////////////////////////////////////////////
  68. public ASSET.Participants.DemandedStatus decide(final ASSET.Participants.Status status,
  69. ASSET.Models.Movement.MovementCharacteristics chars, DemandedStatus demStatus, final ASSET.Models.Detection.DetectionList detections,
  70. ASSET.Scenario.ScenarioActivityMonitor monitor,
  71. final long time)
  72. {
  73. SimpleDemandedStatus res = null;
  74. // is our battery in trouble?
  75. final double fuelLevel = status.getFuelLevel();
  76. // what's our depth?
  77. final double height = -status.getLocation().getDepth();
  78. // final double spd = status.getSpeed().getValueIn(WorldSpeed.M_sec);
  79. // yes, we're charging, decide if we are in trouble
  80. boolean _inTrouble = false;
  81. // first see if there are any hostile contacts
  82. if (_evadeThese != null)
  83. {
  84. // look through detections for threat contact
  85. for (int i = 0; i < detections.size(); i++)
  86. {
  87. final ASSET.Models.Detection.DetectionEvent det = detections.getDetection(i);
  88. final ASSET.Participants.Category target = det.getTargetType();
  89. if (_evadeThese.matches(target))
  90. {
  91. _inTrouble = true;
  92. break; // drop out, we've seen enough
  93. } // whether this target is a hostile one
  94. } // looping through the detections
  95. } // whether we have a hostile contact list
  96. // so, are we in trouble?
  97. if (_inTrouble)
  98. {
  99. res = null;
  100. _recharging = false;
  101. }
  102. else
  103. {
  104. // are we currently charging?
  105. if ((height >= ASSET.Models.Vessels.SSK.CHARGE_HEIGHT))
  106. {
  107. // do we need to continue snorting?
  108. if (fuelLevel >= _safeLevel)
  109. {
  110. // box is full, drop out
  111. res = null;
  112. _recharging = false;
  113. }
  114. else
  115. {
  116. // we still need to snort
  117. res = new SimpleDemandedStatus(time, status);
  118. res.setSpeed(_snortSpeed);
  119. _recharging = true;
  120. }
  121. }
  122. else
  123. {
  124. // we are not currently charging, look at our box level
  125. if (fuelLevel <= _minLevel)
  126. {
  127. // we need to charge up, head for charge depth
  128. res = new SimpleDemandedStatus(time, status);
  129. res.setHeight(ASSET.Models.Vessels.SSK.CHARGE_HEIGHT);
  130. res.setSpeed(_snortSpeed);
  131. _recharging = false;
  132. } // whether we still need to snort
  133. } // whether we are already snorting
  134. } // whether we are in trouble
  135. // and do the name
  136. String activity = "";
  137. if (_recharging)
  138. activity = getName();
  139. else
  140. activity = "Heading to PD";
  141. super.setLastActivity(activity);
  142. return res;
  143. }
  144. public void restart()
  145. {
  146. //
  147. }
  148. /**
  149. * indicate to this model that its execution has been interrupted by another (prob higher priority) model
  150. *
  151. * @param currentStatus
  152. */
  153. public void interrupted(Status currentStatus)
  154. {
  155. // ignore.
  156. }
  157. /**
  158. * whether there is any edit information for this item
  159. * this is a convenience function to save creating the EditorType data
  160. * first
  161. *
  162. * @return yes/no
  163. */
  164. public boolean hasEditor()
  165. {
  166. return true;
  167. }
  168. /**
  169. * get the editor for this item
  170. *
  171. * @return the BeanInfo data for this editable object
  172. */
  173. public MWC.GUI.Editable.EditorType getInfo()
  174. {
  175. if (_myEditor == null)
  176. _myEditor = new SSKRechargeInfo(this);
  177. return _myEditor;
  178. }
  179. /**
  180. * the level we decide to do a recharge at
  181. */
  182. public void setMinLevel(final double newMinLevel)
  183. {
  184. _minLevel = newMinLevel;
  185. }
  186. /**
  187. * the level we decide to do a recharge at
  188. */
  189. public double getMinLevel()
  190. {
  191. return _minLevel;
  192. }
  193. /**
  194. * the types of target we avoid when snorting
  195. */
  196. public void setTargetToEvade(final ASSET.Models.Decision.TargetType target)
  197. {
  198. _evadeThese = target;
  199. }
  200. /**
  201. * the types of target we avoid when snorting
  202. */
  203. public ASSET.Models.Decision.TargetType getTargetToEvade()
  204. {
  205. if (_evadeThese == null)
  206. _evadeThese = new TargetType();
  207. return _evadeThese;
  208. }
  209. /**
  210. * the level we decide we are ok to continue
  211. */
  212. public void setSafeLevel(final double newSafeLevel)
  213. {
  214. _safeLevel = newSafeLevel;
  215. }
  216. /**
  217. * the level we decide we are ok to continue
  218. */
  219. public double getSafeLevel()
  220. {
  221. return _safeLevel;
  222. }
  223. /**
  224. * the speed we travel at when snorting (kts)
  225. */
  226. public void setSnortSpeed(final WorldSpeed newSnortSpeed)
  227. {
  228. _snortSpeed = newSnortSpeed.getValueIn(WorldSpeed.M_sec);
  229. }
  230. /**
  231. * the speed we travel at when snorting (kts)
  232. */
  233. public WorldSpeed getSnortSpeed()
  234. {
  235. return new WorldSpeed(_snortSpeed, WorldSpeed.M_sec);
  236. }
  237. ////////////////////////////////////////////////////////////
  238. // model support
  239. ////////////////////////////////////////////////////////////
  240. /**
  241. * get the version details for this model.
  242. * <pre>
  243. * $Log: SSKRecharge.java,v $
  244. * Revision 1.1 2006/08/08 14:21:37 Ian.Mayo
  245. * Second import
  246. *
  247. * Revision 1.1 2006/08/07 12:25:46 Ian.Mayo
  248. * First versions
  249. *
  250. * Revision 1.17 2004/11/12 16:21:10 Ian.Mayo
  251. * Remove StayAlive parameter for Waterfall
  252. *
  253. * Revision 1.16 2004/09/02 13:17:37 Ian.Mayo
  254. * Reflect CoreDecision handling the toString method
  255. * <p/>
  256. * Revision 1.15 2004/08/26 14:09:50 Ian.Mayo
  257. * Start switching to automated property editor testing. Correct property editor bugs where they arise.
  258. * <p/>
  259. * Revision 1.14 2004/08/25 11:20:46 Ian.Mayo
  260. * Remove main methods which just run junit tests
  261. * <p/>
  262. * Revision 1.13 2004/08/20 13:32:35 Ian.Mayo
  263. * Implement inspection recommendations to overcome hidden parent objects, let CoreDecision handle the activity bits.
  264. * <p/>
  265. * Revision 1.12 2004/08/17 14:22:11 Ian.Mayo
  266. * Refactor to introduce parent class capable of storing name & isActive flag
  267. * <p/>
  268. * Revision 1.11 2004/08/09 15:50:36 Ian.Mayo
  269. * Refactor category types into Force, Environment, Type sub-classes
  270. * <p/>
  271. * Revision 1.10 2004/08/06 12:52:08 Ian.Mayo
  272. * Include current status when firing interruption
  273. * <p/>
  274. * Revision 1.9 2004/08/06 11:14:30 Ian.Mayo
  275. * Introduce interruptable behaviours, and recalc waypoint route after interruption
  276. * <p/>
  277. * Revision 1.8 2004/05/24 15:57:17 Ian.Mayo
  278. * Commit updates from home
  279. * <p/>
  280. * Revision 1.1.1.1 2004/03/04 20:30:52 ian
  281. * no message
  282. * <p/>
  283. * Revision 1.7 2004/02/18 08:48:11 Ian.Mayo
  284. * Sync from home
  285. * <p/>
  286. * Revision 1.5 2003/11/05 09:19:57 Ian.Mayo
  287. * Include MWC Model support
  288. * <p/>
  289. * </pre>
  290. */
  291. public String getVersion()
  292. {
  293. return "$Date$";
  294. }
  295. static public class SSKRechargeInfo extends MWC.GUI.Editable.EditorType
  296. {
  297. /**
  298. * constructor for editable details of a set of Layers
  299. *
  300. * @param data the Layers themselves
  301. */
  302. public SSKRechargeInfo(final SSKRecharge data)
  303. {
  304. super(data, data.getName(), "Recharge");
  305. }
  306. /**
  307. * editable GUI properties for our participant
  308. *
  309. * @return property descriptions
  310. */
  311. public java.beans.PropertyDescriptor[] getPropertyDescriptors()
  312. {
  313. try
  314. {
  315. final java.beans.PropertyDescriptor[] res = {
  316. prop("TargetToEvade", "the types of target we avoid when snorting"),
  317. prop("SafeLevel", "the battery level at which we stop recharging"),
  318. prop("MinLevel", "the battery level at which we start recharging"),
  319. prop("SnortSpeed", "the speed at which this vessel snorts"),
  320. prop("Name", "the name of this SSK Recharge model"),
  321. };
  322. // res[0].setPropertyEditorClass(ASSET.GUI.Editors.TargetTypeEditor.class);
  323. return res;
  324. }
  325. catch (java.beans.IntrospectionException e)
  326. {
  327. e.printStackTrace();
  328. return super.getPropertyDescriptors();
  329. }
  330. }
  331. }
  332. /**
  333. * *************************************************
  334. * testing
  335. * *************************************************
  336. */
  337. static public class testMe extends SupportTesting.EditableTesting
  338. {
  339. static public final String TEST_ALL_TEST_TYPE = "UNIT";
  340. public testMe(final String val)
  341. {
  342. super(val);
  343. }
  344. /**
  345. * get an object which we can test
  346. *
  347. * @return Editable object which we can check the properties for
  348. */
  349. public Editable getEditable()
  350. {
  351. final SSKRecharge recharge = new SSKRecharge();
  352. return recharge;
  353. }
  354. /**
  355. * test we decide to snort correctly
  356. */
  357. public void testSnorting()
  358. {
  359. // setup our scenario
  360. final SSKRecharge recharge = new SSKRecharge();
  361. recharge.setMinLevel(20);
  362. recharge.setSafeLevel(80);
  363. recharge.setSnortSpeed(new WorldSpeed(6, WorldSpeed.M_sec));
  364. recharge.setTargetToEvade(null);
  365. // check when we've no target to avoid
  366. final Status theStat = new Status(12, 0);
  367. theStat.setCourse(12);
  368. theStat.setFuelLevel(100);
  369. theStat.setSpeed(new WorldSpeed(12, WorldSpeed.M_sec));
  370. theStat.setLocation(new WorldLocation(12, 12, 33));
  371. final Category friendly = new Category(Category.Force.RED, Category.Environment.SUBSURFACE, Category.Type.SUBMARINE);
  372. // see what the plan is
  373. DemandedStatus de = recharge.decide(theStat, null, null, null, null, 12);
  374. // check we don't need to recharge
  375. assertEquals("Check we don't need to recharge with full box", de, null);
  376. // check we need to recharge at limit
  377. theStat.setFuelLevel(20);
  378. de = recharge.decide(theStat, null, null, null, null, 12);
  379. assertNotNull("Check we recharge at limit", de);
  380. // check we need to recharge under limit
  381. theStat.setFuelLevel(18);
  382. de = recharge.decide(theStat, null, null, null, null, 12);
  383. assertNotNull("Check we recharge under limit", de);
  384. // check we recharge with friendly contacts
  385. final DetectionList dl = new DetectionList();
  386. final ASSET.Models.Sensor.Initial.OpticSensor optic = new ASSET.Models.Sensor.Initial.OpticSensor(12);
  387. final ParticipantType cp = new CoreParticipant(12);
  388. final DetectionEvent dEvent = new DetectionEvent(0,
  389. cp.getId(), null,
  390. optic,
  391. null,
  392. null,
  393. null,
  394. null,
  395. null,
  396. friendly,
  397. null,
  398. null,
  399. cp);
  400. dl.add(dEvent);
  401. // set the avoid category to blue vessels
  402. recharge.setTargetToEvade(new TargetType(Category.Force.BLUE));
  403. // see what happens now
  404. de = recharge.decide(theStat, null, null, dl, null, 12);
  405. assertNotNull("Check don't avoid friendly", de);
  406. // see about avoiding hostile vessels
  407. // change it so we want to avoid friendly vessels
  408. recharge.setTargetToEvade(new TargetType(Category.Force.RED));
  409. de = recharge.decide(theStat, null, null, dl, null, 12);
  410. assertNull("Check don't snort with hostile", de);
  411. theStat.setFuelLevel(80);
  412. recharge.setTargetToEvade(new TargetType(Category.Force.BLUE));
  413. de = recharge.decide(theStat, null, null, dl, null, 12);
  414. assertNull("Check don't snort when not needed", de);
  415. }
  416. }
  417. }