From 0a8b52e9626565891b4e2e1df13f7442237faa62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20P=C3=B6schl?= <62295828+olepoeschl@users.noreply.github.com> Date: Wed, 24 May 2023 22:44:04 +0200 Subject: [PATCH] furhter validation for deviceConfig, proper Json Property settings --- src/de/nqueensfaf/config/Config.java | 26 +++++++-------- src/de/nqueensfaf/config/DeviceConfig.java | 37 ++++++++++++++++------ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/de/nqueensfaf/config/Config.java b/src/de/nqueensfaf/config/Config.java index 7cc1d272..cab0998f 100644 --- a/src/de/nqueensfaf/config/Config.java +++ b/src/de/nqueensfaf/config/Config.java @@ -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; @@ -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; @@ -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 gpuDeviceConfigsTmp = new ArrayList(); 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); } diff --git a/src/de/nqueensfaf/config/DeviceConfig.java b/src/de/nqueensfaf/config/DeviceConfig.java index 55e04939..e0dbff58 100644 --- a/src/de/nqueensfaf/config/DeviceConfig.java +++ b/src/de/nqueensfaf/config/DeviceConfig.java @@ -1,23 +1,24 @@ 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; @@ -25,6 +26,24 @@ public DeviceConfig(int index, int workgroupSize, int presetQueens, int weight) 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; } @@ -33,7 +52,7 @@ public int getIndex() { return index; } - public void setIdx(int index) { + public void setIndex(int index) { this.index = index; }