class MinExternalConnections extends CriteriaCached {

    @Override
    public List<TopologyWrapper> filter(List<TopologyWrapper> topologies) {
        List<TopologyWrapper> minExternalRTs = new ArrayList<>();
        int minExternalRTsCount = Integer.MAX_VALUE;

        for (TopologyWrapper topology : topologies) {
            int externalRTsCount = 0;

            for (TRelationshipTemplate relationshipTemplate : topology.getConnectsTos(topology.getRelationshipTemplates())) {
                TNodeTemplate source = topology.getSourceNT(relationshipTemplate);
                TNodeTemplate target = topology.getTargetNT(relationshipTemplate);
                // only consider connectsTos between top level nts
                if (!topology.isTopLevelNT(source) || !topology.isTopLevelNT(target)) {
                    continue;
                }

                // external edge?
                String sourceTargetLabel = ModelUtilities.getTargetLabel(source).get();
                String targetTargetLabel = ModelUtilities.getTargetLabel(target).get();
                if (!sourceTargetLabel.equals(targetTargetLabel)) {
                    externalRTsCount++;
                }
            }
            if (externalRTsCount < minExternalRTsCount) {
                minExternalRTs.clear();
                minExternalRTs.add(topology);
                minExternalRTsCount = externalRTsCount;
            } else if (externalRTsCount == minExternalRTsCount) {
                minExternalRTs.add(topology);
            }
        }
        return minExternalRTs;
    }

    public  MinExternalConnections(JsonNode params, int outputCap);

}