From d11add08b46d12f93f0f92c98ce7c38489a87d45 Mon Sep 17 00:00:00 2001 From: Richard Zowalla Date: Mon, 20 Nov 2023 12:18:00 +0100 Subject: [PATCH] Language migrations aids, do not leak logging library --- pom.xml | 1 - .../ClassificationCoreException.java | 4 -- zlibsvm-core/pom.xml | 2 +- .../hhn/mi/domain/NativeSvmModelWrapper.java | 4 +- .../org/apache/commons/lang3/ArrayUtils.java | 4 +- .../java/de/hhn/mi/SvmClassifierTestCase.java | 9 +-- .../de/hhn/mi/SvmConfigurationTestCase.java | 26 +++----- .../java/de/hhn/mi/SvmTrainingTestCase.java | 9 +-- .../tw/edu/ntu/csie/libsvm/svm_train.java | 61 ++++++++++--------- 9 files changed, 51 insertions(+), 69 deletions(-) diff --git a/pom.xml b/pom.xml index 966cab2..98411e7 100644 --- a/pom.xml +++ b/pom.xml @@ -110,7 +110,6 @@ org.apache.logging.log4j log4j-slf4j-impl ${log4j.version} - runtime diff --git a/zlibsvm-api/src/main/java/de/hhn/mi/exception/ClassificationCoreException.java b/zlibsvm-api/src/main/java/de/hhn/mi/exception/ClassificationCoreException.java index 4ec460d..c74fe05 100644 --- a/zlibsvm-api/src/main/java/de/hhn/mi/exception/ClassificationCoreException.java +++ b/zlibsvm-api/src/main/java/de/hhn/mi/exception/ClassificationCoreException.java @@ -31,8 +31,4 @@ public ClassificationCoreException(String message) { super(message); } - public ClassificationCoreException(String message, Throwable throwable) { - super(message, throwable); - } - } diff --git a/zlibsvm-core/pom.xml b/zlibsvm-core/pom.xml index 39b824f..97049a4 100644 --- a/zlibsvm-core/pom.xml +++ b/zlibsvm-core/pom.xml @@ -29,7 +29,7 @@ org.apache.logging.log4j log4j-slf4j-impl - runtime + test junit diff --git a/zlibsvm-core/src/main/java/de/hhn/mi/domain/NativeSvmModelWrapper.java b/zlibsvm-core/src/main/java/de/hhn/mi/domain/NativeSvmModelWrapper.java index 2e93e31..becfa94 100644 --- a/zlibsvm-core/src/main/java/de/hhn/mi/domain/NativeSvmModelWrapper.java +++ b/zlibsvm-core/src/main/java/de/hhn/mi/domain/NativeSvmModelWrapper.java @@ -51,8 +51,8 @@ public NativeSvmModelWrapper(SvmConfigurationBuilder svmConfigurationBuilder, in this(new svm_model()); - svmModel.l = (amountOfSupportVectors); - svmModel.nr_class = (numberOfClasses); + svmModel.l = amountOfSupportVectors; + svmModel.nr_class = numberOfClasses; svmModel.rho = (ArrayUtils.toPrimitive(rhoConstants.toArray(new Double[0]))); svmModel.probA = (ArrayUtils.toPrimitive(probabilityA.toArray(new Double[0]))); diff --git a/zlibsvm-core/src/main/java/de/hhn/mi/shade/org/apache/commons/lang3/ArrayUtils.java b/zlibsvm-core/src/main/java/de/hhn/mi/shade/org/apache/commons/lang3/ArrayUtils.java index 77dc51e..4f20578 100644 --- a/zlibsvm-core/src/main/java/de/hhn/mi/shade/org/apache/commons/lang3/ArrayUtils.java +++ b/zlibsvm-core/src/main/java/de/hhn/mi/shade/org/apache/commons/lang3/ArrayUtils.java @@ -51,7 +51,7 @@ public static double[] toPrimitive(final Double[] array) { } final double[] result = new double[array.length]; for (int i = 0; i < array.length; i++) { - result[i] = array[i].doubleValue(); + result[i] = array[i]; } return result; } @@ -75,7 +75,7 @@ public static int[] toPrimitive(final Integer[] array) { } final int[] result = new int[array.length]; for (int i = 0; i < array.length; i++) { - result[i] = array[i].intValue(); + result[i] = array[i]; } return result; } diff --git a/zlibsvm-core/src/test/java/de/hhn/mi/SvmClassifierTestCase.java b/zlibsvm-core/src/test/java/de/hhn/mi/SvmClassifierTestCase.java index 5622b27..de06431 100644 --- a/zlibsvm-core/src/test/java/de/hhn/mi/SvmClassifierTestCase.java +++ b/zlibsvm-core/src/test/java/de/hhn/mi/SvmClassifierTestCase.java @@ -76,10 +76,9 @@ public void testInvalidParameters() { try { new SvmClassifierImpl(null); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -92,10 +91,9 @@ public void testInvalidParameters() { try { new SvmClassifierImpl(ownModel).classify(null, false); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -108,10 +106,9 @@ public void testInvalidParameters() { try { new SvmClassifierImpl(ownModel).classify(new ArrayList<>(), false); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); diff --git a/zlibsvm-core/src/test/java/de/hhn/mi/SvmConfigurationTestCase.java b/zlibsvm-core/src/test/java/de/hhn/mi/SvmConfigurationTestCase.java index ed87834..c4388de 100644 --- a/zlibsvm-core/src/test/java/de/hhn/mi/SvmConfigurationTestCase.java +++ b/zlibsvm-core/src/test/java/de/hhn/mi/SvmConfigurationTestCase.java @@ -38,10 +38,9 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setDegree(-1); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -54,10 +53,9 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setGamma(-1.0d); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -70,10 +68,9 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setNu(-1.0d); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -87,10 +84,9 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setNu(1.1d); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -103,7 +99,7 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setCacheSize(-1); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { e.printStackTrace(); @@ -119,10 +115,9 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setCost(0.0d); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -135,10 +130,9 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setEpsilon(0.0d); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -151,10 +145,9 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setP(-1.0d); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -167,10 +160,9 @@ public void testBuildConfigurationWithInvalidParameters() { try { builder.setCrossValidation(true, 1); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); diff --git a/zlibsvm-core/src/test/java/de/hhn/mi/SvmTrainingTestCase.java b/zlibsvm-core/src/test/java/de/hhn/mi/SvmTrainingTestCase.java index 5576f77..9844681 100644 --- a/zlibsvm-core/src/test/java/de/hhn/mi/SvmTrainingTestCase.java +++ b/zlibsvm-core/src/test/java/de/hhn/mi/SvmTrainingTestCase.java @@ -64,10 +64,9 @@ public void testInvalidParameters() { try { new SvmTrainerImpl(null, null); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -80,10 +79,9 @@ public void testInvalidParameters() { try { new SvmTrainerImpl(config, MODEL_NAME).train(null); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); @@ -96,10 +94,9 @@ public void testInvalidParameters() { try { new SvmTrainerImpl(config, MODEL_NAME).train(new ArrayList<>()); failed = true; - } catch (AssertionError ae) { + } catch (AssertionError ignored) { } catch (Exception e) { - e.printStackTrace(); fail("caught Exception of type " + e.getClass().getName() + " instead of an AssertionError. See stacktrace for further information."); diff --git a/zlibsvm-core/src/test/java/tw/edu/ntu/csie/libsvm/svm_train.java b/zlibsvm-core/src/test/java/tw/edu/ntu/csie/libsvm/svm_train.java index 98fb0c5..8aba474 100644 --- a/zlibsvm-core/src/test/java/tw/edu/ntu/csie/libsvm/svm_train.java +++ b/zlibsvm-core/src/test/java/tw/edu/ntu/csie/libsvm/svm_train.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -73,34 +73,35 @@ public class svm_train { private static void exit_with_help() { System.out.print( - "Usage: svm_train [options] training_set_file [model_file]\n" - + "options:\n" - + "-s svm_type : set type of SVM (default 0)\n" - + " 0 -- C-SVC (multi-class classification)\n" - + " 1 -- nu-SVC (multi-class classification)\n" - + " 2 -- one-class SVM\n" - + " 3 -- epsilon-SVR (regression)\n" - + " 4 -- nu-SVR (regression)\n" - + "-t kernel_type : set type of kernel function (default 2)\n" - + " 0 -- linear: u'*v\n" - + " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" - + " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" - + " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" - + " 4 -- precomputed kernel (kernel values in training_set_file)\n" - + "-d degree : set degree in kernel function (default 3)\n" - + "-g gamma : set gamma in kernel function (default 1/num_features)\n" - + "-r coef0 : set coef0 in kernel function (default 0)\n" - + "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" - + "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" - + "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" - + "-m cachesize : set cache memory size in MB (default 100)\n" - + "-e epsilon : set tolerance of termination criterion (default 0.001)\n" - + "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" - + "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, " + - "0 or 1 (default 0)\n" - + "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" - + "-v n : n-fold cross validation mode\n" - + "-q : quiet mode (no outputs)\n" + """ + Usage: svm_train [options] training_set_file [model_file] + options: + -s svm_type : set type of SVM (default 0) + 0 -- C-SVC (multi-class classification) + 1 -- nu-SVC (multi-class classification) + 2 -- one-class SVM + 3 -- epsilon-SVR (regression) + 4 -- nu-SVR (regression) + -t kernel_type : set type of kernel function (default 2) + 0 -- linear: u'*v + 1 -- polynomial: (gamma*u'*v + coef0)^degree + 2 -- radial basis function: exp(-gamma*|u-v|^2) + 3 -- sigmoid: tanh(gamma*u'*v + coef0) + 4 -- precomputed kernel (kernel values in training_set_file) + -d degree : set degree in kernel function (default 3) + -g gamma : set gamma in kernel function (default 1/num_features) + -r coef0 : set coef0 in kernel function (default 0) + -c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1) + -n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5) + -p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1) + -m cachesize : set cache memory size in MB (default 100) + -e epsilon : set tolerance of termination criterion (default 0.001) + -h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1) + -b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0) + -wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1) + -v n : n-fold cross validation mode + -q : quiet mode (no outputs) + """ ); System.exit(1); }