aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/population/ChromosomePane.java
blob: bf9db720318564c31541ccad15f3bedc339928e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package jcgp.gui.population;

import java.util.ArrayList;

import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Gene;
import jcgp.backend.population.Input;
import jcgp.backend.population.Node;
import jcgp.backend.population.Output;
import jcgp.gui.GUI;
import jcgp.gui.constants.Position;
import jcgp.gui.handlers.InputHandlers;
import jcgp.gui.handlers.NodeHandlers;
import jcgp.gui.handlers.OutputHandlers;

/**
 * This extension of {@code ScrollPane} contains a series of
 * nodes, inputs and outputs spread across a grid. It also contains
 * all of the connection lines overlaid over the nodes, inputs and outputs.
 * 
 * 
 * @author Eduardo Pedroni
 *
 */
public class ChromosomePane extends ScrollPane {

	private GUINode[][] guiNodes;
	private GUIInput[] guiInputs;
	private GUIOutput[] guiOutputs;

	private Pane content;

	private int rows, columns;

	private boolean target = false;

	public ChromosomePane(Chromosome chromosome) {
		super();

		ArrayList<Line> connectionLines = new ArrayList<Line>();

		rows = GUI.resources.rows();
		columns = GUI.resources.columns();

		content = new Pane();
		content.setId("content pane for genes");

		// generate the GUIGenes
		/* 
		 * inputs
		 */
		guiInputs = new GUIInput[GUI.resources.inputs()];
		for (int i = 0; i < guiInputs.length; i++) {
			// get the backend input
			Input input = chromosome.getInput(i);
			// make the GUI elements
			guiInputs[i] = new GUIInput(input);
			// assign the GUI object to the associated backend element
			input.setGUIObject(guiInputs[i]);
			// position, handlers
			Position.place(guiInputs[i]);
			InputHandlers.addHandlers(guiInputs[i]);
		}
		content.getChildren().addAll(guiInputs);

		/*
		 * nodes
		 */
		guiNodes = new GUINode[rows][columns];
		//double angle, xPos, yPos;

		for (int c = 0; c < columns; c++) {
			for (int r = 0; r < rows; r++) {
				// get the backend node
				Node node = chromosome.getNode(r, c);
				// make the connection lines
				Line lines[] = new Line[GUI.resources.arity()];
				for (int l = 0; l < lines.length; l++) {
					lines[l] = new Line();

					Position.connect(lines[l], (GUIGene) ((Gene) node.getConnection(l)).getGUIObject());

					lines[l].setMouseTransparent(true);
					lines[l].setVisible(false);
					connectionLines.add(lines[l]);
				}
				// make the GUI elements
				guiNodes[r][c] = new GUINode(node, lines);
				// assign the GUI object to the associated backend element
				node.setGUIObject(guiNodes[r][c]);
				// position, handlers
				Position.place(guiNodes[r][c]);
				NodeHandlers.addHandlers(guiNodes[r][c]);
				content.getChildren().add(guiNodes[r][c]);
			}
		}

		/* 
		 * outputs
		 */
		guiOutputs = new GUIOutput[GUI.resources.outputs()];
		for (int i = 0; i < guiOutputs.length; i++) {
			// get the backend output
			Output output = chromosome.getOutput(i);
			// make the GUI elements
			Line line = new Line();
			Position.connect(line, (GUIGene) ((Gene) output.getSource()).getGUIObject());
			line.setVisible(false);
			line.setMouseTransparent(true);
			guiOutputs[i] = new GUIOutput(output, line);
			// assign the GUI object to the associated backend element
			output.setGUIObject(guiOutputs[i]);
			// position, handlers
			Position.place(guiOutputs[i]);
			OutputHandlers.addHandlers(guiOutputs[i]);
			connectionLines.add(line);
		}
		content.getChildren().addAll(guiOutputs);

		// add lines to the pane on top of genes
		content.getChildren().addAll(connectionLines);

		setPrefWidth(620);	
		setContent(content);
	}


	/*
	 * does this work lol
	 */
	//	protected GUIGene getGuiGene(Connection gene) {
	//		if (gene instanceof Input) {
	//			return guiInputs[((Input) gene).getIndex()];
	//		} else if (gene instanceof Node) {
	//			return guiNodes[((Node) gene).getRow()][((Node) gene).getColumn()];
	//		} else {
	//			// something bad happened!
	//			throw new ClassCastException();
	//		}	
	//	}

	protected boolean isTarget() {
		return target;
	}

	protected void setTarget(boolean newValue) {
		target = newValue;
	}

	public void updateGenes(Chromosome chr) {
		for (int r = 0; r < rows; r++) {
			for (int c = 0; c < columns; c++) {
				guiNodes[r][c].setNode(chr.getNode(r, c));
			}
		}
		for (int i = 0; i < guiOutputs.length; i++) {
			guiOutputs[i].setOutput(chr.getOutput(i));
		}
	}

	public static boolean isAllowed(GUIMutable source, GUIConnection target) {
		if (source instanceof GUINode) {
			// if the source is a node, all inputs and some nodes are valid
			if (target instanceof GUIInput) {
				return true;
			} else if (target instanceof GUINode) {
				// target and source are nodes, let's look at levels back
				Node t = ((GUINode) target).getNode(), s = ((GUINode) source).getNode();
				if (s.getColumn() - t.getColumn() > 0 && s.getColumn() - t.getColumn() <= GUI.resources.levelsBack()) {
					return true;
				}
			}
			return false;
		} else if (source instanceof GUIOutput) {
			// if the source is an output, any node or input is valid
			if (target instanceof GUINode || target instanceof GUIInput) {
				return true;
			} else {
				// this should never happen...
				return false;
			}
		}
		// if the source was neither node nor output, something bad is happening
		throw new ClassCastException("Source was neither GUINode nor GUIOutput.");
	}

	//	private GUIConnection getGUIConnection(Connection connection) {
	//		if (connection instanceof Input) {
	//			return guiInputs[((Input) connection).getIndex()];
	//		} else if (connection instanceof Node) {
	//			return guiNodes[((Node) connection).getRow()][((Node) connection).getColumn()];
	//		} else {
	//			// something bad happened!
	//			throw new ClassCastException();
	//		}
	//	}
}