PageRenderTime 102ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ianmayo/debrief
Java | 500 lines | 235 code | 53 blank | 212 comment | 27 complexity | f8ff11e777daedacfa90840721e9a440 MD5 | raw file
  1. package ASSET.Models.Decision.Tactical;
  2. import java.awt.Point;
  3. import java.util.Iterator;
  4. import ASSET.Models.DecisionType;
  5. import ASSET.Models.Decision.CoreDecision;
  6. import ASSET.Models.Decision.Movement.TransitWaypoint;
  7. import ASSET.Models.Detection.DetectionList;
  8. import ASSET.Models.Movement.HighLevelDemandedStatus;
  9. import ASSET.Models.Movement.MovementCharacteristics;
  10. import ASSET.Models.Movement.OnTopWaypoint;
  11. import ASSET.Participants.DemandedStatus;
  12. import ASSET.Participants.Status;
  13. import ASSET.Scenario.ScenarioActivityMonitor;
  14. import MWC.GUI.CanvasType;
  15. import MWC.GenericData.WorldDistance;
  16. import MWC.GenericData.WorldLocation;
  17. import MWC.GenericData.WorldPath;
  18. import MWC.GenericData.WorldSpeed;
  19. public abstract class PatternSearch_Core extends CoreDecision implements
  20. DecisionType.Paintable
  21. {
  22. /**
  23. * whether we've finished yet or not.
  24. */
  25. protected boolean _finished = false;
  26. /**
  27. * the route of points to pass through
  28. */
  29. protected TransitWaypoint _myRoute;
  30. /**
  31. * the (optional) height to conduct search at
  32. */
  33. protected WorldDistance _searchHeight;
  34. /**
  35. * the (optional) speed to conduct search at
  36. */
  37. protected WorldSpeed _searchSpeed;
  38. /**
  39. * store the supplied point in our route, assigning the search height if we
  40. * have one
  41. *
  42. * @param searchHeight
  43. * the (optional) height to search at
  44. * @param currentLocation
  45. * the location to store
  46. * @param route
  47. * the route containing the points
  48. */
  49. protected static void addPointToRoute(final WorldDistance searchHeight,
  50. WorldLocation currentLocation, final WorldPath route)
  51. {
  52. // insert the start datum
  53. // do we have a height?
  54. if (searchHeight != null)
  55. {
  56. // yes, store it.
  57. currentLocation.setDepth(-searchHeight.getValueIn(WorldDistance.METRES));
  58. }
  59. route.addPoint(currentLocation);
  60. }
  61. /**
  62. * decide the course of action to take, or return null to no be used
  63. *
  64. * @param status
  65. * the current status of the participant
  66. * @param chars
  67. * the movement chars for this participant
  68. * @param demStatus
  69. * the current demanded status
  70. * @param detections
  71. * the current list of detections for this participant
  72. * @param monitor
  73. * the object which handles weapons release/detonation
  74. * @param newTime
  75. * the time this decision is to be made
  76. */
  77. public final DemandedStatus decide(final Status status,
  78. MovementCharacteristics chars, final DemandedStatus demStatus,
  79. final DetectionList detections, final ScenarioActivityMonitor monitor,
  80. final long newTime)
  81. {
  82. HighLevelDemandedStatus res = null;
  83. String myActivity = null;
  84. // have we finished yet?
  85. if (!_finished)
  86. {
  87. // no, have we defined our route yet?
  88. if (_myRoute == null)
  89. {
  90. // ok, do we have an origin?
  91. if (getOrigin() == null)
  92. {
  93. // ok, initialise with our current location
  94. setOrigin(status.getLocation());
  95. }
  96. // create the path to follow
  97. final WorldPath route = createSearchRoute(status.getLocation());
  98. // and put it into a route
  99. _myRoute = new TransitWaypoint(route, null, false, new OnTopWaypoint());
  100. myActivity = "Generating new route";
  101. }
  102. // so, we now have our route. Has it finished yet?
  103. if (_myRoute.isFinished())
  104. {
  105. // done
  106. res = null;
  107. // and reset our list of points
  108. _myRoute.getDestinations().getPoints().clear();
  109. _myRoute = null;
  110. // remember the fact that we've now finished
  111. _finished = true;
  112. myActivity = null;
  113. }
  114. else
  115. {
  116. // ok - still going, ask the transitter what it wants to do
  117. res = (HighLevelDemandedStatus) _myRoute.decide(status, chars,
  118. demStatus, detections, monitor, newTime);
  119. // do we have an intended path?
  120. if (res != null)
  121. {
  122. // aah, do we have a search speed?
  123. if (getSearchSpeed() != null)
  124. {
  125. res.setSpeed(getSearchSpeed());
  126. }
  127. }
  128. // just double-check if we have now finished.
  129. if (res == null)
  130. {
  131. myActivity = _myRoute.getActivity();
  132. _finished = true;
  133. }
  134. else
  135. {
  136. myActivity = getDescriptor() + ":";
  137. // have we been interrupted?
  138. if (_myRoute.getVisitor().hasBeenInterrupted())
  139. {
  140. myActivity += " Resuming from interruption";
  141. }
  142. else
  143. {
  144. myActivity += "Heading for point:"
  145. + (_myRoute.getCurrentDestinationIndex());
  146. }
  147. }
  148. }
  149. }
  150. this.setLastActivity(myActivity);
  151. return res;
  152. }
  153. protected abstract String getDescriptor();
  154. protected abstract WorldPath createSearchRoute(WorldLocation currentLocation);
  155. protected EditorType _myEditor;
  156. /**
  157. * the start point for the search
  158. */
  159. protected WorldLocation _myOrigin;
  160. /**
  161. * the track spacing
  162. */
  163. protected WorldDistance _trackSpacing;
  164. /**
  165. * the dimensions of the area
  166. */
  167. protected WorldDistance _width;
  168. protected WorldDistance _height;
  169. /**
  170. * reset this decision model
  171. */
  172. public final void restart()
  173. {
  174. // forget that we were in a cycle
  175. _finished = false;
  176. // and clear the route
  177. _myRoute = null;
  178. }
  179. /**
  180. * indicate to this model that its execution has been interrupted by another
  181. * (prob higher priority) model
  182. *
  183. * @param currentStatus
  184. */
  185. public void interrupted(Status currentStatus)
  186. {
  187. // hey, we may be resuming this behaviour from another location.
  188. // We need to forget the current path to the next target so that it gets
  189. // recalculated
  190. _myRoute.getVisitor().routeInterrupted(currentStatus);
  191. }
  192. public final WorldLocation getOrigin()
  193. {
  194. return _myOrigin;
  195. }
  196. /**
  197. * set the start location for this manoeuvre (or supply null to use the
  198. * platform location when tactic first called)
  199. *
  200. * @param _myOrigin
  201. */
  202. public final void setOrigin(final WorldLocation _myOrigin)
  203. {
  204. this._myOrigin = _myOrigin;
  205. }
  206. public final boolean getFinished()
  207. {
  208. return _finished;
  209. }
  210. /**
  211. * return the (optional) height to search at
  212. *
  213. * @return the height.
  214. */
  215. public WorldDistance getSearchHeight()
  216. {
  217. return _searchHeight;
  218. }
  219. /**
  220. * set the (optional) height to search at
  221. *
  222. * @param searchHeight
  223. * search height
  224. */
  225. public void setSearchHeight(WorldDistance searchHeight)
  226. {
  227. _searchHeight = searchHeight;
  228. }
  229. /**
  230. * the (optional) search speed
  231. *
  232. * @return
  233. */
  234. public WorldSpeed getSearchSpeed()
  235. {
  236. return _searchSpeed;
  237. }
  238. /**
  239. * the (optional) search speed
  240. *
  241. * @param searchSpeed
  242. */
  243. public void setSearchSpeed(WorldSpeed searchSpeed)
  244. {
  245. this._searchSpeed = searchSpeed;
  246. }
  247. /**
  248. * retrieve the points we intend to travel through
  249. *
  250. * @return
  251. */
  252. public final WorldPath getRoute()
  253. {
  254. WorldPath res = null;
  255. if (_myRoute != null)
  256. res = _myRoute.getDestinations();
  257. return res;
  258. }
  259. /**
  260. * set the points we intend to travel through
  261. *
  262. * @param thePath
  263. * the path to follow
  264. */
  265. public final void setRoute(WorldPath thePath)
  266. {
  267. _myRoute.setDestinations(thePath);
  268. }
  269. public PatternSearch_Core(String name, WorldLocation myOrigin,
  270. WorldDistance searchHeight, WorldSpeed searchSpeed,
  271. WorldDistance trackSpacing, WorldDistance height, WorldDistance width)
  272. {
  273. super(name);
  274. _myOrigin = myOrigin;
  275. _searchHeight = searchHeight;
  276. _searchSpeed = searchSpeed;
  277. _trackSpacing = trackSpacing;
  278. _height = height;
  279. _width = width;
  280. }
  281. /**
  282. * whether there is any edit information for this item this is a convenience
  283. * function to save creating the EditorType data first
  284. *
  285. * @return yes/no
  286. */
  287. public boolean hasEditor()
  288. {
  289. return true;
  290. }
  291. /**
  292. * get the editor for this item
  293. *
  294. * @return the BeanInfo data for this editable object
  295. */
  296. abstract public EditorType getInfo();
  297. /**
  298. * get the version details for this model.
  299. *
  300. * <pre>
  301. * $Log: LadderSearch.java,v $
  302. * Revision 1.2 2006/09/11 09:36:04 Ian.Mayo
  303. * Fix dodgy accessor
  304. *
  305. * Revision 1.1 2006/08/08 14:21:34 Ian.Mayo
  306. * Second import
  307. *
  308. * Revision 1.1 2006/08/07 12:25:43 Ian.Mayo
  309. * First versions
  310. *
  311. * Revision 1.16 2004/11/25 14:29:29 Ian.Mayo
  312. * Handle switch to hi-res dates
  313. *
  314. * Revision 1.15 2004/11/03 09:54:50 Ian.Mayo
  315. * Allow search speed to be set
  316. * <p/>
  317. * Revision 1.14 2004/11/01 14:31:44 Ian.Mayo
  318. * Do more elaborate processing when creating sample instance for property testing
  319. * <p/>
  320. * Revision 1.13 2004/11/01 10:47:47 Ian.Mayo
  321. * Provide accessor for series of generated points
  322. * <p/>
  323. * Revision 1.12 2004/08/31 09:36:22 Ian.Mayo
  324. * Rename inner static tests to match signature **Test to make automated testing more consistent
  325. * <p/>
  326. * Revision 1.11 2004/08/26 16:27:03 Ian.Mayo
  327. * Implement editable properties
  328. * <p/>
  329. * Revision 1.10 2004/08/25 11:20:37 Ian.Mayo
  330. * Remove main methods which just run junit tests
  331. * <p/>
  332. * Revision 1.9 2004/08/24 10:36:25 Ian.Mayo
  333. * Do correct activity management bits (using parent)
  334. * <p/>
  335. * Revision 1.8 2004/08/20 13:32:29 Ian.Mayo
  336. * Implement inspection recommendations to overcome hidden parent objects, let CoreDecision handle the activity bits.
  337. * <p/>
  338. * Revision 1.7 2004/08/17 14:22:06 Ian.Mayo
  339. * Refactor to introduce parent class capable of storing name & isActive flag
  340. * <p/>
  341. * Revision 1.6 2004/08/09 15:50:31 Ian.Mayo
  342. * Refactor category types into Force, Environment, Type sub-classes
  343. * <p/>
  344. * Revision 1.5 2004/08/06 12:52:03 Ian.Mayo
  345. * Include current status when firing interruption
  346. * <p/>
  347. * Revision 1.4 2004/08/06 11:14:25 Ian.Mayo
  348. * Introduce interruptable behaviours, and recalc waypoint route after interruption
  349. * <p/>
  350. * Revision 1.3 2004/08/05 14:54:11 Ian.Mayo
  351. * Provide accessor to get series of waypoints
  352. * <p/>
  353. * Revision 1.2 2004/08/04 10:33:13 Ian.Mayo
  354. * Add optional search height, use it
  355. * <p/>
  356. * Revision 1.1 2004/05/24 15:56:29 Ian.Mayo
  357. * Commit updates from home
  358. * <p/>
  359. * Revision 1.4 2004/04/22 21:37:38 ian
  360. * Tidying
  361. * <p/>
  362. * Revision 1.3 2004/04/13 20:53:45 ian
  363. * Implement restart functionality
  364. * <p/>
  365. * Revision 1.2 2004/03/25 21:06:14 ian
  366. * Correct placing of first point, correct tests
  367. * <p/>
  368. * Revision 1.1.1.1 2004/03/04 20:30:52 ian
  369. * no message
  370. * <p/>
  371. * <p/>
  372. * </pre>
  373. */
  374. public String getVersion()
  375. {
  376. return "$Date: 2010-10-28 11:43:11 +0100 (Thu, 28 Oct 2010) $";
  377. }
  378. public WorldDistance getWidth()
  379. {
  380. return _width;
  381. }
  382. public void setWidth(WorldDistance width)
  383. {
  384. this._width = width;
  385. }
  386. public WorldDistance getHeight()
  387. {
  388. return _height;
  389. }
  390. public void setHeight(WorldDistance height)
  391. {
  392. this._height = height;
  393. }
  394. public final WorldDistance getTrackSpacing()
  395. {
  396. return _trackSpacing;
  397. }
  398. public final void setTrackSpacing(final WorldDistance _trackSpacing)
  399. {
  400. this._trackSpacing = _trackSpacing;
  401. }
  402. @Override
  403. public void paint(CanvasType dest)
  404. {
  405. // ok, do we have a route?
  406. if (_myRoute != null)
  407. {
  408. // does it have any points?
  409. if (_myRoute.getDestinations().getPoints().size() > 0)
  410. {
  411. // ok, make it a feint line
  412. dest.setLineStyle(CanvasType.SHORT_DASHES);
  413. // ok, go for it
  414. Iterator<WorldLocation> pts = _myRoute.getDestinations().getPoints()
  415. .iterator();
  416. Point lastP = null;
  417. while (pts.hasNext())
  418. {
  419. WorldLocation thisP = pts.next();
  420. Point pt = dest.toScreen(thisP);
  421. if (lastP != null)
  422. {
  423. dest.drawLine(lastP.x, lastP.y, pt.x, pt.y);
  424. }
  425. // is this the current target?
  426. boolean painted = false;
  427. int index = _myRoute.getCurrentDestinationIndex();
  428. if (index != HighLevelDemandedStatus.PATH_COMPLETE)
  429. {
  430. WorldLocation tgt = _myRoute.getDestinations().getLocationAt(index);
  431. if (tgt.equals(thisP))
  432. {
  433. // shade it in solid
  434. dest.fillRect(pt.x - 3, pt.y - 3, 7, 7);
  435. painted = true;
  436. }
  437. }
  438. if (!painted)
  439. {
  440. dest.drawRect(pt.x - 3, pt.y - 3, 7, 7);
  441. }
  442. lastP = new Point(pt);
  443. }
  444. }
  445. }
  446. }
  447. }