use of edu.cmu.tetrad.session.SessionModel in project tetrad by cmu-phil.
the class EdgewiseComparisonParamsEditor method setup.
public void setup() {
List<GraphSource> graphSources = new LinkedList<>();
for (Object parentModel : parentModels) {
if (parentModel instanceof GraphSource) {
graphSources.add((GraphSource) parentModel);
}
}
if (graphSources.size() == 1 && graphSources.get(0) instanceof GeneralAlgorithmRunner) {
model1 = (GeneralAlgorithmRunner) graphSources.get(0);
model2 = ((GeneralAlgorithmRunner) model1).getDataWrapper();
} else if (graphSources.size() == 2) {
model1 = (SessionModel) graphSources.get(0);
model2 = (SessionModel) graphSources.get(1);
} else {
throw new IllegalArgumentException("Expecting 2 graph source.");
}
setLayout(new BorderLayout());
// Reset?
JRadioButton resetOnExecute = new JRadioButton("Reset");
JRadioButton dontResetOnExecute = new JRadioButton("Appended to");
ButtonGroup group1 = new ButtonGroup();
group1.add(resetOnExecute);
group1.add(dontResetOnExecute);
resetOnExecute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getParams().set("resetTableOnExecute", true);
}
});
dontResetOnExecute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getParams().set("resetTableOnExecute", false);
}
});
if (getParams().getBoolean("resetTableOnExecute", false)) {
resetOnExecute.setSelected(true);
} else {
dontResetOnExecute.setSelected(true);
}
// Latents?
JRadioButton latents = new JRadioButton("Yes");
JRadioButton noLatents = new JRadioButton("No");
ButtonGroup group2 = new ButtonGroup();
group2.add(latents);
group2.add(noLatents);
latents.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getParams().set("keepLatents", true);
}
});
if (getParams().getBoolean("keepLatents", false)) {
latents.setSelected(true);
} else {
noLatents.setSelected(true);
}
// True graph?
JRadioButton graph1 = new JRadioButton(model1.getName());
JRadioButton graph2 = new JRadioButton(model2.getName());
graph1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getParams().set("referenceGraphName", model1.getName());
getParams().set("targetGraphName", model2.getName());
}
});
graph2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getParams().set("referenceGraphName", model2.getName());
getParams().set("targetGraphName", model1.getName());
}
});
ButtonGroup group = new ButtonGroup();
group.add(graph1);
group.add(graph2);
boolean alreadySet = false;
if (model1 instanceof GeneralAlgorithmRunner) {
graph1.setSelected(true);
}
if (model2 instanceof GeneralAlgorithmRunner) {
graph2.setSelected(true);
alreadySet = true;
}
if (model2 instanceof Simulation) {
graph2.setSelected(true);
alreadySet = true;
}
if (!alreadySet) {
graph1.setSelected(true);
}
if (graph1.isSelected()) {
getParams().set("referenceGraphName", model1.getName());
getParams().set("targetGraphName", model2.getName());
} else if (graph2.isSelected()) {
getParams().set("referenceGraphName", model2.getName());
getParams().set("targetGraphName", model1.getName());
}
Box b1 = Box.createVerticalBox();
Box b8 = Box.createHorizontalBox();
b8.add(new JLabel("Which of the two input graphs is the true graph?"));
b8.add(Box.createHorizontalGlue());
b1.add(b8);
Box b9 = Box.createHorizontalBox();
b9.add(graph1);
b9.add(Box.createHorizontalGlue());
b1.add(b9);
Box b10 = Box.createHorizontalBox();
b10.add(graph2);
b10.add(Box.createHorizontalGlue());
b1.add(b10);
b1.add(Box.createHorizontalGlue());
add(b1, BorderLayout.CENTER);
}
use of edu.cmu.tetrad.session.SessionModel in project tetrad by cmu-phil.
the class SessionEditorNode method getPopup.
/**
* Creates the popup for the node.
*/
private JPopupMenu getPopup() {
if (popup != null && popup.isShowing()) {
return popup;
}
popup = new JPopupMenu();
JMenuItem createModel = new JMenuItem("Create Model");
createModel.setToolTipText("<html>Creates a new model for this node" + "<br>of the type selected.</html>");
createModel.addActionListener((e) -> {
try {
if (getSessionNode().getModel() == null) {
createModel(false);
} else {
Component centeringComp = SessionEditorNode.this;
JOptionPane.showMessageDialog(centeringComp, "Please destroy the model model first.");
}
} catch (Exception e1) {
Component centeringComp = SessionEditorNode.this;
JOptionPane.showMessageDialog(centeringComp, "Could not create a model for this box.");
e1.printStackTrace();
}
});
JMenuItem editModel = new JMenuItem("Edit Model");
editModel.setToolTipText("<html>Edits the model in this node.</html>");
editModel.addActionListener((e) -> {
try {
if (getSessionNode().getModel() == null) {
Component centeringComp = SessionEditorNode.this;
JOptionPane.showMessageDialog(centeringComp, "Sorry, no model has been created yet; there's nothing to edit.");
} else {
doDoubleClickAction();
}
} catch (Exception e1) {
Component centeringComp = SessionEditorNode.this;
JOptionPane.showMessageDialog(centeringComp, "Double click failed. See console for exception.");
e1.printStackTrace();
}
});
JMenuItem destroyModel = new JMenuItem("Destroy Model");
destroyModel.setToolTipText("<html>Destroys the model for this node, " + "<br>if it has one, destroying any " + "<br>downstream models as well.</html>");
destroyModel.addActionListener((e) -> {
Component centeringComp = SessionEditorNode.this;
if (getSessionNode().getModel() == null) {
JOptionPane.showMessageDialog(centeringComp, "Sorry, this box does not contain a model to destroy.");
return;
}
Set<SessionNode> children = getSessionNode().getChildren();
boolean found = false;
for (SessionNode child : children) {
if (child.getModel() != null) {
found = true;
}
}
if (found) {
int ret = JOptionPane.showConfirmDialog(centeringComp, "Destroying the model in this box will also destroy models in any boxes\n" + "downstream. Is that OK?", null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (ret != JOptionPane.YES_OPTION) {
return;
}
}
destroyModel();
});
JMenuItem propagateDownstream = new JMenuItem("Propagate changes downstream");
propagateDownstream.setToolTipText("<html>" + "Fills in this box and downstream boxes with models," + "<br>overwriting any models that already exist.</html>");
propagateDownstream.addActionListener((e) -> {
Component centeringComp = SessionEditorNode.this;
if (getSessionNode().getModel() != null && !getSessionNode().getChildren().isEmpty()) {
int ret = JOptionPane.showConfirmDialog(centeringComp, "You will be rewriting all downstream models. Is that OK?", "Confirm", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (ret != JOptionPane.YES_OPTION) {
return;
}
}
try {
createDescendantModels(true);
} catch (RuntimeException e1) {
JOptionPane.showMessageDialog(centeringComp, "Could not complete the creation of descendant models.");
e1.printStackTrace();
}
});
JMenuItem renameBox = new JMenuItem("Rename Box");
renameBox.setToolTipText("<html>Renames this session box.</html>");
renameBox.addActionListener((e) -> {
Component centeringComp = SessionEditorNode.this;
String name = JOptionPane.showInputDialog(centeringComp, "New name:");
if (!NamingProtocol.isLegalName(name)) {
JOptionPane.showMessageDialog(centeringComp, NamingProtocol.getProtocolDescription());
return;
}
SessionNodeWrapper wrapper = (SessionNodeWrapper) getModelNode();
wrapper.setSessionName(name);
getSessionDisplayComp().setName(name);
adjustToModel();
});
JMenuItem cloneBox = new JMenuItem("Clone Box");
cloneBox.setToolTipText("<html>" + "Makes a copy of this session box and its contents. To clone<br>" + "a whole subgraph, or to paste into a different sessions, select<br>" + "the subgraph and use the Copy/Paste gadgets in the Edit menu." + "</html>");
cloneBox.addActionListener((e) -> {
firePropertyChange("cloneMe", null, SessionEditorNode.this);
});
JMenuItem deleteBox = new JMenuItem("Delete Box");
deleteBox.setToolTipText("<html>Deletes this box from the workbench</html>");
deleteBox.addActionListener((e) -> {
Component centeringComp = SessionEditorNode.this;
int ret = JOptionPane.showConfirmDialog(centeringComp, "Are you sure you want to delete this box? It contains some work.", null, JOptionPane.YES_NO_OPTION);
if (ret != JOptionPane.YES_OPTION) {
return;
}
firePropertyChange("deleteNode", null, null);
});
popup.add(createModel);
SessionModel model = getSessionNode().getModel();
Class modelClass = (model == null) ? determineTheModelClass(getSessionNode()) : model.getClass();
if (getSessionNode().existsParameterizedConstructor(modelClass)) {
final ParameterEditor paramEditor = getParameterEditor(modelClass);
if (paramEditor != null) {
JMenuItem editSimulationParameters = new JMenuItem("Edit Parameters...");
editSimulationParameters.setToolTipText("<html>");
editSimulationParameters.addActionListener((e) -> {
Parameters param = getSessionNode().getParam(modelClass);
Object[] arguments = getSessionNode().getModelConstructorArguments(modelClass);
if (param != null) {
try {
editParameters(modelClass, param, arguments);
int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "Should I overwrite the contents of this box and all delete the contents\n" + "of all boxes downstream?", "Double check...", JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.YES_OPTION) {
getSessionNode().destroyModel();
getSessionNode().createModel(modelClass, true);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
popup.add(editSimulationParameters);
}
}
// final SessionNode thisNode = getSessionNode();
//
// // popup.add(getConsistentParentMenuItems(getConsistentParentBoxTypes(thisNode)));
// // popup.add(getConsistentChildBoxMenus(getConsistentChildBoxTypes(thisNode, null)));
//
// addConsistentParentMenuItems(popup, getConsistentParentBoxTypes(thisNode));
// addConsistentChildBoxMenus(popup, getConsistentChildBoxTypes(thisNode, null));
//
// popup.addSeparator();
popup.add(createModel);
popup.add(editModel);
popup.add(destroyModel);
popup.addSeparator();
popup.add(renameBox);
popup.add(cloneBox);
popup.add(deleteBox);
popup.addSeparator();
addEditLoggerSettings(popup);
popup.add(propagateDownstream);
return popup;
}
use of edu.cmu.tetrad.session.SessionModel in project tetrad by cmu-phil.
the class SessionEditorNode method launchEditorVisit.
private void launchEditorVisit() {
try {
// If there is already an editor open, don't launch another one.
if (spawnedEditor() != null) {
return;
}
boolean created = createModel(false);
if (!created) {
return;
}
final SessionNode sessionNode = getSessionNode();
boolean cloned = sessionNode.useClonedModel();
SessionModel model = sessionNode.getModel();
Class<?> modelClass = model.getClass();
SessionNodeModelConfig modelConfig = this.config.getModelConfig(modelClass);
Object[] arguments = new Object[] { model };
JPanel editor = modelConfig.getEditorInstance(arguments);
addEditorListener(editor);
ModificationRegistery.registerEditor(sessionNode, editor);
String descrip = modelConfig.getName();
editor.setName(getName() + " (" + descrip + ")");
EditorWindow editorWindow = new EditorWindow(editor, editor.getName(), "Done", cloned, this);
editorWindow.addInternalFrameListener(new InternalFrameAdapter() {
@Override
public void internalFrameClosing(InternalFrameEvent e) {
if (getChildren().iterator().hasNext()) {
finishedEditingDialog();
}
ModificationRegistery.unregisterSessionNode(sessionNode);
setSpawnedEditor(null);
EditorWindow window = (EditorWindow) e.getSource();
if (window.isCanceled()) {
sessionNode.restoreOriginalModel();
}
sessionNode.forgetSavedModel();
}
});
DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
editorWindow.pack();
editorWindow.setVisible(true);
spawnedEditor = editorWindow;
if (sessionWrapper != null) {
sessionWrapper.setSessionChanged(true);
}
// for (SessionNode child : getChildren()) {
//
// // only break edges to children.
// if (edge.getNode2() == getModelNode()) {
// SessionNodeWrapper otherWrapper =
// (SessionNodeWrapper) edge.getNode1();
// SessionNode other = otherWrapper.getSessionNode();
// if (getChildren().contains(other)) {
// sessionWrapper.removeEdge(edge);
// }
// } else {
// SessionNodeWrapper otherWrapper =
// (SessionNodeWrapper) edge.getNode2();
// SessionNode other = otherWrapper.getSessionNode();
// if (getChildren().contains(other)) {
// sessionWrapper.removeEdge(edge);
// }
// }
// }
// Class[] consistentModelClasses = child.getConsistentModelClasses(false);
// if (consistentModelClasses.length == 0) {
// child.removeParent(sessionNode);
// SessionEditorWorkbench sessionWorkbench = getSessionWorkbench();
// SessionWrapper sessionWrapper = sessionWorkbench.getSessionWrapper();
// Node node1 = sessionWrapper.getNode(sessionNode.getDisplayName());
// Node node2 = sessionWrapper.getNode(child.getDisplayName());
// Edge edge = sessionWrapper.getEdge(node1, node2);
// sessionWrapper.removeEdge(edge);
// }
} catch (CouldNotCreateModelException e) {
SessionUtils.showPermissibleParentsDialog(e.getModelClass(), SessionEditorNode.this, true, true);
e.printStackTrace();
} catch (ClassCastException e) {
e.printStackTrace();
} catch (Exception e) {
Throwable cause = e;
while (cause.getCause() != null) {
cause = cause.getCause();
}
Component centeringComp = SessionEditorNode.this;
String s = cause.getMessage();
if (!"".equals(s)) {
JOptionPane.showMessageDialog(centeringComp, s, null, JOptionPane.WARNING_MESSAGE);
}
e.printStackTrace();
}
}
Aggregations