Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / decisiontrees / DecisionTreeNode.java @ 18276

History | View | Annotate | Download (5.58 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
         *
3
         * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
         *
5
         * This program is free software; you can redistribute it and/or
6
         * modify it under the terms of the GNU General Public License
7
         * as published by the Free Software Foundation; either version 2
8
         * of the License, or (at your option) any later version.
9
         *
10
         * This program is distributed in the hope that it will be useful,
11
         * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
         * GNU General Public License for more details.
14
         *
15
         * You should have received a copy of the GNU General Public License
16
         * along with this program; if not, write to the Free Software
17
         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
         *
19
         * For more information, contact:
20
         *
21
         *  Generalitat Valenciana
22
         *   Conselleria d'Infraestructures i Transport
23
         *   Av. Blasco Iba?ez, 50
24
         *   46010 VALENCIA
25
         *   SPAIN
26
         *
27
         *      +34 963862235
28
         *   gvsig@gva.es
29
         *      www.gvsig.gva.es
30
         *
31
         *    or
32
         *
33
         *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
         *   Campus Universitario s/n
35
         *   02071 Alabacete
36
         *   Spain
37
         *
38
         *   +34 967 599 200
39
         */
40
package org.gvsig.remotesensing.decisiontrees;
41

    
42
import java.util.ArrayList;
43

    
44
import org.nfunk.jep.JEP;
45

    
46
/**
47
 * Clase que representa un n?do en un ?rbol de decisi?n
48
 * 
49
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
50
 *
51
 */
52
public class DecisionTreeNode {
53
        
54
        public static final int finalNode                         = 0;
55
        public static final int decisionNode                 = 1;
56

    
57
        private JEP                                 parser                         = null;
58
        private String                                 expression                 = null;
59
        private DecisionTreeNode         leftChild                 = null;
60
        private DecisionTreeNode         rightChild                 = null;
61
        private int                                 classID                 = -1;
62
        
63
        public DecisionTreeNode() {
64
                parser = new JEP();
65
                parser.setAllowUndeclared(true);
66
                parser.addStandardFunctions();
67
        }
68
        
69
        private DecisionTreeNode(JEP parser) {
70
                this.parser = parser;
71
        }
72

    
73
        public String toString() {
74
                if(expression!=null)
75
                        return expression;
76
                else 
77
                        return String.valueOf(classID);
78
        }
79
        
80
        
81
        public boolean isFinal(){
82
                if (getExpression() == null)
83
                        return true;
84
                else
85
                        return false;
86
        }
87
        
88
        /**
89
         * 
90
         * @return <code>true</code> si la evaluaci?n de la expresi?n da como resultado
91
         * un valor distinto de 0;
92
         */
93
        public boolean evaluate(){
94
                return(parser.getValue()!=0.0);
95
        }
96

    
97
        /**
98
         * Evalua el ?rbol que radica en este nodo.
99
         * 
100
         * @return Clase resultante de la evaluaci?n. 
101
         */
102
        public int execute (){
103
                if (expression!=null)
104
                        if (evaluate()){
105
                                if (rightChild!=null) 
106
                                        return rightChild.execute();
107
                        }
108
                        else if (rightChild!=null)
109
                                return leftChild.execute();
110
                
111
                return classID;
112
        }
113
        
114
        /** 
115
         * 
116
         * @return <code>true</code> si todas las expresiones del sub?rbol que radica en 
117
         * este nodo est?n libres de errores de sintaxis.
118
         */
119
        public boolean hasError(){
120
                if(expression==null)
121
                        return true;
122
                else
123
                        return (parser.hasError() && getLeftChild().hasError() && getRightChild().hasError());
124
        }
125

    
126
        public JEP getParser() {
127
                return parser;
128
        }
129

    
130
        /**
131
         * 
132
         * @return expresi?n asociada al nodo
133
         */
134
        public String getExpression() {
135
                return expression;
136
        }
137
        
138
        /**
139
         * Asigna la expresi?n asociada al nodo
140
         * 
141
         * @param expression 
142
         */
143
        public void setExpression(String expression) {
144
                this.expression = expression;
145
                parser.parseExpression(expression);
146
                classID = -1;
147
        }
148

    
149
        /**
150
         * 
151
         * @return Clase asociada al nodo
152
         */
153
        public int getClassID() {
154
                return classID;
155
        }
156

    
157
        /**
158
         * Establece el identificador de clase
159
         * 
160
         * @param classID
161
         */
162
        public void setClassID(int classID) {
163
                this.classID = classID;
164
        }
165

    
166
        public void addChildren() {
167
                leftChild = new DecisionTreeNode();
168
                rightChild = new DecisionTreeNode();
169
                setExpression("");
170
        }
171
        
172
        /**
173
         * Elimina los hijos del nodo
174
         *
175
         */
176
        public void deleteChildren(){
177
                leftChild = null;
178
                rightChild = null;
179
                expression = null;
180
        }
181

    
182
        /**
183
         * 
184
         * @return Hijo izquierdo
185
         */
186
        public DecisionTreeNode getLeftChild() {
187
                return leftChild;
188
        }
189

    
190
        /**
191
         * 
192
         * @return Hijo Derecho
193
         */
194
        public DecisionTreeNode getRightChild() {
195
                return rightChild;
196
        }
197
        
198
        /**
199
         * Asigna a la variable con nombre <code>varName</code> el valor <code>value</code>
200
         * 
201
         * @param varName Nombre de la variable
202
         * @param value Valor a asignar
203
         */
204
        public void setVarValue (String varName, Object value){
205
                if (expression!=null){
206
                        if(parser.getSymbolTable().keySet().contains(varName))
207
                                parser.setVarValue(varName, value);
208
                        rightChild.setVarValue(varName, value);
209
                        leftChild.setVarValue(varName, value);
210
                }
211
        }
212
        
213
        /**
214
         * 
215
         * @return N?mero de nodos del ?rbol que radica en este nodo.
216
         */
217
        public int size(){
218
                
219
                int leftSize = 0;
220
                int rightSize = 0;
221
                if (leftChild != null)
222
                        leftSize = leftChild.size();
223
                if (rightChild != null)
224
                        rightSize = rightChild.size();
225
                return 1+leftSize+rightSize;
226
        }
227
        
228
        /**
229
         * 
230
         * @return N?mero de niveles del ?rbol que radica en este nodo.
231
         */
232
        public int getLevelsCount(){
233
                int levels = 1;
234
                int leftLevels = 0;
235
                int rightLevels = 0;
236
                if (leftChild!=null)
237
                        leftLevels = leftChild.getLevelsCount();
238
                if (rightChild!=null)
239
                        rightLevels = rightChild.getLevelsCount();
240
                
241
                if (leftLevels>rightLevels)
242
                        levels = levels+leftLevels;
243
                else
244
                        levels = levels+rightLevels;
245
                
246
                return levels;
247
        }
248
        
249
        /**
250
         * 
251
         * @return Lista de los identificadores de los nodos finales
252
         */
253
        public void getFinals(ArrayList finals){
254
                if (isFinal()){
255
                        finals.add(new Integer(getClassID()));
256
                }
257
                else{
258
                        getLeftChild().getFinals(finals);
259
                        getRightChild().getFinals(finals);
260
                }
261
        }
262
}