
com.github.vangj.jbayes.inf.prob.json.JsonGraph Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbayes-inference Show documentation
Show all versions of jbayes-inference Show documentation
A very cool project for BBN inference using approximate and exact algorithms.
The newest version!
package com.github.vangj.jbayes.inf.prob.json;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.vangj.jbayes.inf.prob.Graph;
import com.github.vangj.jbayes.inf.prob.Node;
import com.github.vangj.jbayes.inf.prob.util.CptUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* JSON graph, used for serialization/deserialization (serde).
*/
@JsonInclude(content = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class JsonGraph {
private Map nodes;
private Map> parents;
private Map cpts;
public JsonGraph() {
}
public JsonGraph(Graph g) {
nodes = new HashMap<>();
parents = new HashMap<>();
cpts = new HashMap<>();
g.getNodes().forEach(n -> nodes.put(n.getName(), n));
g.getNodes().forEach(n -> {
List pa = new ArrayList<>();
n.getParents().forEach(p -> pa.add(p.getName()));
parents.put(n.getName(), pa);
});
g.getNodes().forEach(n -> cpts.put(n.getName(), CptUtil.getMatrix(n.getCpt())));
}
public Map getCpts() {
return cpts;
}
public Map getNodes() {
return nodes;
}
public Map> getParents() {
return parents;
}
/**
* Converts JsonGraph to Graph.
*
* @return Graph.
*/
public Graph toGraph() {
nodes.forEach((name, node) -> {
if (parents.containsKey(name)) {
parents.get(name).forEach(paName -> node.addParent(nodes.get(paName)));
}
node.setCpt(cpts.get(name));
});
Graph g = new Graph();
nodes.values().forEach(n -> g.addNode(n));
return g;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy