Accuracy.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package analyzer;
  2. import analyzer.models.Client;
  3. import analyzer.models.Hashtag;
  4. import analyzer.models.User;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.concurrent.ExecutionException;
  12. import java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14. import java.util.concurrent.Future;
  15. /**
  16. * This class evaluates results of the attacks on sets of subjects.
  17. */
  18. public class Accuracy {
  19. private static final int threadPoolSize = 8;
  20. private static final int acceptRangeClientSet = 5;
  21. private static final int acceptRangeHashtagSet = 5;
  22. private static int counter;
  23. private static int totalHashtags = 1000;
  24. private static int totalClients = 1000;
  25. private static final int clientThreshold = 5;
  26. private static volatile int correct = 0;
  27. private static volatile int averageRounds = 0;
  28. private static volatile int averagePosts = 0;
  29. public static final String boundnamesPath = "C:\\Users\\Admin\\Desktop\\Skripts\\Thesis\\Repo\\local\\vm-mount\\logs\\" + Analyzer.logType + Analyzer.clientNo + "\\boundnames.txt" ;
  30. public static final List<String> identifiedLinks = new ArrayList<>();
  31. private static Analyzer A;
  32. public static void main(String[] args) throws InterruptedException, ExecutionException {
  33. A = new Analyzer();
  34. //hashtagTraceAccuracyUserUnknown();
  35. //clientTraceAccuracyUserUnknown();
  36. clientTraceAccuracyUserKnown();
  37. }
  38. //Step: most popular hashtags -> for each hashtag calculate points -> for each client intersect sets -> for each client check result
  39. private static void hashtagTraceAccuracyUserUnknown() throws InterruptedException, ExecutionException {
  40. List<Hashtag> hashtagList = new ArrayList<>(ServerLogParser.hashtags.values());
  41. hashtagList.sort((h1, h2) -> Integer.compare(h2.getTotalPosts(), h1.getTotalPosts()));
  42. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  43. List<Future<?>> tasks = new ArrayList<>();
  44. totalHashtags = Math.min(totalHashtags, ServerLogParser.hashtags.values().size());
  45. for(int i = 0; i < totalHashtags; i++) {
  46. Future<?> task = executor.submit(new CheckHashtagTask(hashtagList.get(i)));
  47. tasks.add(task);
  48. }
  49. for(Future<?> task : tasks)
  50. task.get();
  51. executor.shutdown();
  52. printResult(false);
  53. }
  54. //Step: set intersection -> calculate point ->
  55. private static void clientTraceAccuracyUserUnknown() throws InterruptedException, ExecutionException {
  56. List<Client> clientList = createClientSet(totalClients);
  57. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  58. List<Future<?>> tasks = new ArrayList<>();
  59. totalClients = Math.min(totalClients, clientList.size());
  60. for(int i = 0; i < totalClients; i++) {
  61. Future<?> task = executor.submit(new CheckClientTask(clientList.get(i)));
  62. tasks.add(task);
  63. }
  64. for(Future<?> task : tasks){
  65. task.get();
  66. counter++;
  67. float progress = (float) Math.round(((float) counter / (float) totalHashtags) * 1000.0F) / 10.0F;
  68. System.out.print("\r");
  69. System.out.print(progress + " % done");
  70. }
  71. executor.shutdown();
  72. printResult(false);
  73. }
  74. private static void clientTraceAccuracyUserKnown() throws InterruptedException, ExecutionException {
  75. List<Client> clientList = createClientSet(totalClients);
  76. System.out.println("clientList size " + clientList.size());
  77. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  78. List<Future<?>> tasks = new ArrayList<>();
  79. totalClients = Math.min(totalClients, clientList.size());
  80. for(int i = 0; i < totalClients; i++) {
  81. Future<?> task = executor.submit(new CheckClientTaskUserKnown(clientList.get(i)));
  82. tasks.add(task);
  83. }
  84. for(Future<?> task : tasks){
  85. task.get();
  86. counter++;
  87. float progress = (float) Math.round(((float) counter / (float) totalHashtags) * 1000.0F) / 10.0F;
  88. System.out.print("\r");
  89. System.out.print(progress + " % done");
  90. }
  91. executor.shutdown();
  92. printResult(true);
  93. }
  94. private static boolean checkClientHashtagLink(Client client, Hashtag hashtag) throws IOException {
  95. FileReader fr = new FileReader(boundnamesPath);
  96. BufferedReader br= new BufferedReader(fr);
  97. String line;
  98. while((line = br.readLine()) != null) {
  99. String[] elements = line.trim().split("\t");//userID client-ID
  100. if(elements[1].equals(client.getId())) {
  101. if(ServerLogParser.users.get(Long.parseLong(elements[0])).getHashtags().containsKey(hashtag)) {
  102. br.close();
  103. identifiedLinks.add(client.getId() + "-" + hashtag.getName());
  104. return true;
  105. }
  106. break;
  107. }
  108. }
  109. br.close();
  110. return false;
  111. }
  112. private static boolean checkClientUserLink(Client client, User user) throws IOException {
  113. FileReader fr = new FileReader(boundnamesPath);
  114. BufferedReader br= new BufferedReader(fr);
  115. String line;
  116. while((line = br.readLine()) != null) {
  117. String[] elements = line.trim().split("\t");//userID client-ID
  118. if(Long.parseLong(elements[0]) == user.getId())
  119. return elements[1].equals(client.getId());
  120. }
  121. System.out.println("User not found");
  122. return false;
  123. }
  124. private static List<Client> createClientSet(int num) {
  125. List<Client> result = new ArrayList<>();
  126. for(Client c : ClientLogParser.clients.values()) {
  127. if(result.size() == num)
  128. break;
  129. else {
  130. if(c.getTotalPosts() >= Accuracy.clientThreshold)
  131. result.add(c);
  132. }
  133. }
  134. if(result.size() < num)
  135. totalClients = result.size();
  136. return result;
  137. }
  138. private static void printResult(boolean userKnown) {
  139. System.out.println();
  140. if(!userKnown) {
  141. //System.out.println("acceptRangeClientSet: " + acceptRangeClientSet);
  142. //System.out.println("acceptRangeHashtagSet: " + acceptRangeHashtagSet);
  143. System.out.println("clientThreshold: " + clientThreshold);
  144. double result = ((double) correct / (double) totalClients) * 100.00;
  145. System.out.println(result + " % of " + totalClients);
  146. System.out.println("average most ranked: " + (double) averagePosts/(double)totalClients);
  147. System.out.print(identifiedLinks.size() + " identified links: ");
  148. for(String s: identifiedLinks) {
  149. System.out.print(s + " ");
  150. }
  151. } else {
  152. double result = ((double) averageRounds / (double) totalClients);
  153. System.out.println(result + " rounds of " + totalClients);
  154. }
  155. System.out.println();
  156. }
  157. private static synchronized void incrementCorrect() {
  158. correct++;
  159. }
  160. private static synchronized void addToAverageRounds(int n) {
  161. averageRounds += n;
  162. }
  163. private static synchronized void addToAveragePosts(int n) {
  164. averagePosts += n;
  165. }
  166. private record CheckHashtagTask(Hashtag hashtag) implements Runnable {
  167. @Override
  168. public void run() {
  169. try {
  170. List<Map.Entry<Client, Integer>> lst = A.calculatePointsAllClients(hashtag, acceptRangeClientSet, false);
  171. for(int i = 0; i < acceptRangeClientSet; i++) {
  172. for(Map.Entry<String, Integer> e: A.intersectHashtags(lst.get(i).getKey(), acceptRangeHashtagSet, false))
  173. if(e.getKey().equals(hashtag.getName())) {
  174. if(checkClientHashtagLink(lst.get(i).getKey(), hashtag)) {
  175. incrementCorrect();
  176. break;
  177. }
  178. }
  179. }
  180. counter++;
  181. float progress = (float) Math.round(((float) counter / (float) totalHashtags) * 1000.0F) / 10.0F;
  182. System.out.print("\r");
  183. System.out.print(progress + " % done");
  184. } catch (InterruptedException | IOException | ExecutionException e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. }
  189. private record CheckClientTask(Client client) implements Runnable {
  190. @Override
  191. public void run() {
  192. try {
  193. List<Map.Entry<String, Integer>> intersection = A.intersectHashtags(client, 1, false);
  194. intersection.removeIf(e -> e.getValue() < 0);
  195. Hashtag[] hashtags = new Hashtag[intersection.size()];
  196. for(int i = 0; i < hashtags.length; i++)
  197. hashtags[i] = ServerLogParser.hashtags.get(intersection.get(i).getKey());
  198. List<Map.Entry<Hashtag, Integer>> points = A.calculatePointsGivenHashtags(client, hashtags, 1, false);
  199. addToAveragePosts(points.size());
  200. for(Map.Entry<Hashtag, Integer> e : points) {
  201. boolean identified = checkClientHashtagLink(client, e.getKey());
  202. if(identified){
  203. incrementCorrect();
  204. return;
  205. }
  206. }
  207. } catch (InterruptedException | ExecutionException | IOException e) {
  208. e.printStackTrace();
  209. }
  210. }
  211. }
  212. private record CheckClientTaskUserKnown(Client client) implements Runnable {
  213. @Override
  214. public void run() {
  215. int rounds = A.intersectUsers(client, 1, false);
  216. if (rounds == -1) {
  217. totalClients--;
  218. } else {
  219. addToAverageRounds(rounds);
  220. }
  221. }
  222. }
  223. }