net.anwiba.tools.graphml.utilities.GraphUtilities Maven / Gradle / Ivy
/*
* #%L
* anwiba commons tools
* %%
* Copyright (C) 2007 - 2016 Andreas Bartels
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package net.anwiba.tools.graphml.utilities;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class GraphUtilities {
public static Map> normalize(final Map> graphMap) {
final Graph graph = convert(graphMap);
normalize(graph);
return convert(graph);
}
public static void normalize(final Graph graph) {
for (final Node node : graph.nodes()) {
final Iterable> children = copy(node.children());
for (final Node child : children) {
if (node.childrenContains(child)) {
node.remove(child);
}
}
}
graph.reset();
}
public static Graph add(final Graph graph, final Graph other) {
final Set> nodes = new HashSet<>();
final Graph result = new Graph<>();
for (final Node node : graph.nodes()) {
nodes.add(node);
result.add(new Node<>(node.getObject()));
}
for (final Node node : other.nodes()) {
nodes.add(node);
result.add(new Node<>(node.getObject()));
}
for (final Node node : nodes) {
final Node clone = result.get(node.getObject());
for (final Node child : node.children()) {
if (!nodes.contains(child)) {
continue;
}
clone.add(result.get(child.getObject()));
}
}
return result;
}
public static Graph intersect(final Graph graph, final Graph other) {
final Set> nodes = new HashSet<>();
final Graph result = new Graph<>();
for (final Node node : graph.nodes()) {
if (!other.containts(node)) {
continue;
}
nodes.add(node);
result.add(new Node<>(node.getObject()));
}
for (final Node node : nodes) {
final Node clone = result.get(node.getObject());
for (final Node child : node.children()) {
if (!nodes.contains(child)) {
continue;
}
clone.add(result.get(child.getObject()));
}
}
return result;
}
public static Iterable> copy(final Iterable> nodes) {
final List> copy = new ArrayList<>();
for (final Node node : nodes) {
copy.add(node);
}
return copy;
}
public static Graph convert(final Map> graphMap) {
final Set keys = graphMap.keySet();
final Graph graph = new Graph<>();
for (final String key : keys) {
graph.add(new Node<>(key));
}
for (final String key : keys) {
final Node node = graph.get(key);
final List children = graphMap.get(key);
for (final String child : children) {
node.add(graph.get(child));
}
}
return graph;
}
public static Map> convert(final Graph graph) {
final Map> graphMap = new HashMap<>();
final Iterable> nodes = graph.nodes();
for (final Node node : nodes) {
if (!graphMap.containsKey(node.getObject())) {
graphMap.put(node.getObject(), new ArrayList());
}
final List edges = graphMap.get(node.getObject());
final Iterable> children = node.children();
for (final Node child : children) {
edges.add(child.getObject());
}
}
return graphMap;
}
}