Final renaming (doc), slight clean-up and refactoring.

This commit is contained in:
Marcel Kronfeld
2008-04-29 09:15:23 +00:00
parent a707de1b4b
commit 3013e905d6
25 changed files with 451 additions and 184 deletions

View File

@@ -76,7 +76,7 @@ import wsi.ra.tool.BasicResourceLoader;
public class EvAClient implements RemoteStateListener, Serializable {
public static final String EVA_PROPERTY_FILE = "resources/EvA2.props";
private static Properties EVA_PROPERTIES;
public static final String iconLocation = "resources/images/icon3.gif";
public static final String iconLocation = "resources/images/icon4.gif";
private static final String splashLocation = "resources/images/splashScreen2.png";
private static final String infoTitle = "EvA2 Information";
final int splashScreenTime = 1500;
@@ -478,6 +478,10 @@ public class EvAClient implements RemoteStateListener, Serializable {
}
public static String getProductName() {
return m_ProductName;
}
protected void logMessage(String msg) {
if (TRACE || m_LogPanel == null) System.out.println(msg);
if (m_LogPanel != null) m_LogPanel.logMessage(msg);

View File

@@ -1,5 +1,7 @@
package eva2.gui;
import eva2.tools.ReflectPackage;
/**
* Created by IntelliJ IDEA.
* User: streiche
@@ -13,16 +15,43 @@ public class PropertyFilePath implements java.io.Serializable {
public String FilePath = "";
public String FileExtension = "";
public PropertyFilePath(String s) {
/**
* Constructor setting the absolute path. F
*
* @param s
*/
private PropertyFilePath(String s) {
this.setCompleteFilePath(s);
}
public PropertyFilePath(PropertyFilePath d) {
this.FileName = d.FileName;
this.FilePath = d.FilePath;
this.FileExtension = d.FileExtension;
}
/**
* Get an instance by an absolute path.
*
* @param path
* @return
*/
public static PropertyFilePath getFilePathAbsolute(String path) {
return new PropertyFilePath(path);
}
/**
* Get an instance by a relative path.
*
* @param relPath
* @return
*/
public static PropertyFilePath getFilePathFromResource(String relPath) {
String fName = ReflectPackage.getResourcePathFromCP(relPath);
if (fName == null) return null;
else return new PropertyFilePath(fName);
}
public Object clone() {
return (Object) new PropertyFilePath(this);
}

View File

@@ -26,6 +26,9 @@ public class MutateESSuccessRule extends MutateESStandard implements InterfaceMu
protected double m_SuccessRate = 0.2;
protected double m_Alpha = 1.2;
public MutateESSuccessRule() {
}
public MutateESSuccessRule(MutateESSuccessRule mutator) {
this.m_MutationStepSize = mutator.m_MutationStepSize;
this.m_SuccessRate = mutator.m_SuccessRate;

View File

@@ -20,10 +20,7 @@ import eva2.server.go.tools.FileLoader;
* To change this template use File | Settings | File Templates.
*/
public class MetricD1ApproxParetoFront implements eva2.server.go.operators.paretofrontmetrics.InterfaceParetoFrontMetric, java.io.Serializable {
private String base = System.getProperty("user.dir");
private String FS = System.getProperty("file.separator");
private PropertyFilePath m_InputFilePath = new PropertyFilePath(base+FS+"resources"+FS+"MOPReference"+FS+"T1_250.txt");
private PropertyFilePath m_InputFilePath = PropertyFilePath.getFilePathFromResource("resources/MOPReference/T1_250.txt");
private String[] m_Titles;
private double[][] m_Reference;

View File

@@ -21,9 +21,9 @@ import eva2.server.go.tools.FileLoader;
*/
public class MetricD1TrueParetoFront implements eva2.server.go.operators.paretofrontmetrics.InterfaceParetoFrontMetric, java.io.Serializable {
private String base = System.getProperty("user.dir");
private String FS = System.getProperty("file.separator");
private PropertyFilePath m_InputFilePath = new PropertyFilePath(base+FS+"resources"+FS+"MOPReference"+FS+"T1_250.txt");
// private String base = System.getProperty("user.dir");
// private String FS = System.getProperty("file.separator");
private PropertyFilePath m_InputFilePath = PropertyFilePath.getFilePathFromResource("resources/MOPReference/T1_250.txt");
private String[] m_Titles;
private double[][] m_Reference;

View File

@@ -24,10 +24,7 @@ import eva2.server.go.tools.FileLoader;
* To change this template use File | Settings | File Templates.
*/
public class MetricErrorRatio implements eva2.server.go.operators.paretofrontmetrics.InterfaceParetoFrontMetric, java.io.Serializable {
private String base = System.getProperty("user.dir");
private String FS = System.getProperty("file.separator");
private PropertyFilePath m_InputFilePath = new PropertyFilePath(base+FS+"resources"+FS+"MOPReference"+FS+"T1_250.txt");
private PropertyFilePath m_InputFilePath = PropertyFilePath.getFilePathFromResource("resources/MOPReference/T1_250.txt");
private double m_Epsilon = 0.0001;
private String[] m_Titles;
private double[][] m_Reference;

View File

@@ -19,10 +19,7 @@ import eva2.server.go.tools.FileLoader;
* To change this template use File | Settings | File Templates.
*/
public class MetricMaximumParetoFrontError implements eva2.server.go.operators.paretofrontmetrics.InterfaceParetoFrontMetric, java.io.Serializable {
private String base = System.getProperty("user.dir");
private String FS = System.getProperty("file.separator");
private PropertyFilePath m_InputFilePath = new PropertyFilePath(base+FS+"resources"+FS+"MOPReference"+FS+"T1_250.txt");
private PropertyFilePath m_InputFilePath = PropertyFilePath.getFilePathFromResource("resources/MOPReference/T1_250.txt");
private String[] m_Titles;
private double[][] m_Reference;

View File

@@ -20,11 +20,8 @@ import eva2.server.go.tools.FileLoader;
* To change this template use File | Settings | File Templates.
*/
public class MetricSWithReference implements InterfaceParetoFrontMetric, java.io.Serializable {
private double[][] m_ObjectiveSpaceRange;
private String base = System.getProperty("user.dir");
private String FS = System.getProperty("file.separator");
private PropertyFilePath m_InputFilePath = new PropertyFilePath(base+FS+"resources"+FS+"MOPReference"+FS+"T1_250.txt");
private PropertyFilePath m_InputFilePath = PropertyFilePath.getFilePathFromResource("resources/MOPReference/T1_250.txt");
private String[] m_Titles;
private double[][] m_Reference;
private double m_ReferenceSMetric = -1;
@@ -42,8 +39,6 @@ public class MetricSWithReference implements InterfaceParetoFrontMetric, java.io
}
}
this.m_InputFilePath = b.m_InputFilePath;
this.base = System.getProperty("user.dir");
this.FS = System.getProperty("file.separator");
if (b.m_Titles != null) {
this.m_Titles = new String[b.m_Titles.length];
System.arraycopy(b.m_Titles, 0, this.m_Titles, 0, this.m_Titles.length);

View File

@@ -51,7 +51,17 @@ public abstract class AbstractProblemBinary extends AbstractOptimizationProblem
* @return the problem dimension
*/
public abstract int getProblemDimension();
/**
* Initialize a single individual with index k in the
* initPopulation cycle.
* @param k
* @param indy
*/
protected void initIndy(int k, AbstractEAIndividual indy) {
indy.init(this);
}
@Override
public void initPopulation(Population population) {
AbstractEAIndividual tmpIndy;
@@ -61,7 +71,7 @@ public abstract class AbstractProblemBinary extends AbstractOptimizationProblem
for (int i = 0; i < population.getPopulationSize(); i++) {
tmpIndy = (AbstractEAIndividual)((AbstractEAIndividual)this.m_Template).clone();
tmpIndy.init(this);
initIndy(i, tmpIndy);
population.add(tmpIndy);
}
// population init must be last

View File

@@ -36,25 +36,6 @@ public class B1Problem extends AbstractProblemBinary implements java.io.Serializ
return (Object) new B1Problem(this);
}
/** This method inits a given population
* @param population The populations that is to be inited
*/
public void initPopulation(Population population) {
AbstractEAIndividual tmpIndy;
population.clear();
((InterfaceDataTypeBinary)this.m_Template).setBinaryDataLength(this.m_ProblemDimension);
for (int i = 0; i < population.getPopulationSize(); i++) {
tmpIndy = (AbstractEAIndividual)((AbstractEAIndividual)this.m_Template).clone();
tmpIndy.init(this);
population.add(tmpIndy);
}
// population init must be last
// it set's fitcalls and generation to zero
population.init();
}
/** This is a simple method that evaluates a given Individual. The fitness
* values of the individual will be set inside this method.
* @param b The BitSet that is to be evaluated.

View File

@@ -2,14 +2,13 @@ package eva2.server.go.problems;
import java.util.*;
import java.util.BitSet;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.GAIndividualBinaryData;
import eva2.server.go.individuals.InterfaceDataTypeBinary;
import eva2.server.go.populations.Population;
import eva2.server.go.strategies.InterfaceOptimizer;
import wsi.ra.math.RNG;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.InterfaceDataTypeBinary;
import eva2.server.go.strategies.InterfaceOptimizer;
import eva2.server.go.problems.AbstractProblemBinary;
/**
* Created by IntelliJ IDEA.
@@ -18,7 +17,7 @@ import wsi.ra.math.RNG;
* Time: 15:12:46
* To change this template use Options | File Templates.
*/
public class BKnapsackProblem extends AbstractOptimizationProblem implements java.io.Serializable {
public class BKnapsackProblem extends AbstractProblemBinary implements java.io.Serializable {
private int m_Limit = 5000;
private double m_Punish = 2.0;
@@ -128,12 +127,12 @@ public class BKnapsackProblem extends AbstractOptimizationProblem implements jav
{392,-386}};
public BKnapsackProblem() {
this.m_Template = new GAIndividualBinaryData();
super();
}
public BKnapsackProblem(BKnapsackProblem b) {
//AbstractOptimizationProblem
if (b.m_Template != null)
this.m_Template = (AbstractEAIndividual)((AbstractEAIndividual)b.m_Template).clone();
cloneObjects(b);
// BKnapsackProblem
this.m_Limit = b.m_Limit;
this.m_Punish = b.m_Punish;
@@ -141,6 +140,10 @@ public class BKnapsackProblem extends AbstractOptimizationProblem implements jav
this.m_Lamarkism = b.m_Lamarkism;
}
public int getProblemDimension() {
return items.length;
}
/** This method returns a deep clone of the problem.
* @return the clone
*/
@@ -154,45 +157,28 @@ public class BKnapsackProblem extends AbstractOptimizationProblem implements jav
// nothing to init here
}
/** This method inits a given population
* @param population The populations that is to be inited
*/
public void initPopulation(Population population) {
AbstractEAIndividual tmpIndy;
protected void initIndy(int k, AbstractEAIndividual indy) {
indy.init(this);
if (RNG.flipCoin(this.m_ProblemSpecificInit)) {
BitSet tmpSet = new BitSet();
tmpSet.clear();
population.clear();
((InterfaceDataTypeBinary)this.m_Template).setBinaryDataLength(items.length);
for (int i = 0; i < population.getPopulationSize(); i++) {
tmpIndy = (AbstractEAIndividual)((AbstractEAIndividual)this.m_Template).clone();
tmpIndy.init(this);
if (RNG.flipCoin(this.m_ProblemSpecificInit)) {
BitSet tmpSet = new BitSet();
tmpSet.clear();
while (evaluate(tmpSet, items.length)[1] > 0) {
tmpSet.set(RNG.randomInt(0,items.length-1));
}
((InterfaceDataTypeBinary)tmpIndy).SetBinaryDataLamarkian(tmpSet);
}
population.add(tmpIndy);
}
// population init must be last
// it set's fitcalls and generation to zero
population.init();
while (eval(tmpSet)[1] > 0) {
tmpSet.set(RNG.randomInt(0,items.length-1));
}
((InterfaceDataTypeBinary)indy).SetBinaryDataLamarkian(tmpSet);
}
}
/** This method evaluates a single individual and sets the fitness values
* @param individual The individual that is to be evalutated
*/
public void evaluate(AbstractEAIndividual individual) {
BitSet tmpBitSet;
double[] result;
InterfaceDataTypeBinary tmpIndy;
tmpIndy = (InterfaceDataTypeBinary) individual;
tmpBitSet = tmpIndy.getBinaryData();
result = this.evaluate(tmpBitSet, tmpIndy.size());
tmpBitSet = ((InterfaceDataTypeBinary) individual).getBinaryData();
result = this.eval(tmpBitSet);
if (RNG.flipCoin(this.m_LocalSearch)) {
// first remove surplus assets
while (result[1] > 0) {
@@ -210,7 +196,7 @@ public class BKnapsackProblem extends AbstractOptimizationProblem implements jav
}
// remove the weakest
tmpBitSet.clear(weakest);
result = this.evaluate(tmpBitSet, tmpIndy.size());
result = this.eval(tmpBitSet);
}
// now lets see if we can replace some guy with a more efficient one
int weakest = tmpBitSet.nextSetBit(0);
@@ -228,7 +214,7 @@ public class BKnapsackProblem extends AbstractOptimizationProblem implements jav
}
tmpBitSet.clear(weakest);
result = this.evaluate(tmpBitSet, tmpIndy.size());
result = this.eval(tmpBitSet);
int weight = 0;
for (int i = 0; i < items.length; i++) {
if (tmpBitSet.get(i)) weight += items[i][0];
@@ -244,11 +230,11 @@ public class BKnapsackProblem extends AbstractOptimizationProblem implements jav
}
}
if (stronger >= 0) tmpBitSet.set(stronger);
result = this.evaluate(tmpBitSet, tmpIndy.size());
result = this.eval(tmpBitSet);
}
if (this.m_Lamarkism) {
tmpIndy.SetBinaryDataLamarkian(tmpBitSet);
((InterfaceDataTypeBinary) individual).SetBinaryDataLamarkian(tmpBitSet);
}
}
result[0] += 5100;
@@ -261,9 +247,11 @@ public class BKnapsackProblem extends AbstractOptimizationProblem implements jav
* @param l The length of the BitSet.
* @return Double[]
*/
public double[] evaluate(BitSet b, int l) {
public double[] eval(BitSet b) {
double[] result = new double[3];
int l = items.length;
if (getProblemDimension() != l) System.err.println("Error in BKnapsack!");
result[0] = 0;
result[1] = 0; // the weight exceed
result[2] = 0; // net worth
@@ -292,7 +280,7 @@ public class BKnapsackProblem extends AbstractOptimizationProblem implements jav
tmpIndy = (InterfaceDataTypeBinary) individual;
tmpBitSet = tmpIndy.getBinaryData();
report = this.evaluate(tmpBitSet, tmpIndy.size());
report = this.eval(tmpBitSet);
result += individual.getStringRepresentation() + "\n";
result += "Is worth: " + Math.abs(report[2]) + " and ";
if (report[1] == 0) result += "does not exceed the weight limit!";

View File

@@ -13,7 +13,6 @@ import eva2.gui.BeanInspector;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.ESIndividualDoubleData;
import eva2.server.go.individuals.InterfaceDataTypeDouble;
import eva2.server.go.operators.distancemetric.PhenotypeMetric;
import eva2.server.go.operators.postprocess.InterfacePostProcessParams;
import eva2.server.go.operators.postprocess.PostProcess;
import eva2.server.go.operators.postprocess.PostProcessParams;
@@ -38,7 +37,7 @@ public class MatlabProblem extends AbstractProblemDouble implements InterfaceTex
public static boolean hideFromGOE = true;
private F1Problem f1 = new F1Problem(); // TODO
// private F1Problem f1 = new F1Problem(); // TODO
// transient private double[] currArray = null;
// private String mtCmd = null;
@@ -194,11 +193,11 @@ public class MatlabProblem extends AbstractProblemDouble implements InterfaceTex
public double[] eval(double[] x) {
log("evaluating " + BeanInspector.toString(x) + "\n");
double[] res = handler.requestEval(this, x);
double diff = PhenotypeMetric.euclidianDistance(res, f1.eval(x));
log("result: " + BeanInspector.toString(res) + " compared to " + BeanInspector.toString(f1.eval(x)) + "\n");
if (diff != 0) {
log("!!! diff is " + diff + "\n");
}
// double diff = PhenotypeMetric.euclidianDistance(res, f1.eval(x));
// log("result: " + BeanInspector.toString(res) + " compared to " + BeanInspector.toString(f1.eval(x)) + "\n");
// if (diff != 0) {
// log("!!! diff is " + diff + "\n");
// }
return res;
// synchronized (this) {

View File

@@ -299,7 +299,7 @@ public class TF1Problem extends AbstractMultiObjectiveOptimizationProblem implem
int points = 500;
String base = System.getProperty("user.dir");
String FS = System.getProperty("file.separator");
PropertyFilePath fileOutPath = new PropertyFilePath(base+FS+"resources"+FS+"MOPReference"+FS+"T1_"+points+".txt");
PropertyFilePath fileOutPath = PropertyFilePath.getFilePathFromResource("resources/MOPReference"+FS+"T1_"+points+".txt");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(fileOutPath.getCompleteFilePath()));

View File

@@ -7,7 +7,7 @@ import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.InterfaceGAIndividual;
import eva2.server.go.operators.selection.InterfaceSelection;
import eva2.server.go.operators.selection.SelectBest;
import eva2.server.go.operators.selection.SelectBestSingle;
import eva2.server.go.operators.selection.SelectRandom;
import eva2.server.go.populations.PBILPopulation;
import eva2.server.go.populations.Population;
@@ -38,7 +38,7 @@ public class CHCAdaptiveSearchAlgorithm implements InterfaceOptimizer, java.io.S
private Population m_Population = new Population();
private InterfaceOptimizationProblem m_Problem = new B1Problem();
private InterfaceSelection m_RecombSelectionOperator = new SelectRandom();
private InterfaceSelection m_PopulSelectionOperator = new SelectBest();
private InterfaceSelection m_PopulSelectionOperator = new SelectBestSingle();
transient private String m_Identifier = "";
transient private InterfacePopulationChangedEventListener m_Listener;

View File

@@ -19,6 +19,7 @@ import eva2.server.go.operators.cluster.ClusteringDensityBased;
import eva2.server.go.operators.cluster.InterfaceClustering;
import eva2.server.go.operators.mutation.InterfaceMutation;
import eva2.server.go.operators.mutation.MutateESGlobal;
//import eva2.server.go.populations.Distraction;
import eva2.server.go.populations.Population;
import eva2.server.go.problems.B1Problem;
import eva2.server.go.problems.Interface2DBorderProblem;
@@ -65,7 +66,7 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
private int m_PopulationSize = 50;
private int convergedCnt = 0;
private static boolean TRACE = true;
private static boolean TRACE = false;
private int m_ShowCycle = 100;
transient private TopoPlot m_Topology;
private int haltingWindow = 15;
@@ -586,12 +587,10 @@ public class ClusterBasedNichingEA implements InterfacePopulationChangedEventLis
this.m_Population.addPopulation((Population)this.m_Species.get(i));
}
if (TRACE) System.out.println("Population size: " + this.m_Population.size());
if (TRACE) {
// for (int i=0; i<m_Species.size(); i++) {
// System.out.print("distraction center at " + BeanInspector.toString(m_Species.get(i).getDistractionCenter()));
// System.out.println(", strength " + m_Species.get(i).getDistractionStrength());
// }
}
// if (TRACE) {
// Distraction distr = new Distraction(5., 0, m_Species);
// if (m_Undifferentiated.size()>0) distr.calcDistractionFor(m_Undifferentiated.getBestEAIndividual());
// }
this.firePropertyChangedEvent("NextGenerationPerformed");
}

View File

@@ -10,6 +10,7 @@ import eva2.server.go.problems.AbstractOptimizationProblem;
import eva2.server.go.problems.F1Problem;
import eva2.server.go.problems.InterfaceOptimizationProblem;
import wsi.ra.math.RNG;
import eva2.tools.EVAERROR;
import eva2.tools.SelectedTag;
import eva2.tools.Tag;
@@ -241,7 +242,7 @@ public class DifferentialEvolution implements InterfaceOptimizer, java.io.Serial
indy1 = (InterfaceESIndividual)pop.get(RNG.randomInt(0, pop.size()-1));
indy2 = (InterfaceESIndividual)pop.get(RNG.randomInt(0, pop.size()-1));
} catch (java.lang.ClassCastException e) {
System.out.println("Differential Evolution currently requires InterfaceESIndividual as basic data type!");
EVAERROR.errorMsgOnce("Differential Evolution currently requires InterfaceESIndividual as basic data type!");
}
xk = indy1.getDGenotype();
xl = indy2.getDGenotype();

View File

@@ -21,7 +21,7 @@ import wsi.ra.jproxy.RMIServer;
/** The one and only island model for parallelization. Since parallelization based
* on the RMIProxyRemoteThread is on the one hand much slower than benchmark function
* evaluation and on the other hand the GUI based distribution scheme is rather prone
* to config errors (the correct ssh version is required, the JOpt.jar needs to be in
* to config errors (the correct ssh version is required, the jar needs to be in
* the working dir and possible problem data must be on the servers to) an implicit
* island-model has been implemented too to allow fast and reliabel computation.
* This is still usefull, since it is less prone to premature convergence and also

View File

@@ -146,7 +146,7 @@ public class Tribes implements InterfaceOptimizer, java.io.Serializable {
public static int[] strategies = new int[10]; // Just for information
public static int[] status = new int[9]; // Just for information
public static boolean testBC = false; // TODO project to JOpt
public static boolean testBC = false; // TODO project to EvA2
public static int adaptOption = 2;
public static double blind=0; // 0.5 //"Blind" move for very good particles, with a probability Tribes.blind
public static boolean repel=false; // If 1, use a "repelling" strategy (see moveExplorer() )
@@ -625,7 +625,7 @@ public class Tribes implements InterfaceOptimizer, java.io.Serializable {
/**
* As TRIBES manages an own structured set of particles (the list of Tribes containing explorers
* and memories), the setPopulation method is only telling Tribes the range
* of the indiviuals in the beginning of the run, the indiviudals will be discarded.
* of the indiviuals in the beginning of the run, the individuals will be discarded.
*/
public void setPopulation(Population pop) {
if (pop == null) return;

View File

@@ -2,11 +2,10 @@ package eva2.server.modules;
import java.io.Serializable;
import eva2.gui.BeanInspector;
import eva2.server.go.InterfaceGOParameters;
import eva2.server.go.InterfacePopulationChangedEventListener;
import eva2.server.go.individuals.GAIndividualDoubleData;
import eva2.server.go.operators.terminators.EvaluationTerminator;
import eva2.server.go.problems.B1Problem;
import eva2.server.go.problems.F1Problem;
import eva2.server.go.strategies.GeneticAlgorithm;
import eva2.tools.Serializer;
@@ -45,7 +44,8 @@ public class GOParameters extends AbstractGOParameters implements InterfaceGOPar
*
*/
public GOParameters() {
super(new GeneticAlgorithm(), new B1Problem(), new EvaluationTerminator(1000));
super(new GeneticAlgorithm(), new F1Problem(), new EvaluationTerminator(1000));
((F1Problem)m_Problem).setEAIndividual(new GAIndividualDoubleData());
}
/**

View File

@@ -195,7 +195,7 @@ public class Processor extends Thread implements InterfaceProcessor, InterfacePo
this.goParams.getOptimizer().optimize();
// registerPopulationStateChanged *SHOULD* be fired by the optimizer or resp. the population
// as we are event listener
// System.out.println(this.goParams.getOptimizer().getPopulation().getIndyList());
//System.out.println(this.goParams.getOptimizer().getPopulation().getIndyList());
} while (isOptRunning() && !this.goParams.getTerminator().isTerminated(this.goParams.getOptimizer().getPopulation()));
runCounter++;

View File

@@ -3,15 +3,18 @@ package eva2.tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import wsi.ra.tool.BasicResourceLoader;
//import wsi.ra.tool.BasicResourceLoader;
/**
@@ -238,74 +241,117 @@ public class ReflectPackage {
classPath = System.getProperty("java.class.path",".");
if (useFilteredClassPath) {
try {
String[] pathElements = getClassPathElements();
File f;
ArrayList<String> valids = new ArrayList<String>(pathElements.length);
for (int i=0; i<pathElements.length; i++) {
// System.err.println(pathElements[i]);
f = new File(pathElements[i]);
// if (f.canRead()) {valids.add(pathElements[i]);}
if (f.exists() && f.canRead()) {
valids.add(pathElements[i]);
}
}
// dynCP = valids.toArray(dynCP); // this causes Matlab to crash meanly.
dynCP = new String[valids.size()];
for (int i=0; i<valids.size(); i++) dynCP[i] = valids.get(i);
dynCP = getValidCPArray();
} catch (Exception e) {
System.err.println(e.getMessage());
}
} else dynCP = getClassPathElements();
}
// dynCP = System.getProperty("java.class.path",".").split(File.pathSeparator);
if (TRACE) System.out.println("classpath is " + classPath);
// System.err.println("no of path elements is " + dynCP.length);
// if (usePathMap) {
// System.err.println("Checking for " + pckg);
// ArrayList<String> pathes = pathMap.get(pckg);
// System.err.println("stored objects: " + ((pathes != null) ? pathes.size() : 0));
// if (pathes == null) {
// pathes = new ArrayList<String>();
// for (int i=0; i<dynCP.length; i++) {
// int added = 0;
// if (dynCP[i].endsWith(".jar")) {
// added = getClassesFromJarFltr(set, dynCP[i], pckg, includeSubs, reqSuperCls);
// } else {
// added = getClassesFromFilesFltr(set, dynCP[i], pckg, includeSubs, reqSuperCls);
// }
// if (added > 0) pathes.add(dynCP[i]);
// }
// pathMap.put(pckg, pathes);
// } else {
// for (int i=0; i<pathes.size(); i++) {
// System.err.println("reusing " + pathes.get(i));
// if (pathes.get(i).endsWith(".jar")) getClassesFromJarFltr(set, pathes.get(i), pckg, includeSubs, reqSuperCls);
// else getClassesFromFilesFltr(set, pathes.get(i), pckg, includeSubs, reqSuperCls);
// }
// }
// } else {
for (int i=0; i<dynCP.length; i++) {
if (TRACE) System.out.println("reading element "+dynCP[i]);
if (dynCP[i].endsWith(".jar")) {
getClassesFromJarFltr(set, dynCP[i], pckg, includeSubs, reqSuperCls);
} else {
if (TRACE) System.out.println("reading from files: "+dynCP[i]+" "+pckg);
getClassesFromFilesFltr(set, dynCP[i], pckg, includeSubs, reqSuperCls);
}
for (int i=0; i<dynCP.length; i++) {
if (TRACE) System.out.println("reading element "+dynCP[i]);
if (dynCP[i].endsWith(".jar")) {
getClassesFromJarFltr(set, dynCP[i], pckg, includeSubs, reqSuperCls);
} else {
if (TRACE) System.out.println("reading from files: "+dynCP[i]+" "+pckg);
getClassesFromFilesFltr(set, dynCP[i], pckg, includeSubs, reqSuperCls);
}
// }
}
Object[] clsArr = set.toArray();
if (bSort) {
Arrays.sort(clsArr, new ClassComparator());
}
List list;
// #1
list = Arrays.asList(clsArr);
return (Class[])list.toArray(new Class[list.size()]);
}
/**
* Request a valid absolute resource path from a relative resource path
* by searching the classpath entries. Returns null if the resource is not found.
* @param res
*/
public static String getResourcePathFromCP(String res) {
String[] cpEntries = getClassPathElements();
URL url = ClassLoader.getSystemResource(res);
if (TRACE) System.out.println(res + ((url == null) ? " not" : " was") + " found by classloader.");
if (url != null) return url.getFile();
File f;
String fNameSep;
if (res.startsWith(System.getProperty("file.separator"))) fNameSep = res;
else fNameSep = System.getProperty("file.separator")+res;
for (int i=0; i<cpEntries.length; i++) {
if (!cpEntries[i].endsWith(".jar")) { // its a fs directory (hopefully)
if (TRACE) System.out.println("reading element "+cpEntries[i]);
f = new File(cpEntries[i]+fNameSep);
if (TRACE) System.out.println(res + ((!f.exists()) ? " not" : " was") + " found in " + cpEntries[i]);
if (f.exists()) return f.getAbsolutePath();
}
}
return null;
}
/**
* Request a valid absolute resource path from a relative resource path
* by searching the classpath entries. Returns null if the resource is not found.
* @param res
*/
public static InputStream getResourceStreamFromCP(String res) {
InputStream in = BasicResourceLoader.instance().getStreamFromResourceLocation(res);
if (TRACE) System.out.println(res + ((in == null) ? " not" : " was") + " found by classloader.");
if (in != null) return in;
String[] cpEntries = getClassPathElements();
File f;
String fNameSep;
if (res.startsWith(System.getProperty("file.separator"))) fNameSep = res;
else fNameSep = System.getProperty("file.separator")+res;
for (int i=0; i<cpEntries.length; i++) {
if (!cpEntries[i].endsWith(".jar")) { // its a fs directory (hopefully)
if (TRACE) System.out.println("reading element "+cpEntries[i]);
f = new File(cpEntries[i]+fNameSep);
if (TRACE) System.out.println(res + ((!f.exists()) ? " not" : " was") + " found in " + cpEntries[i]);
if (f.exists()) {
try {
return new FileInputStream(f);
} catch(Exception e) {
System.err.println("error reading file " + f.getAbsolutePath() + ": " + e.getMessage());
}
}
}
}
return null;
}
public static ArrayList<String> getValidCPEntries() {
String[] pathElements = getClassPathElements();
File f;
ArrayList<String> valids = new ArrayList<String>(pathElements.length);
for (int i=0; i<pathElements.length; i++) {
// System.err.println(pathElements[i]);
f = new File(pathElements[i]);
// if (f.canRead()) {valids.add(pathElements[i]);}
if (f.exists() && f.canRead()) {
valids.add(pathElements[i]);
}
}
return valids;
}
public static String[] getValidCPArray() {
ArrayList<String> valids = getValidCPEntries();
// vp = valids.toArray(dynCP); // this causes Matlab to crash meanly.
String[] vp = new String[valids.size()];
for (int i=0; i<valids.size(); i++) vp[i] = valids.get(i);
return vp;
}
/**
* Retrieve assignable classes of the given package from classpath.

View File

@@ -302,8 +302,7 @@ public class BasicResourceLoader implements ResourceLoader
* @param rawResrcLoc Description of the Parameter
* @return the byte array of file.
*/
public byte[] getBytesFromResourceLocation(String rawResrcLoc)
{
public InputStream getStreamFromResourceLocation(String rawResrcLoc) {
String resourceLocation = rawResrcLoc.replace('\\', '/');
//System.out.println("Try to get: "+resourceLocation);
@@ -318,14 +317,14 @@ public class BasicResourceLoader implements ResourceLoader
resourceLocation = resourceLocation.trim();
// is a relative path defined ?
// this can only be possible, if this is a file resource loacation
// this can only be possible, if this is a file resource location
if (resourceLocation.startsWith("..") ||
resourceLocation.startsWith("/") ||
resourceLocation.startsWith("\\") ||
((resourceLocation.length() > 1) &&
(resourceLocation.charAt(1) == ':')))
{
return getBytesFromFile(resourceLocation);
return getStreamFromFile(resourceLocation);
}
InputStream in = ClassLoader.getSystemResourceAsStream(resourceLocation);
@@ -346,10 +345,23 @@ public class BasicResourceLoader implements ResourceLoader
{
logger.debug("Stream opened for " + resourceLocation);
}
return in;
}
/**
* Gets the byte data from a file at the given resource location.
*
* @param rawResrcLoc Description of the Parameter
* @return the byte array of file.
*/
public byte[] getBytesFromResourceLocation(String rawResrcLoc)
{
InputStream in = getStreamFromResourceLocation(rawResrcLoc);
byte[] bytes = getBytesFromStream(in);
return bytes;
if (in == null) {
return null;
}
return getBytesFromStream(in);
}
/**
@@ -442,7 +454,7 @@ public class BasicResourceLoader implements ResourceLoader
* @param fileName Description of the Parameter
* @return the byte array of the file.
*/
private byte[] getBytesFromFile(String fileName)
private FileInputStream getStreamFromFile(String fileName)
{
if (fileName.startsWith("/cygdrive/"))
{
@@ -462,6 +474,7 @@ public class BasicResourceLoader implements ResourceLoader
try
{
fis = new FileInputStream(file);
return fis;
}
catch (Exception e)
{
@@ -470,11 +483,25 @@ public class BasicResourceLoader implements ResourceLoader
return null;
}
}
/**
* Gets the byte data from a file.
*
* @param fileName Description of the Parameter
* @return the byte array of the file.
*/
private byte[] getBytesFromFile(String fileName) {
FileInputStream fis = getStreamFromFile(fileName);
if (fis == null) {
System.err.println("couldnt get file input stream!");
return null;
}
BufferedInputStream bis = new BufferedInputStream(fis);
// only files with <65536 bytes are allowed
//if( file.length() > 65536 ) System.out.println("Resource files should be smaller than 65536 bytes...");
int size = (int) file.length();
int size = (int) new File(fileName).length();
byte[] b = new byte[size];
int rb = 0;
int chunk = 0;