001    /**
002     * 
003     */
004    package org.wdssii.decisiontree;
005    
006    /**
007     * 
008     * A decision tree each of whose branches depends on only one attribute. An
009     * object of this class typically represents a learned decision tree although
010     * the object can be built directly by adding branches.
011     * 
012     * @see org.wdssii.decisiontree.QuinlanC45AxialDecisionTreeCreator
013     * @author lakshman
014     * 
015     */
016    @SuppressWarnings("serial")
017    public class AxialDecisionTree implements DecisionTree {
018            private AxialTreeNode rootNode;
019    
020            private int defaultValue;
021    
022            private int numAttributes;
023            
024            private int numCategories;
025            
026            public AxialDecisionTree() {
027            }
028    
029            /**
030             * Create with root node and the default category to be used if decision
031             * tree lands up in unlearned space.
032             */
033            public AxialDecisionTree(AxialTreeNode node, int defaultValue, int numAttributes, int numCategories) {
034                    this.rootNode = node;
035                    this.defaultValue = defaultValue;
036                    this.numAttributes = numAttributes;
037                    this.numCategories = numCategories;
038            }
039    
040            /**
041             * @inheritDoc
042             */
043            public int classify(float[] data) {
044                    int result = defaultValue;
045                    if (rootNode != null) {
046                            result = rootNode.classify(data);
047                            if (result < 0) {
048                                    result = defaultValue;
049                            }
050                    }
051                    return result;
052            }
053    
054            public String toJava() {
055                    String newline = System.getProperty("line.separator");
056                    StringBuilder sb = new StringBuilder();
057                    if (rootNode != null) {
058                            rootNode.appendJava(sb, 0, newline);
059                    } else {
060                            sb.append("return ").append(defaultValue).append(";").append(
061                                            newline);
062                    }
063                    return sb.toString();
064            }
065    
066            public int getDefaultValue() {
067                    return defaultValue;
068            }
069    
070            public void setDefaultValue(int defaultValue) {
071                    this.defaultValue = defaultValue;
072            }
073    
074            public AxialTreeNode getRootNode() {
075                    return rootNode;
076            }
077    
078            public void setRootNode(AxialTreeNode rootNode) {
079                    this.rootNode = rootNode;
080            }
081    
082            public int getNumAttributes() {
083                    return numAttributes;
084            }
085    
086            public void setNumAttributes(int numAttributes) {
087                    this.numAttributes = numAttributes;
088            }
089    
090            public int getNumCategories() {
091                    return numCategories;
092            }
093    
094            public void setNumCategories(int numCategories) {
095                    this.numCategories = numCategories;
096            }
097    
098    }