use of org.apache.commons.collections4.map.MultiKeyMap in project pinpoint by naver.
the class HbaseDataSourceListDao method reorderDataSourceListBos.
private List<DataSourceListBo> reorderDataSourceListBos(List<DataSourceListBo> dataSourceListBos) {
// reorder dataSourceBo using id and timeSlot
MultiKeyMap<Long, DataSourceListBo> dataSourceListBoMap = new MultiKeyMap<>();
for (DataSourceListBo dataSourceListBo : dataSourceListBos) {
for (DataSourceBo dataSourceBo : dataSourceListBo.getList()) {
int id = dataSourceBo.getId();
long timestamp = dataSourceBo.getTimestamp();
long timeSlot = AgentStatUtils.getBaseTimestamp(timestamp);
DataSourceListBo mappedDataSourceListBo = dataSourceListBoMap.get(id, timeSlot);
if (mappedDataSourceListBo == null) {
mappedDataSourceListBo = new DataSourceListBo();
mappedDataSourceListBo.setAgentId(dataSourceBo.getAgentId());
mappedDataSourceListBo.setStartTimestamp(dataSourceBo.getStartTimestamp());
mappedDataSourceListBo.setTimestamp(dataSourceBo.getTimestamp());
dataSourceListBoMap.put((long) id, timeSlot, mappedDataSourceListBo);
}
// set fastest timestamp
if (mappedDataSourceListBo.getTimestamp() > dataSourceBo.getTimestamp()) {
mappedDataSourceListBo.setTimestamp(dataSourceBo.getTimestamp());
}
mappedDataSourceListBo.add(dataSourceBo);
}
}
Collection<DataSourceListBo> values = dataSourceListBoMap.values();
return new ArrayList<>(values);
}
use of org.apache.commons.collections4.map.MultiKeyMap in project tetrad by cmu-phil.
the class LayeredDrawing method placeInTiers.
// ============================PRIVATE METHODS=========================//
private List<List<Node>> placeInTiers(Graph graph) {
List<List<Node>> connectedComponents = GraphUtils.connectedComponents(graph);
List<List<Node>> tiers = new ArrayList<>();
for (List<Node> component : connectedComponents) {
// Recursively map each node to its tier inside the component,
// starting with the first node. These tiers are relative and
// can be negative.
Node firstNode = component.get(0);
Map<Node, Integer> componentTiers = new HashMap<>();
placeNodes(firstNode, componentTiers, graph);
// Reverse the map. The domain of this map is now possibly negative
// tiers.
Map reversedMap = new MultiKeyMap();
for (Node _node : component) {
Integer _tier = componentTiers.get(_node);
reversedMap.put(_tier, _node);
}
List<Integer> indices = new ArrayList<>(reversedMap.keySet());
Collections.sort(indices);
for (int i : indices) {
Collection<Node> collection = (Collection<Node>) reversedMap.get(i);
tiers.add(new ArrayList<>(collection));
}
// Do some heuristic uncrossing of edges in successive tiers.
for (int i = 0; i < tiers.size() - 1; i++) {
List<Node> tier1 = tiers.get(i);
List<Node> tier2 = tiers.get(i + 1);
List<Node> reorderTier2 = new ArrayList<>();
for (Node node : tier1) {
List<Node> adj = graph.getAdjacentNodes(node);
adj.retainAll(tier2);
adj.removeAll(reorderTier2);
reorderTier2.addAll(adj);
}
// List<Node> saveArray = new ArrayList<Node>();
// int saveCrossings = Integer.MAX_VALUE;
//
// for (int j = 0; j < 4 * tier2.size(); j++) {
// Collections.shuffle(tier2);
// int numCrossings = numCrossings(tier1, tier2, graph);
//
// if (numCrossings < saveCrossings) {
// saveArray = new ArrayList<Node>(tier2);
// saveCrossings = numCrossings;
// }
// }
tiers.set(i + 1, reorderTier2);
// tiers.set(i + 1, saveArray);
}
}
return tiers;
}
use of org.apache.commons.collections4.map.MultiKeyMap in project tetrad by cmu-phil.
the class SearchGraphUtils method getReachableNodes.
/**
* @param initialNodes The nodes that reachability undirectedPaths start from.
* @param legalPairs Specifies initial edges (given initial nodes) and legal edge pairs.
* @param c a set of vertices (intuitively, the set of variables to be conditioned on.
* @param d a set of vertices (intuitively to be used in tests of legality, for example, the set of
* ancestors of c).
* @param graph the graph with respect to which reachability is
* @return the set of nodes reachable from the given set of initial nodes in the given graph according to the
* criteria in the given legal pairs object.
* <p>
* A variable v is reachable from initialNodes iff for some variable X in initialNodes thers is a path U [X, Y1,
* ..., v] such that legalPairs.isLegalFirstNode(X, Y1) and for each [H1, H2, H3] as subpaths of U,
* legalPairs.isLegalPairs(H1, H2, H3).
* <p>
* The algorithm used is a variant of Algorithm 1 from Geiger, Verma, & Pearl (1990).
*/
public static Set<Node> getReachableNodes(List<Node> initialNodes, LegalPairs legalPairs, List<Node> c, List<Node> d, Graph graph, int maxPathLength) {
HashSet<Node> reachable = new HashSet<>();
MultiKeyMap visited = new MultiKeyMap();
List<ReachabilityEdge> nextEdges = new LinkedList<>();
for (Node x : initialNodes) {
List<Node> adjX = graph.getAdjacentNodes(x);
for (Node y : adjX) {
if (legalPairs.isLegalFirstEdge(x, y)) {
reachable.add(y);
nextEdges.add(new ReachabilityEdge(x, y));
visited.put(x, y, Boolean.TRUE);
}
}
}
int pathLength = 1;
while (nextEdges.size() > 0) {
// System.out.println("Path length = " + pathLength);
if (++pathLength > maxPathLength)
return reachable;
List<ReachabilityEdge> currEdges = nextEdges;
nextEdges = new LinkedList<>();
for (ReachabilityEdge edge : currEdges) {
Node x = edge.getFrom();
Node y = edge.getTo();
List<Node> adjY = graph.getAdjacentNodes(y);
for (Node z : adjY) {
if ((visited.get(y, z)) == Boolean.TRUE) {
continue;
}
if (legalPairs.isLegalPair(x, y, z, c, d)) {
reachable.add(z);
nextEdges.add(new ReachabilityEdge(y, z));
visited.put(y, z, Boolean.TRUE);
}
}
}
}
return reachable;
}
Aggregations