Search in sources :

Example 1 with LingRunner

use of edu.cmu.tetradapp.model.LingRunner 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)

Example 2 with LingRunner

use of edu.cmu.tetradapp.model.LingRunner in project tetrad by cmu-phil.

the class LingSearchEditor method arrangeGraphs.

private Ling.StoredGraphs arrangeGraphs() {
    LingRunner runner = (LingRunner) getAlgorithmRunner();
    Graph resultGraph = runner.getResultGraph();
    Ling.StoredGraphs storedGraphs = runner.getStoredGraphs();
    if (storedGraphs == null)
        storedGraphs = new Ling.StoredGraphs();
    Graph latestWorkbenchGraph = (Graph) runner.getParams().get("sourceGraph", null);
    Graph sourceGraph = runner.getSourceGraph();
    boolean arrangedAll = false;
    for (int i = 0; i < storedGraphs.getNumGraphs(); i++) {
        arrangedAll = GraphUtils.arrangeBySourceGraph(storedGraphs.getGraph(i), latestWorkbenchGraph);
    }
    if (!arrangedAll) {
        for (int i = 0; i < storedGraphs.getNumGraphs(); i++) {
            arrangedAll = GraphUtils.arrangeBySourceGraph(resultGraph, sourceGraph);
        }
    }
    if (!arrangedAll) {
        for (int i = 0; i < storedGraphs.getNumGraphs(); i++) {
            GraphUtils.circleLayout(resultGraph, 200, 200, 150);
        }
    }
    return storedGraphs;
}
Also used : LingRunner(edu.cmu.tetradapp.model.LingRunner) Ling(edu.cmu.tetrad.search.Ling) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph)

Aggregations

EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)2 Graph (edu.cmu.tetrad.graph.Graph)2 LingRunner (edu.cmu.tetradapp.model.LingRunner)2 IKnowledge (edu.cmu.tetrad.data.IKnowledge)1 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)1 Ling (edu.cmu.tetrad.search.Ling)1 WatchedProcess (edu.cmu.tetradapp.util.WatchedProcess)1 CharArrayWriter (java.io.CharArrayWriter)1 PrintWriter (java.io.PrintWriter)1 TitledBorder (javax.swing.border.TitledBorder)1