Skip to content

Commit

Permalink
furhter validation for deviceConfig, proper Json Property settings
Browse files Browse the repository at this point in the history
  • Loading branch information
olepoeschl committed May 24, 2023
1 parent f58380b commit 0a8b52e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
26 changes: 13 additions & 13 deletions src/de/nqueensfaf/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.ArrayList;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.core.exc.StreamWriteException;
Expand All @@ -17,35 +18,33 @@ public class Config {
// all configurable fields and their default values

// CPU or GPU ?
@JsonProperty(value = "type", required = true)
private String type;
// for CPU
@JsonProperty(value = "cpuThreadcount")
private int cpuThreadcount;
// for GPU
@JsonProperty(value = "gpuDeviceConfigs")
private DeviceConfig[] gpuDeviceConfigs;
@JsonProperty(value = "gpuPresetQueens")
private int gpuPresetQueens;
// general
@JsonProperty(value = "progressUpdateDelay")
private long progressUpdateDelay;
@JsonProperty(value = "autoSaveEnabled")
private boolean autoSaveEnabled;
@JsonProperty(value = "autoDeleteEnabled")
private boolean autoDeleteEnabled;
@JsonProperty(value = "autoSavePercentageStep")
private int autoSavePercentageStep;
@JsonProperty(value = "autoSaveFilePath")
private String autoSaveFilePath;

public Config() {
super();
}

public Config(String type, int cpuThreadcount, DeviceConfig[] gpuDeviceConfigs, int gpuPresetQueens,
long progressUpdateDelay, boolean autoSaveEnabled, boolean autoDeleteEnabled, int autoSavePercentageStep,
String autoSaveFilePath) {
@JsonCreator
public Config(@JsonProperty(value = "type", required = true) String type,
@JsonProperty(value = "cpuThreadcount") int cpuThreadcount,
@JsonProperty(value = "gpuDeviceConfigs") DeviceConfig[] gpuDeviceConfigs,
@JsonProperty(value = "gpuPresetQueens") int gpuPresetQueens,
@JsonProperty(value = "progressUpdateDelay") long progressUpdateDelay,
@JsonProperty(value = "autoSaveEnabled") boolean autoSaveEnabled,
@JsonProperty(value = "autoDeleteEnabled") boolean autoDeleteEnabled,
@JsonProperty(value = "autoSavePercentageStep") int autoSavePercentageStep,
@JsonProperty(value = "autoSaveFilePath") String autoSaveFilePath) {
this.type = type;
this.cpuThreadcount = cpuThreadcount;
this.gpuDeviceConfigs = gpuDeviceConfigs;
Expand Down Expand Up @@ -92,13 +91,14 @@ public void validate() {
cpuThreadcount = getDefaultConfig().getCPUThreadcount();

if(gpuDeviceConfigs == null || gpuDeviceConfigs.length == 0)
gpuDeviceConfigs = getDefaultConfig().getGPUDeviceConfigs();
gpuDeviceConfigs = new DeviceConfig[] {DeviceConfig.getDefaultDeviceConfig()};
else {
// check for invalid values and remove each invalid value that is found
ArrayList<DeviceConfig> gpuDeviceConfigsTmp = new ArrayList<DeviceConfig>();
for(var deviceConfig : gpuDeviceConfigs) {
if(gpuDeviceConfigsTmp.stream().anyMatch(dvcCfg -> deviceConfig.getIndex() == dvcCfg.getIndex())) // check for duplicates
continue;
deviceConfig.fillEmptyFields();
if(deviceConfig.isValid())
gpuDeviceConfigsTmp.add(deviceConfig);
}
Expand Down
37 changes: 28 additions & 9 deletions src/de/nqueensfaf/config/DeviceConfig.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
package de.nqueensfaf.config;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class DeviceConfig {

@JsonProperty(value = "index", required = true)
private int index;
@JsonProperty(value = "workgroupSize", required = true)
private int workgroupSize;
@JsonProperty(value = "presetQueens", required = true)
private int presetQueens;
@JsonProperty(value = "weight")
private int weight;
private int workgroupSize = 0;
private int presetQueens = 0;
private int weight = 0;

public DeviceConfig() {
super();
}

public DeviceConfig(int index, int workgroupSize, int presetQueens, int weight) {
@JsonCreator
public DeviceConfig(@JsonProperty(value = "index", required = true) int index,
@JsonProperty(value = "workgroupSize") int workgroupSize,
@JsonProperty(value = "presetQueens") int presetQueens,
@JsonProperty(value = "weight") int weight) {
super();
this.index = index;
this.workgroupSize = workgroupSize;
this.presetQueens = presetQueens;
this.weight = weight;
}

public static DeviceConfig getDefaultDeviceConfig() {
final DeviceConfig dc = new DeviceConfig();
dc.setIndex(0);
dc.setWorkgroupSize(64);
dc.setPresetQueens(6);
dc.setWeight(1);
return dc;
}

public void fillEmptyFields() {
if(workgroupSize == 0)
workgroupSize = getDefaultDeviceConfig().workgroupSize;
if(presetQueens == 0)
presetQueens = getDefaultDeviceConfig().presetQueens;
if(weight == 0)
weight = getDefaultDeviceConfig().weight;
}

public boolean isValid() {
return index >= 0 && workgroupSize > 0 && presetQueens >= 4;
}
Expand All @@ -33,7 +52,7 @@ public int getIndex() {
return index;
}

public void setIdx(int index) {
public void setIndex(int index) {
this.index = index;
}

Expand Down

0 comments on commit 0a8b52e

Please sign in to comment.