Created
May 13, 2014 14:04
-
-
Save mariofusco/0f39cac323bf0fe66fff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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