public Map < String , OrdinalAttribute > classify ( State state ) throws ClassifyingFailureException { // Start at the root of the decision tree. DecisionTree currentNode = dt ; // Loop down the decision tree until a leaf node is found. while ( true ) // !currentNode.isLeaf()) { DecisionTreeElement element = currentNode . getElement ( ) ; // Check that the current element really is a decision. if ( element instanceof Decision ) { Decision decision = ( Decision ) element ; // Apply the decision at the current node to the state to be classified to get a new tree. currentNode = decision . decide ( state ) ; // , currentNode); } else if ( element instanceof Assignment ) { // Cast the element to an Assignment as this is the only type of leaf that is possible. Assignment assignment = ( Assignment ) element ; // Return the assignment in a map. Map < String , OrdinalAttribute > assignmentMap = new HashMap < String , OrdinalAttribute > ( ) ; assignmentMap . put ( assignment . getPropertyName ( ) , assignment . getAttribute ( ) ) ; return assignmentMap ; } // It is possible that a node may be of type Pending if an incomplete tree has been used to // run this classification on. else { // Throw a classification exception due to an incomplete decision tree. throw new ClassifyingFailureException ( "A node which is not a decision was encountered." , null ) ; } // What happens if the decision could not operate on the state, either because of a missing property or // because its property was not of the type that the decision was expecting? Can either throw an exception, // return an empty assignment, or implement an algorithm for coping with missing properties. } }
Classifies a state using the decision tree .