From 59c255e615e22a9d2e42400374b34470e0a2e7ce Mon Sep 17 00:00:00 2001 From: christian Date: Wed, 1 Dec 2021 18:59:02 +0100 Subject: [PATCH] did slight changes in terms of synchronization to hopefully prevent the atypical crashes of undefined behaviour due to string list being used while being added to --- .../src/main/java/FunctionLayer/Datahandler.kt | 14 ++++++++------ .../src/main/java/FunctionLayer/DoStuff.java | 7 +++---- .../java/PresentationLayer/DiscordHandler.java | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.kt b/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.kt index 4adce474..33713070 100644 --- a/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.kt +++ b/ArtificialAutism/src/main/java/FunctionLayer/Datahandler.kt @@ -158,6 +158,7 @@ public class Datahandler { return StanfordCoreNLP(propsSentiment) } + @Synchronized fun updateStringCache() { if (stopwatch.elapsed(TimeUnit.MINUTES) >= EXPIRE_TIME_IN_MINUTES || !stopwatch.isRunning) { if (!stopwatch.isRunning) { @@ -170,15 +171,16 @@ public class Datahandler { val arrayList = java.util.ArrayList(stringCache) DataMapper.InsertMYSQLStrings(arrayList) DataMapper.checkStringsToDelete(); - stringCache = ArrayList(); - initiateMYSQL(); + for (str: String in DataMapper.getAllStrings()) { + if (str in stringCache) { + continue + } + stringCache.add(str) + } + System.out.println("post updateStringCache"); } } - fun initiateMYSQL() { - stringCache.addAll(DataMapper.getAllStrings()) - } - private fun trimString(str: String): String { var message = str.trim { it <= ' ' } if (message.startsWith("<@")) { diff --git a/ArtificialAutism/src/main/java/FunctionLayer/DoStuff.java b/ArtificialAutism/src/main/java/FunctionLayer/DoStuff.java index eb4a506b..25f7c85d 100644 --- a/ArtificialAutism/src/main/java/FunctionLayer/DoStuff.java +++ b/ArtificialAutism/src/main/java/FunctionLayer/DoStuff.java @@ -26,15 +26,15 @@ import reactor.core.publisher.Mono; */ public class DoStuff { - - public static void doStuff(MessageCreateEvent event, String usernameBot, Datahandler datahandler, - StanfordCoreNLP stanfordCoreNLP, StanfordCoreNLP stanfordCoreNLPSentiment) { + public synchronized static void doStuff(MessageCreateEvent event, String usernameBot, Datahandler datahandler, + StanfordCoreNLP stanfordCoreNLP, StanfordCoreNLP stanfordCoreNLPSentiment) { String username = ""; try { username = event.getMessage().getAuthor().get().getUsername(); } catch (java.util.NoSuchElementException e) { username = null; } + datahandler.updateStringCache(); if (username != null && !username.equals(usernameBot)) { TextChannel block = event.getMessage().getChannel().cast(TextChannel.class).block(); String name = block.getCategory().block().getName(); @@ -81,7 +81,6 @@ public class DoStuff { } } } - datahandler.updateStringCache(); } } } diff --git a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java index b3d1bb16..8998a4c4 100644 --- a/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java +++ b/ArtificialAutism/src/main/java/PresentationLayer/DiscordHandler.java @@ -80,7 +80,7 @@ public class DiscordHandler { //variables until the tests pass public static void main(String[] args) throws IOException, SQLException { Datahandler datahandler = new Datahandler(); - datahandler.initiateMYSQL(); + datahandler.updateStringCache(); PipelineJMWESingleton.getINSTANCE(); StanfordCoreNLP stanfordCoreNLP = datahandler.pipeLineSetUp();