Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.github.jasiolek173;
- import java.util.*;
- import java.util.stream.Collectors;
- public class ChainReactionTest {
- private final int linksNumber;
- private final long connectionNumber;
- private final ChainReactionInterface chainReactionInterface;
- private final DamagePropagation damagePropagationInterface;
- private final Random random = new Random();
- private final Object lockForLinks = new Object();
- private final Object lockForConnections = new Object();
- private String[] road;
- public ChainReactionTest(int linksNumber, long connectionNumber, ChainReactionInterface chainReactionInterface, DamagePropagation damagePropagationInterface) {
- this.linksNumber = linksNumber;
- long fact = possiblePair(linksNumber);
- System.out.println("All possible combinations: " + fact);
- if (connectionNumber > fact) {
- System.out.println("Given number of connection is over maximum.");
- connectionNumber = fact;
- }
- this.connectionNumber = connectionNumber;
- this.chainReactionInterface = chainReactionInterface;
- this.damagePropagationInterface = damagePropagationInterface;
- }
- public void test() throws InterruptedException {
- final Vector<Integer> linksSet = new Vector<>();
- final Vector<Pair> connectionSet = new Vector<>();
- final Vector<Integer> firstDestroyed = new Vector<>();
- int creatingThreadsCount = random.nextInt(5) + 1;
- List<Thread> list = new ArrayList<Thread>();
- for (int i = 0; i < creatingThreadsCount; i++) {
- Thread thread = new Thread(new AddingThread(linksSet, connectionSet));
- list.add(thread);
- thread.start();
- }
- for (Thread t : list)
- t.join();
- System.out.println("Adding Ended");
- chainReactionInterface.setDamagePropagator(damagePropagationInterface);
- int removingThread = random.nextInt(5) + 1;
- for (int i = 0; i < removingThread; i++) {
- new Thread(() -> {
- int destroyIndexCount = random.nextInt(linksSet.size() / 4);
- for (int j = 0; j < destroyIndexCount; j++) {
- int idToDestroy = linksSet.elementAt(random.nextInt(linksSet.size()));
- chainReactionInterface.destroyed(idToDestroy);
- firstDestroyed.add(idToDestroy);
- }
- }).start();
- }
- while (!chainReactionInterface.resultReady()) {
- Thread.sleep(20);
- System.out.println("Checking if results are ready");
- }
- System.out.println("Checking result");
- road = new String[linksSet.size()];
- firstDestroyed.forEach(integer -> road[integer] = integer + "(first)");
- for (Integer i : linksSet) {
- road[i] = checkLink(i, damagePropagationInterface, connectionSet, new ArrayList<>());
- if (road[i] != null && road[i].length() > 0) {
- System.out.println("ROAD FOR " + i);
- System.out.println(road[i]);
- }
- }
- System.out.println("COMPARING TO CHAIN REACTION");
- boolean everythingOk = true;
- for (Integer i : linksSet) {
- if (road[i].equals("") == chainReactionInterface.isDestroyed(i)) {
- System.out.println("ERROR ID: " + i);
- everythingOk = false;
- }
- }
- if (everythingOk) {
- System.out.println("Zero errors! But it could be coincidence. Test it again");
- }
- }
- private String checkLink(Integer link, DamagePropagation damagePropagationInterface, Vector<Pair> connectionSet, List<Integer> wasThere) {
- if (road[link] != null && road[link].length() > 0)
- return road[link];
- List<Pair> possibilities = connectionSet
- .stream()
- .filter(pair -> pair.dest == link || pair.source == link)
- .filter(pair -> !wasThere.contains(pair.dest) && !wasThere.contains(pair.source))
- .filter(pair -> damagePropagationInterface.checkInMap(pair.dest, pair.source))
- .collect(Collectors.toList());
- for (Pair pair : possibilities) {
- int neigh = (pair.source == link) ? pair.dest : pair.source;
- List<Integer> newWasThere = new ArrayList<>(wasThere);
- newWasThere.add(link);
- String r = checkLink(neigh, damagePropagationInterface, connectionSet, newWasThere);
- if (r.length() > 0) {
- r = link + "->" + r;
- road[link] = r;
- return r;
- }
- }
- return "";
- }
- private static long possiblePair(long n) {
- return n * (n - 1) / 2;
- }
- private class AddingThread implements Runnable {
- private final Vector<Integer> linksSet;
- private final Vector<Pair> connectionSet;
- AddingThread(Vector<Integer> linksSet, Vector<Pair> connectionSet) {
- this.linksSet = linksSet;
- this.connectionSet = connectionSet;
- }
- @Override
- public void run() {
- while (linksSet.size() < linksNumber || connectionSet.size() < connectionNumber) {
- if (linksSet.size() < linksNumber && connectionSet.size() < connectionNumber) {
- switch (random.nextInt(2)) {
- case 0:
- addLink();
- break;
- case 1:
- addNeighbour();
- break;
- }
- } else if (linksSet.size() < linksNumber) {
- addLink();
- } else {
- addNeighbour();
- }
- }
- }
- private void addLink() {
- synchronized (lockForLinks) {
- int size = linksSet.size();
- if (size < linksNumber) {
- chainReactionInterface.addObject(size);
- linksSet.add(size);
- }
- }
- }
- private void addNeighbour() {
- if (possiblePair(linksSet.size()) <= connectionSet.size()) {
- return;
- }
- Pair newConnection = null;
- do {
- if (connectionSet.size() >= connectionNumber)
- return;
- newConnection = new Pair(linksSet.elementAt(random.nextInt(linksSet.size())), linksSet.elementAt(random.nextInt(linksSet.size())));
- } while (!addPairToVector(newConnection));
- chainReactionInterface.neighbour(newConnection.dest, newConnection.source);
- }
- private boolean addPairToVector(Pair pair) {
- synchronized (lockForConnections) {
- if (pair.dest == pair.source || connectionSet.size() >= connectionNumber || connectionSet.contains(pair))
- return false;
- connectionSet.add(pair);
- }
- return true;
- }
- }
- private static class Pair {
- int source;
- int dest;
- public Pair(int source, int dest) {
- this.source = source;
- this.dest = dest;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Pair pair = (Pair) o;
- return (source == pair.source &&
- dest == pair.dest)
- || (source == pair.dest &&
- dest == pair.source);
- }
- @Override
- public int hashCode() {
- return Objects.hash(source, dest) + Objects.hash(dest, source);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement