net.sf.tweety.commons.util.rules.DerivationGraph Maven / Gradle / Ivy
package net.sf.tweety.commons.util.rules;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.sf.tweety.commons.Formula;
import net.sf.tweety.commons.util.Digraph;
import net.sf.tweety.commons.util.DigraphNode;
/**
*
* @author Nils Geilen
* expands a set of formulae to a graph where nodes are formulae and edges show, which forumla can be composed of which formulae
* @param a formula type
* @param a rule type F^n -> F
*/
public class DerivationGraph> extends Digraph {
public DerivationGraph() {
super(false);
}
/**
* expands the rule set
* @param rules a collection of rules
*/
public void allDerivations (Collection rules) {
Collection open = new ArrayList<>();
open.addAll(rules);
for(R r:open)
if (r.isFact())
this.addNode(r);
open.removeAll(getValues());
while(true) {
int noe = this.numberOfEdges();
rule_loop: for(R r: open) {
List params = new ArrayList<>();
params.addAll(r.getPremise());
List>> pre = new ArrayList<>();
for(int i = 0 ; i < params.size() ; i++) {
pre.add(new ArrayList<>());
F param = params.get(i);
for(DigraphNode n:this)
if(n.getValue().getConclusion().equals(param))
pre.get(i).add(n);
}
for(List> l:pre) {
if(l.isEmpty())
continue rule_loop;
}
Digraph> param_tree = new Digraph<>(false);
param_tree.addNode(null);
for(List> l:pre) {
Collection>> leafs = param_tree.getLeafs();
for(DigraphNode> leaf:leafs)
for(DigraphNode n:l){
leaf.addEdge(param_tree.addNode(n));
}
}
for(DigraphNode> leaf: param_tree.getLeafs()) {
List> path = new ArrayList<>();
do {
path.add(leaf.getValue());
leaf = leaf.getParent();
} while (! leaf.isRoot());
if(containsNode(r, path))
continue;
DigraphNode new_node = this.addNode(r);
for(DigraphNode node: path)
node.addEdge(new_node);
}
}
//printTrees(System.out);
//System.out.println(size());
//System.out.println(numberOfEdges());
if(noe == this.numberOfEdges())
break;
}
}
private boolean containsNode(R value, List> parents) {
next_node : for(DigraphNode node:this) {
if(node.getValue().equals(value)) {
for(DigraphNode n:parents) {
if(!node.getParents().contains(n))
continue next_node;
}
return true;
}
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy