Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 KB | None | 0 0
  1. package com.github.jasiolek173;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class ChainReactionTest {
  7. private final int linksNumber;
  8. private final long connectionNumber;
  9. private final ChainReactionInterface chainReactionInterface;
  10. private final DamagePropagation damagePropagationInterface;
  11. private final Random random = new Random();
  12. private final Object lockForLinks = new Object();
  13. private final Object lockForConnections = new Object();
  14.  
  15. private String[] road;
  16.  
  17. public ChainReactionTest(int linksNumber, long connectionNumber, ChainReactionInterface chainReactionInterface, DamagePropagation damagePropagationInterface) {
  18. this.linksNumber = linksNumber;
  19. long fact = possiblePair(linksNumber);
  20. System.out.println("All possible combinations: " + fact);
  21. if (connectionNumber > fact) {
  22. System.out.println("Given number of connection is over maximum.");
  23. connectionNumber = fact;
  24. }
  25. this.connectionNumber = connectionNumber;
  26. this.chainReactionInterface = chainReactionInterface;
  27. this.damagePropagationInterface = damagePropagationInterface;
  28. }
  29.  
  30.  
  31. public void test() throws InterruptedException {
  32. final Vector<Integer> linksSet = new Vector<>();
  33. final Vector<Pair> connectionSet = new Vector<>();
  34. final Vector<Integer> firstDestroyed = new Vector<>();
  35. int creatingThreadsCount = random.nextInt(5) + 1;
  36. List<Thread> list = new ArrayList<Thread>();
  37. for (int i = 0; i < creatingThreadsCount; i++) {
  38. Thread thread = new Thread(new AddingThread(linksSet, connectionSet));
  39. list.add(thread);
  40. thread.start();
  41. }
  42. for (Thread t : list)
  43. t.join();
  44. System.out.println("Adding Ended");
  45. chainReactionInterface.setDamagePropagator(damagePropagationInterface);
  46.  
  47. int removingThread = random.nextInt(5) + 1;
  48. for (int i = 0; i < removingThread; i++) {
  49. new Thread(() -> {
  50. int destroyIndexCount = random.nextInt(linksSet.size() / 4);
  51. for (int j = 0; j < destroyIndexCount; j++) {
  52. int idToDestroy = linksSet.elementAt(random.nextInt(linksSet.size()));
  53. chainReactionInterface.destroyed(idToDestroy);
  54. firstDestroyed.add(idToDestroy);
  55. }
  56. }).start();
  57. }
  58.  
  59. while (!chainReactionInterface.resultReady()) {
  60. Thread.sleep(20);
  61. System.out.println("Checking if results are ready");
  62. }
  63.  
  64. System.out.println("Checking result");
  65. road = new String[linksSet.size()];
  66. firstDestroyed.forEach(integer -> road[integer] = integer + "(first)");
  67. for (Integer i : linksSet) {
  68. road[i] = checkLink(i, damagePropagationInterface, connectionSet, new ArrayList<>());
  69. if (road[i] != null && road[i].length() > 0) {
  70. System.out.println("ROAD FOR " + i);
  71. System.out.println(road[i]);
  72. }
  73. }
  74.  
  75. System.out.println("COMPARING TO CHAIN REACTION");
  76. boolean everythingOk = true;
  77. for (Integer i : linksSet) {
  78. if (road[i].equals("") == chainReactionInterface.isDestroyed(i)) {
  79. System.out.println("ERROR ID: " + i);
  80. everythingOk = false;
  81. }
  82. }
  83. if (everythingOk) {
  84. System.out.println("Zero errors! But it could be coincidence. Test it again");
  85. }
  86. }
  87.  
  88. private String checkLink(Integer link, DamagePropagation damagePropagationInterface, Vector<Pair> connectionSet, List<Integer> wasThere) {
  89. if (road[link] != null && road[link].length() > 0)
  90. return road[link];
  91. List<Pair> possibilities = connectionSet
  92. .stream()
  93. .filter(pair -> pair.dest == link || pair.source == link)
  94. .filter(pair -> !wasThere.contains(pair.dest) && !wasThere.contains(pair.source))
  95. .filter(pair -> damagePropagationInterface.checkInMap(pair.dest, pair.source))
  96. .collect(Collectors.toList());
  97. for (Pair pair : possibilities) {
  98. int neigh = (pair.source == link) ? pair.dest : pair.source;
  99. List<Integer> newWasThere = new ArrayList<>(wasThere);
  100. newWasThere.add(link);
  101. String r = checkLink(neigh, damagePropagationInterface, connectionSet, newWasThere);
  102. if (r.length() > 0) {
  103. r = link + "->" + r;
  104. road[link] = r;
  105. return r;
  106. }
  107. }
  108. return "";
  109. }
  110.  
  111.  
  112. private static long possiblePair(long n) {
  113. return n * (n - 1) / 2;
  114. }
  115.  
  116. private class AddingThread implements Runnable {
  117. private final Vector<Integer> linksSet;
  118. private final Vector<Pair> connectionSet;
  119.  
  120. AddingThread(Vector<Integer> linksSet, Vector<Pair> connectionSet) {
  121. this.linksSet = linksSet;
  122. this.connectionSet = connectionSet;
  123. }
  124.  
  125. @Override
  126. public void run() {
  127. while (linksSet.size() < linksNumber || connectionSet.size() < connectionNumber) {
  128. if (linksSet.size() < linksNumber && connectionSet.size() < connectionNumber) {
  129. switch (random.nextInt(2)) {
  130. case 0:
  131. addLink();
  132. break;
  133. case 1:
  134. addNeighbour();
  135. break;
  136. }
  137. } else if (linksSet.size() < linksNumber) {
  138. addLink();
  139. } else {
  140. addNeighbour();
  141. }
  142. }
  143. }
  144.  
  145. private void addLink() {
  146. synchronized (lockForLinks) {
  147. int size = linksSet.size();
  148. if (size < linksNumber) {
  149. chainReactionInterface.addObject(size);
  150. linksSet.add(size);
  151. }
  152. }
  153. }
  154.  
  155. private void addNeighbour() {
  156. if (possiblePair(linksSet.size()) <= connectionSet.size()) {
  157. return;
  158. }
  159. Pair newConnection = null;
  160. do {
  161. if (connectionSet.size() >= connectionNumber)
  162. return;
  163. newConnection = new Pair(linksSet.elementAt(random.nextInt(linksSet.size())), linksSet.elementAt(random.nextInt(linksSet.size())));
  164. } while (!addPairToVector(newConnection));
  165. chainReactionInterface.neighbour(newConnection.dest, newConnection.source);
  166. }
  167.  
  168. private boolean addPairToVector(Pair pair) {
  169. synchronized (lockForConnections) {
  170. if (pair.dest == pair.source || connectionSet.size() >= connectionNumber || connectionSet.contains(pair))
  171. return false;
  172. connectionSet.add(pair);
  173. }
  174. return true;
  175. }
  176. }
  177.  
  178. private static class Pair {
  179. int source;
  180. int dest;
  181.  
  182. public Pair(int source, int dest) {
  183. this.source = source;
  184. this.dest = dest;
  185. }
  186.  
  187. @Override
  188. public boolean equals(Object o) {
  189. if (this == o) return true;
  190. if (o == null || getClass() != o.getClass()) return false;
  191. Pair pair = (Pair) o;
  192. return (source == pair.source &&
  193. dest == pair.dest)
  194. || (source == pair.dest &&
  195. dest == pair.source);
  196. }
  197.  
  198. @Override
  199. public int hashCode() {
  200. return Objects.hash(source, dest) + Objects.hash(dest, source);
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement