FlexManager.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. import classes.AbstractCpsObject;
  7. import classes.CpsUpperNode;
  8. import classes.Flexibility;
  9. import classes.HolonElement;
  10. import classes.HolonObject;
  11. import ui.model.Model;
  12. /**
  13. * Class to Manage to flexibilities.
  14. * To order, view Flexibilities.
  15. * @author tom
  16. *
  17. */
  18. public class FlexManager {
  19. private int timeStep;
  20. private List<Flexibility> allFlexModels;
  21. private List<Flexibility> allFlexesOrderedThisTimeStep = new ArrayList<Flexibility>();
  22. private HashMap<Flexibility, FlexWrapper> accessFlexMap = new HashMap<Flexibility, FlexWrapper>();
  23. public FlexManager(Model model, int timeStep, FlexManager timestepBefore){
  24. this.timeStep = timeStep;
  25. allFlexModels = getAllFlexFromModel(model);
  26. //fill accessFlexMap
  27. allFlexModels.stream().map(flex -> new FlexWrapper(flex, timeStep, (timestepBefore != null)?timestepBefore.getFlexWrapper(flex):null)).forEach(flexWrapper -> accessFlexMap.put(flexWrapper.getFlex(), flexWrapper));
  28. }
  29. private FlexWrapper getFlexWrapper(Flexibility flex) {
  30. return accessFlexMap.getOrDefault(flex, null);
  31. }
  32. public List<FlexWrapper> getAllFlexWrapper() {
  33. return accessFlexMap.values().stream().collect(Collectors.toList());
  34. }
  35. public List<FlexWrapper> getAllFlexWrapperWithState(FlexState state) {
  36. return accessFlexMap.values().stream().filter(flexWrapper -> (flexWrapper.getState() == state)).collect(Collectors.toList());
  37. }
  38. private List<Flexibility> getAllFlexFromModel(Model model) {
  39. return createListOfHolonObjects(model.getObjectsOnCanvas()).stream().flatMap(hObject -> hObject.getElements().stream()).flatMap(hElement -> hElement.flexList.stream()).collect(Collectors.toList());
  40. }
  41. private List<HolonObject> createListOfHolonObjects(List<AbstractCpsObject> objectsOnCanvas) {
  42. List<HolonObject> list = new ArrayList<HolonObject>();
  43. for(AbstractCpsObject aCps : objectsOnCanvas) {
  44. if(aCps instanceof HolonObject) list.add((HolonObject) aCps);
  45. else if(aCps instanceof CpsUpperNode)list.addAll(createListOfHolonObjects(((CpsUpperNode)aCps).getNodes()));
  46. }
  47. return list;
  48. }
  49. public int getTimeStep() {
  50. return timeStep;
  51. }
  52. public List<Flexibility> getAllFlexesOrderedThisTimeStep() {
  53. return allFlexesOrderedThisTimeStep;
  54. }
  55. public void orderFlexFromList(List<Flexibility> flexList) {
  56. flexList.stream().forEach(flex -> {
  57. FlexWrapper flexToOrder = accessFlexMap.get(flex);
  58. if(flexToOrder!=null)flexToOrder.order();
  59. });
  60. }
  61. public boolean isAFlexInUseOfHolonElement(HolonElement ele) {
  62. return ele.flexList.stream().filter(flex -> this.accessFlexMap.containsKey(flex)).anyMatch(flex -> (this.accessFlexMap.get(flex).getState() == FlexState.IN_USE));
  63. }
  64. public static enum FlexState{
  65. IN_USE, ON_COOLDOWN, OFFERED, NOT_OFFERED, UNAVAILABLE
  66. }
  67. //Classes
  68. public class FlexWrapper{
  69. private Flexibility flex;
  70. private FlexState state;
  71. int timeStep;
  72. int durationEndTime = -1;
  73. int coolDownEndTime = -1;
  74. public FlexWrapper(Flexibility flex, int timeStep, FlexWrapper old) {
  75. this.flex = flex;
  76. this.timeStep = timeStep;
  77. if(old == null) {
  78. state = flex.fulfillsConstrains()?(flex.offered?FlexState.OFFERED:FlexState.NOT_OFFERED):FlexState.UNAVAILABLE;
  79. }else {
  80. durationEndTime = old.durationEndTime;
  81. coolDownEndTime = old.coolDownEndTime;
  82. if(remainingTimeTillActivation() == 0) state = flex.fulfillsConstrains()?(flex.offered?FlexState.OFFERED:FlexState.NOT_OFFERED):FlexState.UNAVAILABLE;
  83. else if(remainingDuration()== 0) state = FlexState.ON_COOLDOWN;
  84. else state = FlexState.IN_USE;
  85. }
  86. }
  87. public Flexibility getFlex() {
  88. return flex;
  89. }
  90. public FlexState getState() {
  91. return state;
  92. }
  93. public boolean canOrder() {
  94. return (state == FlexState.OFFERED) && //Right state
  95. !accessFlexMap.values().stream().anyMatch(flexWrapper -> (flexWrapper.getFlex().getElement() == flex.getElement() && flexWrapper != this && (flexWrapper.getState() == FlexState.IN_USE || flexWrapper.getState() == FlexState.ON_COOLDOWN))); //No other flex of this ele in use
  96. }
  97. public boolean order() {
  98. if(canOrder()) {
  99. state=FlexState.IN_USE;
  100. allFlexesOrderedThisTimeStep.add(flex);
  101. durationEndTime = timeStep + flex.getDuration();
  102. coolDownEndTime = durationEndTime + flex.getCooldown();
  103. return true;
  104. }
  105. return false;
  106. }
  107. public int remainingTimeTillActivation() {
  108. return Math.max(0, coolDownEndTime - timeStep);
  109. }
  110. public int remainingDuration() {
  111. return Math.max(0, durationEndTime - timeStep);
  112. }
  113. }
  114. }