use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.
the class KnowledgeBoxEditor method resetEdgeDisplayOld.
/**
* This is an old method that needs to be removed when cleaning up code.
*/
private void resetEdgeDisplayOld() {
List<String> varNames = getVarNames();
IKnowledge knowledge = getKnowledge();
for (String name : varNames) {
knowledge.addVariable(name);
}
KnowledgeGraph graph = new KnowledgeGraph(getKnowledge());
for (String varName : varNames) {
graph.addNode(new KnowledgeModelNode(varName));
}
if (this.showRequiredByGroups) {
for (Iterator<KnowledgeEdge> i = knowledge.requiredEdgesIterator(); i.hasNext(); ) {
KnowledgeEdge edge = i.next();
String from = edge.getFrom();
String to = edge.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) {
for (Iterator<KnowledgeEdge> i = knowledge.forbiddenEdgesIterator(); i.hasNext(); ) {
KnowledgeEdge edge = i.next();
String from = edge.getFrom();
String to = edge.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) {
for (Iterator<KnowledgeEdge> i = knowledge.explicitlyRequiredEdgesIterator(); i.hasNext(); ) {
KnowledgeEdge pair = i.next();
String from = pair.getFrom();
String to = pair.getTo();
KnowledgeModelNode fromNode = (KnowledgeModelNode) graph.getNode(from);
KnowledgeModelNode toNode = (KnowledgeModelNode) graph.getNode(to);
if (fromNode == null || toNode == null) {
continue;
}
graph.addEdge(new KnowledgeModelEdge(fromNode, toNode, KnowledgeModelEdge.REQUIRED));
}
}
if (showForbiddenByTiers) {
for (Iterator<KnowledgeEdge> i = knowledge.forbiddenEdgesIterator(); i.hasNext(); ) {
KnowledgeEdge pair = i.next();
String from = pair.getFrom();
String to = pair.getTo();
if (!knowledge.isForbiddenByTiers(from, to)) {
continue;
}
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);
// Hacked fix. Jdramsey.
// if (!graph.containsEdge(knowledgeModelEdge)) {
// graph.addEdge(knowledgeModelEdge);
// }
}
}
if (showForbiddenExplicitly) {
for (Iterator<KnowledgeEdge> i = knowledge.explicitlyForbiddenEdgesIterator(); i.hasNext(); ) {
KnowledgeEdge pair = i.next();
String from = pair.getFrom();
String to = pair.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();
}
}
use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.
the class TsFci method fciOrientbk.
/**
* Orients according to background knowledge
*/
private void fciOrientbk(IKnowledge bk, Graph graph, List<Node> variables) {
logger.log("info", "Starting BK Orientation.");
for (Iterator<KnowledgeEdge> it = bk.forbiddenEdgesIterator(); it.hasNext(); ) {
if (Thread.currentThread().isInterrupted()) {
break;
}
KnowledgeEdge edge = it.next();
// match strings to variables in the graph.
Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
Node to = SearchGraphUtils.translate(edge.getTo(), variables);
if (from == null || to == null) {
continue;
}
if (graph.getEdge(from, to) == null) {
continue;
}
// Orient to*->from
graph.setEndpoint(to, from, Endpoint.ARROW);
graph.setEndpoint(from, to, Endpoint.CIRCLE);
logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
}
for (Iterator<KnowledgeEdge> it = bk.requiredEdgesIterator(); it.hasNext(); ) {
if (Thread.currentThread().isInterrupted()) {
break;
}
KnowledgeEdge edge = it.next();
// match strings to variables in this graph
Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
Node to = SearchGraphUtils.translate(edge.getTo(), variables);
if (from == null || to == null) {
continue;
}
if (graph.getEdge(from, to) == null) {
continue;
}
graph.setEndpoint(to, from, Endpoint.TAIL);
graph.setEndpoint(from, to, Endpoint.ARROW);
logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
}
logger.log("info", "Finishing BK Orientation.");
}
use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.
the class TsFges2 method addRequiredEdges.
// Adds edges required by knowledge.
private void addRequiredEdges(Graph graph) {
if (!existsKnowledge())
return;
for (Iterator<KnowledgeEdge> it = getKnowledge().requiredEdgesIterator(); it.hasNext(); ) {
if (Thread.currentThread().isInterrupted()) {
break;
}
KnowledgeEdge next = it.next();
Node nodeA = graph.getNode(next.getFrom());
Node nodeB = graph.getNode(next.getTo());
if (!graph.isAncestorOf(nodeB, nodeA)) {
graph.removeEdges(nodeA, nodeB);
graph.addDirectedEdge(nodeA, nodeB);
TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeA, nodeB));
}
}
for (Edge edge : graph.getEdges()) {
if (Thread.currentThread().isInterrupted()) {
break;
}
final String A = edge.getNode1().getName();
final String B = edge.getNode2().getName();
if (knowledge.isForbidden(A, B)) {
Node nodeA = edge.getNode1();
Node nodeB = edge.getNode2();
if (nodeA == null || nodeB == null)
throw new NullPointerException();
if (graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) {
if (!graph.isAncestorOf(nodeA, nodeB)) {
graph.removeEdges(nodeA, nodeB);
graph.addDirectedEdge(nodeB, nodeA);
TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
}
}
if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) {
if (!graph.isAncestorOf(nodeA, nodeB)) {
graph.removeEdges(nodeA, nodeB);
graph.addDirectedEdge(nodeB, nodeA);
TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
}
}
} else if (knowledge.isForbidden(B, A)) {
Node nodeA = edge.getNode2();
Node nodeB = edge.getNode1();
if (nodeA == null || nodeB == null)
throw new NullPointerException();
if (graph.isAdjacentTo(nodeA, nodeB) && !graph.isChildOf(nodeA, nodeB)) {
if (!graph.isAncestorOf(nodeA, nodeB)) {
graph.removeEdges(nodeA, nodeB);
graph.addDirectedEdge(nodeB, nodeA);
TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
}
}
if (!graph.isChildOf(nodeA, nodeB) && getKnowledge().isForbidden(nodeA.getName(), nodeB.getName())) {
if (!graph.isAncestorOf(nodeA, nodeB)) {
graph.removeEdges(nodeA, nodeB);
graph.addDirectedEdge(nodeB, nodeA);
TetradLogger.getInstance().log("insertedEdges", "Adding edge by knowledge: " + graph.getEdge(nodeB, nodeA));
}
}
}
}
}
use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.
the class Cfci method fciOrientbk.
/**
* Orients according to background knowledge
*/
private void fciOrientbk(IKnowledge bk, Graph graph, List<Node> variables) {
if (verbose) {
logger.log("info", "Starting BK Orientation.");
}
for (Iterator<KnowledgeEdge> it = bk.forbiddenEdgesIterator(); it.hasNext(); ) {
KnowledgeEdge edge = it.next();
// match strings to variables in the graph.
Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
Node to = SearchGraphUtils.translate(edge.getTo(), variables);
if (from == null || to == null) {
continue;
}
if (graph.getEdge(from, to) == null) {
continue;
}
// Orient to*->from
graph.setEndpoint(to, from, Endpoint.ARROW);
if (verbose) {
logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
}
}
for (Iterator<KnowledgeEdge> it = bk.requiredEdgesIterator(); it.hasNext(); ) {
KnowledgeEdge edge = it.next();
// match strings to variables in this graph
Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
Node to = SearchGraphUtils.translate(edge.getTo(), variables);
if (from == null || to == null) {
continue;
}
if (graph.getEdge(from, to) == null) {
continue;
}
if (verbose) {
System.out.println("Rule T3: Orienting " + from + "-->" + to);
}
graph.setEndpoint(to, from, Endpoint.TAIL);
graph.setEndpoint(from, to, Endpoint.ARROW);
if (verbose) {
logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
}
}
if (verbose) {
logger.log("info", "Finishing BK Orientation.");
}
}
use of edu.cmu.tetrad.data.KnowledgeEdge in project tetrad by cmu-phil.
the class FciMax method fciOrientbk.
// ===========================PRIVATE METHODS=========================//
/**
* Orients according to background knowledge
*/
private void fciOrientbk(IKnowledge bk, Graph graph, List<Node> variables) {
logger.log("info", "Starting BK Orientation.");
for (Iterator<KnowledgeEdge> it = bk.forbiddenEdgesIterator(); it.hasNext(); ) {
KnowledgeEdge edge = it.next();
// match strings to variables in the graph.
Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
Node to = SearchGraphUtils.translate(edge.getTo(), variables);
if (from == null || to == null) {
continue;
}
if (graph.getEdge(from, to) == null) {
continue;
}
// Orient to*->from
graph.setEndpoint(to, from, Endpoint.ARROW);
graph.setEndpoint(from, to, Endpoint.CIRCLE);
logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
}
for (Iterator<KnowledgeEdge> it = bk.requiredEdgesIterator(); it.hasNext(); ) {
KnowledgeEdge edge = it.next();
// match strings to variables in this graph
Node from = SearchGraphUtils.translate(edge.getFrom(), variables);
Node to = SearchGraphUtils.translate(edge.getTo(), variables);
if (from == null || to == null) {
continue;
}
if (graph.getEdge(from, to) == null) {
continue;
}
graph.setEndpoint(to, from, Endpoint.TAIL);
graph.setEndpoint(from, to, Endpoint.ARROW);
logger.log("knowledgeOrientation", SearchLogUtils.edgeOrientedMsg("Knowledge", graph.getEdge(from, to)));
}
logger.log("info", "Finishing BK Orientation.");
}
Aggregations