Search in sources :

Example 6 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.

the class SessionEditorNode method createDescendantModels.

private void createDescendantModels(final boolean overwrite) {
    Window owner = (Window) getTopLevelAncestor();
    new WatchedProcess(owner) {

        @Override
        public void watch() {
            Class clazz = SessionEditorWorkbench.class;
            Container container = SwingUtilities.getAncestorOfClass(clazz, SessionEditorNode.this);
            SessionEditorWorkbench workbench = (SessionEditorWorkbench) container;
            if (workbench != null) {
                workbench.getSimulationStudy().createDescendantModels(getSessionNode(), overwrite);
            }
        }
    };
}
Also used : EditorWindow(edu.cmu.tetradapp.editor.EditorWindow) Window(java.awt.Window) Container(java.awt.Container) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess)

Example 7 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.

the class FgesSearchEditor method execute.

public void execute() {
    Window owner = (Window) getTopLevelAncestor();
    final WatchedProcess process = new WatchedProcess(owner) {

        public void watch() {
            getExecuteButton().setEnabled(false);
            setErrorMessage(null);
            if (!knowledgeMessageShown) {
                IKnowledge knowledge = (IKnowledge) getAlgorithmRunner().getParams().get("knowledge", new Knowledge2());
                if (!knowledge.isEmpty()) {
                    JOptionPane.showMessageDialog(getWorkbench(), "Using previously set knowledge. (To edit, use " + "the Knowledge menu.)");
                    knowledgeMessageShown = true;
                }
            }
            try {
                storeLatestWorkbenchGraph();
                getAlgorithmRunner().execute();
                IFgesRunner runner = (IFgesRunner) getAlgorithmRunner();
                arrangeGraphs();
                gesDisplay.resetGraphs(runner.getTopGraphs());
            } catch (Exception e) {
                CharArrayWriter writer1 = new CharArrayWriter();
                PrintWriter writer2 = new PrintWriter(writer1);
                e.printStackTrace(writer2);
                String message = writer1.toString();
                writer2.close();
                e.printStackTrace(System.out);
                TetradLogger.getInstance().error(message);
                String messageString = e.getMessage();
                if (e.getCause() != null) {
                    messageString = e.getCause().getMessage();
                }
                if (messageString == null) {
                    messageString = message;
                }
                setErrorMessage(messageString);
                TetradLogger.getInstance().error("************Algorithm stopped!");
                getExecuteButton().setEnabled(true);
                throw new RuntimeException(e);
            }
            getWorkbenchScroll().setBorder(new TitledBorder(getResultLabel()));
            Graph resultGraph = resultGraph();
            doDefaultArrangement(resultGraph);
            getWorkbench().setBackground(Color.WHITE);
            getWorkbench().setGraph(resultGraph);
            // getWorkbench().setGraph(gesDisplay.getTopGraphs().get(gesDisplay.getTopGraphs().size() - 1).getGraph());
            getGraphHistory().clear();
            getGraphHistory().add(resultGraph);
            getWorkbench().repaint();
            // For Mimbuild, e.g., that need to do a second stage.
            firePropertyChange("algorithmFinished", null, null);
            getExecuteButton().setEnabled(true);
            firePropertyChange("modelChanged", null, null);
            doPostExecutionSteps();
        }
    };
    Thread watcher = new Thread() {

        public void run() {
            while (true) {
                try {
                    sleep(300);
                    if (!process.isAlive()) {
                        getExecuteButton().setEnabled(true);
                        return;
                    }
                } catch (InterruptedException e) {
                    getExecuteButton().setEnabled(true);
                    return;
                }
            }
        }
    };
    watcher.start();
}
Also used : WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) TitledBorder(javax.swing.border.TitledBorder) CharArrayWriter(java.io.CharArrayWriter) PrintWriter(java.io.PrintWriter)

Example 8 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.

the class FgesSearchEditor method addSpecialMenus.

protected void addSpecialMenus(JMenuBar menuBar) {
    if (!(getAlgorithmRunner() instanceof FgesRunner)) {
        JMenu test = new JMenu("Independence");
        menuBar.add(test);
        IndTestMenuItems.addIndependenceTestChoices(test, this);
    // test.addSeparator();
    // 
    // AlgorithmRunner algorithmRunner = getAlgorithmRunner();
    // if (algorithmRunner instanceof IndTestProducer) {
    // IndTestProducer p = (IndTestProducer) algorithmRunner;
    // IndependenceFactsAction action =
    // new IndependenceFactsAction(this, p, "Independence Facts...");
    // test.add(action);
    // }
    }
    JMenu graph = new JMenu("Graph");
    JMenuItem showDags = new JMenuItem("Show DAGs in forbid_latent_common_causes");
    // JMenuItem meekOrient = new JMenuItem("Meek Orientation");
    final JMenuItem dagInPattern = new JMenuItem("Choose DAG in forbid_latent_common_causes");
    JMenuItem gesOrient = new JMenuItem("Global Score-based Reorientation");
    JMenuItem nextGraph = new JMenuItem("Next Graph");
    JMenuItem previousGraph = new JMenuItem("Previous Graph");
    graph.add(new GraphPropertiesAction(getWorkbench()));
    graph.add(new PathsAction(getWorkbench()));
    // graph.add(new DirectedPathsAction(getWorkbench()));
    // graph.add(new TreksAction(getWorkbench()));
    // graph.add(new AllPathsAction(getWorkbench()));
    // graph.add(new NeighborhoodsAction(getWorkbench()));
    // graph.add(new TriplesAction(getWorkbench(), getAlgorithmRunner()));
    graph.addSeparator();
    // graph.add(meekOrient);
    graph.add(dagInPattern);
    graph.add(gesOrient);
    graph.addSeparator();
    graph.add(previousGraph);
    graph.add(nextGraph);
    graph.addSeparator();
    graph.add(showDags);
    graph.addSeparator();
    graph.add(new JMenuItem(new SelectBidirectedAction(getWorkbench())));
    graph.add(new JMenuItem(new SelectUndirectedAction(getWorkbench())));
    menuBar.add(graph);
    showDags.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Window owner = (Window) getTopLevelAncestor();
            new WatchedProcess(owner) {

                public void watch() {
                    // Needs to be a pattern search; this isn't checked
                    // before running the algorithm because of allowable
                    // "slop"--e.g. bidirected edges.
                    AlgorithmRunner runner = getAlgorithmRunner();
                    Graph graph = runner.getGraph();
                    if (graph == null) {
                        JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "No result gaph.");
                        return;
                    }
                    if (runner instanceof FgesRunner) {
                        GraphScorer scorer = ((FgesRunner) runner).getGraphScorer();
                        Graph _graph = ((FgesRunner) runner).getTopGraphs().get(getIndex()).getGraph();
                        ScoredGraphsDisplay display = new ScoredGraphsDisplay(_graph, scorer);
                        GraphWorkbench workbench = getWorkbench();
                        EditorWindow editorWindow = new EditorWindow(display, "Independence Facts", "Close", false, workbench);
                        DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
                        editorWindow.setVisible(true);
                    } else {
                        PatternDisplay display = new PatternDisplay(graph);
                        GraphWorkbench workbench = getWorkbench();
                        EditorWindow editorWindow = new EditorWindow(display, "Independence Facts", "Close", false, workbench);
                        DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
                        editorWindow.setVisible(true);
                    }
                }
            };
        }
    });
    // meekOrient.addActionListener(new ActionListener() {
    // public void actionPerformed(ActionEvent e) {
    // ImpliedOrientation rules = getAlgorithmRunner().getMeekRules();
    // rules.setKnowledge((IKnowledge) getAlgorithmRunner().getParams().get("knowledge", new Knowledge2()));
    // rules.orientImplied(getGraph());
    // getGraphHistory().add(getGraph());
    // getWorkbench().setGraph(getGraph());
    // firePropertyChange("modelChanged", null, null);
    // }
    // });
    dagInPattern.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Graph graph = new EdgeListGraph(getGraph());
            // Removing bidirected edges from the pattern before selecting a DAG.                                   4
            for (Edge edge : graph.getEdges()) {
                if (Edges.isBidirectedEdge(edge)) {
                    graph.removeEdge(edge);
                }
            }
            PatternToDag search = new PatternToDag(new EdgeListGraphSingleConnections(graph));
            Graph dag = search.patternToDagMeek();
            getGraphHistory().add(dag);
            getWorkbench().setGraph(dag);
            ((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(dag);
            firePropertyChange("modelChanged", null, null);
        }
    });
    gesOrient.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            DataModel dataModel = getAlgorithmRunner().getDataModel();
            final Graph graph = SearchGraphUtils.reorient(getGraph(), dataModel, getKnowledge());
            getGraphHistory().add(graph);
            getWorkbench().setGraph(graph);
            firePropertyChange("modelChanged", null, null);
        }
    });
    nextGraph.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Graph next = getGraphHistory().next();
            getWorkbench().setGraph(next);
            ((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(next);
            firePropertyChange("modelChanged", null, null);
        }
    });
    previousGraph.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Graph previous = getGraphHistory().previous();
            getWorkbench().setGraph(previous);
            ((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(previous);
            firePropertyChange("modelChanged", null, null);
        }
    });
    menuBar.add(new LayoutMenu(this));
}
Also used : LayoutMenu(edu.cmu.tetradapp.workbench.LayoutMenu) ActionEvent(java.awt.event.ActionEvent) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) ActionListener(java.awt.event.ActionListener) GraphWorkbench(edu.cmu.tetradapp.workbench.GraphWorkbench)

Example 9 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.

the class IonSearchEditor method addSpecialMenus.

protected void addSpecialMenus(JMenuBar menuBar) {
    if (!(getAlgorithmRunner() instanceof IGesRunner)) {
        JMenu test = new JMenu("Independence");
        menuBar.add(test);
        IndTestMenuItems.addIndependenceTestChoices(test, this);
    // test.addSeparator();
    // 
    // AlgorithmRunner algorithmRunner = getAlgorithmRunner();
    // if (algorithmRunner instanceof IndTestProducer) {
    // IndTestProducer p = (IndTestProducer) algorithmRunner;
    // IndependenceFactsAction action =
    // new IndependenceFactsAction(this, p, "Independence Facts...");
    // test.add(action);
    // }
    }
    JMenu graph = new JMenu("Graph");
    JMenuItem showDags = new JMenuItem("Show DAGs in forbid_latent_common_causes");
    // JMenuItem meekOrient = new JMenuItem("Meek Orientation");
    JMenuItem dagInPattern = new JMenuItem("Choose DAG in forbid_latent_common_causes");
    JMenuItem gesOrient = new JMenuItem("Global Score-based Reorientation");
    JMenuItem nextGraph = new JMenuItem("Next Graph");
    JMenuItem previousGraph = new JMenuItem("Previous Graph");
    // graph.add(new LayoutMenu(this));
    graph.add(new GraphPropertiesAction(getWorkbench()));
    graph.add(new PathsAction(getWorkbench()));
    // graph.add(new DirectedPathsAction(getWorkbench()));
    // graph.add(new TreksAction(getWorkbench()));
    // graph.add(new AllPathsAction(getWorkbench()));
    // graph.add(new NeighborhoodsAction(getWorkbench()));
    graph.add(new TriplesAction(getWorkbench().getGraph(), getAlgorithmRunner()));
    graph.addSeparator();
    // graph.add(meekOrient);
    graph.add(dagInPattern);
    graph.add(gesOrient);
    graph.addSeparator();
    graph.add(previousGraph);
    graph.add(nextGraph);
    graph.addSeparator();
    graph.add(showDags);
    graph.addSeparator();
    graph.add(new JMenuItem(new SelectBidirectedAction(getWorkbench())));
    graph.add(new JMenuItem(new SelectUndirectedAction(getWorkbench())));
    menuBar.add(graph);
    showDags.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Window owner = (Window) getTopLevelAncestor();
            new WatchedProcess(owner) {

                public void watch() {
                    // Needs to be a pattern search; this isn't checked
                    // before running the algorithm because of allowable
                    // "slop"--e.g. bidirected edges.
                    AlgorithmRunner runner = getAlgorithmRunner();
                    Graph graph = runner.getGraph();
                    if (graph == null) {
                        JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "No result gaph.");
                        return;
                    }
                    // if (runner instanceof ImagesRunner) {
                    // GraphScorer scorer = ((ImagesRunner) runner).getGraphScorer();
                    // Graph _graph = ((ImagesRunner) runner).getTopGraphs().get(getIndex()).getGraph();
                    // 
                    // ScoredGraphsDisplay display = new ScoredGraphsDisplay(_graph, scorer);
                    // GraphWorkbench workbench = getWorkbench();
                    // 
                    // EditorWindow editorWindow =
                    // new EditorWindow(display, "Independence Facts",
                    // "Close", false, workbench);
                    // DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
                    // editorWindow.setVisible(true);
                    // }
                    // else {
                    PatternDisplay display = new PatternDisplay(graph);
                    GraphWorkbench workbench = getWorkbench();
                    EditorWindow editorWindow = new EditorWindow(display, "Independence Facts", "Close", false, workbench);
                    DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
                    editorWindow.setVisible(true);
                // }
                }
            };
        }
    });
    // meekOrient.addActionListener(new ActionListener() {
    // public void actionPerformed(ActionEvent e) {
    // ImpliedOrientation rules = getAlgorithmRunner().getMeekRules();
    // rules.setKnowledge((IKnowledge) getAlgorithmRunner().getParams().get("knowledge", new Knowledge2()));
    // rules.orientImplied(getGraph());
    // getGraphHistory().add(getGraph());
    // getWorkbench().setGraph(getGraph());
    // firePropertyChange("modelChanged", null, null);
    // }
    // });
    dagInPattern.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Graph graph = new EdgeListGraph(getGraph());
            // Removing bidirected edges from the pattern before selecting a DAG.                                   4
            for (Edge edge : graph.getEdges()) {
                if (Edges.isBidirectedEdge(edge)) {
                    graph.removeEdge(edge);
                }
            }
            PatternToDag search = new PatternToDag(new EdgeListGraphSingleConnections(graph));
            Graph dag = search.patternToDagMeek();
            getGraphHistory().add(dag);
            getWorkbench().setGraph(dag);
            ((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(dag);
            firePropertyChange("modelChanged", null, null);
        }
    });
    gesOrient.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            DataModel dataModel = getAlgorithmRunner().getDataModel();
            final Graph graph = SearchGraphUtils.reorient(getGraph(), dataModel, getKnowledge());
            getGraphHistory().add(graph);
            getWorkbench().setGraph(graph);
            firePropertyChange("modelChanged", null, null);
        }
    });
    nextGraph.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Graph next = getGraphHistory().next();
            getWorkbench().setGraph(next);
            ((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(next);
            firePropertyChange("modelChanged", null, null);
        }
    });
    previousGraph.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Graph previous = getGraphHistory().previous();
            getWorkbench().setGraph(previous);
            ((AbstractAlgorithmRunner) getAlgorithmRunner()).setResultGraph(previous);
            firePropertyChange("modelChanged", null, null);
        }
    });
    // if (getAlgorithmRunner().supportsKnowledge()) {
    // menuBar.add(new Knowledge2Menu(this));
    // }
    menuBar.add(new LayoutMenu(this));
}
Also used : LayoutMenu(edu.cmu.tetradapp.workbench.LayoutMenu) PatternToDag(edu.cmu.tetrad.search.PatternToDag) ActionEvent(java.awt.event.ActionEvent) WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) ActionListener(java.awt.event.ActionListener) GraphWorkbench(edu.cmu.tetradapp.workbench.GraphWorkbench) DataModel(edu.cmu.tetrad.data.DataModel)

Example 10 with WatchedProcess

use of edu.cmu.tetradapp.util.WatchedProcess in project tetrad by cmu-phil.

the class LingSearchEditor method execute.

/**
 * Executes the algorithm. The execution takes place inside a thread, so one
 * cannot count on a result graph having been found when the method
 */
public void execute() {
    Window owner = (Window) getTopLevelAncestor();
    final WatchedProcess process = new WatchedProcess(owner) {

        public void watch() {
            getExecuteButton().setEnabled(false);
            setErrorMessage(null);
            if (!knowledgeMessageShown) {
                IKnowledge knowledge = (IKnowledge) getAlgorithmRunner().getParams().get("knowledge", new Knowledge2());
                if (!knowledge.isEmpty()) {
                    JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Using previously set knowledge. (To edit, use " + "the Knowledge menu.)");
                    knowledgeMessageShown = true;
                }
            }
            try {
                storeLatestWorkbenchGraph();
                getAlgorithmRunner().execute();
                LingRunner runner = (LingRunner) getAlgorithmRunner();
                arrangeGraphs();
                lingDisplay.resetGraphs(runner.getStoredGraphs());
            } catch (Exception e) {
                CharArrayWriter writer1 = new CharArrayWriter();
                PrintWriter writer2 = new PrintWriter(writer1);
                e.printStackTrace(writer2);
                String message = writer1.toString();
                writer2.close();
                e.printStackTrace(System.out);
                TetradLogger.getInstance().error(message);
                String messageString = e.getMessage();
                if (e.getCause() != null) {
                    messageString = e.getCause().getMessage();
                }
                if (messageString == null) {
                    messageString = message;
                }
                setErrorMessage(messageString);
                TetradLogger.getInstance().error("************Algorithm stopped!");
                getExecuteButton().setEnabled(true);
                throw new RuntimeException(e);
            }
            getWorkbenchScroll().setBorder(new TitledBorder(getResultLabel()));
            Graph resultGraph = resultGraph();
            doDefaultArrangement(resultGraph);
            getWorkbench().setBackground(Color.WHITE);
            getWorkbench().setGraph(resultGraph);
            getGraphHistory().clear();
            getGraphHistory().add(resultGraph);
            getWorkbench().repaint();
            // For Mimbuild, e.g., that need to do a second stage.
            firePropertyChange("algorithmFinished", null, null);
            getExecuteButton().setEnabled(true);
            firePropertyChange("modelChanged", null, null);
        }
    };
    Thread watcher = new Thread() {

        public void run() {
            while (true) {
                try {
                    sleep(300);
                    if (!process.isAlive()) {
                        getExecuteButton().setEnabled(true);
                        return;
                    }
                } catch (InterruptedException e) {
                    getExecuteButton().setEnabled(true);
                    return;
                }
            }
        }
    };
    watcher.start();
}
Also used : WatchedProcess(edu.cmu.tetradapp.util.WatchedProcess) Knowledge2(edu.cmu.tetrad.data.Knowledge2) TitledBorder(javax.swing.border.TitledBorder) CharArrayWriter(java.io.CharArrayWriter) IKnowledge(edu.cmu.tetrad.data.IKnowledge) LingRunner(edu.cmu.tetradapp.model.LingRunner) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) PrintWriter(java.io.PrintWriter)

Aggregations

WatchedProcess (edu.cmu.tetradapp.util.WatchedProcess)24 ActionEvent (java.awt.event.ActionEvent)13 ActionListener (java.awt.event.ActionListener)13 GraphWorkbench (edu.cmu.tetradapp.workbench.GraphWorkbench)8 LayoutMenu (edu.cmu.tetradapp.workbench.LayoutMenu)7 TitledBorder (javax.swing.border.TitledBorder)6 CharArrayWriter (java.io.CharArrayWriter)5 PrintWriter (java.io.PrintWriter)5 DataModel (edu.cmu.tetrad.data.DataModel)4 IKnowledge (edu.cmu.tetrad.data.IKnowledge)4 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)4 Parameters (edu.cmu.tetrad.util.Parameters)4 Window (java.awt.Window)4 PatternToDag (edu.cmu.tetrad.search.PatternToDag)3 EditorWindow (edu.cmu.tetradapp.editor.EditorWindow)3 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)2 Graph (edu.cmu.tetrad.graph.Graph)2 Container (java.awt.Container)2 IOException (java.io.IOException)2 List (java.util.List)2