Skip to content

Commit

Permalink
Randomize parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kernicPanel committed Jul 26, 2022
1 parent ea2bed6 commit de235aa
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.kernicpanel</groupId>
<artifactId>randomizer</artifactId>
<name>Randomizer</name>
<version>1.1</version>
<version>2.0</version>
<build>
<plugins>
<plugin>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<artifactId>randomizer</artifactId>
<packaging>jar</packaging>
<name>Randomizer</name>
<version>1.1</version>
<version>2.0</version>

<repositories>
<repository>
Expand Down
82 changes: 74 additions & 8 deletions src/main/java/com/kernicpanel/RandomizerExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -67,16 +87,28 @@ 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());
documentState
.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() {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public String getAuthor() {

@Override
public String getVersion() {
return "1.1";
return "2.0";
}

@Override
Expand Down

0 comments on commit de235aa

Please sign in to comment.