Search in sources :

Example 26 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class KnowledgeBoxEditor method resetTextDisplay.

private void resetTextDisplay() {
    getTextArea().setFont(new Font("Monospaced", Font.PLAIN, 12));
    getTextArea().setBorder(new CompoundBorder(new LineBorder(Color.black), new EmptyBorder(3, 3, 3, 3)));
    try {
        IKnowledge knowledge = getKnowledge();
        CharArrayWriter out = new CharArrayWriter();
        Knowledge.saveKnowledge(knowledge, out);
        getTextArea().setText(out.toString());
    } catch (IOException e) {
        getTextArea().setText("Could not render knowledge.");
    }
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) LineBorder(javax.swing.border.LineBorder) CompoundBorder(javax.swing.border.CompoundBorder) IOException(java.io.IOException) EmptyBorder(javax.swing.border.EmptyBorder) Font(java.awt.Font) CharArrayWriter(java.io.CharArrayWriter)

Example 27 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class KnowledgeBoxEditor method resetEdgeDisplay.

private void resetEdgeDisplay(JCheckBox checkBox) {
    IKnowledge knowledge = getKnowledge();
    KnowledgeGraph graph = new KnowledgeGraph(getKnowledge());
    getVarNames().forEach(e -> {
        knowledge.addVariable(e);
        graph.addNode(new KnowledgeModelNode(e));
    });
    if (this.showRequiredByGroups) {
        List<KnowledgeEdge> list = knowledge.getListOfRequiredEdges();
        if (list.size() > EDGE_LIMIT) {
            showRequiredByGroups = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                if (knowledge.isRequiredByGroups(from, to)) {
                    KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                    KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                    graph.addEdge(new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.REQUIRED_BY_GROUPS));
                }
            });
        }
    }
    if (this.showForbiddenByGroups) {
        List<KnowledgeEdge> list = knowledge.getListOfForbiddenEdges();
        if (list.size() > EDGE_LIMIT) {
            showForbiddenByGroups = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                if (knowledge.isForbiddenByGroups(from, to)) {
                    KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                    KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                    graph.addEdge(new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.FORBIDDEN_BY_GROUPS));
                }
            });
        }
    }
    if (showRequired) {
        List<KnowledgeEdge> list = knowledge.getListOfExplicitlyRequiredEdges();
        if (list.size() > EDGE_LIMIT) {
            showRequired = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                if (!(fromNode == null || toNode == null)) {
                    graph.addEdge(new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.REQUIRED));
                }
            });
        }
    }
    if (showForbiddenByTiers) {
        List<KnowledgeEdge> list = knowledge.getListOfForbiddenEdges();
        if (list.size() > EDGE_LIMIT) {
            showForbiddenByTiers = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                if (knowledge.isForbiddenByTiers(from, to)) {
                    KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                    KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                    if (fromNode == null) {
                        graph.addNode(new KnowledgeModelNode(from));
                        fromNode = (KnowledgeModelNode) graph.getNode(from);
                    }
                    if (toNode == null) {
                        graph.addNode(new KnowledgeModelNode(to));
                        toNode = (KnowledgeModelNode) graph.getNode(to);
                    }
                    KnowledgeModelEdge knowledgeModelEdge = new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.FORBIDDEN_BY_TIERS);
                    graph.addEdge(knowledgeModelEdge);
                }
            });
        }
    }
    if (showForbiddenExplicitly) {
        List<KnowledgeEdge> list = knowledge.getListOfExplicitlyForbiddenEdges();
        if (list.size() > EDGE_LIMIT) {
            showForbiddenExplicitly = false;
            if (checkBox != null) {
                checkBox.setSelected(false);
            }
            String errMsg = String.format("The number of edges to show exceeds the limit %d.", EDGE_LIMIT);
            JOptionPane.showMessageDialog(this, errMsg, "Unable To Display Edges", JOptionPane.ERROR_MESSAGE);
        } else {
            list.forEach(e -> {
                String from = e.getFrom();
                String to = e.getTo();
                KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
                KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
                KnowledgeModelEdge edge = new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.FORBIDDEN_EXPLICITLY);
                if (!graph.containsEdge(edge)) {
                    graph.addEdge(edge);
                }
            });
        }
    }
    boolean arrangedAll = GraphUtils.arrangeBySourceGraph(graph, edgeWorkbench.getGraph());
    if (!arrangedAll) {
        GraphUtils.circleLayout(graph, 200, 200, 150);
    }
    edgeWorkbench.setGraph(graph);
    if (knowledgeBoxModel != null) {
        notifyKnowledge();
    }
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge)

Example 28 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class DataAlgorithmRunner method execute.

// ===================PUBLIC METHODS OVERRIDING ABSTRACT================//
public void execute() {
    IKnowledge knowledge = (IKnowledge) getParams().get("knowledge", new Knowledge2());
    int depth = getParams().getInt("depth", -1);
    Graph graph;
    Pc pc = new Pc(getIndependenceTest());
    pc.setKnowledge(knowledge);
    pc.setAggressivelyPreventCycles(isAggressivelyPreventCycles());
    pc.setDepth(depth);
    pc.setInitialGraph(initialGraph);
    graph = pc.search();
    System.out.println(graph);
    if (getSourceGraph() != null) {
        GraphUtils.arrangeBySourceGraph(graph, getSourceGraph());
    } else if (knowledge.isDefaultToKnowledgeLayout()) {
        SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
    } else {
        GraphUtils.circleLayout(graph, 200, 200, 150);
    }
    setResultGraph(graph);
    setPcFields(pc);
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) Knowledge2(edu.cmu.tetrad.data.Knowledge2)

Example 29 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class KnowledgeLoaderWizard method actionPerformed.

/**
 * Performs the action of loading a session from a file.
 */
public void actionPerformed(ActionEvent e) {
    int ret = 1;
    while (ret == 1) {
        JFileChooser chooser = getJFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        Component comp = (this.knowledgeEditable instanceof Component) ? (Component) this.knowledgeEditable : null;
        chooser.showOpenDialog(comp);
        File file = chooser.getSelectedFile();
        if (file != null) {
            Preferences.userRoot().put("fileSaveLocation", file.getParent());
        }
        KnowledgeLoaderWizard wizard = new KnowledgeLoaderWizard(file, knowledgeEditable);
        wizard.setCommentIndicator(commentIndicator);
        ret = JOptionPane.showOptionDialog(null, wizard, "Knowledge Import Wizard", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new String[] { "Cancel", "Select Another File", "Import Data" }, "Import Data");
        this.delimiters = wizard.getDelimiters();
        this.commentIndicator = wizard.getCommentIndicator();
        // Import...
        if (ret == JOptionPane.OK_OPTION) {
            try {
                DataReader reader = new DataReader();
                IKnowledge knowledge = reader.parseKnowledge(file);
                this.knowledgeEditable.setKnowledge(knowledge);
            } catch (Exception e1) {
                String message = e1.getMessage() == null ? e1.getClass().getName() : e1.getMessage();
                if ("".equals(message)) {
                    message = "Could not load knowledge.";
                }
                JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), message);
            }
        }
    }
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) DataReader(edu.cmu.tetrad.data.DataReader) File(java.io.File) IOException(java.io.IOException)

Example 30 with IKnowledge

use of edu.cmu.tetrad.data.IKnowledge in project tetrad by cmu-phil.

the class LofsSearchEditor method layoutByKnowledge.

public void layoutByKnowledge() {
    GraphWorkbench resultWorkbench = getWorkbench();
    Graph graph = resultWorkbench.getGraph();
    IKnowledge knowledge = (IKnowledge) getAlgorithmRunner().getParams().get("knowledge", new Knowledge2());
    SearchGraphUtils.arrangeByKnowledgeTiers(graph, knowledge);
// resultWorkbench.setGraph(graph);
}
Also used : IKnowledge(edu.cmu.tetrad.data.IKnowledge) GraphWorkbench(edu.cmu.tetradapp.workbench.GraphWorkbench) Knowledge2(edu.cmu.tetrad.data.Knowledge2)

Aggregations

IKnowledge (edu.cmu.tetrad.data.IKnowledge)58 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)46 Parameters (edu.cmu.tetrad.util.Parameters)18 Graph (edu.cmu.tetrad.graph.Graph)12 ArrayList (java.util.ArrayList)10 GraphWorkbench (edu.cmu.tetradapp.workbench.GraphWorkbench)8 IOException (java.io.IOException)7 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)5 CharArrayWriter (java.io.CharArrayWriter)5 DataReader (edu.cmu.tetrad.data.DataReader)4 DataSet (edu.cmu.tetrad.data.DataSet)4 Node (edu.cmu.tetrad.graph.Node)4 WatchedProcess (edu.cmu.tetradapp.util.WatchedProcess)4 File (java.io.File)4 PrintWriter (java.io.PrintWriter)4 List (java.util.List)4 KnowledgeBoxInput (edu.cmu.tetrad.data.KnowledgeBoxInput)3 KnowledgeEdge (edu.cmu.tetrad.data.KnowledgeEdge)3 Test (org.junit.Test)3 BdeuScore (edu.cmu.tetrad.algcomparison.score.BdeuScore)2