Revision 17088 trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/decisiontrees/DecisionTreeNode.java

View differences:

DecisionTreeNode.java
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
	 */
1 40
package org.gvsig.remotesensing.decisiontrees;
2 41

  
3 42
import org.nfunk.jep.JEP;
......
2 41

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

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

  
5 58
	private JEP 				parser 			= null;
......
10 63
	private DecisionTreeNode 	rightChild 		= null;
11 64
	private int 				classID 		= -1;
12 65
	
13
	public DecisionTreeNode(String expresion) {
14
		this.expression = expresion;
15
		parser = new JEP();
16
	}
17
	
18
	public DecisionTreeNode(int classID) {
19
		this.classID = classID;
20
	}
21
	
22 66
	public DecisionTreeNode() {
23 67
		parser = new JEP();
68
		parser.setAllowUndeclared(true);
69
		parser.addStandardFunctions();
24 70
	}
25 71
	
26 72
	private DecisionTreeNode(JEP parser) {
27 73
		this.parser = parser;
28 74
	}
29
	
30
	public void setChildrens(DecisionTreeNode leftChild, DecisionTreeNode rightChild){
31
		this.leftChild = leftChild;
32
		this.rightChild = leftChild;
33
	}
34 75

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

  
39 81
	public int execute (){
40 82
		if (expression!=null)
41 83
			if (evaluate()){
42
				if (leftChild!=null) 
43
					return leftChild.execute();
84
				if (rightChild!=null) 
85
					return rightChild.execute();
44 86
			}
45 87
			else if (rightChild!=null)
46
				return rightChild.execute();
88
				return leftChild.execute();
47 89
		
48 90
		return classID;
49 91
	}
50 92
	
51 93
	public int validate(){
94
		parser.parseExpression(expression);
52 95
		return 0;
53 96
	}
54 97

  
......
62 105
	
63 106
	public void setExpression(String expression) {
64 107
		this.expression = expression;
108
		parser.parseExpression(expression);
109
		classID = -1;
65 110
	}
66 111

  
67 112
	public int getClassID() {
......
76 121
	public void setClassID(int classID) {
77 122
		this.classID = classID;
78 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
	}
79 157
}

Also available in: Unified diff