/
ConstructTemplateAction.java
457 lines (374 loc) · 17.4 KB
/
ConstructTemplateAction.java
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
///////////////////////////////////////////////////////////////////////////////
// For information as to what this class does, see the Javadoc, below. //
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, //
// 2007, 2008, 2009, 2010, 2014, 2015, 2022 by Peter Spirtes, Richard //
// Scheines, Joseph Ramsey, and Clark Glymour. //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
///////////////////////////////////////////////////////////////////////////////
package edu.cmu.tetradapp.app;
import edu.cmu.tetrad.graph.Edge;
import edu.cmu.tetrad.graph.Endpoint;
import edu.cmu.tetrad.graph.Node;
import edu.cmu.tetrad.session.SessionNode;
import edu.cmu.tetradapp.model.SessionNodeWrapper;
import edu.cmu.tetradapp.model.SessionWrapper;
import edu.cmu.tetradapp.util.DesktopController;
import edu.cmu.tetradapp.util.SessionEditorIndirectRef;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* Adds a new template session subgraph to the frontmost session editor. of one
* of three types.
*
* @author josephramsey
*/
final class ConstructTemplateAction extends AbstractAction {
/**
* The names of the templates supported by this action.
*/
private static final String[] TEMPLATE_NAMES = {
"Simulate from a given graph, then search",
"Simulate, search, then compare",
"Load data and search",
"Search then estimate",
"Search, estimate, then update",
// "MIMBuild" // Removed 4/9/2019 Folded into FOFC
};
/**
* The name of the template.
*/
private final String templateName;
/**
* The session workbench. Needed for selection.
*/
private SessionEditorWorkbench sessionWorkbench;
//============================CONSTUCTORS============================//
/**
* Constucts an action for adding a new template to the frontmost session.
*/
public ConstructTemplateAction(String templateName) {
super(templateName);
if (templateName == null) {
throw new NullPointerException(
"Template filename must not be " + "null.");
}
this.templateName = templateName;
}
//==========================PUBLIC METHODS===========================//
/**
* @return a copy of the template names. Must be public.
*/
public static String[] getTemplateNames() {
String[] templateNamesCopy = new String[ConstructTemplateAction.TEMPLATE_NAMES.length];
System.arraycopy(ConstructTemplateAction.TEMPLATE_NAMES, 0, templateNamesCopy, 0,
ConstructTemplateAction.TEMPLATE_NAMES.length);
return templateNamesCopy;
}
/**
* Performs the action of adding the specified templatew into the frontmost
* session. It is assumed that all example sessions will be located in
* directory "example_sessions".
*/
@Override
public void actionPerformed(ActionEvent e) {
int leftX = getLeftX();
if (this.templateName.equals(ConstructTemplateAction.getTemplateNames()[0])) {
simulateDataFixedIM(leftX);
} else if (this.templateName.equals(ConstructTemplateAction.getTemplateNames()[1])) {
searchFromSimulatedDataWithCompare(leftX);
} else if (this.templateName.equals(ConstructTemplateAction.getTemplateNames()[2])) {
searchFromLoadedOrSimulatedData(leftX);
} else if (this.templateName.equals(ConstructTemplateAction.getTemplateNames()[3])) {
estimateFromSimulatedData(leftX);
} else if (this.templateName.equals(ConstructTemplateAction.getTemplateNames()[4])) {
estimateThenUpdateUsingSearchResult(leftX);
}
// Removed 4/9/2019 Folded into FOFC
// else if (this.templateName.equals(getTemplateNames()[5])) {
// mimbuild(leftX);
// }
else {
throw new IllegalStateException("Unrecognized template name: " + this.templateName);
}
}
public void addParent(SessionEditorNode thisNode, String type) {
String name = ConstructTemplateAction.nextName(type);
addNode(type, name, thisNode.getX() - 50, thisNode.getY() - 50);
addEdge(name, thisNode.getName());
}
//=============================PRIVATE METHODS========================//
private int getLeftX() {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
SessionEditorWorkbench sessionWorkbench
= sessionEditor.getSessionWorkbench();
sessionWorkbench.deselectAll();
Component[] components = sessionWorkbench.getComponents();
int leftX = 0;
for (Component component : components) {
Rectangle bounds = component.getBounds();
int rightmost = bounds.x + bounds.width;
if (rightmost > leftX) {
leftX = rightmost;
}
}
leftX += 100;
return leftX;
}
private void searchFromLoadedOrSimulatedData(int leftX) {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
SessionEditorWorkbench sessionWorkbench
= sessionEditor.getSessionWorkbench();
sessionWorkbench.deselectAll();
List<Node> nodes = new LinkedList<>();
String data = ConstructTemplateAction.nextName("Data");
String search = ConstructTemplateAction.nextName("Search");
nodes.add(addNode("Data", data, leftX, 100));
nodes.add(addNode("Search", search, 125 + leftX, 100));
addEdge(data, search);
ConstructTemplateAction.selectSubgraph(nodes);
}
private void simulateDataFixedIM(int leftX) {
getSessionWorkbench().deselectAll();
List<Node> nodes = new LinkedList<>();
String graph = ConstructTemplateAction.nextName("Graph");
String pm = ConstructTemplateAction.nextName("PM");
String im = ConstructTemplateAction.nextName("IM");
String data = ConstructTemplateAction.nextName("Simulation");
String search = ConstructTemplateAction.nextName("Search");
nodes.add(addNode("Graph", graph, leftX, 100));
nodes.add(addNode("PM", pm, leftX, 200));
nodes.add(addNode("IM", im, leftX, 300));
nodes.add(addNode("Simulation", data, leftX, 400));
nodes.add(addNode("Search", search, 125 + leftX, 400));
addEdge(graph, pm);
addEdge(pm, im);
addEdge(im, data);
addEdge(data, search);
ConstructTemplateAction.selectSubgraph(nodes);
}
private void searchFromSimulatedDataWithCompare(int leftX) {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
SessionEditorWorkbench sessionWorkbench
= sessionEditor.getSessionWorkbench();
sessionWorkbench.deselectAll();
List<Node> nodes = new LinkedList<>();
String data = ConstructTemplateAction.nextName("Simulation");
String search = ConstructTemplateAction.nextName("Search");
String compare = ConstructTemplateAction.nextName("Compare");
nodes.add(addNode("Simulation", data, leftX, 100));
nodes.add(addNode("Search", search, 150 + leftX, 100));
nodes.add(addNode("Compare", compare, 80 + leftX, 200));
addEdge(data, search);
addEdge(data, compare);
addEdge(search, compare);
ConstructTemplateAction.selectSubgraph(nodes);
}
private void estimateFromSimulatedData(int leftX) {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
SessionEditorWorkbench sessionWorkbench
= sessionEditor.getSessionWorkbench();
sessionWorkbench.deselectAll();
List<Node> nodes = new LinkedList<>();
String data = ConstructTemplateAction.nextName("Data");
String search = ConstructTemplateAction.nextName("Search");
nodes.add(addNode("Data", data, leftX, 100));
nodes.add(addNode("Search", search, leftX + 150, 100));
String graph = ConstructTemplateAction.nextName("Graph");
nodes.add(addNode("Graph", graph, leftX + 150, 200));
String pm = ConstructTemplateAction.nextName("PM");
nodes.add(addNode("PM", pm, leftX + 150, 300));
String estimator = ConstructTemplateAction.nextName("Estimator");
nodes.add(addNode("Estimator", estimator, leftX, 300));
addEdge(data, search);
addEdge(search, graph);
addEdge(graph, pm);
addEdge(data, estimator);
addEdge(data, pm);
addEdge(pm, estimator);
ConstructTemplateAction.selectSubgraph(nodes);
}
private void estimateThenUpdateUsingSearchResult(int leftX) {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
SessionEditorWorkbench sessionWorkbench
= sessionEditor.getSessionWorkbench();
sessionWorkbench.deselectAll();
List<Node> nodes = new LinkedList<>();
String data = ConstructTemplateAction.nextName("Data");
String search = ConstructTemplateAction.nextName("Search");
nodes.add(addNode("Data", data, leftX, 100));
nodes.add(addNode("Search", search, leftX + 150, 100));
String graph = ConstructTemplateAction.nextName("Graph");
nodes.add(addNode("Graph", graph, leftX + 150, 200));
String pm = ConstructTemplateAction.nextName("PM");
nodes.add(addNode("PM", pm, leftX + 150, 300));
String estimator = ConstructTemplateAction.nextName("Estimator");
nodes.add(addNode("Estimator", estimator, leftX, 300));
String updater = ConstructTemplateAction.nextName("Updater");
nodes.add(addNode("Updater", updater, leftX, 400));
addEdge(data, search);
addEdge(search, graph);
addEdge(graph, pm);
addEdge(data, estimator);
addEdge(data, pm);
addEdge(pm, estimator);
addEdge(estimator, updater);
ConstructTemplateAction.selectSubgraph(nodes);
}
private static void selectSubgraph(List<Node> nodes) {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
SessionEditorWorkbench sessionWorkbench
= sessionEditor.getSessionWorkbench();
for (Node node : nodes) {
sessionWorkbench.selectNode(node);
}
Set<Edge> edges = sessionWorkbench.getGraph().getEdges();
for (Edge edge : edges) {
Node node1 = edge.getNode1();
Node node2 = edge.getNode2();
if (nodes.contains(node1) && nodes.contains(node2)) {
sessionWorkbench.selectEdge(edge);
}
}
sessionWorkbench.scrollNodesToVisible(nodes);
}
/**
* Returns the next string in the sequence.
*
* @param base the string base of the name--for example, "Graph".
* @return the next string in the sequence--for example, "Graph1".
*/
private static String nextName(String base) {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
SessionEditorWorkbench sessionWorkbench
= sessionEditor.getSessionWorkbench();
SessionWrapper graph = sessionWorkbench.getSessionWrapper();
if (base == null) {
throw new NullPointerException("Base name must be non-null.");
}
int i = 0; // Sequence 1, 2, 3, ...
loop:
while (true) {
i++;
String name = base + i;
for (Node o : graph.getNodes()) {
if (o.getName().equals(name)) {
continue loop;
}
}
break;
}
return base + i;
}
private SessionWrapper getSessionWrapper() {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
SessionEditorWorkbench sessionWorkbench
= sessionEditor.getSessionWorkbench();
sessionWorkbench.deselectAll();
return sessionWorkbench.getSessionWrapper();
}
private SessionEditorWorkbench getSessionWorkbench() {
if (this.sessionWorkbench == null) {
SessionEditorIndirectRef sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
SessionEditor sessionEditor = (SessionEditor) sessionEditorRef;
if (sessionEditor == null) {
DesktopController.getInstance().newSessionEditor();
sessionEditorRef
= DesktopController.getInstance().getFrontmostSessionEditor();
sessionEditor = (SessionEditor) sessionEditorRef;
}
this.sessionWorkbench = sessionEditor.getSessionWorkbench();
}
return this.sessionWorkbench;
}
private Node addNode(String nodeType, String nodeName, int centerX,
int centerY) {
SessionNodeWrapper node = ConstructTemplateAction.getNewModelNode(nodeType, nodeName);
node.setCenter(centerX, centerY);
getSessionWrapper().addNode(node);
return node;
}
public void addEdge(String nodeName1, String nodeName2) {
// Retrieve the nodes from the session wrapper.
Node node1 = getSessionWrapper().getNode(nodeName1);
Node node2 = getSessionWrapper().getNode(nodeName2);
// Make sure nodes existed in the session wrapper by these names.
if (node1 == null) {
throw new RuntimeException(
"There was no node by name nodeName1 in "
+ "the session wrapper: " + nodeName1);
}
if (node2 == null) {
throw new RuntimeException(
"There was no node by name nodeName2 in "
+ "the session wrapper: " + nodeName2);
}
// Construct an edge.
SessionNodeWrapper nodeWrapper1 = (SessionNodeWrapper) node1;
SessionNodeWrapper nodeWrapper2 = (SessionNodeWrapper) node2;
Edge edge = new Edge(nodeWrapper1, nodeWrapper2, Endpoint.TAIL,
Endpoint.ARROW);
// Add the edge.
getSessionWrapper().addEdge(edge);
getSessionWorkbench().revalidate();
getSessionWorkbench().repaint();
}
private static SessionNodeWrapper getNewModelNode(String nextButtonType,
String name) {
if (nextButtonType == null) {
throw new NullPointerException(
"Next button type must be a " + "non-null string.");
}
Class[] modelClasses = ConstructTemplateAction.getModelClasses(nextButtonType);
SessionNode newNode
= new SessionNode(nextButtonType, name, modelClasses);
SessionNodeWrapper nodeWrapper = new SessionNodeWrapper(newNode);
nodeWrapper.setButtonType(nextButtonType);
return nodeWrapper;
}
/**
* @return the model classes associated with the given button type.
* @throws NullPointerException if no classes are stored for the given type.
*/
private static Class[] getModelClasses(String nextButtonType) {
TetradApplicationConfig tetradConfig = TetradApplicationConfig.getInstance();
SessionNodeConfig config = tetradConfig.getSessionNodeConfig(nextButtonType);
if (config == null) {
throw new NullPointerException("There is no configuration for button: " + nextButtonType);
}
return config.getModels();
}
}