Revision 17088

View differences:

trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/decisiontrees/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
}
trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/decisiontrees/gui/listener/DecisionTreePanelListener.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.gui.listener;
2 41

  
3 42
import java.awt.event.ActionEvent;
......
6 45
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
7 46
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
8 47

  
48
/**
49
 * Lister del panel de ?rboles de decisi?n.
50
 * 
51
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
52
 *
53
 */
9 54
public class DecisionTreePanelListener implements ButtonsPanelListener, ActionListener {
10 55

  
11 56
	public void actionPerformed(ActionEvent e) {
trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/decisiontrees/gui/listener/GraphListener.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.gui.listener;
2 41

  
3 42
import java.awt.event.MouseEvent;
......
3 42
import java.awt.event.MouseListener;
4 43

  
44
import javax.swing.JOptionPane;
45

  
46
/**
47
 * Listener para el grafico del panel de ?rboles de decisi?n.
48
 * 
49
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
50
 *
51
 */
5 52
public class GraphListener implements MouseListener {
6 53

  
......
21 68
	}
22 69

  
23 70
	public void mousePressed(MouseEvent e) {
24
		// TODO Auto-generated method stub
25

  
71
		if (e.getButton() ==  MouseEvent.BUTTON3){
72
			JOptionPane.showMessageDialog(null, "Esto es un pop up men?. ?Que no?", "popup Menu", JOptionPane.INFORMATION_MESSAGE);
73
		}
74
		if (e.getClickCount() == 2) {
75
			JOptionPane.showMessageDialog(null, "Ay Gal?n", "doble clicki", JOptionPane.INFORMATION_MESSAGE);
76
		}
26 77
	}
27 78

  
28 79
	public void mouseReleased(MouseEvent e) {
trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/decisiontrees/gui/DecisionTreeDialog.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.gui;
2 41

  
3 42
import java.awt.BorderLayout;
trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/decisiontrees/gui/DecisionTreePanel.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.gui;
2 41

  
3 42
import java.awt.Color;
......
2 41
import java.awt.Dimension;
3
import java.awt.event.MouseAdapter;
4
import java.awt.event.MouseEvent;
5
import java.awt.geom.Rectangle2D;
6 42

  
7 43
import javax.swing.BorderFactory;
8
import javax.swing.JOptionPane;
9 44
import javax.swing.JScrollPane;
10 45

  
11 46
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
47
import org.gvsig.remotesensing.decisiontrees.DecisionTreeNode;
48
import org.gvsig.remotesensing.decisiontrees.gui.listener.GraphListener;
12 49
import org.jgraph.JGraph;
......
19 56
import org.jgraph.graph.GraphConstants;
20 57
import org.jgraph.graph.GraphModel;
21 58

  
59
/**
60
 * Panel para la herramienta de ?rboles de decisi?n.
61
 * 
62
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
63
 *
64
 */
22 65
public class DecisionTreePanel extends DefaultButtonsPanel {
23 66

  
24 67
	/**
......
29 72
	private JGraph 			  		jGraph     	   		= null;
30 73
	private DecisionTreeDialog  	decisionTreeDialog 	= null;
31 74
	private JScrollPane				scrollPane			= null;
75
	private DecisionTreeNode 		root				= null;
32 76

  
33 77

  
34 78
	public DecisionTreePanel(DecisionTreeDialog decisionTreeDialog) {
......
38 82
	}
39 83
	
40 84
	private void initialize() {
41
		
85
		initTree();
86
		reloadGraph();
42 87
		add(getScrollPane());
88
	}
89

  
90
	/**
91
	 * Crea el ?rbol de decisi?n inicial
92
	 *
93
	 */
94
	private void initTree() {
95
		root = new DecisionTreeNode();
96
		root.addChildren();
97
		root.setExpression("a<b");
98
		root.getLeftChild().setClassID(0);
99
		root.getRightChild().setClassID(1);
100
	}
101
	
102
	/**
103
	 * Reconstruye el gr?fico a partir del ?rbol (root).
104
	 *
105
	 */
106
	private void reloadGraph(){
107
		insertCells(root,null);
108
	}
109

  
110
	private void insertCells(DecisionTreeNode node, DefaultGraphCell parent) {
111
		DefaultGraphCell nodeVertex = createVertex(node, 20, 20, 40, 20, null, false);
43 112
		
113
		getJGraph().getGraphLayoutCache().insert(nodeVertex);
114
		if (parent!=null){
115
			DefaultEdge edge = new DefaultEdge();
116
			edge.setSource(parent);
117
			edge.setTarget(nodeVertex);
118
			GraphConstants.setLineEnd(edge.getAttributes(), GraphConstants.ARROW_CLASSIC);
119
			GraphConstants.setEndFill(edge.getAttributes(), true);
120
			GraphConstants.setAutoSize(edge.getAttributes(), true);
121
			getJGraph().getGraphLayoutCache().insert(edge);
122
		}
123
			
124
		if (node.getLeftChild()!=null){
125
			insertCells(node.getLeftChild(), nodeVertex);
126
		}
127
		if (node.getRightChild()!=null){
128
			insertCells(node.getRightChild(), nodeVertex);
129
		}
44 130
	}
45 131

  
46 132
	public DefaultGraphCell createVertex(Object name, double x,
......
50 136
			DefaultGraphCell cell = new DefaultGraphCell(name);
51 137

  
52 138
			// Set bounds
53
			GraphConstants.setBounds(cell.getAttributes(),
54
					new Rectangle2D.Double(x, y, w, h));
139
		//	GraphConstants.setBounds(cell.getAttributes(),
140
			//		new Rectangle2D.Double(x, y, w, h));
55 141

  
56 142
			// Set fill color
57 143
			if (bg != null) {
58 144
				GraphConstants.setGradientColor(
59
					cell.getAttributes(), Color.orange);
145
					cell.getAttributes(), bg);
60 146
				GraphConstants.setOpaque(
61 147
					cell.getAttributes(), true);
62 148
			}
......
75 161
			DefaultPort port = new DefaultPort();
76 162
			cell.add(port);
77 163
			port.setParent(cell);
164
			
165
			GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createEtchedBorder());
166
	    	GraphConstants.setAutoSize(cell.getAttributes(),true);
167
	    	GraphConstants.setResize(cell.getAttributes(),true);
168
	    	GraphConstants.setInset(cell.getAttributes(), 10);
78 169

  
79 170
			return cell;
80 171
		}
......
82 173
	public JScrollPane getScrollPane() {
83 174
		if (scrollPane == null){
84 175
			scrollPane = new JScrollPane(getJGraph());
176
			scrollPane.setPreferredSize(new Dimension(500,500));
85 177
		}
86
			
87 178
		return scrollPane;
88 179
	}
89 180

  
......
99 190
	        jGraph.setDisconnectable(false);
100 191
	        jGraph.setEditable(false);
101 192
	        jGraph.setEnabled(true);
102
	        jGraph.setMoveable(false);
193
	        
194
	        jGraph.setSize(300, 300);
103 195
			
104
			DefaultGraphCell[] cells = new DefaultGraphCell[3];
105

  
106
			// Create Hello Vertex
107
			cells[0] = createVertex("Adios", 20, 20, 40, 20, 
108
						null, false);
109

  
110
			// Create World Vertex
111
			cells[1] = createVertex("HOla", 140, 140, 40, 20, 
112
						Color.ORANGE, true);
113

  
114
			// Create Edge
115
			DefaultEdge edge = new DefaultEdge();
116
			// Fetch the ports from the new vertices, 
117
			// and connect them with the edge
118
			edge.setSource(cells[0].getChildAt(0));
119
			edge.setTarget(cells[1].getChildAt(0));
120
			cells[2] = edge;
121

  
122
			// Set Arrow Style for edge
123
			int arrow = GraphConstants.ARROW_CLASSIC;
124
			GraphConstants.setLineEnd(edge.getAttributes(), arrow);
125
			GraphConstants.setEndFill(edge.getAttributes(), true);
126

  
127
			// Insert the cells via the cache, so they get selected
128
			jGraph.getGraphLayoutCache().insert(cells);
129
			
130
			jGraph.addMouseListener(new MouseAdapter() {
131
	        	public void mousePressed(MouseEvent e) {
132
	        		if (e.getButton() ==  MouseEvent.BUTTON3){
133
	        			JOptionPane.showMessageDialog(null, "Esto es un pop up men?. ?Que no?", "popup Menu", JOptionPane.INFORMATION_MESSAGE);
134
	        		}
135
	        		if (e.getClickCount() == 2) {
136
	        			JOptionPane.showMessageDialog(null, "Ay Gal?n", "doble clicki", JOptionPane.INFORMATION_MESSAGE);
137
	        		}
138
	        	}
139
	        });
196
			GraphListener listener = new GraphListener();
197
			jGraph.addMouseListener(listener);
140 198
		}
141 199
		return jGraph;
142 200
	}
trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/decisiontrees/DecisionTree.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

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

  
5 50
}

Also available in: Unified diff