org.aksw.commons.collections.trees.TreeReplace Maven / Gradle / Ivy
package org.aksw.commons.collections.trees;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Tree wrapper with bidirectional replacement of certain nodes with leaf nodes.
* Useful to replace sub-trees with single nodes.
*
*
* @author raven
*
* @param
*/
public class TreeReplace
extends TreeBase
{
protected Tree delegate;
//protected BiMap delegateToReplacement;
protected Map delegateToReplacement;
protected Map replacementToDelegate;
public TreeReplace(Tree delegate, Map delegateToReplacement, Map replacementToDelegate) {
super();
this.delegate = delegate;
this.delegateToReplacement = delegateToReplacement;
this.replacementToDelegate = replacementToDelegate;
}
@Override
public T getRoot() {
T b = delegate.getRoot();
T result = delegateToReplacement.getOrDefault(b, b);
return result;
}
@Override
public List getChildren(T b) {
List result;
if(replacementToDelegate.containsKey(b)) {
result = Collections.emptyList();
} else {
//T a = delegateToReplacement.inverse().getOrDefault(b, b);
Collection bs = delegate.getChildren(b);
result = bs.stream().map(bx -> delegateToReplacement.getOrDefault(bx, bx)).collect(Collectors.toList());
}
return result;
}
@Override
public T getParent(T b) {
T a = replacementToDelegate.getOrDefault(b, b);
T result = delegate.getParent(a);
return result;
}
// public void writeTree(T node, IndentedWriter writer) {
// String tmp = "- " + node;
// writer.println(tmp);
// writer.incIndent();
// List children = getChildren(node);
// if(children != null) {
// for(T child : children) {
// writeTree(child, writer);
// }
// }
//
// writer.decIndent();
// }
//
// //@Override
// public String effectiveString() {
// try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
// IndentedWriter writer = new IndentedWriter(out);
// writer.setPadString("| ");
// writeTree(getRoot(), writer);
//
// writer.flush();
// String result = out.toString(StandardCharsets.UTF_8.name());
// return result;
// } catch(Exception e) {
// throw new RuntimeException(e);
// }
// }
@Override
public String toString() {
// String result = effectiveString();
// return result;
return "TreeReplace [delegate=" + delegate + ", delegateToReplacement=" + delegateToReplacement + "]";
}
@Override
public T copy(T node, List children) {
throw new UnsupportedOperationException();
}
@Override
public Tree createNew(T root) {
throw new UnsupportedOperationException();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy