org.jgrapht.traverse.TopologicalOrderIterator Java Examples

The following examples show how to use org.jgrapht.traverse.TopologicalOrderIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelAssembler.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: RunnableTaskDagBuilder.java    From workflow with Apache License 2.0 6 votes vote down vote up
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));
    }
}
 
Example #3
Source File: JPQLStatementFromClause.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
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, " ");

	}
 
Example #4
Source File: TypeDescription.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example #5
Source File: ModulesConfiguration.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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;
}
 
Example #6
Source File: DependencyGraph.java    From james-project with Apache License 2.0 4 votes vote down vote up
public Stream<T> getBuildChain() throws CycleDetectedException {
    DefaultDirectedGraph<T, DefaultEdge> graph = builder.build();
    ensureNoCycle(graph);
    return Iterators.toStream(new TopologicalOrderIterator<>(graph));
}