use of org.batfish.datamodel.RoleEdge in project batfish by batfish.
the class InferRoles method computeRoleScore.
private double computeRoleScore(String regex) {
SortedMap<String, SortedSet<String>> nodeRolesMap = regexToRoleSpecifier(regex).createNodeRolesMap(new TreeSet<>(_nodes));
Topology topology = _batfish.getEnvironmentTopology();
// produce a role-level topology and the list of nodes in each edge's source role
// that have an edge to some node in the edge's target role
SortedMap<RoleEdge, SortedSet<String>> roleEdges = new TreeMap<>();
for (Edge e : topology.getEdges()) {
String n1 = e.getNode1();
String n2 = e.getNode2();
SortedSet<String> roles1 = nodeRolesMap.get(n1);
SortedSet<String> roles2 = nodeRolesMap.get(n2);
if (roles1 != null && roles2 != null && roles1.size() == 1 && roles2.size() == 1) {
String role1 = roles1.first();
String role2 = roles2.first();
// ignore self-edges
if (role1.equals(role2)) {
continue;
}
RoleEdge redge = new RoleEdge(role1, role2);
SortedSet<String> roleEdgeNodes = roleEdges.getOrDefault(redge, new TreeSet<>());
roleEdgeNodes.add(n1);
roleEdges.put(redge, roleEdgeNodes);
}
}
int numEdges = roleEdges.size();
if (numEdges == 0) {
return 0.0;
}
// compute the "support" of each edge in the role-level topology:
// the percentage of nodes playing the source role that have an edge
// to a node in the target role.
// the score of this regex is then the average support across all role edges
SortedMap<String, SortedSet<String>> roleNodesMap = regexToRoleSpecifier(regex).createRoleNodesMap(_configurations.keySet());
double supportSum = 0.0;
for (Map.Entry<RoleEdge, SortedSet<String>> roleEdgeCount : roleEdges.entrySet()) {
RoleEdge redge = roleEdgeCount.getKey();
int count = roleEdgeCount.getValue().size();
supportSum += (double) count / roleNodesMap.get(redge.getRole1()).size();
}
return supportSum / numEdges;
}
Aggregations