http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningCurve.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningCurve.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningCurve.java index e79dde8..907371c 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningCurve.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningCurve.java @@ -30,103 +30,103 @@ import com.yahoo.labs.samoa.moa.core.StringUtils; /** * Class that stores and keeps the history of evaluation measurements. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class LearningCurve extends AbstractMOAObject { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - protected List<String> measurementNames = new ArrayList<String>(); + protected List<String> measurementNames = new ArrayList<String>(); - protected List<double[]> measurementValues = new ArrayList<double[]>(); + protected List<double[]> measurementValues = new ArrayList<double[]>(); - public LearningCurve(String orderingMeasurementName) { - this.measurementNames.add(orderingMeasurementName); - } + public LearningCurve(String orderingMeasurementName) { + this.measurementNames.add(orderingMeasurementName); + } - public String getOrderingMeasurementName() { - return this.measurementNames.get(0); - } + public String getOrderingMeasurementName() { + return this.measurementNames.get(0); + } - public void insertEntry(LearningEvaluation learningEvaluation) { - Measurement[] measurements = learningEvaluation.getMeasurements(); - Measurement orderMeasurement = Measurement.getMeasurementNamed( - getOrderingMeasurementName(), measurements); - if (orderMeasurement == null) { - throw new IllegalArgumentException(); - } - DoubleVector entryVals = new DoubleVector(); - for (Measurement measurement : measurements) { - entryVals.setValue(addMeasurementName(measurement.getName()), - measurement.getValue()); - } - double orderVal = orderMeasurement.getValue(); - int index = 0; - while ((index < this.measurementValues.size()) - && (orderVal > this.measurementValues.get(index)[0])) { - index++; - } - this.measurementValues.add(index, entryVals.getArrayRef()); + public void insertEntry(LearningEvaluation learningEvaluation) { + Measurement[] measurements = learningEvaluation.getMeasurements(); + Measurement orderMeasurement = Measurement.getMeasurementNamed( + getOrderingMeasurementName(), measurements); + if (orderMeasurement == null) { + throw new IllegalArgumentException(); } - - public int numEntries() { - return this.measurementValues.size(); + DoubleVector entryVals = new DoubleVector(); + for (Measurement measurement : measurements) { + entryVals.setValue(addMeasurementName(measurement.getName()), + measurement.getValue()); } - - protected int addMeasurementName(String name) { - int index = this.measurementNames.indexOf(name); - if (index < 0) { - index = this.measurementNames.size(); - this.measurementNames.add(name); - } - return index; + double orderVal = orderMeasurement.getValue(); + int index = 0; + while ((index < this.measurementValues.size()) + && (orderVal > this.measurementValues.get(index)[0])) { + index++; } - - public String headerToString() { - StringBuilder sb = new StringBuilder(); - boolean first = true; - for (String name : this.measurementNames) { - if (!first) { - sb.append(','); - } else { - first = false; - } - sb.append(name); - } - return sb.toString(); + this.measurementValues.add(index, entryVals.getArrayRef()); + } + + public int numEntries() { + return this.measurementValues.size(); + } + + protected int addMeasurementName(String name) { + int index = this.measurementNames.indexOf(name); + if (index < 0) { + index = this.measurementNames.size(); + this.measurementNames.add(name); } - - public String entryToString(int entryIndex) { - StringBuilder sb = new StringBuilder(); - double[] vals = this.measurementValues.get(entryIndex); - for (int i = 0; i < this.measurementNames.size(); i++) { - if (i > 0) { - sb.append(','); - } - if ((i >= vals.length) || Double.isNaN(vals[i])) { - sb.append('?'); - } else { - sb.append(Double.toString(vals[i])); - } - } - return sb.toString(); + return index; + } + + public String headerToString() { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (String name : this.measurementNames) { + if (!first) { + sb.append(','); + } else { + first = false; + } + sb.append(name); } - - @Override - public void getDescription(StringBuilder sb, int indent) { - sb.append(headerToString()); - for (int i = 0; i < numEntries(); i++) { - StringUtils.appendNewlineIndented(sb, indent, entryToString(i)); - } + return sb.toString(); + } + + public String entryToString(int entryIndex) { + StringBuilder sb = new StringBuilder(); + double[] vals = this.measurementValues.get(entryIndex); + for (int i = 0; i < this.measurementNames.size(); i++) { + if (i > 0) { + sb.append(','); + } + if ((i >= vals.length) || Double.isNaN(vals[i])) { + sb.append('?'); + } else { + sb.append(Double.toString(vals[i])); + } } - - public double getMeasurement(int entryIndex, int measurementIndex) { - return this.measurementValues.get(entryIndex)[measurementIndex]; + return sb.toString(); + } + + @Override + public void getDescription(StringBuilder sb, int indent) { + sb.append(headerToString()); + for (int i = 0; i < numEntries(); i++) { + StringUtils.appendNewlineIndented(sb, indent, entryToString(i)); } + } - public String getMeasurementName(int measurementIndex) { - return this.measurementNames.get(measurementIndex); - } + public double getMeasurement(int entryIndex, int measurementIndex) { + return this.measurementValues.get(entryIndex)[measurementIndex]; + } + + public String getMeasurementName(int measurementIndex) { + return this.measurementNames.get(measurementIndex); + } }
http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningEvaluation.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningEvaluation.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningEvaluation.java index b0aee36..67b978c 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningEvaluation.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningEvaluation.java @@ -29,35 +29,35 @@ import java.util.List; /** * Class that stores an array of evaluation measurements. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class LearningEvaluation extends AbstractMOAObject { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - protected Measurement[] measurements; + protected Measurement[] measurements; - public LearningEvaluation(Measurement[] measurements) { - this.measurements = measurements.clone(); - } + public LearningEvaluation(Measurement[] measurements) { + this.measurements = measurements.clone(); + } - public LearningEvaluation(Measurement[] evaluationMeasurements, - LearningPerformanceEvaluator cpe, Learner model) { - List<Measurement> measurementList = new LinkedList<Measurement>(); - measurementList.addAll(Arrays.asList(evaluationMeasurements)); - measurementList.addAll(Arrays.asList(cpe.getPerformanceMeasurements())); - measurementList.addAll(Arrays.asList(model.getModelMeasurements())); - this.measurements = measurementList.toArray(new Measurement[measurementList.size()]); - } + public LearningEvaluation(Measurement[] evaluationMeasurements, + LearningPerformanceEvaluator cpe, Learner model) { + List<Measurement> measurementList = new LinkedList<Measurement>(); + measurementList.addAll(Arrays.asList(evaluationMeasurements)); + measurementList.addAll(Arrays.asList(cpe.getPerformanceMeasurements())); + measurementList.addAll(Arrays.asList(model.getModelMeasurements())); + this.measurements = measurementList.toArray(new Measurement[measurementList.size()]); + } - public Measurement[] getMeasurements() { - return this.measurements.clone(); - } + public Measurement[] getMeasurements() { + return this.measurements.clone(); + } - @Override - public void getDescription(StringBuilder sb, int indent) { - Measurement.getMeasurementsDescription(this.measurements, sb, indent); - } + @Override + public void getDescription(StringBuilder sb, int indent) { + Measurement.getMeasurementsDescription(this.measurements, sb, indent); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningPerformanceEvaluator.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningPerformanceEvaluator.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningPerformanceEvaluator.java index 86b571d..b236066 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningPerformanceEvaluator.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/LearningPerformanceEvaluator.java @@ -20,42 +20,43 @@ package com.yahoo.labs.samoa.moa.evaluation; * #L% */ - import com.yahoo.labs.samoa.moa.MOAObject; import com.yahoo.labs.samoa.moa.core.Example; import com.yahoo.labs.samoa.moa.core.Measurement; /** - * Interface implemented by learner evaluators to monitor - * the results of the learning process. - * + * Interface implemented by learner evaluators to monitor the results of the + * learning process. + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public interface LearningPerformanceEvaluator<E extends Example> extends MOAObject { - /** - * Resets this evaluator. It must be similar to - * starting a new evaluator from scratch. - * - */ - public void reset(); - - /** - * Adds a learning result to this evaluator. - * - * @param example the example to be classified - * @param classVotes an array containing the estimated membership - * probabilities of the test instance in each class - * @return an array of measurements monitored in this evaluator - */ - public void addResult(E example, double[] classVotes); - - /** - * Gets the current measurements monitored by this evaluator. - * - * @return an array of measurements monitored by this evaluator - */ - public Measurement[] getPerformanceMeasurements(); + /** + * Resets this evaluator. It must be similar to starting a new evaluator from + * scratch. + * + */ + public void reset(); + + /** + * Adds a learning result to this evaluator. + * + * @param example + * the example to be classified + * @param classVotes + * an array containing the estimated membership probabilities of the + * test instance in each class + * @return an array of measurements monitored in this evaluator + */ + public void addResult(E example, double[] classVotes); + + /** + * Gets the current measurements monitored by this evaluator. + * + * @return an array of measurements monitored by this evaluator + */ + public Measurement[] getPerformanceMeasurements(); } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MeasureCollection.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MeasureCollection.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MeasureCollection.java index 898aca3..9fe1fc3 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MeasureCollection.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MeasureCollection.java @@ -1,4 +1,3 @@ - package com.yahoo.labs.samoa.moa.evaluation; /* @@ -27,233 +26,236 @@ import com.yahoo.labs.samoa.moa.AbstractMOAObject; import com.yahoo.labs.samoa.moa.cluster.Clustering; import com.yahoo.labs.samoa.moa.core.DataPoint; -public abstract class MeasureCollection extends AbstractMOAObject{ - private String[] names; - private ArrayList<Double>[] values; - private ArrayList<Double>[] sortedValues; - private ArrayList<String> events; - - private double[] minValue; - private double[] maxValue; - private double[] sumValues; - private boolean[] enabled; - private boolean[] corrupted; - private double time; - private boolean debug = true; - private MembershipMatrix mm = null; - - private HashMap<String, Integer> map; - - private int numMeasures = 0; - - - - - public MeasureCollection() { - names = getNames(); - numMeasures = names.length; - map = new HashMap<String, Integer>(numMeasures); - for (int i = 0; i < names.length; i++) { - map.put(names[i],i); - } - values = (ArrayList<Double>[]) new ArrayList[numMeasures]; - sortedValues = (ArrayList<Double>[]) new ArrayList[numMeasures]; - maxValue = new double[numMeasures]; - minValue = new double[numMeasures]; - sumValues = new double[numMeasures]; - corrupted = new boolean[numMeasures]; - enabled = getDefaultEnabled(); - time = 0; - events = new ArrayList<String>(); - - for (int i = 0; i < numMeasures; i++) { - values[i] = new ArrayList<Double>(); - sortedValues[i] = new ArrayList<Double>(); - maxValue[i] = Double.MIN_VALUE; - minValue[i] = Double.MAX_VALUE; - corrupted[i] = false; - sumValues[i] = 0.0; - } - +public abstract class MeasureCollection extends AbstractMOAObject { + private String[] names; + private ArrayList<Double>[] values; + private ArrayList<Double>[] sortedValues; + private ArrayList<String> events; + + private double[] minValue; + private double[] maxValue; + private double[] sumValues; + private boolean[] enabled; + private boolean[] corrupted; + private double time; + private boolean debug = true; + private MembershipMatrix mm = null; + + private HashMap<String, Integer> map; + + private int numMeasures = 0; + + public MeasureCollection() { + names = getNames(); + numMeasures = names.length; + map = new HashMap<String, Integer>(numMeasures); + for (int i = 0; i < names.length; i++) { + map.put(names[i], i); } - - protected abstract String[] getNames(); - - public void addValue(int index, double value){ - if(Double.isNaN(value)){ - if(debug) - System.out.println("NaN for "+names[index]); - corrupted[index] = true; - } - if(value < 0){ - if(debug) - System.out.println("Negative value for "+names[index]); - } - - values[index].add(value); - sumValues[index]+=value; - if(value < minValue[index]) minValue[index] = value; - if(value > maxValue[index]) maxValue[index] = value; - } - - protected void addValue(String name, double value){ - if(map.containsKey(name)){ - addValue(map.get(name),value); - } - else{ - System.out.println(name+" is not a valid measure key, no value added"); - } - } - - //add an empty entry e.g. if evaluation crashed internally - public void addEmptyValue(int index){ - values[index].add(Double.NaN); - corrupted[index] = true; - } - - public int getNumMeasures(){ - return numMeasures; - } - - public String getName(int index){ - return names[index]; - } - - public double getMaxValue(int index){ - return maxValue[index]; - } - - public double getMinValue(int index){ - return minValue[index]; - } - - public double getLastValue(int index){ - if(values[index].size()<1) return Double.NaN; - return values[index].get(values[index].size()-1); - } - - public double getMean(int index){ - if(corrupted[index] || values[index].size()<1) - return Double.NaN; - - return sumValues[index]/values[index].size(); - } - - private void updateSortedValues(int index){ - //naive implementation of insertion sort - for (int i = sortedValues[index].size(); i < values[index].size(); i++) { - double v = values[index].get(i); - int insertIndex = 0; - while(!sortedValues[index].isEmpty() && insertIndex < sortedValues[index].size() && v > sortedValues[index].get(insertIndex)) - insertIndex++; - sortedValues[index].add(insertIndex,v); - } -// for (int i = 0; i < sortedValues[index].size(); i++) { -// System.out.print(sortedValues[index].get(i)+" "); -// } -// System.out.println(); - } - - public void clean(int index){ - sortedValues[index].clear(); - } - - public double getMedian(int index){ - updateSortedValues(index); - int size = sortedValues[index].size(); - - if(size > 0){ - if(size%2 == 1) - return sortedValues[index].get((int)(size/2)); - else - return (sortedValues[index].get((size-1)/2)+sortedValues[index].get((size-1)/2+1))/2.0; - } - return Double.NaN; + values = (ArrayList<Double>[]) new ArrayList[numMeasures]; + sortedValues = (ArrayList<Double>[]) new ArrayList[numMeasures]; + maxValue = new double[numMeasures]; + minValue = new double[numMeasures]; + sumValues = new double[numMeasures]; + corrupted = new boolean[numMeasures]; + enabled = getDefaultEnabled(); + time = 0; + events = new ArrayList<String>(); + + for (int i = 0; i < numMeasures; i++) { + values[i] = new ArrayList<Double>(); + sortedValues[i] = new ArrayList<Double>(); + maxValue[i] = Double.MIN_VALUE; + minValue[i] = Double.MAX_VALUE; + corrupted[i] = false; + sumValues[i] = 0.0; } - public double getLowerQuartile(int index){ - updateSortedValues(index); - int size = sortedValues[index].size(); - if(size > 11){ - return sortedValues[index].get(Math.round(size*0.25f)); - } - return Double.NaN; - } - - public double getUpperQuartile(int index){ - updateSortedValues(index); - int size = sortedValues[index].size(); - if(size > 11){ - return sortedValues[index].get(Math.round(size*0.75f-1)); - } - return Double.NaN; - } - - - public int getNumberOfValues(int index){ - return values[index].size(); - } - - public double getValue(int index, int i){ - if(i>=values[index].size()) return Double.NaN; - return values[index].get(i); - } - - public ArrayList<Double> getAllValues(int index){ - return values[index]; - } - - public void setEnabled(int index, boolean value){ - enabled[index] = value; - } - - public boolean isEnabled(int index){ - return enabled[index]; - } - - public double getMeanRunningTime(){ - if(values[0].size()!=0) - return (time/10e5/values[0].size()); - else - return 0; - } - - protected boolean[] getDefaultEnabled(){ - boolean[] defaults = new boolean[numMeasures]; - for (int i = 0; i < defaults.length; i++) { - defaults[i] = true; - } - return defaults; - } - - protected abstract void evaluateClustering(Clustering clustering, Clustering trueClustering, ArrayList<DataPoint> points) throws Exception; - - /* - * Evaluate Clustering - * - * return Time in milliseconds - */ - public double evaluateClusteringPerformance(Clustering clustering, Clustering trueClustering, ArrayList<DataPoint> points) throws Exception{ - long start = System.nanoTime(); - evaluateClustering(clustering, trueClustering, points); - long duration = System.nanoTime()-start; - time+=duration; - duration/=10e5; - return duration; - } - - public void getDescription(StringBuilder sb, int indent) { + } + + protected abstract String[] getNames(); + public void addValue(int index, double value) { + if (Double.isNaN(value)) { + if (debug) + System.out.println("NaN for " + names[index]); + corrupted[index] = true; + } + if (value < 0) { + if (debug) + System.out.println("Negative value for " + names[index]); } - public void addEventType(String type){ - events.add(type); - } - public String getEventType(int index){ - if(index < events.size()) - return events.get(index); - else - return null; - } + values[index].add(value); + sumValues[index] += value; + if (value < minValue[index]) + minValue[index] = value; + if (value > maxValue[index]) + maxValue[index] = value; + } + + protected void addValue(String name, double value) { + if (map.containsKey(name)) { + addValue(map.get(name), value); + } + else { + System.out.println(name + " is not a valid measure key, no value added"); + } + } + + // add an empty entry e.g. if evaluation crashed internally + public void addEmptyValue(int index) { + values[index].add(Double.NaN); + corrupted[index] = true; + } + + public int getNumMeasures() { + return numMeasures; + } + + public String getName(int index) { + return names[index]; + } + + public double getMaxValue(int index) { + return maxValue[index]; + } + + public double getMinValue(int index) { + return minValue[index]; + } + + public double getLastValue(int index) { + if (values[index].size() < 1) + return Double.NaN; + return values[index].get(values[index].size() - 1); + } + + public double getMean(int index) { + if (corrupted[index] || values[index].size() < 1) + return Double.NaN; + + return sumValues[index] / values[index].size(); + } + + private void updateSortedValues(int index) { + // naive implementation of insertion sort + for (int i = sortedValues[index].size(); i < values[index].size(); i++) { + double v = values[index].get(i); + int insertIndex = 0; + while (!sortedValues[index].isEmpty() && insertIndex < sortedValues[index].size() + && v > sortedValues[index].get(insertIndex)) + insertIndex++; + sortedValues[index].add(insertIndex, v); + } + // for (int i = 0; i < sortedValues[index].size(); i++) { + // System.out.print(sortedValues[index].get(i)+" "); + // } + // System.out.println(); + } + + public void clean(int index) { + sortedValues[index].clear(); + } + + public double getMedian(int index) { + updateSortedValues(index); + int size = sortedValues[index].size(); + + if (size > 0) { + if (size % 2 == 1) + return sortedValues[index].get((int) (size / 2)); + else + return (sortedValues[index].get((size - 1) / 2) + sortedValues[index].get((size - 1) / 2 + 1)) / 2.0; + } + return Double.NaN; + } + + public double getLowerQuartile(int index) { + updateSortedValues(index); + int size = sortedValues[index].size(); + if (size > 11) { + return sortedValues[index].get(Math.round(size * 0.25f)); + } + return Double.NaN; + } + + public double getUpperQuartile(int index) { + updateSortedValues(index); + int size = sortedValues[index].size(); + if (size > 11) { + return sortedValues[index].get(Math.round(size * 0.75f - 1)); + } + return Double.NaN; + } + + public int getNumberOfValues(int index) { + return values[index].size(); + } + + public double getValue(int index, int i) { + if (i >= values[index].size()) + return Double.NaN; + return values[index].get(i); + } + + public ArrayList<Double> getAllValues(int index) { + return values[index]; + } + + public void setEnabled(int index, boolean value) { + enabled[index] = value; + } + + public boolean isEnabled(int index) { + return enabled[index]; + } + + public double getMeanRunningTime() { + if (values[0].size() != 0) + return (time / 10e5 / values[0].size()); + else + return 0; + } + + protected boolean[] getDefaultEnabled() { + boolean[] defaults = new boolean[numMeasures]; + for (int i = 0; i < defaults.length; i++) { + defaults[i] = true; + } + return defaults; + } + + protected abstract void evaluateClustering(Clustering clustering, Clustering trueClustering, + ArrayList<DataPoint> points) throws Exception; + + /* + * Evaluate Clustering + * + * return Time in milliseconds + */ + public double evaluateClusteringPerformance(Clustering clustering, Clustering trueClustering, + ArrayList<DataPoint> points) throws Exception { + long start = System.nanoTime(); + evaluateClustering(clustering, trueClustering, points); + long duration = System.nanoTime() - start; + time += duration; + duration /= 10e5; + return duration; + } + + public void getDescription(StringBuilder sb, int indent) { + + } + + public void addEventType(String type) { + events.add(type); + } + + public String getEventType(int index) { + if (index < events.size()) + return events.get(index); + else + return null; + } } - http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MembershipMatrix.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MembershipMatrix.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MembershipMatrix.java index 14227ad..c14c145 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MembershipMatrix.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/evaluation/MembershipMatrix.java @@ -1,4 +1,3 @@ - package com.yahoo.labs.samoa.moa.evaluation; /* @@ -26,128 +25,124 @@ import java.util.HashMap; import com.yahoo.labs.samoa.moa.cluster.Clustering; import com.yahoo.labs.samoa.moa.core.DataPoint; - public class MembershipMatrix { - HashMap<Integer, Integer> classmap; - int cluster_class_weights[][]; - int cluster_sums[]; - int class_sums[]; - int total_entries; - int class_distribution[]; - int total_class_entries; - int initalBuildTimestamp = -1; - - public MembershipMatrix(Clustering foundClustering, ArrayList<DataPoint> points) { - classmap = Clustering.classValues(points); -// int lastID = classmap.size()-1; -// classmap.put(-1, lastID); - int numClasses = classmap.size(); - int numCluster = foundClustering.size()+1; - - cluster_class_weights = new int[numCluster][numClasses]; - class_distribution = new int[numClasses]; - cluster_sums = new int[numCluster]; - class_sums = new int[numClasses]; - total_entries = 0; - total_class_entries = points.size(); - for (int p = 0; p < points.size(); p++) { - int worklabel = classmap.get((int)points.get(p).classValue()); - //real class distribution - class_distribution[worklabel]++; - boolean covered = false; - for (int c = 0; c < numCluster-1; c++) { - double prob = foundClustering.get(c).getInclusionProbability(points.get(p)); - if(prob >= 1){ - cluster_class_weights[c][worklabel]++; - class_sums[worklabel]++; - cluster_sums[c]++; - total_entries++; - covered = true; - } - } - if(!covered){ - cluster_class_weights[numCluster-1][worklabel]++; - class_sums[worklabel]++; - cluster_sums[numCluster-1]++; - total_entries++; - } - + HashMap<Integer, Integer> classmap; + int cluster_class_weights[][]; + int cluster_sums[]; + int class_sums[]; + int total_entries; + int class_distribution[]; + int total_class_entries; + int initalBuildTimestamp = -1; + + public MembershipMatrix(Clustering foundClustering, ArrayList<DataPoint> points) { + classmap = Clustering.classValues(points); + // int lastID = classmap.size()-1; + // classmap.put(-1, lastID); + int numClasses = classmap.size(); + int numCluster = foundClustering.size() + 1; + + cluster_class_weights = new int[numCluster][numClasses]; + class_distribution = new int[numClasses]; + cluster_sums = new int[numCluster]; + class_sums = new int[numClasses]; + total_entries = 0; + total_class_entries = points.size(); + for (int p = 0; p < points.size(); p++) { + int worklabel = classmap.get((int) points.get(p).classValue()); + // real class distribution + class_distribution[worklabel]++; + boolean covered = false; + for (int c = 0; c < numCluster - 1; c++) { + double prob = foundClustering.get(c).getInclusionProbability(points.get(p)); + if (prob >= 1) { + cluster_class_weights[c][worklabel]++; + class_sums[worklabel]++; + cluster_sums[c]++; + total_entries++; + covered = true; } - - initalBuildTimestamp = points.get(0).getTimestamp(); - } - - public int getClusterClassWeight(int i, int j){ - return cluster_class_weights[i][j]; - } - - public int getClusterSum(int i){ - return cluster_sums[i]; - } - - public int getClassSum(int j){ - return class_sums[j]; - } - - public int getClassDistribution(int j){ - return class_distribution[j]; - } - - public int getClusterClassWeightByLabel(int cluster, int classLabel){ - return cluster_class_weights[cluster][classmap.get(classLabel)]; - } - - public int getClassSumByLabel(int classLabel){ - return class_sums[classmap.get(classLabel)]; - } + } + if (!covered) { + cluster_class_weights[numCluster - 1][worklabel]++; + class_sums[worklabel]++; + cluster_sums[numCluster - 1]++; + total_entries++; + } - public int getClassDistributionByLabel(int classLabel){ - return class_distribution[classmap.get(classLabel)]; } - public int getTotalEntries(){ - return total_entries; + initalBuildTimestamp = points.get(0).getTimestamp(); + } + + public int getClusterClassWeight(int i, int j) { + return cluster_class_weights[i][j]; + } + + public int getClusterSum(int i) { + return cluster_sums[i]; + } + + public int getClassSum(int j) { + return class_sums[j]; + } + + public int getClassDistribution(int j) { + return class_distribution[j]; + } + + public int getClusterClassWeightByLabel(int cluster, int classLabel) { + return cluster_class_weights[cluster][classmap.get(classLabel)]; + } + + public int getClassSumByLabel(int classLabel) { + return class_sums[classmap.get(classLabel)]; + } + + public int getClassDistributionByLabel(int classLabel) { + return class_distribution[classmap.get(classLabel)]; + } + + public int getTotalEntries() { + return total_entries; + } + + public int getNumClasses() { + return classmap.size(); + } + + public boolean hasNoiseClass() { + return classmap.containsKey(-1); + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("Membership Matrix\n"); + for (int i = 0; i < cluster_class_weights.length; i++) { + for (int j = 0; j < cluster_class_weights[i].length; j++) { + sb.append(cluster_class_weights[i][j] + "\t "); + } + sb.append("| " + cluster_sums[i] + "\n"); } - - public int getNumClasses(){ - return classmap.size(); + // sb.append("-----------\n"); + for (int i = 0; i < class_sums.length; i++) { + sb.append(class_sums[i] + "\t "); } + sb.append("| " + total_entries + "\n"); - public boolean hasNoiseClass(){ - return classmap.containsKey(-1); + sb.append("Real class distribution \n"); + for (int i = 0; i < class_distribution.length; i++) { + sb.append(class_distribution[i] + "\t "); } + sb.append("| " + total_class_entries + "\n"); - @Override - public String toString() { - StringBuffer sb = new StringBuffer(); - sb.append("Membership Matrix\n"); - for (int i = 0; i < cluster_class_weights.length; i++) { - for (int j = 0; j < cluster_class_weights[i].length; j++) { - sb.append(cluster_class_weights[i][j]+"\t "); - } - sb.append("| "+cluster_sums[i]+"\n"); - } - //sb.append("-----------\n"); - for (int i = 0; i < class_sums.length; i++) { - sb.append(class_sums[i]+"\t "); - } - sb.append("| "+total_entries+"\n"); - + return sb.toString(); + } - sb.append("Real class distribution \n"); - for (int i = 0; i < class_distribution.length; i++) { - sb.append(class_distribution[i]+"\t "); - } - sb.append("| "+total_class_entries+"\n"); - - return sb.toString(); - } - - - public int getInitalBuildTimestamp(){ - return initalBuildTimestamp; - } - + public int getInitalBuildTimestamp() { + return initalBuildTimestamp; + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/learners/Learner.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/learners/Learner.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/learners/Learner.java index 15de94b..be63e30 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/learners/Learner.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/learners/Learner.java @@ -27,110 +27,109 @@ import com.yahoo.labs.samoa.moa.core.Measurement; import com.yahoo.labs.samoa.moa.options.OptionHandler; /** - * Learner interface for incremental learning models. - * + * Learner interface for incremental learning models. + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public interface Learner<E extends Example> extends MOAObject, OptionHandler { + /** + * Gets whether this learner needs a random seed. Examples of methods that + * needs a random seed are bagging and boosting. + * + * @return true if the learner needs a random seed. + */ + public boolean isRandomizable(); + + /** + * Sets the seed for random number generation. + * + * @param s + * the seed + */ + public void setRandomSeed(int s); + + /** + * Gets whether training has started. + * + * @return true if training has started + */ + public boolean trainingHasStarted(); + + /** + * Gets the sum of the weights of the instances that have been used by this + * learner during the training in <code>trainOnInstance</code> + * + * @return the weight of the instances that have been used training + */ + public double trainingWeightSeenByModel(); + + /** + * Resets this learner. It must be similar to starting a new learner from + * scratch. + * + */ + public void resetLearning(); + + /** + * Trains this learner incrementally using the given example. + * + * @param inst + * the instance to be used for training + */ + public void trainOnInstance(E example); + + /** + * Predicts the class memberships for a given instance. If an instance is + * unclassified, the returned array elements must be all zero. + * + * @param inst + * the instance to be classified + * @return an array containing the estimated membership probabilities of the + * test instance in each class + */ + public double[] getVotesForInstance(E example); + + /** + * Gets the current measurements of this learner. + * + * @return an array of measurements to be used in evaluation tasks + */ + public Measurement[] getModelMeasurements(); + + /** + * Gets the learners of this ensemble. Returns null if this learner is a + * single learner. + * + * @return an array of the learners of the ensemble + */ + public Learner[] getSublearners(); + + /** + * Gets the model if this learner. + * + * @return the copy of this learner + */ + public MOAObject getModel(); + + /** + * Sets the reference to the header of the data stream. The header of the data + * stream is extended from WEKA <code>Instances</code>. This header is needed + * to know the number of classes and attributes + * + * @param ih + * the reference to the data stream header + */ + public void setModelContext(InstancesHeader ih); + + /** + * Gets the reference to the header of the data stream. The header of the data + * stream is extended from WEKA <code>Instances</code>. This header is needed + * to know the number of classes and attributes + * + * @return the reference to the data stream header + */ + public InstancesHeader getModelContext(); - /** - * Gets whether this learner needs a random seed. - * Examples of methods that needs a random seed are bagging and boosting. - * - * @return true if the learner needs a random seed. - */ - public boolean isRandomizable(); - - /** - * Sets the seed for random number generation. - * - * @param s the seed - */ - public void setRandomSeed(int s); - - /** - * Gets whether training has started. - * - * @return true if training has started - */ - public boolean trainingHasStarted(); - - /** - * Gets the sum of the weights of the instances that have been used - * by this learner during the training in <code>trainOnInstance</code> - * - * @return the weight of the instances that have been used training - */ - public double trainingWeightSeenByModel(); - - /** - * Resets this learner. It must be similar to - * starting a new learner from scratch. - * - */ - public void resetLearning(); - - /** - * Trains this learner incrementally using the given example. - * - * @param inst the instance to be used for training - */ - public void trainOnInstance(E example); - - /** - * Predicts the class memberships for a given instance. If - * an instance is unclassified, the returned array elements - * must be all zero. - * - * @param inst the instance to be classified - * @return an array containing the estimated membership - * probabilities of the test instance in each class - */ - public double[] getVotesForInstance(E example); - - /** - * Gets the current measurements of this learner. - * - * @return an array of measurements to be used in evaluation tasks - */ - public Measurement[] getModelMeasurements(); - - /** - * Gets the learners of this ensemble. - * Returns null if this learner is a single learner. - * - * @return an array of the learners of the ensemble - */ - public Learner[] getSublearners(); - - /** - * Gets the model if this learner. - * - * @return the copy of this learner - */ - public MOAObject getModel(); - - /** - * Sets the reference to the header of the data stream. - * The header of the data stream is extended from WEKA <code>Instances</code>. - * This header is needed to know the number of classes and attributes - * - * @param ih the reference to the data stream header - */ - public void setModelContext(InstancesHeader ih); - - /** - * Gets the reference to the header of the data stream. - * The header of the data stream is extended from WEKA <code>Instances</code>. - * This header is needed to know the number of classes and attributes - * - * @return the reference to the data stream header - */ - public InstancesHeader getModelContext(); - } - - - http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java index 1dd66c1..0685855 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java @@ -29,208 +29,226 @@ import com.yahoo.labs.samoa.moa.tasks.TaskMonitor; /** * Abstract class option. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision$ */ public abstract class AbstractClassOption extends AbstractOption { - private static final long serialVersionUID = 1L; - - /** The prefix text to use to indicate file. */ - public static final String FILE_PREFIX_STRING = "file:"; - - /** The prefix text to use to indicate inmem. */ - public static final String INMEM_PREFIX_STRING = "inmem:"; - - /** The current object */ - protected Object currentValue; - - /** The class type */ - protected Class<?> requiredType; - - /** The default command line interface text. */ - protected String defaultCLIString; - - /** The null text. */ - protected String nullString; - - /** - * Creates a new instance of an abstract option given its class name, - * command line interface text, its purpose, its class type and its default - * command line interface text. - * - * @param name the name of this option - * @param cliChar the command line interface text - * @param purpose the text describing the purpose of this option - * @param requiredType the class type - * @param defaultCLIString the default command line interface text - */ - public AbstractClassOption(String name, char cliChar, String purpose, - Class<?> requiredType, String defaultCLIString) { - this(name, cliChar, purpose, requiredType, defaultCLIString, null); - } - - /** - * Creates a new instance of an abstract option given its class name, - * command line interface text, its purpose, its class type, default - * command line interface text, and its null text. - * - * @param name the name of this option - * @param cliChar the command line interface text - * @param purpose the text describing the purpose of this option - * @param requiredType the class type - * @param defaultCLIString the default command line interface text - * @param nullString the null text - */ - public AbstractClassOption(String name, char cliChar, String purpose, - Class<?> requiredType, String defaultCLIString, String nullString) { - super(name, cliChar, purpose); - this.requiredType = requiredType; - this.defaultCLIString = defaultCLIString; - this.nullString = nullString; - resetToDefault(); - } - - /** - * Sets current object. - * - * @param obj the object to set as current. - */ - public void setCurrentObject(Object obj) { - if (((obj == null) && (this.nullString != null)) - || this.requiredType.isInstance(obj) - || (obj instanceof String) - || (obj instanceof File) - || ((obj instanceof Task) && this.requiredType.isAssignableFrom(((Task) obj).getTaskResultType()))) { - this.currentValue = obj; - } else { - throw new IllegalArgumentException("Object not of required type."); - } - } - - /** - * Returns the current object. - * - * @return the current object - */ - public Object getPreMaterializedObject() { - return this.currentValue; - } - - /** - * Gets the class type of this option. - * - * @return the class type of this option - */ - public Class<?> getRequiredType() { - return this.requiredType; + private static final long serialVersionUID = 1L; + + /** The prefix text to use to indicate file. */ + public static final String FILE_PREFIX_STRING = "file:"; + + /** The prefix text to use to indicate inmem. */ + public static final String INMEM_PREFIX_STRING = "inmem:"; + + /** The current object */ + protected Object currentValue; + + /** The class type */ + protected Class<?> requiredType; + + /** The default command line interface text. */ + protected String defaultCLIString; + + /** The null text. */ + protected String nullString; + + /** + * Creates a new instance of an abstract option given its class name, command + * line interface text, its purpose, its class type and its default command + * line interface text. + * + * @param name + * the name of this option + * @param cliChar + * the command line interface text + * @param purpose + * the text describing the purpose of this option + * @param requiredType + * the class type + * @param defaultCLIString + * the default command line interface text + */ + public AbstractClassOption(String name, char cliChar, String purpose, + Class<?> requiredType, String defaultCLIString) { + this(name, cliChar, purpose, requiredType, defaultCLIString, null); + } + + /** + * Creates a new instance of an abstract option given its class name, command + * line interface text, its purpose, its class type, default command line + * interface text, and its null text. + * + * @param name + * the name of this option + * @param cliChar + * the command line interface text + * @param purpose + * the text describing the purpose of this option + * @param requiredType + * the class type + * @param defaultCLIString + * the default command line interface text + * @param nullString + * the null text + */ + public AbstractClassOption(String name, char cliChar, String purpose, + Class<?> requiredType, String defaultCLIString, String nullString) { + super(name, cliChar, purpose); + this.requiredType = requiredType; + this.defaultCLIString = defaultCLIString; + this.nullString = nullString; + resetToDefault(); + } + + /** + * Sets current object. + * + * @param obj + * the object to set as current. + */ + public void setCurrentObject(Object obj) { + if (((obj == null) && (this.nullString != null)) + || this.requiredType.isInstance(obj) + || (obj instanceof String) + || (obj instanceof File) + || ((obj instanceof Task) && this.requiredType.isAssignableFrom(((Task) obj).getTaskResultType()))) { + this.currentValue = obj; + } else { + throw new IllegalArgumentException("Object not of required type."); } - - /** - * Gets the null string of this option. - * - * @return the null string of this option - */ - public String getNullString() { - return this.nullString; - } - - /** - * Gets a materialized object of this option. - * - * @param monitor the task monitor to use - * @param repository the object repository to use - * @return the materialized object - */ - public Object materializeObject(TaskMonitor monitor, - ObjectRepository repository) { - if ((this.currentValue == null) - || this.requiredType.isInstance(this.currentValue)) { - return this.currentValue; - } else if (this.currentValue instanceof String) { - if (repository != null) { - Object inmemObj = repository.getObjectNamed((String) this.currentValue); - if (inmemObj == null) { - throw new RuntimeException("No object named " - + this.currentValue + " found in repository."); - } - return inmemObj; - } - throw new RuntimeException("No object repository available."); - } else if (this.currentValue instanceof Task) { - Task task = (Task) this.currentValue; - Object result = task.doTask(monitor, repository); - return result; - } else if (this.currentValue instanceof File) { - File inputFile = (File) this.currentValue; - Object result = null; - try { - result = SerializeUtils.readFromFile(inputFile); - } catch (Exception ex) { - throw new RuntimeException("Problem loading " - + this.requiredType.getName() + " object from file '" - + inputFile.getName() + "':\n" + ex.getMessage(), ex); - } - return result; - } else { - throw new RuntimeException( - "Could not materialize object of required type " - + this.requiredType.getName() + ", found " - + this.currentValue.getClass().getName() - + " instead."); + } + + /** + * Returns the current object. + * + * @return the current object + */ + public Object getPreMaterializedObject() { + return this.currentValue; + } + + /** + * Gets the class type of this option. + * + * @return the class type of this option + */ + public Class<?> getRequiredType() { + return this.requiredType; + } + + /** + * Gets the null string of this option. + * + * @return the null string of this option + */ + public String getNullString() { + return this.nullString; + } + + /** + * Gets a materialized object of this option. + * + * @param monitor + * the task monitor to use + * @param repository + * the object repository to use + * @return the materialized object + */ + public Object materializeObject(TaskMonitor monitor, + ObjectRepository repository) { + if ((this.currentValue == null) + || this.requiredType.isInstance(this.currentValue)) { + return this.currentValue; + } else if (this.currentValue instanceof String) { + if (repository != null) { + Object inmemObj = repository.getObjectNamed((String) this.currentValue); + if (inmemObj == null) { + throw new RuntimeException("No object named " + + this.currentValue + " found in repository."); } + return inmemObj; + } + throw new RuntimeException("No object repository available."); + } else if (this.currentValue instanceof Task) { + Task task = (Task) this.currentValue; + Object result = task.doTask(monitor, repository); + return result; + } else if (this.currentValue instanceof File) { + File inputFile = (File) this.currentValue; + Object result = null; + try { + result = SerializeUtils.readFromFile(inputFile); + } catch (Exception ex) { + throw new RuntimeException("Problem loading " + + this.requiredType.getName() + " object from file '" + + inputFile.getName() + "':\n" + ex.getMessage(), ex); + } + return result; + } else { + throw new RuntimeException( + "Could not materialize object of required type " + + this.requiredType.getName() + ", found " + + this.currentValue.getClass().getName() + + " instead."); } - - @Override - public String getDefaultCLIString() { - return this.defaultCLIString; - } - - /** - * Gets the command line interface text of the class. - * - * @param aClass the class - * @param requiredType the class type - * @return the command line interface text of the class - */ - public static String classToCLIString(Class<?> aClass, Class<?> requiredType) { - String className = aClass.getName(); - String packageName = requiredType.getPackage().getName(); - if (className.startsWith(packageName)) { - // cut off package name - className = className.substring(packageName.length() + 1, className.length()); - } else if (Task.class.isAssignableFrom(aClass)) { - packageName = Task.class.getPackage().getName(); - if (className.startsWith(packageName)) { - // cut off task package name - className = className.substring(packageName.length() + 1, - className.length()); - } - } - return className; + } + + @Override + public String getDefaultCLIString() { + return this.defaultCLIString; + } + + /** + * Gets the command line interface text of the class. + * + * @param aClass + * the class + * @param requiredType + * the class type + * @return the command line interface text of the class + */ + public static String classToCLIString(Class<?> aClass, Class<?> requiredType) { + String className = aClass.getName(); + String packageName = requiredType.getPackage().getName(); + if (className.startsWith(packageName)) { + // cut off package name + className = className.substring(packageName.length() + 1, className.length()); + } else if (Task.class.isAssignableFrom(aClass)) { + packageName = Task.class.getPackage().getName(); + if (className.startsWith(packageName)) { + // cut off task package name + className = className.substring(packageName.length() + 1, + className.length()); + } } - - @Override - public abstract String getValueAsCLIString(); - - @Override - public abstract void setValueViaCLIString(String s); - - //@Override - //public abstract JComponent getEditComponent(); - - /** - * Gets the class name without its package name prefix. - * - * @param className the name of the class - * @param expectedType the type of the class - * @return the class name without its package name prefix - */ - public static String stripPackagePrefix(String className, Class<?> expectedType) { - if (className.startsWith(expectedType.getPackage().getName())) { - return className.substring(expectedType.getPackage().getName().length() + 1); - } - return className; + return className; + } + + @Override + public abstract String getValueAsCLIString(); + + @Override + public abstract void setValueViaCLIString(String s); + + // @Override + // public abstract JComponent getEditComponent(); + + /** + * Gets the class name without its package name prefix. + * + * @param className + * the name of the class + * @param expectedType + * the type of the class + * @return the class name without its package name prefix + */ + public static String stripPackagePrefix(String className, Class<?> expectedType) { + if (className.startsWith(expectedType.getPackage().getName())) { + return className.substring(expectedType.getPackage().getName().length() + 1); } + return className; + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java index 22cc1f0..546e678 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java @@ -27,167 +27,142 @@ import com.yahoo.labs.samoa.moa.tasks.NullMonitor; import com.yahoo.labs.samoa.moa.tasks.TaskMonitor; /** - * Abstract Option Handler. All classes that have options in - * MOA extend this class. - * + * Abstract Option Handler. All classes that have options in MOA extend this + * class. + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public abstract class AbstractOptionHandler extends AbstractMOAObject implements - OptionHandler { - - private static final long serialVersionUID = 1L; + OptionHandler { - /** Options to handle */ - //protected Options options; + private static final long serialVersionUID = 1L; - /** Dictionary with option texts and objects */ - //protected Map<String, Object> classOptionNamesToPreparedObjects; - - @Override - public String getPurposeString() { - return "Anonymous object: purpose undocumented."; - } + /** Options to handle */ + // protected Options options; - @Override - public Options getOptions() { - /*if (this.options == null) { - this.options = new Options(); - if (this.config== null) { - this.config = new OptionsHandler(this, ""); - this.config.prepareForUse(); - } - Option[] myOptions = this.config.discoverOptionsViaReflection(); - for (Option option : myOptions) { - this.options.addOption(option); - } - } - return this.options;*/ - if ( this.config == null){ - this.config = new OptionsHandler(this, ""); - //this.config.prepareForUse(monitor, repository); - } - return this.config.getOptions(); - } - - @Override - public void prepareForUse() { - prepareForUse(new NullMonitor(), null); - } + /** Dictionary with option texts and objects */ + // protected Map<String, Object> classOptionNamesToPreparedObjects; - protected OptionsHandler config; - - @Override - public void prepareForUse(TaskMonitor monitor, ObjectRepository repository) { - //prepareClassOptions(monitor, repository); - if ( this.config == null){ - this.config = new OptionsHandler(this, ""); - this.config.prepareForUse(monitor, repository); - } - prepareForUseImpl(monitor, repository); - } + @Override + public String getPurposeString() { + return "Anonymous object: purpose undocumented."; + } - /** - * This method describes the implementation of how to prepare this object for use. - * All classes that extends this class have to implement <code>prepareForUseImpl</code> - * and not <code>prepareForUse</code> since - * <code>prepareForUse</code> calls <code>prepareForUseImpl</code>. - * - * @param monitor the TaskMonitor to use - * @param repository the ObjectRepository to use + @Override + public Options getOptions() { + /* + * if (this.options == null) { this.options = new Options(); if + * (this.config== null) { this.config = new OptionsHandler(this, ""); + * this.config.prepareForUse(); } Option[] myOptions = + * this.config.discoverOptionsViaReflection(); for (Option option : + * myOptions) { this.options.addOption(option); } } return this.options; */ - protected abstract void prepareForUseImpl(TaskMonitor monitor, - ObjectRepository repository); - - @Override - public String getCLICreationString(Class<?> expectedType) { - return ClassOption.stripPackagePrefix(this.getClass().getName(), - expectedType) - + " " + getOptions().getAsCLIString(); - } - - @Override - public OptionHandler copy() { - return (OptionHandler) super.copy(); + if (this.config == null) { + this.config = new OptionsHandler(this, ""); + // this.config.prepareForUse(monitor, repository); } - - /** - * Gets the options of this class via reflection. - * - * @return an array of options - */ - /* protected Option[] discoverOptionsViaReflection() { - Class<? extends AbstractOptionHandler> c = this.getClass(); - Field[] fields = c.getFields(); - List<Option> optList = new LinkedList<Option>(); - for (Field field : fields) { - String fName = field.getName(); - Class<?> fType = field.getType(); - if (fType.getName().endsWith("Option")) { - if (Option.class.isAssignableFrom(fType)) { - Option oVal = null; - try { - field.setAccessible(true); - oVal = (Option) field.get(this); - } catch (IllegalAccessException ignored) { - // cannot access this field - } - if (oVal != null) { - optList.add(oVal); - } - } - } - } - return optList.toArray(new Option[optList.size()]); - }*/ - - /** - * Prepares the options of this class. - * - * @param monitor the TaskMonitor to use - * @param repository the ObjectRepository to use - */ - protected void prepareClassOptions(TaskMonitor monitor, - ObjectRepository repository) { - this.config.prepareClassOptions(monitor, repository); - }/* - this.classOptionNamesToPreparedObjects = null; - Option[] optionArray = getOptions().getOptionArray(); - for (Option option : optionArray) { - if (option instanceof ClassOption) { - ClassOption classOption = (ClassOption) option; - monitor.setCurrentActivity("Materializing option " - + classOption.getName() + "...", -1.0); - Object optionObj = classOption.materializeObject(monitor, - repository); - if (monitor.taskShouldAbort()) { - return; - } - if (optionObj instanceof OptionHandler) { - monitor.setCurrentActivity("Preparing option " - + classOption.getName() + "...", -1.0); - ((OptionHandler) optionObj).prepareForUse(monitor, - repository); - if (monitor.taskShouldAbort()) { - return; - } - } - if (this.classOptionNamesToPreparedObjects == null) { - this.classOptionNamesToPreparedObjects = new HashMap<String, Object>(); - } - this.classOptionNamesToPreparedObjects.put(option.getName(), - optionObj); - } - } - }*/ - - /** - * Gets a prepared option of this class. - * - * @param opt the class option to get - * @return an option stored in the dictionary - */ - protected Object getPreparedClassOption(ClassOption opt) { - return this.config.getPreparedClassOption(opt); + return this.config.getOptions(); + } + + @Override + public void prepareForUse() { + prepareForUse(new NullMonitor(), null); + } + + protected OptionsHandler config; + + @Override + public void prepareForUse(TaskMonitor monitor, ObjectRepository repository) { + // prepareClassOptions(monitor, repository); + if (this.config == null) { + this.config = new OptionsHandler(this, ""); + this.config.prepareForUse(monitor, repository); } + prepareForUseImpl(monitor, repository); + } + + /** + * This method describes the implementation of how to prepare this object for + * use. All classes that extends this class have to implement + * <code>prepareForUseImpl</code> and not <code>prepareForUse</code> since + * <code>prepareForUse</code> calls <code>prepareForUseImpl</code>. + * + * @param monitor + * the TaskMonitor to use + * @param repository + * the ObjectRepository to use + */ + protected abstract void prepareForUseImpl(TaskMonitor monitor, + ObjectRepository repository); + + @Override + public String getCLICreationString(Class<?> expectedType) { + return ClassOption.stripPackagePrefix(this.getClass().getName(), + expectedType) + + " " + getOptions().getAsCLIString(); + } + + @Override + public OptionHandler copy() { + return (OptionHandler) super.copy(); + } + + /** + * Gets the options of this class via reflection. + * + * @return an array of options + */ + /* + * protected Option[] discoverOptionsViaReflection() { Class<? extends + * AbstractOptionHandler> c = this.getClass(); Field[] fields = c.getFields(); + * List<Option> optList = new LinkedList<Option>(); for (Field field : fields) + * { String fName = field.getName(); Class<?> fType = field.getType(); if + * (fType.getName().endsWith("Option")) { if + * (Option.class.isAssignableFrom(fType)) { Option oVal = null; try { + * field.setAccessible(true); oVal = (Option) field.get(this); } catch + * (IllegalAccessException ignored) { // cannot access this field } if (oVal + * != null) { optList.add(oVal); } } } } return optList.toArray(new + * Option[optList.size()]); } + */ + + /** + * Prepares the options of this class. + * + * @param monitor + * the TaskMonitor to use + * @param repository + * the ObjectRepository to use + */ + protected void prepareClassOptions(TaskMonitor monitor, + ObjectRepository repository) { + this.config.prepareClassOptions(monitor, repository); + }/* + * this.classOptionNamesToPreparedObjects = null; Option[] optionArray = + * getOptions().getOptionArray(); for (Option option : optionArray) { if + * (option instanceof ClassOption) { ClassOption classOption = (ClassOption) + * option; monitor.setCurrentActivity("Materializing option " + + * classOption.getName() + "...", -1.0); Object optionObj = + * classOption.materializeObject(monitor, repository); if + * (monitor.taskShouldAbort()) { return; } if (optionObj instanceof + * OptionHandler) { monitor.setCurrentActivity("Preparing option " + + * classOption.getName() + "...", -1.0); ((OptionHandler) + * optionObj).prepareForUse(monitor, repository); if + * (monitor.taskShouldAbort()) { return; } } if + * (this.classOptionNamesToPreparedObjects == null) { + * this.classOptionNamesToPreparedObjects = new HashMap<String, Object>(); } + * this.classOptionNamesToPreparedObjects.put(option.getName(), optionObj); } + * } } + */ + + /** + * Gets a prepared option of this class. + * + * @param opt + * the class option to get + * @return an option stored in the dictionary + */ + protected Object getPreparedClassOption(ClassOption opt) { + return this.config.getPreparedClassOption(opt); + } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java index 64eff95..3e8e62c 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java @@ -28,148 +28,149 @@ import com.yahoo.labs.samoa.moa.tasks.Task; /** * Class option. - * + * * @author Richard Kirkby ([email protected]) * @version $Revision: 7 $ */ public class ClassOption extends AbstractClassOption { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public ClassOption(String name, char cliChar, String purpose, - Class<?> requiredType, String defaultCLIString) { - super(name, cliChar, purpose, requiredType, defaultCLIString); - } + public ClassOption(String name, char cliChar, String purpose, + Class<?> requiredType, String defaultCLIString) { + super(name, cliChar, purpose, requiredType, defaultCLIString); + } - public ClassOption(String name, char cliChar, String purpose, - Class<?> requiredType, String defaultCLIString, String nullString) { - super(name, cliChar, purpose, requiredType, defaultCLIString, nullString); - } + public ClassOption(String name, char cliChar, String purpose, + Class<?> requiredType, String defaultCLIString, String nullString) { + super(name, cliChar, purpose, requiredType, defaultCLIString, nullString); + } - @Override - public String getValueAsCLIString() { - if ((this.currentValue == null) && (this.nullString != null)) { - return this.nullString; - } - return objectToCLIString(this.currentValue, this.requiredType); + @Override + public String getValueAsCLIString() { + if ((this.currentValue == null) && (this.nullString != null)) { + return this.nullString; } + return objectToCLIString(this.currentValue, this.requiredType); + } - @Override - public void setValueViaCLIString(String s) { - if ((this.nullString != null) - && ((s == null) || (s.length() == 0) || s.equals(this.nullString))) { - this.currentValue = null; - } else { - try { - this.currentValue = cliStringToObject(s, this.requiredType, - null); - } catch (Exception e) { - throw new IllegalArgumentException("Problems with option: " + getName(), e); - } - } + @Override + public void setValueViaCLIString(String s) { + if ((this.nullString != null) + && ((s == null) || (s.length() == 0) || s.equals(this.nullString))) { + this.currentValue = null; + } else { + try { + this.currentValue = cliStringToObject(s, this.requiredType, + null); + } catch (Exception e) { + throw new IllegalArgumentException("Problems with option: " + getName(), e); + } } + } - public static String objectToCLIString(Object obj, Class<?> requiredType) { - if (obj == null) { - return ""; - } - if (obj instanceof File) { - return (FILE_PREFIX_STRING + ((File) obj).getPath()); - } - if (obj instanceof String) { - return (INMEM_PREFIX_STRING + obj); - } - String className = classToCLIString(obj.getClass(), requiredType); - if (obj instanceof OptionHandler) { - String subOptions = ((OptionHandler) obj).getOptions().getAsCLIString(); - if (subOptions.length() > 0) { - return (className + " " + subOptions); - } - } - return className; + public static String objectToCLIString(Object obj, Class<?> requiredType) { + if (obj == null) { + return ""; + } + if (obj instanceof File) { + return (FILE_PREFIX_STRING + ((File) obj).getPath()); + } + if (obj instanceof String) { + return (INMEM_PREFIX_STRING + obj); } + String className = classToCLIString(obj.getClass(), requiredType); + if (obj instanceof OptionHandler) { + String subOptions = ((OptionHandler) obj).getOptions().getAsCLIString(); + if (subOptions.length() > 0) { + return (className + " " + subOptions); + } + } + return className; + } - public static Object cliStringToObject(String cliString, - Class<?> requiredType, Option[] externalOptions) throws Exception { - if (cliString.startsWith(FILE_PREFIX_STRING)) { - return new File(cliString.substring(FILE_PREFIX_STRING.length())); - } - if (cliString.startsWith(INMEM_PREFIX_STRING)) { - return cliString.substring(INMEM_PREFIX_STRING.length()); - } - cliString = cliString.trim(); - int firstSpaceIndex = cliString.indexOf(' ', 0); - String className; - String classOptions; - if (firstSpaceIndex > 0) { - className = cliString.substring(0, firstSpaceIndex); - classOptions = cliString.substring(firstSpaceIndex + 1, cliString.length()); - classOptions = classOptions.trim(); - } else { - className = cliString; - classOptions = ""; - } - Class<?> classObject; + public static Object cliStringToObject(String cliString, + Class<?> requiredType, Option[] externalOptions) throws Exception { + if (cliString.startsWith(FILE_PREFIX_STRING)) { + return new File(cliString.substring(FILE_PREFIX_STRING.length())); + } + if (cliString.startsWith(INMEM_PREFIX_STRING)) { + return cliString.substring(INMEM_PREFIX_STRING.length()); + } + cliString = cliString.trim(); + int firstSpaceIndex = cliString.indexOf(' ', 0); + String className; + String classOptions; + if (firstSpaceIndex > 0) { + className = cliString.substring(0, firstSpaceIndex); + classOptions = cliString.substring(firstSpaceIndex + 1, cliString.length()); + classOptions = classOptions.trim(); + } else { + className = cliString; + classOptions = ""; + } + Class<?> classObject; + try { + classObject = Class.forName(className); + } catch (Throwable t1) { + try { + // try prepending default package + classObject = Class.forName(requiredType.getPackage().getName() + + "." + className); + } catch (Throwable t2) { try { - classObject = Class.forName(className); - } catch (Throwable t1) { - try { - // try prepending default package - classObject = Class.forName(requiredType.getPackage().getName() - + "." + className); - } catch (Throwable t2) { - try { - // try prepending task package - classObject = Class.forName(Task.class.getPackage().getName() - + "." + className); - } catch (Throwable t3) { - throw new Exception("Class not found: " + className); - } - } + // try prepending task package + classObject = Class.forName(Task.class.getPackage().getName() + + "." + className); + } catch (Throwable t3) { + throw new Exception("Class not found: " + className); } - Object classInstance; - try { - classInstance = classObject.newInstance(); - } catch (Exception ex) { - throw new Exception("Problem creating instance of class: " - + className, ex); + } + } + Object classInstance; + try { + classInstance = classObject.newInstance(); + } catch (Exception ex) { + throw new Exception("Problem creating instance of class: " + + className, ex); + } + if (requiredType.isInstance(classInstance) + || ((classInstance instanceof Task) && requiredType + .isAssignableFrom(((Task) classInstance).getTaskResultType()))) { + Options options = new Options(); + if (externalOptions != null) { + for (Option option : externalOptions) { + options.addOption(option); } - if (requiredType.isInstance(classInstance) - || ((classInstance instanceof Task) && requiredType.isAssignableFrom(((Task) classInstance).getTaskResultType()))) { - Options options = new Options(); - if (externalOptions != null) { - for (Option option : externalOptions) { - options.addOption(option); - } - } - if (classInstance instanceof OptionHandler) { - Option[] objectOptions = ((OptionHandler) classInstance).getOptions().getOptionArray(); - for (Option option : objectOptions) { - options.addOption(option); - } - } - try { - options.setViaCLIString(classOptions); - } catch (Exception ex) { - throw new Exception("Problem with options to '" - + className - + "'." - + "\n\nValid options for " - + className - + ":\n" - + ((OptionHandler) classInstance).getOptions().getHelpString(), ex); - } finally { - options.removeAllOptions(); // clean up listener refs - } - } else { - throw new Exception("Class named '" + className - + "' is not an instance of " + requiredType.getName() + "."); + } + if (classInstance instanceof OptionHandler) { + Option[] objectOptions = ((OptionHandler) classInstance).getOptions().getOptionArray(); + for (Option option : objectOptions) { + options.addOption(option); } - return classInstance; + } + try { + options.setViaCLIString(classOptions); + } catch (Exception ex) { + throw new Exception("Problem with options to '" + + className + + "'." + + "\n\nValid options for " + + className + + ":\n" + + ((OptionHandler) classInstance).getOptions().getHelpString(), ex); + } finally { + options.removeAllOptions(); // clean up listener refs + } + } else { + throw new Exception("Class named '" + className + + "' is not an instance of " + requiredType.getName() + "."); } + return classInstance; + } - //@Override - //public JComponent getEditComponent() { - // return new ClassOptionEditComponent(this); - //} + // @Override + // public JComponent getEditComponent() { + // return new ClassOptionEditComponent(this); + // } } http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/23a35dbe/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java index b88cada..699303b 100644 --- a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java @@ -27,53 +27,55 @@ import com.yahoo.labs.samoa.moa.core.ObjectRepository; import com.yahoo.labs.samoa.moa.tasks.TaskMonitor; /** - * Interface representing an object that handles options or parameters. - * + * Interface representing an object that handles options or parameters. + * * @author Richard Kirkby ([email protected]) - * @version $Revision: 7 $ + * @version $Revision: 7 $ */ public interface OptionHandler extends MOAObject, Configurable { - /** - * Gets the purpose of this object - * - * @return the string with the purpose of this object - */ - public String getPurposeString(); + /** + * Gets the purpose of this object + * + * @return the string with the purpose of this object + */ + public String getPurposeString(); - /** - * Gets the options of this object - * - * @return the options of this object - */ - public Options getOptions(); + /** + * Gets the options of this object + * + * @return the options of this object + */ + public Options getOptions(); - /** - * This method prepares this object for use. - * - */ - public void prepareForUse(); + /** + * This method prepares this object for use. + * + */ + public void prepareForUse(); - /** - * This method prepares this object for use. - * - * @param monitor the TaskMonitor to use - * @param repository the ObjectRepository to use - */ - public void prepareForUse(TaskMonitor monitor, ObjectRepository repository); + /** + * This method prepares this object for use. + * + * @param monitor + * the TaskMonitor to use + * @param repository + * the ObjectRepository to use + */ + public void prepareForUse(TaskMonitor monitor, ObjectRepository repository); - /** - * This method produces a copy of this object. - * - * @return a copy of this object - */ - @Override - public OptionHandler copy(); + /** + * This method produces a copy of this object. + * + * @return a copy of this object + */ + @Override + public OptionHandler copy(); - /** - * Gets the Command Line Interface text to create the object - * - * @return the Command Line Interface text to create the object - */ - public String getCLICreationString(Class<?> expectedType); + /** + * Gets the Command Line Interface text to create the object + * + * @return the Command Line Interface text to create the object + */ + public String getCLICreationString(Class<?> expectedType); }
