Skip to content

Commit 568a736

Browse files
ChristophBodensteinChristophBodenstein
authored andcommitted
added faster search in cache (only hash-comparision) and some status label outputs.
1 parent 06e01a6 commit 568a736

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed

TimeNETOptimizationEnvironment/src/toe/datamodel/SimulationType.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class SimulationType {
2727
private String xmlFileName = "";
2828
private boolean isFromCache = false;//is true, if from cache and false if logfile is parsed
2929
private boolean isFromDistributedSimulation = false;//Is False, if local simulated, true if simulated via Web
30+
private String hashString="";//Hash String to identify this Simulation based on the parameterset
3031

3132
/**
3233
* the default constructor for parser-objects
@@ -338,4 +339,19 @@ public boolean isIsFromDistributedSimulation() {
338339
public void setIsFromDistributedSimulation(boolean isFromDistributedSimulation) {
339340
this.isFromDistributedSimulation = isFromDistributedSimulation;
340341
}
342+
343+
/**
344+
* @return the hashString
345+
*/
346+
public String getHashString() {
347+
return hashString;
348+
}
349+
350+
/**
351+
* Update the HashString of current parameterlist
352+
*/
353+
public void updateHashString() {
354+
this.hashString = support.getHashStringForParameterList(parameterList);
355+
//support.log(this.hashString+" = HashString", typeOfLogLevel.VERBOSE);
356+
}
341357
}

TimeNETOptimizationEnvironment/src/toe/simulation/SimulationCache.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class SimulationCache {
3737
//ArrayList<MeasureType> MeasureList;
3838
private ArrayList<SimulationType> simulationList;
3939
private int localSimulationCounter = 0;
40+
private String tmpHash = "";
4041

4142
public SimulationCache() {
4243
//this.MeasureList = new ArrayList<MeasureType>();
@@ -293,21 +294,26 @@ public boolean checkIfAllParameterMatchTable(parameterTableModel myTableModel) {
293294
}
294295

295296
/**
296-
* Returns all Measures, selected by one parameterset(ParameterList)
297+
* Returns all Measures, selected by one parameterset(ParameterList).
298+
*
299+
* It will just compare the hash values of the sorted parameterLists!
297300
*
298301
* @param parameterList given Set of Parameters to by virtually simulated
299302
* @return ArrayList of MeasureTypes
300303
*/
301304
public ArrayList<MeasureType> getAllMeasuresWithParameterList(ArrayList<parameter> parameterList) {
302305
SimulationType tmpSimulation;
303306
ArrayList<MeasureType> myTmpList = new ArrayList();
307+
tmpHash = support.getHashStringForParameterList(parameterList);
308+
//support.log(tmpHash + " = Needle.", typeOfLogLevel.VERBOSE);
304309

305310
//support.log("Size of All Measures from File: "+MeasureList.size());
306311
//Go through all Measures, find the one with the same parameterlist
307312
for (int i = 0; i < this.getSimulationList().size(); i++) {
308313
tmpSimulation = this.getSimulationList().get(i);
309-
if (compareParameterList(parameterList, tmpSimulation.getListOfParameters())) {
310-
myTmpList = tmpSimulation.getMeasures(); //assumming cache-file did not have experiments with same Parameterset
314+
//if (compareParameterList(parameterList, tmpSimulation.getListOfParameters())) {
315+
if (tmpHash.equals(tmpSimulation.getHashString())) {
316+
myTmpList = tmpSimulation.getMeasures(); //assuming cache-file did not have experiments with same Parameterset
311317
}
312318
}
313319
return myTmpList;
@@ -479,16 +485,31 @@ public ArrayList<SimulationType> getListOfCompletedSimulationParsers(ArrayList<A
479485
setLocalSimulationCounter(simulationCounter);
480486
ArrayList<SimulationType> myParserList = new ArrayList<SimulationType>();
481487

482-
for (ArrayList<parameter> parameterSet : parameterSetList) {
488+
support.log("Updating all simulation hash values.", typeOfLogLevel.INFO);
489+
for (int i = 0; i < this.getSimulationList().size(); i++) {
490+
this.getSimulationList().get(i).updateHashString();
491+
support.setStatusText("Updating Hashs: " + i * 100 / this.getSimulationList().size() + " %");
492+
if (support.isCancelEverything()) {
493+
return null;
494+
}
495+
}
496+
support.log("All hash values in cache up to date.", typeOfLogLevel.VERBOSE);
497+
498+
//for (ArrayList<parameter> parameterSet : parameterSetList) {
499+
for (int i = 0; i < parameterSetList.size(); i++) {
500+
ArrayList<parameter> parameterSet = parameterSetList.get(i);
501+
483502
//Create Arraylist from array of parameters
484503
/*ArrayList<parameter> tmpParameterList = new ArrayList<parameter>();
485504
for (parameter myParameter : parameterSet) {
486505
tmpParameterList.add(myParameter);
487506
}*/
488507
support.spinInLabel();
508+
509+
support.setStatusText("Searching in cache: " + i * 100 / parameterSetList.size() + " %");
489510
//Get local simulation results
490511
ArrayList<MeasureType> listOfMeasureWithGivenParameters = this.getAllMeasuresWithParameterList(parameterSet);
491-
//support.log("Size of ParameterList: "+ tmpParameterList.size() + " results in " +listOfMeasureWithGivenParameters.size()+ " Measurements.");
512+
support.log("Size of ParameterList: " + parameterSet.size() + " results in " + listOfMeasureWithGivenParameters.size() + " Measurements.", typeOfLogLevel.VERBOSE);
492513
//append if listSize is > 0
493514
if (listOfMeasureWithGivenParameters.size() > 0) {
494515
/*SimulationType tmpParser=new SimulationType();

TimeNETOptimizationEnvironment/src/toe/support.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@
1111
import toe.datamodel.parameter;
1212
import toe.datamodel.MeasureType;
1313
import java.io.*;
14+
import java.math.BigInteger;
1415
import java.net.HttpURLConnection;
1516
import java.net.URL;
17+
import java.nio.charset.Charset;
18+
import java.security.MessageDigest;
19+
import java.security.NoSuchAlgorithmException;
1620
import java.text.SimpleDateFormat;
1721
import java.util.ArrayList;
1822
import java.util.Calendar;
23+
import java.util.Collections;
24+
import java.util.Comparator;
1925
import java.util.Properties;
2026
import java.util.Scanner;
2127
import java.util.Timer;
2228
import java.util.TimerTask;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
2331
import javax.swing.JFileChooser;
2432
import javax.swing.JLabel;
2533
import javax.swing.JOptionPane;
@@ -692,14 +700,16 @@ public static void addLinesToLogFileFromListOfParser(ArrayList<SimulationType> p
692700
fw.close();
693701
} catch (Exception e) {
694702
support.log("Exception while writing things to summary log-file.", typeOfLogLevel.ERROR);
695-
support.log("Name of logfile: "+logFileName, typeOfLogLevel.ERROR);
696-
support.log("Trace: "+e.getMessage(), typeOfLogLevel.ERROR);
703+
support.log("Name of logfile: " + logFileName, typeOfLogLevel.ERROR);
704+
support.log("Trace: " + e.getMessage(), typeOfLogLevel.ERROR);
697705
}
698706
}
699707

700708
/**
701-
* Add lines to logfile from parserlist, same like addLinesToLogFileFromListOfParser
702-
* but with number of runs. Only used in old version of OptimizerABC
709+
* Add lines to logfile from parserlist, same like
710+
* addLinesToLogFileFromListOfParser but with number of runs. Only used in
711+
* old version of OptimizerABC
712+
*
703713
* @param pList
704714
* @param numRunList
705715
* @param numCachedSimulations
@@ -1962,4 +1972,39 @@ public static String getDefaultPathToR() {
19621972
return "/usr";
19631973
}
19641974
}
1975+
1976+
/**
1977+
* Creates an identifierString to use as primary key when searching for
1978+
* simulations in cache
1979+
*
1980+
* @param parameterList List of parameters to be converted into String
1981+
* @return hopefully unique string to be used as primary key
1982+
* @see https://dzone.com/articles/get-md5-hash-few-lines-java
1983+
*/
1984+
public static String getHashStringForParameterList(ArrayList<parameter> parameterList) {
1985+
String returnValue = "";
1986+
try {
1987+
1988+
Collections.sort(parameterList, new Comparator<parameter>() {
1989+
@Override
1990+
public int compare(parameter o1, parameter o2) {
1991+
return o1.getName().compareTo(o2.getName());
1992+
}
1993+
});
1994+
1995+
for (int i = 0; i < parameterList.size(); i++) {
1996+
returnValue += parameterList.get(i).getStringValue();
1997+
}
1998+
1999+
byte[] bytesOfMessage = returnValue.getBytes("UTF-8");
2000+
2001+
MessageDigest md = MessageDigest.getInstance("MD5");
2002+
md.update(returnValue.getBytes(), 0, returnValue.length());
2003+
return (new BigInteger(1, md.digest()).toString(16));
2004+
2005+
} catch (Exception ex) {
2006+
support.log("Could not create MD5-hash for " + returnValue, typeOfLogLevel.ERROR);
2007+
}
2008+
return returnValue;
2009+
}
19652010
}

0 commit comments

Comments
 (0)