RDG1.java

Index Score
weka.datagenerators.classifiers.classification
Weka

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
DECL_COMMENTSComments in declarations
DOC_COMMENTNumber of javadoc comment lines
JAVA0034JAVA0034 Missing braces in if statement
COMMENTSComment lines
LOOPSNumber of loops
SIZESize of the file in bytes
LINESNumber of lines in the source file
RETURNSNumber of return points from functions
JAVA0177JAVA0177 Variable declaration missing initializer
FUNCTIONSNumber of function declarations
LOGICAL_LINESNumber of statements
COMPARISONSNumber of comparison operators
JAVA0233JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
EXITSProcedure exits
CYCLOMATICCyclomatic complexity
ELOCEffective lines of code
INTERFACE_COMPLEXITYInterface complexity
OPERATORSNumber of operators
PROGRAM_LENGTHHalstead program length
LOCLines of code
JAVA0035JAVA0035 Missing braces in for statement
OPERANDSNumber of operands
UNIQUE_OPERANDSNumber of unique operands
JAVA0119JAVA0119 Control variable changed within body of for loop
PROGRAM_VOCABHalstead program vocabulary
EXEC_COMMENTSComments in executable code
BLOCKSNumber of blocks
JAVA0123JAVA0123 Use all three components of for loop
WHITESPACENumber of whitespace lines
JAVA0081JAVA0081 Boolean literal in comparison
JAVA0144JAVA0144 Line exceeds maximum M characters
JAVA0096JAVA0096 Field in nested class hides outer field
JAVA0117JAVA0117 Missing javadoc: method 'method'
UNIQUE_OPERATORSNumber of unique operators
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0160JAVA0160 Method does not throw specified exception
JAVA0126JAVA0126 Method declares unchecked exception in throws
LINE_COMMENTNumber of line comments
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
JAVA0043JAVA0043 Inner class does not use outer class
JAVA0067JAVA0067 Array descriptor on identifier name
PARAMSNumber of formal parameter declarations
JAVA0100JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0145JAVA0145 Tab character used in source file
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* * RDG1.java * Copyright (C) 2000 University of Waikato, Hamilton, New Zealand * */ package weka.datagenerators.classifiers.classification; import weka.core.Attribute; import weka.core.FastVector; import weka.core.Instance; import weka.core.Instances; import weka.core.Option; import weka.core.RevisionHandler; import weka.core.RevisionUtils; import weka.core.Utils; import weka.datagenerators.ClassificationGenerator; import weka.datagenerators.Test; import java.io.Serializable; import java.util.Enumeration; import java.util.Random; import java.util.Vector; /** <!-- globalinfo-start --> * A data generator that produces data randomly by producing a decision list.<br/> * The decision list consists of rules.<br/> * Instances are generated randomly one by one. If decision list fails to classify the current instance, a new rule according to this current instance is generated and added to the decision list.<br/> * <br/> * The option -V switches on voting, which means that at the end of the generation all instances are reclassified to the class value that is supported by the most rules.<br/> * <br/> * This data generator can generate 'boolean' attributes (= nominal with the values {true, false}) and numeric attributes. The rules can be 'A' or 'NOT A' for boolean values and 'B &lt; random_value' or 'B &gt;= random_value' for numeric values. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -h * Prints this help.</pre> * * <pre> -o &lt;file&gt; * The name of the output file, otherwise the generated data is * printed to stdout.</pre> * * <pre> -r &lt;name&gt; * The name of the relation.</pre> * * <pre> -d * Whether to print debug informations.</pre> * * <pre> -S * The seed for random function (default 1)</pre> * * <pre> -n &lt;num&gt; * The number of examples to generate (default 100)</pre> * * <pre> -a &lt;num&gt; * The number of attributes (default 10).</pre> * * <pre> -c &lt;num&gt; * The number of classes (default 2)</pre> * * <pre> -R &lt;num&gt; * maximum size for rules (default 10) </pre> * * <pre> -M &lt;num&gt; * minimum size for rules (default 1) </pre> * * <pre> -I &lt;num&gt; * number of irrelevant attributes (default 0)</pre> * * <pre> -N * number of numeric attributes (default 0)</pre> * * <pre> -V * switch on voting (default is no voting)</pre> * <!-- options-end --> * * Following an example of a generated dataset: <br/> * <pre> * % * % weka.datagenerators.RDG1 -r expl -a 2 -c 3 -n 4 -N 1 -I 0 -M 2 -R 10 -S 2 * % * relation expl * * attribute a0 {false,true} * attribute a1 numeric * attribute class {c0,c1,c2} * * data * * true,0.496823,c0 * false,0.743158,c1 * false,0.408285,c1 * false,0.993687,c2 * % * % Number of attributes chosen as irrelevant = 0 * % * % DECISIONLIST (number of rules = 3): * % RULE 0: c0 := a1 &lt; 0.986, a0 * % RULE 1: c1 := a1 &lt; 0.95, not(a0) * % RULE 2: c2 := not(a0), a1 &gt;= 0.562 * </pre> * * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz) * @version $Revision: 1.5 $ */ public class RDG1 extends ClassificationGenerator { /** for serialization */ static final long serialVersionUID = 7751005204635320414L; /** * class to represent decisionlist */ private class RuleList implements Serializable, RevisionHandler { /** for serialization */ static final long serialVersionUID = 2830125413361938177L; /** rule list */ private FastVector m_RuleList = null; /** class */ double m_ClassValue = 0.0; /** * returns the class value * * @return the class value */ public double getClassValue() { return m_ClassValue; } /** * sets the class value * * @param newClassValue the new classvalue */ public void setClassValue(double newClassValue) { m_ClassValue = newClassValue; } /** * adds the given test to the list * * @param newTest the test to add */ private void addTest (Test newTest) { if (m_RuleList == null) m_RuleList = new FastVector(); m_RuleList.addElement(newTest); } /** * classifies the given example * * @param example the instance to classify * @return the classification * @throws Exception if classification fails */ private double classifyInstance (Instance example) throws Exception { boolean passedAllTests = true; for (Enumeration e = m_RuleList.elements(); passedAllTests && e.hasMoreElements(); ) { Test test = (Test) e.nextElement(); passedAllTests = test.passesTest(example); } if (passedAllTests) return m_ClassValue; else return -1.0; } /** * returns a string representation of the rule list * * @return the rule list as string */ public String toString () { StringBuffer str = new StringBuffer(); str = str.append(" c" + (int) m_ClassValue + " := "); Enumeration e = m_RuleList.elements(); if (e.hasMoreElements()) { Test test = (Test) e.nextElement(); str = str.append(test.toPrologString()); } while (e.hasMoreElements()) { Test test = (Test) e.nextElement(); str = str.append(", " + test.toPrologString()); } return str.toString(); } /** * Returns the revision string. * * @return the revision */ public String getRevision() { return RevisionUtils.extract("$Revision: 1.5 $"); } } /*end class RuleList ******/ /** Number of attribute the dataset should have */ protected int m_NumAttributes; /** Number of Classes the dataset should have */ protected int m_NumClasses; /** maximum rule size*/ private int m_MaxRuleSize; /** minimum rule size*/ private int m_MinRuleSize; /** number of irrelevant attributes.*/ private int m_NumIrrelevant; /** number of numeric attribute*/ private int m_NumNumeric; /** flag that stores if voting is wished*/ private boolean m_VoteFlag = false; /** decision list */ private FastVector m_DecisionList = null; /** array defines which attributes are irrelevant, with: * true = attribute is irrelevant; false = attribute is not irrelevant*/ boolean[] m_AttList_Irr; /** * initializes the generator with default values */ public RDG1() { super(); setNumAttributes(defaultNumAttributes()); setNumClasses(defaultNumClasses()); setMaxRuleSize(defaultMaxRuleSize()); setMinRuleSize(defaultMinRuleSize()); setNumIrrelevant(defaultNumIrrelevant()); setNumNumeric(defaultNumNumeric()); } /** * Returns a string describing this data generator. * * @return a description of the data generator suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "A data generator that produces data randomly by producing a decision list.\n" + "The decision list consists of rules.\n" + "Instances are generated randomly one by one. If decision list fails " + "to classify the current instance, a new rule according to this current " + "instance is generated and added to the decision list.\n\n" + "The option -V switches on voting, which means that at the end " + "of the generation all instances are " + "reclassified to the class value that is supported by the most rules.\n\n" + "This data generator can generate 'boolean' attributes (= nominal with " + "the values {true, false}) and numeric attributes. The rules can be " + "'A' or 'NOT A' for boolean values and 'B < random_value' or " + "'B >= random_value' for numeric values."; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options */ public Enumeration listOptions() { Vector result = enumToVector(super.listOptions()); result.addElement(new Option( "\tThe number of attributes (default " + defaultNumAttributes() + ").", "a", 1, "-a <num>")); result.addElement(new Option( "\tThe number of classes (default " + defaultNumClasses() + ")", "c", 1, "-c <num>")); result.addElement(new Option( "\tmaximum size for rules (default " + defaultMaxRuleSize() + ") ", "R", 1, "-R <num>")); result.addElement(new Option( "\tminimum size for rules (default " + defaultMinRuleSize() + ") ", "M", 1, "-M <num>")); result.addElement(new Option( "\tnumber of irrelevant attributes (default " + defaultNumIrrelevant() + ")", "I", 1, "-I <num>")); result.addElement(new Option( "\tnumber of numeric attributes (default " + defaultNumNumeric() + ")", "N", 1, "-N")); result.addElement(new Option( "\tswitch on voting (default is no voting)", "V", 1, "-V")); return result.elements(); } /** * Parses a list of options for this object. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -h * Prints this help.</pre> * * <pre> -o &lt;file&gt; * The name of the output file, otherwise the generated data is * printed to stdout.</pre> * * <pre> -r &lt;name&gt; * The name of the relation.</pre> * * <pre> -d * Whether to print debug informations.</pre> * * <pre> -S * The seed for random function (default 1)</pre> * * <pre> -n &lt;num&gt; * The number of examples to generate (default 100)</pre> * * <pre> -a &lt;num&gt; * The number of attributes (default 10).</pre> * * <pre> -c &lt;num&gt; * The number of classes (default 2)</pre> * * <pre> -R &lt;num&gt; * maximum size for rules (default 10) </pre> * * <pre> -M &lt;num&gt; * minimum size for rules (default 1) </pre> * * <pre> -I &lt;num&gt; * number of irrelevant attributes (default 0)</pre> * * <pre> -N * number of numeric attributes (default 0)</pre> * * <pre> -V * switch on voting (default is no voting)</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String tmpStr; super.setOptions(options); tmpStr = Utils.getOption('a', options); if (tmpStr.length() != 0) setNumAttributes(Integer.parseInt(tmpStr)); else setNumAttributes(defaultNumAttributes()); tmpStr = Utils.getOption('c', options); if (tmpStr.length() != 0) setNumClasses(Integer.parseInt(tmpStr)); else setNumClasses(defaultNumClasses()); tmpStr = Utils.getOption('R', options); if (tmpStr.length() != 0) setMaxRuleSize(Integer.parseInt(tmpStr)); else setMaxRuleSize(defaultMaxRuleSize()); tmpStr = Utils.getOption('M', options); if (tmpStr.length() != 0) setMinRuleSize(Integer.parseInt(tmpStr)); else setMinRuleSize(defaultMinRuleSize()); tmpStr = Utils.getOption('I', options); if (tmpStr.length() != 0) setNumIrrelevant(Integer.parseInt(tmpStr)); else setNumIrrelevant(defaultNumIrrelevant()); if ((getNumAttributes() - getNumIrrelevant()) < getMinRuleSize()) throw new Exception("Possible rule size is below minimal rule size."); tmpStr = Utils.getOption('N', options); if (tmpStr.length() != 0) setNumNumeric(Integer.parseInt(tmpStr)); else setNumNumeric(defaultNumNumeric()); setVoteFlag(Utils.getFlag('V', options)); } /** * Gets the current settings of the datagenerator RDG1. * * @return an array of strings suitable for passing to setOptions */ public String[] getOptions() { Vector result; String[] options; int i; result = new Vector(); options = super.getOptions(); for (i = 0; i < options.length; i++) result.add(options[i]); result.add("-a"); result.add("" + getNumAttributes()); result.add("-c"); result.add("" + getNumClasses()); result.add("-N"); result.add("" + getNumNumeric()); result.add("-I"); result.add("" + getNumIrrelevant()); result.add("-M"); result.add("" + getMinRuleSize()); result.add("-R"); result.add("" + getMaxRuleSize()); if (getVoteFlag()) result.add("-V"); return (String[]) result.toArray(new String[result.size()]); } /** * returns the default number of attributes * * @return the default number of attributes */ protected int defaultNumAttributes() { return 10; } /** * Sets the number of attributes the dataset should have. * @param numAttributes the new number of attributes */ public void setNumAttributes(int numAttributes) { m_NumAttributes = numAttributes; } /** * Gets the number of attributes that should be produced. * @return the number of attributes that should be produced */ public int getNumAttributes() { return m_NumAttributes; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numAttributesTipText() { return "The number of attributes the generated data will contain."; } /** * returns the default number of classes * * @return the default number of classes */ protected int defaultNumClasses() { return 2; } /** * Sets the number of classes the dataset should have. * @param numClasses the new number of classes */ public void setNumClasses(int numClasses) { m_NumClasses = numClasses; } /** * Gets the number of classes the dataset should have. * @return the number of classes the dataset should have */ public int getNumClasses() { return m_NumClasses; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numClassesTipText() { return "The number of classes to generate."; } /** * returns the default max size of rules * * @return the default max size of rules */ protected int defaultMaxRuleSize() { return 10; } /** * Gets the maximum number of tests in rules. * * @return the maximum number of tests allowed in rules */ public int getMaxRuleSize() { return m_MaxRuleSize; } /** * Sets the maximum number of tests in rules. * * @param newMaxRuleSize new maximum number of tests allowed in rules. */ public void setMaxRuleSize(int newMaxRuleSize) { m_MaxRuleSize = newMaxRuleSize; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String maxRuleSizeTipText() { return "The maximum number of tests in rules."; } /** * returns the default min size of rules * * @return the default min size of rules */ protected int defaultMinRuleSize() { return 1; } /** * Gets the minimum number of tests in rules. * * @return the minimum number of tests allowed in rules */ public int getMinRuleSize() { return m_MinRuleSize; } /** * Sets the minimum number of tests in rules. * * @param newMinRuleSize new minimum number of test in rules. */ public void setMinRuleSize(int newMinRuleSize) { m_MinRuleSize = newMinRuleSize; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String minRuleSizeTipText() { return "The minimum number of tests in rules."; } /** * returns the default number of irrelevant attributes * * @return the default number of irrelevant attributes */ protected int defaultNumIrrelevant() { return 0; } /** * Gets the number of irrelevant attributes. * * @return the number of irrelevant attributes */ public int getNumIrrelevant() { return m_NumIrrelevant; } /** * Sets the number of irrelevant attributes. * * @param newNumIrrelevant the number of irrelevant attributes. */ public void setNumIrrelevant(int newNumIrrelevant) { m_NumIrrelevant = newNumIrrelevant; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numIrrelevantTipText() { return "The number of irrelevant attributes."; } /** * returns the default number of numeric attributes * * @return the default number of numeric attributes */ protected int defaultNumNumeric() { return 0; } /** * Gets the number of numerical attributes. * * @return the number of numerical attributes. */ public int getNumNumeric() { return m_NumNumeric; } /** * Sets the number of numerical attributes. * * @param newNumNumeric the number of numerical attributes. */ public void setNumNumeric(int newNumNumeric) { m_NumNumeric = newNumNumeric; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numNumericTipText() { return "The number of numerical attributes."; } /** * Gets the vote flag. * * @return voting flag. */ public boolean getVoteFlag() { return m_VoteFlag; } /** * Sets the vote flag. * * @param newVoteFlag boolean with the new setting of the vote flag. */ public void setVoteFlag(boolean newVoteFlag) { m_VoteFlag = newVoteFlag; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String voteFlagTipText() { return "Whether to use voting or not."; } /** * Gets the single mode flag. * * @return true if methode generateExample can be used. */ public boolean getSingleModeFlag() { return (!getVoteFlag()); } /** * Gets the array that defines which of the attributes * are seen to be irrelevant. * * @return the array that defines the irrelevant attributes */ public boolean[] getAttList_Irr() { return m_AttList_Irr; } /** * Sets the array that defines which of the attributes * are seen to be irrelevant. * * @param newAttList_Irr array that defines the irrelevant attributes. */ public void setAttList_Irr(boolean[] newAttList_Irr) { m_AttList_Irr = newAttList_Irr; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String attList_IrrTipText() { return "The array with the indices of the irrelevant attributes."; } /** * Initializes the format for the dataset produced. * * @return the output data format * @throws Exception data format could not be defined */ public Instances defineDataFormat() throws Exception { Instances dataset; Random random = new Random (getSeed()); setRandom(random); m_DecisionList = new FastVector(); // number of examples is the same as given per option setNumExamplesAct(getNumExamples()); // define dataset dataset = defineDataset(random); return dataset; } /** * Generate an example of the dataset dataset. * @return the instance generated * @throws Exception if format not defined or generating <br/> * examples one by one is not possible, because voting is chosen */ public Instance generateExample() throws Exception { Random random = getRandom(); Instances format = getDatasetFormat(); if (format == null) throw new Exception("Dataset format not defined."); if (getVoteFlag()) throw new Exception("Examples cannot be generated one by one."); // generate values for all attributes format = generateExamples(1, random, format); return format.lastInstance(); } /** * Generate all examples of the dataset. * @return the instance generated * @throws Exception if format not defined or generating <br/> * examples one by one is not possible, because voting is chosen */ public Instances generateExamples() throws Exception { Random random = getRandom(); Instances format = getDatasetFormat(); if (format == null) throw new Exception("Dataset format not defined."); // generate values for all attributes format = generateExamples(getNumExamplesAct(), random, format); // vote all examples, and set new class value if (getVoteFlag()) format = voteDataset(format); return format; } /** * Generate all examples of the dataset. * @param num the number of examples to generate * @param random the random number generator to use * @param format the dataset format * @return the instance generated * @throws Exception if format not defined or generating <br/> * examples one by one is not possible, because voting is chosen */ public Instances generateExamples(int num, Random random, Instances format) throws Exception { if (format == null) throw new Exception("Dataset format not defined."); // generate values for all attributes for (int i = 0; i < num; i++) { // over all examples to be produced Instance example = generateExample(random, format); // set class of example using decision list boolean classDefined = classifyExample(example); if (!classDefined) { // set class with newly generated rule example = updateDecisionList(random, example); } example.setDataset(format); format.add(example); } return (format); } /** * Generates a new rule for the decision list. * and classifies the new example * @param random random number generator * @param example example used to update decision list * @return the classified example * @throws Exception if dataset format not defined */ private Instance updateDecisionList(Random random, Instance example) throws Exception { FastVector TestList; Instances format = getDatasetFormat(); if (format == null) throw new Exception("Dataset format not defined."); TestList = generateTestList(random, example); int maxSize = getMaxRuleSize() < TestList.size() ? getMaxRuleSize() : TestList.size(); int ruleSize = ((int) (random.nextDouble() * (double) (maxSize - getMinRuleSize()))) + getMinRuleSize(); RuleList newRule = new RuleList(); for (int i=0; i < ruleSize; i++) { int testIndex = (int) (random.nextDouble() * (double) TestList.size()); Test test = (Test) TestList.elementAt(testIndex); newRule.addTest(test); TestList.removeElementAt(testIndex); } double newClassValue = 0.0; if (m_DecisionList.size() > 0) { RuleList r = (RuleList)(m_DecisionList.lastElement()); double oldClassValue = (double) (r.getClassValue()); newClassValue = (double)((int)oldClassValue + 1) % getNumClasses(); } newRule.setClassValue(newClassValue); m_DecisionList.addElement(newRule); example = (Instance)example.copy(); example.setDataset(format); example.setClassValue(newClassValue); return example; } /** * Generates a new rule for the decision list * and classifies the new example. * * @param random random number generator * @param example the instance to classify * @return a list of tests * @throws Exception if dataset format not defined */ private FastVector generateTestList(Random random, Instance example) throws Exception { Instances format = getDatasetFormat(); if (format == null) throw new Exception("Dataset format not defined."); int numTests = getNumAttributes() - getNumIrrelevant(); FastVector TestList = new FastVector(numTests); boolean[] irrelevant = getAttList_Irr(); for (int i = 0; i < getNumAttributes(); i++) { if (!irrelevant[i]) { Test newTest = null; Attribute att = example.attribute(i); if (att.isNumeric()) { double newSplit = random.nextDouble(); boolean newNot = newSplit < example.value(i); newTest = new Test(i, newSplit, format, newNot); } else { newTest = new Test(i, example.value(i), format, false); } TestList.addElement (newTest); } } return TestList; } /** * Generates an example with its classvalue set to missing * and binds it to the datasets. * * @param random random number generator * @param format dataset the example gets bind to * @return the generated example * @throws Exception if attribute type not supported */ private Instance generateExample(Random random, Instances format) throws Exception { double[] attributes; Instance example; attributes = new double[getNumAttributes() + 1]; for (int i = 0; i < getNumAttributes(); i++) { double value = random.nextDouble(); if (format.attribute(i).isNumeric()) { attributes[i] = value; } else { if (format.attribute(i).isNominal()) attributes[i] = (value > 0.5) ? 1.0 : 0.0; else throw new Exception ("Attribute type is not supported."); } } example = new Instance(0, attributes); example.setDataset(format); example.setClassMissing(); return example; } /** * Tries to classify an example. * * @param example the example to classify * @return true if it could be classified * @throws Exception if something goes wrong */ private boolean classifyExample(Instance example) throws Exception { double classValue = -1.0; for (Enumeration e = m_DecisionList.elements(); e.hasMoreElements() && classValue < 0.0;) { RuleList rl = (RuleList) e.nextElement(); classValue = rl.classifyInstance(example); } if (classValue >= 0.0) { example.setClassValue(classValue); return true; } else { return false; } } /** * Classify example with maximum vote the following way. * With every rule in the decisionlist, it is evaluated if * the given instance could be the class of the rule. * Finally the class value that receives the highest number of votes * is assigned to the example. * * @param example example to be reclassified * @return instance with new class value * @throws Exception if classification fails */ private Instance votedReclassifyExample(Instance example) throws Exception { int classVotes[] = new int [getNumClasses()]; for (int i = 0; i < classVotes.length; i++) classVotes[i] = 0; for (Enumeration e = m_DecisionList.elements(); e.hasMoreElements();) { RuleList rl = (RuleList) e.nextElement(); int classValue = (int) rl.classifyInstance(example); if (classValue >= 0) classVotes[classValue]++; } int maxVote = 0; int vote = -1; for (int i = 0; i < classVotes.length; i++) { if (classVotes[i] > maxVote) { maxVote = classVotes[i]; vote = i; } } if (vote >= 0) example.setClassValue((double) vote); else throw new Exception ("Error in instance classification."); return example; } /** * Returns a dataset header. * @param random random number generator * @return dataset header * @throws Exception if something goes wrong */ private Instances defineDataset(Random random) throws Exception { boolean[] attList_Irr; int[] attList_Num; FastVector attributes = new FastVector(); Attribute attribute; FastVector nominalValues = new FastVector (2); nominalValues.addElement("false"); nominalValues.addElement("true"); FastVector classValues = new FastVector (getNumClasses()); Instances dataset; // set randomly those attributes that are irrelevant attList_Irr = defineIrrelevant(random); setAttList_Irr(attList_Irr); // set randomly those attributes that are numeric attList_Num = defineNumeric(random); // define dataset for (int i = 0; i < getNumAttributes(); i++) { if (attList_Num[i] == Attribute.NUMERIC) attribute = new Attribute("a" + i); else attribute = new Attribute("a" + i, nominalValues); attributes.addElement(attribute); } for (int i = 0; i < classValues.capacity(); i++) classValues.addElement("c" + i); attribute = new Attribute ("class", classValues); attributes.addElement(attribute); dataset = new Instances(getRelationNameToUse(), attributes, getNumExamplesAct()); dataset.setClassIndex(getNumAttributes()); // set dataset format of this class Instances format = new Instances(dataset, 0); setDatasetFormat(format); return dataset; } /** * Defines randomly the attributes as irrelevant. * Number of attributes to be set as irrelevant is either set * with a preceeding call of setNumIrrelevant() or is per default 0. * * @param random the random number generator to use * @return list of boolean values with one value for each attribute, * and each value set true or false according to if the corresponding * attribute was defined irrelevant or not */ private boolean[] defineIrrelevant(Random random) { boolean[] irr = new boolean [getNumAttributes()]; // initialize for (int i = 0; i < irr.length; i++) irr[i] = false; // set randomly int numIrr = 0; for (int i = 0; (numIrr < getNumIrrelevant()) && (i < getNumAttributes() * 5); i++) { int maybeNext = (int) (random.nextDouble() * (double) irr.length); if (irr[maybeNext] == false) { irr [maybeNext] = true; numIrr++; } } return irr; } /** * Chooses randomly the attributes that get datatyp numeric. * @param random the random number generator to use * @return list of integer values, with one value for each attribute, * and each value set to Attribut.NOMINAL or Attribut.NUMERIC */ private int[] defineNumeric(Random random) { int[] num = new int [getNumAttributes()]; // initialize for (int i = 0; i < num.length; i++) num[i] = Attribute.NOMINAL; int numNum = 0; for (int i = 0; (numNum < getNumNumeric()) && (i < getNumAttributes() * 5); i++) { int maybeNext = (int) (random.nextDouble() * (double) num.length); if (num[maybeNext] != Attribute.NUMERIC) { num[maybeNext] = Attribute.NUMERIC; numNum++; } } return num; } /** * Generates a comment string that documentates the data generator. * By default this string is added at the beginning of the produced output * as ARFF file type, next after the options. * * @return string contains info about the generated rules */ public String generateStart () { return ""; } /** * Compiles documentation about the data generation. This is the number of * irrelevant attributes and the decisionlist with all rules. * Considering that the decisionlist might get enhanced until * the last instance is generated, this method should be called at the * end of the data generation process. * * @return string with additional information about generated dataset * @throws Exception no input structure has been defined */ public String generateFinished() throws Exception { StringBuffer dLString = new StringBuffer(); // string for output at end of ARFF-File boolean[] attList_Irr = getAttList_Irr(); Instances format = getDatasetFormat(); dLString.append("%\n% Number of attributes chosen as irrelevant = " + getNumIrrelevant() + "\n"); for (int i = 0; i < attList_Irr.length; i++) { if (attList_Irr[i]) dLString.append("% " + format.attribute(i).name() + "\n"); } dLString.append("%\n% DECISIONLIST (number of rules = " + m_DecisionList.size() + "):\n"); for (int i = 0; i < m_DecisionList.size(); i++) { RuleList rl = (RuleList) m_DecisionList.elementAt(i); dLString.append("% RULE " + i + ": " + rl.toString() + "\n"); } return dLString.toString(); } /** * Resets the class values of all instances using voting. * For each instance the class value that satisfies the most rules * is choosen as new class value. * * @param dataset the dataset to work on * @return the changed instances * @throws Exception if something goes wrong */ private Instances voteDataset(Instances dataset) throws Exception { for (int i = 0; i < dataset.numInstances(); i++) { Instance inst = dataset.firstInstance(); inst = votedReclassifyExample(inst); dataset.add(inst); dataset.delete(0); } return dataset; } /** * Returns the revision string. * * @return the revision */ public String getRevision() { return RevisionUtils.extract("$Revision: 1.5 $"); } /** * Main method for testing this class. * * @param args should contain arguments for the data producer: */ public static void main(String[] args) { runDataGenerator(new RDG1(), args); } }

The table below shows all metrics for RDG1.java.

MetricValueDescription
BLOCKS87.00Number of blocks
BLOCK_COMMENT20.00Number of block comment lines
COMMENTS532.00Comment lines
COMMENT_DENSITY 1.17Comment density
COMPARISONS87.00Number of comparison operators
CYCLOMATIC117.00Cyclomatic complexity
DECL_COMMENTS76.00Comments in declarations
DOC_COMMENT495.00Number of javadoc comment lines
ELOC454.00Effective lines of code
EXEC_COMMENTS17.00Comments in executable code
EXITS88.00Procedure exits
FUNCTIONS59.00Number of function declarations
HALSTEAD_DIFFICULTY85.02Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY123.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 1.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 1.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 0.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA003432.00JAVA0034 Missing braces in if statement
JAVA0035 5.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 1.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 0.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 1.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 1.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA0076 2.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 1.00JAVA0081 Boolean literal in comparison
JAVA0082 1.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 1.00JAVA0096 Field in nested class hides outer field
JAVA0098 1.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 1.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 0.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 0.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 0.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 0.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 0.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA0117 0.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 4.00JAVA0119 Control variable changed within body of for loop
JAVA0123 3.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 0.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 3.00JAVA0144 Line exceeds maximum M characters
JAVA014516.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 1.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 0.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA017712.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 2.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 1.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 1.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 0.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES1226.00Number of lines in the source file
LINE_COMMENT17.00Number of line comments
LOC540.00Lines of code
LOGICAL_LINES317.00Number of statements
LOOPS20.00Number of loops
NEST_DEPTH 4.00Maximum nesting depth
OPERANDS1167.00Number of operands
OPERATORS2313.00Number of operators
PARAMS28.00Number of formal parameter declarations
PROGRAM_LENGTH3480.00Halstead program length
PROGRAM_VOCAB401.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS95.00Number of return points from functions
SIZE35007.00Size of the file in bytes
UNIQUE_OPERANDS350.00Number of unique operands
UNIQUE_OPERATORS51.00Number of unique operators
WHITESPACE154.00Number of whitespace lines