HolonElementSketch.java 3.2 KB

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