svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / decisiontrees / DecisionTreeNode.java @ 18276
History | View | Annotate | Download (5.58 KB)
1 | 17088 | gsdiego | /* 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 | 16928 | gsdiego | package org.gvsig.remotesensing.decisiontrees; |
41 | |||
42 | 17773 | dguerrero | import java.util.ArrayList; |
43 | |||
44 | 16928 | gsdiego | import org.nfunk.jep.JEP; |
45 | |||
46 | 17088 | gsdiego | /**
|
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 | 16928 | gsdiego | public class DecisionTreeNode { |
53 | |||
54 | 17757 | dguerrero | public static final int finalNode = 0; |
55 | public static final int decisionNode = 1; |
||
56 | 17088 | gsdiego | |
57 | 16928 | gsdiego | 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 | 17088 | gsdiego | parser.setAllowUndeclared(true);
|
66 | parser.addStandardFunctions(); |
||
67 | 16928 | gsdiego | } |
68 | |||
69 | private DecisionTreeNode(JEP parser) {
|
||
70 | this.parser = parser;
|
||
71 | } |
||
72 | |||
73 | 17757 | dguerrero | public String toString() { |
74 | if(expression!=null) |
||
75 | return expression;
|
||
76 | else
|
||
77 | return String.valueOf(classID); |
||
78 | } |
||
79 | |||
80 | 17773 | dguerrero | |
81 | public boolean isFinal(){ |
||
82 | 17757 | dguerrero | if (getExpression() == null) |
83 | 17773 | dguerrero | return true; |
84 | 17757 | dguerrero | else
|
85 | 17773 | dguerrero | return false; |
86 | 17757 | dguerrero | } |
87 | |||
88 | 17517 | gsdiego | /**
|
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 | 16928 | gsdiego | public boolean evaluate(){ |
94 | 17088 | gsdiego | return(parser.getValue()!=0.0); |
95 | 16928 | gsdiego | } |
96 | |||
97 | 17517 | gsdiego | /**
|
98 | * Evalua el ?rbol que radica en este nodo.
|
||
99 | *
|
||
100 | * @return Clase resultante de la evaluaci?n.
|
||
101 | */
|
||
102 | 16928 | gsdiego | public int execute (){ |
103 | if (expression!=null) |
||
104 | if (evaluate()){
|
||
105 | 17088 | gsdiego | if (rightChild!=null) |
106 | return rightChild.execute();
|
||
107 | 16928 | gsdiego | } |
108 | else if (rightChild!=null) |
||
109 | 17088 | gsdiego | return leftChild.execute();
|
110 | 16928 | gsdiego | |
111 | return classID;
|
||
112 | } |
||
113 | |||
114 | 17757 | dguerrero | /**
|
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 | 16928 | gsdiego | } |
125 | |||
126 | public JEP getParser() {
|
||
127 | return parser;
|
||
128 | } |
||
129 | |||
130 | 17517 | gsdiego | /**
|
131 | *
|
||
132 | * @return expresi?n asociada al nodo
|
||
133 | */
|
||
134 | 16928 | gsdiego | public String getExpression() { |
135 | return expression;
|
||
136 | } |
||
137 | |||
138 | 17517 | gsdiego | /**
|
139 | * Asigna la expresi?n asociada al nodo
|
||
140 | *
|
||
141 | * @param expression
|
||
142 | */
|
||
143 | 16928 | gsdiego | public void setExpression(String expression) { |
144 | this.expression = expression;
|
||
145 | 17088 | gsdiego | parser.parseExpression(expression); |
146 | classID = -1;
|
||
147 | 16928 | gsdiego | } |
148 | |||
149 | 17517 | gsdiego | /**
|
150 | *
|
||
151 | * @return Clase asociada al nodo
|
||
152 | */
|
||
153 | 16928 | gsdiego | public int getClassID() { |
154 | return classID;
|
||
155 | } |
||
156 | |||
157 | /**
|
||
158 | 17517 | gsdiego | * Establece el identificador de clase
|
159 | 16928 | gsdiego | *
|
160 | * @param classID
|
||
161 | */
|
||
162 | public void setClassID(int classID) { |
||
163 | this.classID = classID;
|
||
164 | } |
||
165 | 17088 | gsdiego | |
166 | public void addChildren() { |
||
167 | 17757 | dguerrero | leftChild = new DecisionTreeNode();
|
168 | rightChild = new DecisionTreeNode();
|
||
169 | 17088 | gsdiego | setExpression("");
|
170 | } |
||
171 | 17208 | gsdiego | |
172 | 17517 | gsdiego | /**
|
173 | * Elimina los hijos del nodo
|
||
174 | *
|
||
175 | */
|
||
176 | 17208 | gsdiego | public void deleteChildren(){ |
177 | leftChild = null;
|
||
178 | rightChild = null;
|
||
179 | expression = null;
|
||
180 | } |
||
181 | 17088 | gsdiego | |
182 | 17517 | gsdiego | /**
|
183 | *
|
||
184 | * @return Hijo izquierdo
|
||
185 | */
|
||
186 | 17088 | gsdiego | public DecisionTreeNode getLeftChild() {
|
187 | return leftChild;
|
||
188 | } |
||
189 | |||
190 | 17517 | gsdiego | /**
|
191 | *
|
||
192 | * @return Hijo Derecho
|
||
193 | */
|
||
194 | 17088 | gsdiego | public DecisionTreeNode getRightChild() {
|
195 | return rightChild;
|
||
196 | } |
||
197 | |||
198 | 17517 | gsdiego | /**
|
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 | 17088 | gsdiego | public void setVarValue (String varName, Object value){ |
205 | 17757 | dguerrero | 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 | 17088 | gsdiego | } |
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 | 17208 | gsdiego | |
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 | 17773 | dguerrero | |
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 | 16928 | gsdiego | } |