From de235aaf40b531315a080dedad0cafdefe185992 Mon Sep 17 00:00:00 2001 From: Nicolas Clerc Date: Fri, 22 Jul 2022 15:03:13 +0200 Subject: [PATCH] Randomize parameters --- dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- .../com/kernicpanel/RandomizerExtension.java | 82 +++++++++++++++++-- .../RandomizerExtensionDefinition.java | 2 +- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 7332aa2..8126bd8 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -4,7 +4,7 @@ com.kernicpanel randomizer Randomizer - 1.1 + 2.0 diff --git a/pom.xml b/pom.xml index 1b03e21..037d59e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ randomizer jar Randomizer - 1.1 + 2.0 diff --git a/src/main/java/com/kernicpanel/RandomizerExtension.java b/src/main/java/com/kernicpanel/RandomizerExtension.java index 8a46dbd..0210cbe 100644 --- a/src/main/java/com/kernicpanel/RandomizerExtension.java +++ b/src/main/java/com/kernicpanel/RandomizerExtension.java @@ -11,6 +11,7 @@ import java.util.Arrays; import java.util.Locale; import java.util.Random; +import java.util.stream.IntStream; public class RandomizerExtension extends ControllerExtension { Random rand = new Random(); @@ -21,13 +22,19 @@ public class RandomizerExtension extends ControllerExtension { private static final String DEFAULT_DATE_FORMAT_TEMPLATE = "yyyy-MM-dd_"; private ControllerHost host; + private Application application; private DocumentState documentState; private PopupBrowser popupBrowser; private CursorTrack cursorTrack; + private CursorDevice cursorDevice; + private CursorRemoteControlsPage remoteControlsPage; private BrowserResultsItemBank resultsItemBank; + private Preferences preferences; private SettableStringValue filenameOutput; private SettableStringValue nameOutput; + private RemoteControl parameter; + private SettableRangedValue randomRatio; protected RandomizerExtension( final RandomizerExtensionDefinition definition, final ControllerHost host) { @@ -42,21 +49,34 @@ private void printer(String s) { @Override public void init() { host = getHost(); + printer("RandomizerExtension initialized"); + application = host.createApplication(); documentState = host.getDocumentState(); popupBrowser = host.createPopupBrowser(); + preferences = host.getPreferences(); popupBrowser.exists().markInterested(); popupBrowser.resultsColumn().entryCount().markInterested(); cursorTrack = host.createCursorTrack(0, 0); + preferences = host.getPreferences(); + cursorDevice = cursorTrack.createCursorDevice(); + remoteControlsPage = cursorDevice.createCursorRemoteControlsPage(80); + + remoteControlsPage.pageCount().markInterested(); + remoteControlsPage.pageNames().markInterested(); + remoteControlsPage.selectedPageIndex().markInterested(); + + for (int parameterIndex = 0; parameterIndex < 8; parameterIndex++) { + remoteControlsPage.getParameter(parameterIndex).name().markInterested(); + } resultsItemBank = popupBrowser.resultsColumn().createItemBank(100000); - useDate = - host.getPreferences().getBooleanSetting("Prepend date for filename", "Random name", true); + // Naming settings + useDate = preferences.getBooleanSetting("Prefix filename with date", "Random name", true); dateFormatTemplate = - host.getPreferences() - .getStringSetting( - "Format string for date prefix", "Random name", 15, DEFAULT_DATE_FORMAT_TEMPLATE); + preferences.getStringSetting( + "Format string for date prefix", "Random name", 15, DEFAULT_DATE_FORMAT_TEMPLATE); dateFormatTemplate.addValueObserver( value -> { try { @@ -67,6 +87,12 @@ public void init() { } }); + // Naming controls + filenameOutput = documentState.getStringSetting("Filename", "Random name", 50, ""); + nameOutput = documentState.getStringSetting("Name", "Random name", 50, ""); + documentState.getSignalSetting(" ", "Random name", "Generate").addSignalObserver(randomName()); + + // Browser selection controls documentState .getSignalSetting("Select", "Randomize browser selection", "Select random item") .addSignalObserver(selectRandomItem()); @@ -74,9 +100,15 @@ public void init() { .getSignalSetting("Add", "Randomize browser selection", "Add current item") .addSignalObserver(popupBrowser::commit); - filenameOutput = documentState.getStringSetting("Filename", "Random name", 50, ""); - nameOutput = documentState.getStringSetting("Name", "Random name", 50, ""); - documentState.getSignalSetting(" ", "Random name", "Generate").addSignalObserver(randomName()); + // Parameters controls + randomRatio = + documentState.getNumberSetting("Ratio", "Randomize Device parameters", 0, 100, 1, "%", 15); + documentState + .getSignalSetting(" ", "Randomize Device parameters", "Randomize current page") + .addSignalObserver(randomizeCurrentPageDeviceParameters()); + documentState + .getSignalSetting(" ", "Randomize Device parameters", "Randomize all parameters") + .addSignalObserver(randomizeDeviceParameters()); } private NoArgsCallback selectRandomItem() { @@ -128,6 +160,40 @@ private NoArgsCallback randomName() { }; } + private NoArgsCallback randomizeCurrentPageDeviceParameters() { + return () -> { + IntStream.range(0, 8) + .forEach( + parameterIndex -> { + double randomValue = rand.nextDouble(); + parameter = remoteControlsPage.getParameter(parameterIndex); + String parameterName = parameter.name().get(); + + if (!parameterName.trim().isEmpty() + && !parameterName.toLowerCase(Locale.ROOT).equals("output")) { + double newValue = ((2 * randomValue) - 1) * randomRatio.get(); + parameter.inc(newValue); + } + }); + }; + } + + private NoArgsCallback randomizeDeviceParameters() { + return () -> { + IntStream.range(0, remoteControlsPage.pageCount().get()) + .forEachOrdered( + (pageIndex) -> { + randomizeCurrentPageDeviceParameters().call(); + try { + remoteControlsPage.selectNextPage(true); + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + }; + } + @Override public void exit() { // TODO: Perform any cleanup once the driver exits diff --git a/src/main/java/com/kernicpanel/RandomizerExtensionDefinition.java b/src/main/java/com/kernicpanel/RandomizerExtensionDefinition.java index 2d8f123..c68d2f3 100644 --- a/src/main/java/com/kernicpanel/RandomizerExtensionDefinition.java +++ b/src/main/java/com/kernicpanel/RandomizerExtensionDefinition.java @@ -24,7 +24,7 @@ public String getAuthor() { @Override public String getVersion() { - return "1.1"; + return "2.0"; } @Override