Skip to content

Commit

Permalink
Resolved bugs (mostly in TSC) (#154)
Browse files Browse the repository at this point in the history
* Resolved bugs (mostly in TSC)

* Removed crazy copies of WEKA classifiers

* removed a vulnerability (just to trigger the CI)

* test whether CI is triggered

* trigger CI

* trigger CI

* resolved code smell

* Resolved several code smells and removed RandomRegressionTree

* resolved code smells
  • Loading branch information
fmohr committed Jun 3, 2019
1 parent ede6f83 commit e691e12
Show file tree
Hide file tree
Showing 91 changed files with 1,743 additions and 6,410 deletions.
Expand Up @@ -18,7 +18,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ai.libs.jaicore.basic.sets.SetUtil.Pair;
import ai.libs.jaicore.basic.sets.Pair;

/**
* This is a simple util class for easy database access and query execution in sql. You need to make sure that the respective JDBC connector is in the class path. By default, the adapter uses the mysql driver, but any jdbc driver can be
Expand Down
Expand Up @@ -53,10 +53,10 @@ public static char[] getCommonChars(final boolean includeDigits) {
* @param chars The set of characters to be used to generate a random string.
* @return The generated random string.
*/
public static String getRandomString(final int length, final char[] chars) {
public static String getRandomString(final int length, final char[] chars, final long seed) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < length; i++) {
s.append(chars[new Random().nextInt(chars.length)]);
s.append(chars[new Random(seed).nextInt(chars.length)]);
}
return s.toString();
}
Expand Down Expand Up @@ -210,9 +210,9 @@ public static String fromBinary(String binarySequence) {
binarySequence = binarySequence.replace(" ", "");
Arrays.stream( // Create a Stream
binarySequence.split("(?<=\\G.{8})") // Splits the input string into 8-char-sections (Since a char has 8 bits = 1 byte)
).forEach(s -> // Go through each 8-char-section...
sb.append((char) Integer.parseInt(s, 2)) // ...and turn it into an int and then to a char
);
).forEach(s -> // Go through each 8-char-section...
sb.append((char) Integer.parseInt(s, 2)) // ...and turn it into an int and then to a char
);

return sb.toString(); // Output text (t)
}
Expand Down
Expand Up @@ -292,6 +292,6 @@ public L getList() {
}

private Class<?> getClassWithoutGenerics(final String className) throws ClassNotFoundException {
return Class.forName(className.replaceAll("(<.*>)", ""));
return Class.forName(className.replaceAll("(<[^>*]>)", ""));
}
}
@@ -0,0 +1,64 @@
package ai.libs.jaicore.basic.sets;

public class Pair<X,Y> {
private X x;
private Y y;

public Pair(final X x, final Y y) {
super();
this.x = x;
this.y = y;
}

public X getX() {
return this.x;
}

public Y getY() {
return this.y;
}

@Override
public String toString() {
return "<" + this.x + ", " + this.y + ">";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.x == null) ? 0 : this.x.hashCode());
result = prime * result + ((this.y == null) ? 0 : this.y.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
@SuppressWarnings("unchecked")
Pair<X, Y> other = (Pair<X, Y>) obj;
if (this.x == null) {
if (other.x != null) {
return false;
}
} else if (!this.x.equals(other.x)) {
return false;
}
if (this.y == null) {
if (other.y != null) {
return false;
}
} else if (!this.y.equals(other.y)) {
return false;
}
return true;
}
}
Expand Up @@ -34,69 +34,6 @@ private SetUtil() {
// prevent instantiation of this util class
}

public static class Pair<X, Y> {
private X x;
private Y y;

public Pair(final X x, final Y y) {
super();
this.x = x;
this.y = y;
}

public X getX() {
return this.x;
}

public Y getY() {
return this.y;
}

@Override
public String toString() {
return "<" + this.x + ", " + this.y + ">";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.x == null) ? 0 : this.x.hashCode());
result = prime * result + ((this.y == null) ? 0 : this.y.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
@SuppressWarnings("unchecked")
Pair<X, Y> other = (Pair<X, Y>) obj;
if (this.x == null) {
if (other.x != null) {
return false;
}
} else if (!this.x.equals(other.x)) {
return false;
}
if (this.y == null) {
if (other.y != null) {
return false;
}
} else if (!this.y.equals(other.y)) {
return false;
}
return true;
}
}

/* BASIC SET OPERATIONS */
@SafeVarargs
public static <T> Collection<T> union(final Collection<T>... set) {
Expand Down Expand Up @@ -814,7 +751,7 @@ private static <K, V> Set<Map<K, V>> allFunctionsFromFunctionallyDenotedRelation
}

/* ORDER OPERATIONS (SHUFFLE, SORT, PERMUTATE) */
public static <T> void shuffle(final List<T> list) {
public static <T> void shuffle(final List<T> list, final long seed) {

/* preliminaries */
List<Integer> unusedItems = new ArrayList<>();
Expand All @@ -827,13 +764,13 @@ public static <T> void shuffle(final List<T> list) {

/* select randomly from unusedItems until unusedItems is empty */
while (!unusedItems.isEmpty()) {
int index = new Random().nextInt(unusedItems.size());
int index = new Random(seed).nextInt(unusedItems.size());
list.add(copy.get(unusedItems.get(index)));
}
}

public static <T> T getRandomElement(final Collection<T> set) {
int choice = new Random().nextInt(set.size());
public static <T> T getRandomElement(final Collection<T> set, final long seed) {
int choice = new Random(seed).nextInt(set.size());
if (set instanceof List) {
return ((List<T>) set).get(choice);
}
Expand Down
Expand Up @@ -10,8 +10,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import ai.libs.jaicore.basic.sets.Pair;
import ai.libs.jaicore.basic.sets.SetUtil;
import ai.libs.jaicore.basic.sets.SetUtil.Pair;

public class Graph<T> {

Expand Down
Expand Up @@ -5,8 +5,8 @@
import java.util.List;
import java.util.Map;

import ai.libs.jaicore.basic.sets.Pair;
import ai.libs.jaicore.basic.sets.SetUtil;
import ai.libs.jaicore.basic.sets.SetUtil.Pair;

public class LabeledGraph<T, L> extends Graph<T> {

Expand Down
Expand Up @@ -4,7 +4,7 @@
import java.util.Arrays;
import java.util.List;

import ai.libs.jaicore.basic.sets.SetUtil.Pair;
import ai.libs.jaicore.basic.sets.Pair;

public class LoggerUtil {

Expand Down
Expand Up @@ -6,52 +6,52 @@

import org.apache.commons.lang3.builder.HashCodeBuilder;

import ai.libs.jaicore.basic.sets.SetUtil.Pair;
import ai.libs.jaicore.basic.sets.Pair;
import weka.classifiers.Classifier;
import weka.core.Instances;

public class ClassifierCache extends HashMap<Integer, Pair<Classifier, Instances>> {

/**
*
*/
private static final long serialVersionUID = -8463580964568016772L;

private Lock cacheLock = new ReentrantLock();

public ClassifierCache() {

}

public Classifier getCachedClassifier(final String classifierName, final EMCNodeType classificationStrategy, final Instances data) {
int hashCode = new HashCodeBuilder().append(classifierName).append(classificationStrategy).append(data.toString()).toHashCode();
Pair<Classifier, Instances> pair = this.get(hashCode);
if (pair == null) {
return null;
} else {
return this.get(hashCode).getX();
}
}

public void cacheClassifier(final String classifierName, final EMCNodeType classificationStrategy, final Instances data, final Classifier classifier) {
// int hashCode = new
// HashCodeBuilder().append(classifierName).append(classificationStrategy).append(data.toString()).toHashCode();
// this.cacheLock.lock();
// try {
// this.put(hashCode, new Pair<>(classifier, data));
// } finally {
// this.cacheLock.unlock();
// }
}

public Instances getCachedTrainingData(final String classifierName, final EMCNodeType classificationStrategy, final Instances data) {
int hashCode = new HashCodeBuilder().append(classifierName).append(classificationStrategy).append(data.toString()).toHashCode();
Pair<Classifier, Instances> pair = this.get(hashCode);
if (pair == null) {
return null;
} else {
return this.get(hashCode).getY();
}
}
/**
*
*/
private static final long serialVersionUID = -8463580964568016772L;

private Lock cacheLock = new ReentrantLock();

public ClassifierCache() {

}

public Classifier getCachedClassifier(final String classifierName, final EMCNodeType classificationStrategy, final Instances data) {
int hashCode = new HashCodeBuilder().append(classifierName).append(classificationStrategy).append(data.toString()).toHashCode();
Pair<Classifier, Instances> pair = this.get(hashCode);
if (pair == null) {
return null;
} else {
return this.get(hashCode).getX();
}
}

public void cacheClassifier(final String classifierName, final EMCNodeType classificationStrategy, final Instances data, final Classifier classifier) {
// int hashCode = new
// HashCodeBuilder().append(classifierName).append(classificationStrategy).append(data.toString()).toHashCode();
// this.cacheLock.lock();
// try {
// this.put(hashCode, new Pair<>(classifier, data));
// } finally {
// this.cacheLock.unlock();
// }
}

public Instances getCachedTrainingData(final String classifierName, final EMCNodeType classificationStrategy, final Instances data) {
int hashCode = new HashCodeBuilder().append(classifierName).append(classificationStrategy).append(data.toString()).toHashCode();
Pair<Classifier, Instances> pair = this.get(hashCode);
if (pair == null) {
return null;
} else {
return this.get(hashCode).getY();
}
}

}
Expand Up @@ -5,7 +5,7 @@
import java.util.List;
import java.util.Random;

import ai.libs.jaicore.basic.sets.SetUtil.Pair;
import ai.libs.jaicore.basic.sets.Pair;
import ai.libs.jaicore.ml.core.dataset.IDataset;
import ai.libs.jaicore.ml.core.dataset.ILabeledInstance;
import ai.libs.jaicore.ml.core.dataset.sampling.inmemory.ASamplingAlgorithm;
Expand All @@ -15,28 +15,28 @@ public abstract class CaseControlLikeSampling<I extends ILabeledInstance<?>, D e
protected Random rand;
protected List<Pair<I, Double>> probabilityBoundaries = null;

protected CaseControlLikeSampling(D input) {
protected CaseControlLikeSampling(final D input) {
super(input);
}

public List<Pair<I, Double>> getProbabilityBoundaries() {
return probabilityBoundaries;
return this.probabilityBoundaries;
}

public void setProbabilityBoundaries(List<Pair<I, Double>> probabilityBoundaries) {
public void setProbabilityBoundaries(final List<Pair<I, Double>> probabilityBoundaries) {
this.probabilityBoundaries = probabilityBoundaries;
}

/**
* Count occurrences of every class. Needed to determine the probability for all
* instances of that class
*
*
* @param dataset
* Dataset of the sample algorithm object
* @return HashMap of occurrences
* @throws ClassNotFoundException
*/
protected HashMap<Object, Integer> countClassOccurrences(D dataset) {
protected HashMap<Object, Integer> countClassOccurrences(final D dataset) {
HashMap<Object, Integer> classOccurrences = new HashMap<>();
for (I instance : dataset) {
boolean classExists = false;
Expand All @@ -55,8 +55,8 @@ protected HashMap<Object, Integer> countClassOccurrences(D dataset) {
return classOccurrences;
}

protected List<Pair<I, Double>> calculateInstanceBoundaries(HashMap<Object, Integer> classOccurrences,
int numberOfClasses) {
protected List<Pair<I, Double>> calculateInstanceBoundaries(final HashMap<Object, Integer> classOccurrences,
final int numberOfClasses) {
double boundaryOfCurrentInstance = 0.0;
List<Pair<I, Double>> boundaries = new ArrayList<>();
for (I instance : this.getInput()) {
Expand Down

0 comments on commit e691e12

Please sign in to comment.