Skip to content

Instantly share code, notes, and snippets.

@mariofusco
Created May 13, 2014 14:04
Show Gist options
  • Save mariofusco/0f39cac323bf0fe66fff to your computer and use it in GitHub Desktop.
Save mariofusco/0f39cac323bf0fe66fff to your computer and use it in GitHub Desktop.
private List<ObjectSinkNode> getSortedSinks() {
List<ObjectSinkNode> sinks = new ArrayList<ObjectSinkNode>();
if ( this.hashableSinks != null ) {
for ( ObjectSinkNode sink = this.hashableSinks.getFirst(); sink != null; sink = sink.getNextObjectSinkNode() ) {
sinks.add(sink);
}
}
if ( this.otherSinks != null ) {
for ( ObjectSinkNode sink = this.otherSinks.getFirst(); sink != null; sink = sink.getNextObjectSinkNode() ) {
sinks.add(sink);
}
}
Collections.sort(sinks, SinkComparator.INSTANCE);
return sinks;
}
private static class SinkComparator implements Comparator<ObjectSinkNode> {
static final SinkComparator INSTANCE = new SinkComparator();
public int compare(ObjectSinkNode sink1, ObjectSinkNode sink2) {
return getMaxSalience(sink1) - getMaxSalience(sink2);
}
private int getMaxSalience(ObjectSinkNode sink) {
return getMaxSalience(sink, Integer.MIN_VALUE);
}
private int getMaxSalience(Sink node, int currentMax) {
Sink[] sinks = null;
if (node instanceof ObjectSource) {
sinks = ((ObjectSource) node).getSinkPropagator().getSinks();
} else if (node instanceof LeftTupleSource) {
sinks = ((LeftTupleSource) node).getSinkPropagator().getSinks();
}
if (sinks != null) {
for (Sink sink : sinks) {
currentMax = getMaxSalience(sink, currentMax);
}
} else if (node instanceof RuleTerminalNode) {
int ruleSalience = ((RuleTerminalNode)node).getRule().getSalience().getValue();
if (ruleSalience > currentMax) {
currentMax = ruleSalience;
}
}
return currentMax;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment