HolonElementSketch.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package addOns.JSON;
  2. import classes.Constrain;
  3. import classes.Flexibility;
  4. import classes.HolonElement;
  5. import classes.HolonElement.Priority;
  6. import ui.model.Model;
  7. public class HolonElementSketch {
  8. //HolonElement
  9. public String name;
  10. public int minAmount;
  11. public int maxAmount;
  12. public float energy;
  13. public String priority;
  14. public FlexibilitySketch onFlex;
  15. public FlexibilitySketch offFlex;
  16. public HolonElementSketch(String name, int minAmount, int maxAmount, float energy) {
  17. this.name = name;
  18. this.minAmount = minAmount;
  19. this.maxAmount = maxAmount;
  20. this.energy = energy;
  21. }
  22. public HolonElement createHolonElement(Model model, boolean active, String nameOfHolonObject) {
  23. HolonElement ele = new HolonElement(name, Random.nextIntegerInRange(minAmount, maxAmount + 1), energy , model);
  24. ele.setActive(active);
  25. if(onFlex != null && Random.nextDouble() < onFlex.flexChance)addFlex(ele, nameOfHolonObject, true);
  26. if(offFlex != null && Random.nextDouble() < offFlex.flexChance)addFlex(ele, nameOfHolonObject, false);
  27. ele.setPriority(Priority.valueOf(priority));
  28. return ele;
  29. }
  30. public void checkValues() {
  31. minAmount = Math.abs(minAmount);
  32. maxAmount = Math.abs(maxAmount);
  33. if(maxAmount < minAmount) {
  34. //Swap
  35. int intermediate = minAmount;
  36. minAmount = maxAmount;
  37. maxAmount = intermediate;
  38. }
  39. if(onFlex != null) {
  40. onFlex.checkValues();
  41. }
  42. if(offFlex != null) {
  43. onFlex.checkValues();
  44. }
  45. }
  46. public void addFlex(HolonElement ele, String nameOfHolonObject,boolean onConstrain) {
  47. Flexibility toCreateFlex = new Flexibility(ele);
  48. FlexibilitySketch constrain = onConstrain?onFlex:offFlex;
  49. toCreateFlex.name = nameOfHolonObject + "_" + ele.getEleName() + (onConstrain?"_OnFlex":"_OffFlex");
  50. toCreateFlex.speed = 0;
  51. toCreateFlex.setDuration(Random.nextIntegerInRange(constrain.minDuration, constrain.maxDuration+1));
  52. toCreateFlex.cost = Random.nextFloatInRange(constrain.minCost, constrain.maxCost);
  53. toCreateFlex.setCooldown(Random.nextIntegerInRange(constrain.minCooldown, constrain.maxCooldown+1));
  54. toCreateFlex.offered=true;
  55. if(onConstrain) {
  56. toCreateFlex.constrainList.add(Constrain.createOnConstrain());
  57. }else {
  58. toCreateFlex.constrainList.add(Constrain.createOffConstrain());
  59. }
  60. ele.flexList.add(toCreateFlex);
  61. }
  62. private static class Random{
  63. private static java.util.Random random = new java.util.Random();
  64. /**
  65. * True or false
  66. * @return the random boolean.
  67. */
  68. public static boolean nextBoolean(){
  69. return random.nextBoolean();
  70. }
  71. /**
  72. * Between 0.0(inclusive) and 1.0 (exclusive)
  73. * @return the random double.
  74. */
  75. public static double nextDouble() {
  76. return random.nextDouble();
  77. }
  78. public static float nextFloatInRange(float min, float max) {
  79. return min + random.nextFloat() * Math.abs(max - min);
  80. }
  81. public static double nextDoubleInRange(double min, double max) {
  82. return min + random.nextDouble() * Math.abs(max - min);
  83. }
  84. /**
  85. * Random Int in Range [min;max[ with UniformDistirbution
  86. * @param min
  87. * @param max
  88. * @return
  89. */
  90. public static int nextIntegerInRange(int min, int max) {
  91. return min + random.nextInt(max - min);
  92. }
  93. }
  94. }