Java Code Examples for org.jgrapht.DirectedGraph#addVertex()

The following examples show how to use org.jgrapht.DirectedGraph#addVertex() . 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: GenerateStratification.java    From BART with MIT License 6 votes vote down vote up
private DirectedGraph<Dependency, DefaultEdge> initDependencyGraph(List<Dependency> dependencies, Map<Dependency, Set<AttributeRef>> affectedAttributes, Map<Dependency, Set<AttributeRef>> queriedAttributes) {
    DirectedGraph<Dependency, DefaultEdge> dependencyGraph = new DefaultDirectedGraph<Dependency, DefaultEdge>(DefaultEdge.class);
    for (Dependency dependency : dependencies) {
        dependencyGraph.addVertex(dependency);
    }
    for (int i = 0; i < dependencies.size(); i++) {
        Dependency d1 = dependencies.get(i);
        for (int j = 0; j < dependencies.size(); j++) {
            if (i == j) {
                continue;
            }
            Dependency d2 = dependencies.get(j);
            if (haveOverlap(d1, d2, affectedAttributes, queriedAttributes)) {
                if (logger.isDebugEnabled()) logger.debug("Edge btw " + d1.getId() + " and " + d2.getId());
                dependencyGraph.addEdge(d1, d2);
            }
        }
    }
    return dependencyGraph;
}
 
Example 2
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 3
Source File: BuildFaginDependencyGraph.java    From Llunatic with GNU General Public License v3.0 6 votes vote down vote up
private void addNodes(DirectedGraph<AttributeRef, ExtendedEdge> dependencyGraph, IFormula formula, Set<AttributeRef> positionsAdded) {
    for (IFormulaAtom atom : formula.getAtoms()) {
        if (!(atom instanceof RelationalAtom)) {
            continue;
        }
        RelationalAtom relationalAtom = (RelationalAtom) atom;
        if (relationalAtom.isSource()) {
            continue;
        }
        String tableName = relationalAtom.getTableAlias().getTableName();
        for (FormulaAttribute attribute : relationalAtom.getAttributes()) {
            String attributeName = attribute.getAttributeName();
            AttributeRef position = buildPosition(tableName, attributeName);
            if (positionsAdded.contains(position)) {
                continue;
            }
            dependencyGraph.addVertex(position);
            positionsAdded.add(position);
        }
    }
}
 
Example 4
Source File: BuildEGDStratification.java    From Llunatic with GNU General Public License v3.0 6 votes vote down vote up
private DirectedGraph<EGDStratum, DefaultEdge> buildEGDStrataGraph(DirectedGraph<ExtendedEGD, DefaultEdge> dependencyGraph, List<EGDStratum> egdStrata) {
    DirectedGraph<EGDStratum, DefaultEdge> strataGraph = new DefaultDirectedGraph<EGDStratum, DefaultEdge>(DefaultEdge.class);
    for (EGDStratum stratum : egdStrata) {
        strataGraph.addVertex(stratum);
    }
    for (EGDStratum stratumA : egdStrata) {
        for (EGDStratum stratumB : egdStrata) {
            if(stratumA.equals(stratumB)){
                continue;
            }
            if (existsPath(dependencyGraph, stratumA, stratumB)) {
                strataGraph.addEdge(stratumA, stratumB);
            }
        }
    }
    return strataGraph;
}
 
Example 5
Source File: BuildTGDStratification.java    From Llunatic with GNU General Public License v3.0 6 votes vote down vote up
private DirectedGraph<TGDStratum, DefaultEdge> buildStrataGraph(DirectedGraph<Dependency, DefaultEdge> dependencyGraph, List<TGDStratum> tgdStrata) {
    DirectedGraph<TGDStratum, DefaultEdge> strataGraph = new DefaultDirectedGraph<TGDStratum, DefaultEdge>(DefaultEdge.class);
    for (TGDStratum stratum : tgdStrata) {
        strataGraph.addVertex(stratum);
    }
    for (TGDStratum stratumA : tgdStrata) {
        for (TGDStratum stratumB : tgdStrata) {
            if(stratumA.equals(stratumB)){
                continue;
            }
            if (existsPath(dependencyGraph, stratumA, stratumB)) {
                strataGraph.addEdge(stratumA, stratumB);
            }
        }
    }
    return strataGraph;
}
 
Example 6
Source File: CheckConflicts.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
private void addVertices(IDatabase database, boolean isSource, DirectedGraph<AttributeRef, DefaultEdge> dependencyGraph) {
    for (String tableName : database.getTableNames()) {
        ITable table = database.getTable(tableName);
        for (Attribute attribute : table.getAttributes()) {
            if (attribute.getName().equals(SpeedyConstants.OID)) {
                continue;
            }
            AttributeRef attributeRef = new AttributeRef(new TableAlias(table.getName(), isSource), attribute.getName());
            if (logger.isDebugEnabled()) logger.debug("Adding vertex " + attributeRef);
            dependencyGraph.addVertex(attributeRef);
        }
    }
}
 
Example 7
Source File: BuildTGDStratification.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
private DirectedGraph<Dependency, DefaultEdge> buildChaseGraph(Map<Dependency, Set<Dependency>> affectedDependencies) {
    DirectedGraph<Dependency, DefaultEdge> chaseGraph = new DefaultDirectedGraph<Dependency, DefaultEdge>(DefaultEdge.class);
    for (Dependency tgd : affectedDependencies.keySet()) {
        chaseGraph.addVertex(tgd);
    }
    for (Dependency dependency : affectedDependencies.keySet()) {
        for (Dependency connectedDependency : affectedDependencies.get(dependency)) {
            chaseGraph.addEdge(dependency, connectedDependency);
        }
    }
    return chaseGraph;
}
 
Example 8
Source File: GenerateDOTMojo.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param info
 * @param graph
 */
private void addGraphDependencies(AddonInfo info, DirectedGraph<AddonVertex, AddonDependencyEdge> graph)
{
   AddonId addon = info.getAddon();
   AddonVertex rootVertex = new AddonVertex(addon.getName(), addon.getVersion());
   graph.addVertex(rootVertex);
   for (AddonDependencyEntry entry : info.getDependencyEntries())
   {
      AddonVertex depVertex = new AddonVertex(entry.getName(), entry.getVersionRange().getMax());
      graph.addVertex(depVertex);
      graph.addEdge(rootVertex, depVertex,
               new AddonDependencyEdge(entry.getVersionRange(), entry.isExported(), entry.isOptional()));
   }
}
 
Example 9
Source File: GraphUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * replace the vertices in the graph with an alternate set of vertices.
 * @param graph graph to be mutated
 * @param alternates alternate vertices to insert into the graph. map of vertices to their direct dependents.
 */
public static <V> void swapVertices(DirectedGraph<V, DefaultEdge> graph, Map<V, Set<V>> alternates) {
    Objects.requireNonNull(graph,"graph");
    Objects.requireNonNull(alternates, "alternates");
    // add all of the new vertices to prep for linking
    addAllVertices(graph, alternates.keySet());

    for (Entry<V, Set<V>> entry : alternates.entrySet()) {
        V alternateVertex = entry.getKey();
        Set<V> dependencies = entry.getValue();
        // make sure we can satisfy all dependencies before continuing
        // TODO: this should probably done outside so it can be handled better.
        if (!graph.vertexSet().containsAll(dependencies)) {
            continue;
        }

        // figure out which vertices depend on the incumbent vertex
        Set<V> dependents = Collections.emptySet();
        if (graph.containsVertex(alternateVertex)) {
            dependents = getIncomingVertices(graph, alternateVertex);
            graph.removeVertex(alternateVertex);
        }
        // (re)insert the vertex and re-link the dependents
        graph.addVertex(alternateVertex);
        addIncomingEdges(graph, alternateVertex, dependents);

        // create the dependencies
        addOutgoingEdges(graph, alternateVertex, dependencies);
    }
}
 
Example 10
Source File: GraphUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Add all of the vertices to the graph without any edges. If the the graph
 * already contains any one of the vertices, that vertex is not added.
 * @param graph graph to be mutated
 * @param vertices vertices to add
 */
public static <V> void addAllVertices(DirectedGraph<V, DefaultEdge> graph, Set<V> vertices) {
    // add all of the new vertices to prep for linking
    for (V vertex : vertices) {
        graph.addVertex(vertex);
    }
}
 
Example 11
Source File: GraphUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Add dependencies to the given source vertex. Whether duplicates are created is dependent
 * on the underlying {@link DirectedGraph} implementation.
 * @param graph graph to be mutated
 * @param source source vertex of the new edges
 * @param targets target vertices for the new edges
 */
public static <V> void addOutgoingEdges(DirectedGraph<V, DefaultEdge> graph, V source, Set<V> targets) {
    if (!graph.containsVertex(source)) {
        graph.addVertex(source);
    }
    for (V target : targets) {
        if (!graph.containsVertex(target)) {
            graph.addVertex(target);
        }
        graph.addEdge(source, target);
    }
}
 
Example 12
Source File: GraphUtils.java    From Nicobar with Apache License 2.0 5 votes vote down vote up
/**
 * Add dependents on the give target vertex. Whether duplicates are created is dependent
 * on the underlying {@link DirectedGraph} implementation.
 * @param graph graph to be mutated
 * @param target target vertex for the new edges
 * @param sources source vertices for the new edges
 */
public static <N> void addIncomingEdges(DirectedGraph<N, DefaultEdge> graph, N target, Set<N> sources) {
    if (!graph.containsVertex(target)) {
        graph.addVertex(target);
    }
    for (N source : sources) {
        if (!graph.containsVertex(source)) {
            graph.addVertex(source);
        }
        graph.addEdge(source, target);
    }
}