Statistics
| Revision:

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

History | View | Annotate | Download (3.52 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 org.nfunk.jep.JEP;
43

    
44
/**
45
 * Clase que representa un n?do en un ?rbol de decisi?n
46
 * 
47
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
48
 *
49
 */
50
public class DecisionTreeNode {
51
        
52

    
53
        public String toString() {
54
                if(expression!=null)
55
                        return expression;
56
                else 
57
                        return String.valueOf(classID);
58
        }
59

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

    
76
        public boolean evaluate(){
77
                parser.parseExpression(expression);
78
                return(parser.getValue()!=0.0);
79
        }
80

    
81
        public int execute (){
82
                if (expression!=null)
83
                        if (evaluate()){
84
                                if (rightChild!=null) 
85
                                        return rightChild.execute();
86
                        }
87
                        else if (rightChild!=null)
88
                                return leftChild.execute();
89
                
90
                return classID;
91
        }
92
        
93
        public int validate(){
94
                parser.parseExpression(expression);
95
                return 0;
96
        }
97

    
98
        public JEP getParser() {
99
                return parser;
100
        }
101

    
102
        public String getExpression() {
103
                return expression;
104
        }
105
        
106
        public void setExpression(String expression) {
107
                this.expression = expression;
108
                parser.parseExpression(expression);
109
                classID = -1;
110
        }
111

    
112
        public int getClassID() {
113
                return classID;
114
        }
115

    
116
        /**
117
         * Establece el identificador de clase.
118
         * 
119
         * @param classID
120
         */
121
        public void setClassID(int classID) {
122
                this.classID = classID;
123
        }
124

    
125
        public void addChildren() {
126
                leftChild = new DecisionTreeNode(parser);
127
                rightChild = new DecisionTreeNode(parser);
128
                setExpression("");
129
        }
130

    
131
        public DecisionTreeNode getLeftChild() {
132
                return leftChild;
133
        }
134

    
135
        public DecisionTreeNode getRightChild() {
136
                return rightChild;
137
        }
138
        
139
        public void setVarValue (String varName, Object value){
140
                parser.setVarValue(varName, value);
141
        }
142
        
143
        /**
144
         * 
145
         * @return N?mero de nodos del ?rbol que radica en este nodo.
146
         */
147
        public int size(){
148
                
149
                int leftSize = 0;
150
                int rightSize = 0;
151
                if (leftChild != null)
152
                        leftSize = leftChild.size();
153
                if (rightChild != null)
154
                        rightSize = rightChild.size();
155
                return 1+leftSize+rightSize;
156
        }
157
}