Java源码示例:org.jgrapht.traverse.TopologicalOrderIterator
示例1
private Iterable<SpeciesDescription> getSpeciesInHierarchicalOrder(final ModelDescription model) {
final DirectedGraph<SpeciesDescription, Object> hierarchy = new SimpleDirectedGraph<>(Object.class);
final DescriptionVisitor visitor = desc -> {
if (desc instanceof ModelDescription) { return true; }
final SpeciesDescription sd = ((SpeciesDescription) desc).getParent();
if (sd == null || sd == desc) { return false; }
hierarchy.addVertex((SpeciesDescription) desc);
if (!sd.isBuiltIn()) {
hierarchy.addVertex(sd);
hierarchy.addEdge(sd, (SpeciesDescription) desc);
}
return true;
};
model.visitAllSpecies(visitor);
return () -> new TopologicalOrderIterator<>(hierarchy);
}
示例2
private void build(Task task, ImmutableList.Builder<RunnableTaskDag> entriesBuilder, ImmutableMap.Builder<TaskId, Task> tasksBuilder)
{
DefaultDirectedGraph<TaskId, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
worker(graph, task, null, tasksBuilder, Sets.newHashSet());
CycleDetector<TaskId, DefaultEdge> cycleDetector = new CycleDetector<>(graph);
if ( cycleDetector.detectCycles() )
{
throw new RuntimeException("The Task DAG contains cycles: " + task);
}
TopologicalOrderIterator<TaskId, DefaultEdge> orderIterator = new TopologicalOrderIterator<>(graph);
while ( orderIterator.hasNext() )
{
TaskId taskId = orderIterator.next();
Set<DefaultEdge> taskIdEdges = graph.edgesOf(taskId);
Set<TaskId> processed = taskIdEdges
.stream()
.map(graph::getEdgeSource)
.filter(edge -> !edge.equals(taskId) && !edge.getId().equals(""))
.collect(Collectors.toSet());
entriesBuilder.add(new RunnableTaskDag(taskId, processed));
}
}
示例3
public String buildClause(QueryGraph queryGraph, Map<String, String> queryEntityAliases, Map entityAliasesMaps) {
GraphIterator<IModelEntity, Relationship> iterator = new TopologicalOrderIterator<>(queryGraph);
GraphIteratorListener listener = new GraphIteratorListener(entityAliasesMaps, queryEntityAliases);
iterator.addTraversalListener(listener);
IModelEntity firstEntity = null;
if (iterator.hasNext()) {
firstEntity = iterator.next();
}
while (iterator.hasNext()) {
iterator.next();
}
List<String> fromClauseElements = new ArrayList<>();
String fistEntityName = firstEntity.getName();
String firtEntityAlias = getAlias(entityAliasesMaps, queryEntityAliases, firstEntity);
List<String> joinStatments = listener.getJoinStatments();
fromClauseElements.add(FROM);
fromClauseElements.add(fistEntityName);
fromClauseElements.add(firtEntityAlias);
fromClauseElements.addAll(joinStatments);
return StringUtils.join(fromClauseElements, " ");
}
示例4
public Collection<String> getOrderedAttributeNames(final Set<String> facetsToConsider) {
// AD Revised in Aug 2019 for Issue #2869: keep constraints between superspecies and subspecies
final DirectedGraph<String, Object> dependencies = new DefaultDirectedGraph<>(Object.class);
final Map<String, VariableDescription> all = new HashMap<>();
this.visitAllAttributes((d) -> {
all.put(d.getName(), (VariableDescription) d);
return true;
});
Graphs.addAllVertices(dependencies, all.keySet());
final VariableDescription shape = getAttribute(SHAPE);
final Collection<VariableDescription> shapeDependencies =
shape == null ? Collections.EMPTY_LIST : shape.getDependencies(facetsToConsider, false, true);
all.forEach((an, var) -> {
for (final VariableDescription newVar : var.getDependencies(facetsToConsider, false, true)) {
final String other = newVar.getName();
// AD Revision in April 2019 for Issue #2624: prevent cycles when building the graph
if (!dependencies.containsEdge(an, other)) {
dependencies.addEdge(other, an);
}
}
// Adding a constraint between the shape of the macrospecies and the populations of microspecies
if (var.isSyntheticSpeciesContainer() && !shapeDependencies.contains(var)) {
dependencies.addEdge(SHAPE, an);
}
});
// June 2020: moving (back) to Iterables instead of Streams.
return Lists.newArrayList(new TopologicalOrderIterator<>(dependencies));
// return StreamEx.of(new TopologicalOrderIterator<>(dependencies)).toList();
}
示例5
private List<MappingConfiguration> getTopologicallyOrderedMappingConfigurations(
JsonixContext context,
final List<ModuleConfiguration> moduleConfigurations) {
final DirectedGraph<MappingConfiguration, Object> mappingConfigurationDependencyGraph = buildMappingConfigurationDependencyGraph(
context, moduleConfigurations);
final StrongConnectivityInspector<MappingConfiguration, Object> strongConnectivityInspector = new StrongConnectivityInspector<MappingConfiguration, Object>(
mappingConfigurationDependencyGraph);
final List<Set<MappingConfiguration>> stronglyConnectedSets = strongConnectivityInspector
.stronglyConnectedSets();
for (Set<MappingConfiguration> stronglyConnectedSet : stronglyConnectedSets) {
if (stronglyConnectedSet.size() > 1) {
throw new IllegalArgumentException(MessageFormat.format(
"Mappings have the following dependency cycle: {0}",
stronglyConnectedSet.toString()));
}
}
final List<MappingConfiguration> mappingConfigurations = new ArrayList<MappingConfiguration>(
mappingConfigurationDependencyGraph.vertexSet().size());
for (Iterator<MappingConfiguration> mappingConfigurationsInTopologicalOrderIterator = new TopologicalOrderIterator<MappingConfiguration, Object>(
mappingConfigurationDependencyGraph); mappingConfigurationsInTopologicalOrderIterator
.hasNext();) {
mappingConfigurations
.add(mappingConfigurationsInTopologicalOrderIterator.next());
}
return mappingConfigurations;
}
示例6
public Stream<T> getBuildChain() throws CycleDetectedException {
DefaultDirectedGraph<T, DefaultEdge> graph = builder.build();
ensureNoCycle(graph);
return Iterators.toStream(new TopologicalOrderIterator<>(graph));
}