本文整理汇总了Java中org.jgrapht.DirectedGraph.addEdge方法的典型用法代码示例。如果您正苦于以下问题:Java DirectedGraph.addEdge方法的具体用法?Java DirectedGraph.addEdge怎么用?Java DirectedGraph.addEdge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jgrapht.DirectedGraph
的用法示例。
在下文中一共展示了DirectedGraph.addEdge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testComplexDag
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testComplexDag() {
DirectedGraph<String, DefaultEdge> g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
g.addVertex("a");
g.addVertex("b");
g.addVertex("c");
g.addVertex("d");
g.addVertex("e");
g.addVertex("f");
g.addVertex("g");
g.addEdge("a", "b");
g.addEdge("b", "c");
g.addEdge("c", "d");
g.addEdge("d", "f");
g.addEdge("b", "e");
g.addEdge("e", "f");
g.addEdge("f", "g");
g.addEdge("a", "f");
Assert.assertEquals("b", new TarjanLowestCommonAncestor<String, DefaultEdge>(g).calculate("a", "e", "c"));
}
示例2: extractMentionNetwork
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
protected DirectedGraph<Figure, DefaultEdge> extractMentionNetwork(JCas jcas) {
DirectedGraph<Figure, DefaultEdge> graph = new DirectedPseudograph<Figure, DefaultEdge>(DefaultEdge.class);
for (Utterance utterance : JCasUtil.select(jcas, Utterance.class)) {
Speaker speaker = DramaUtil.getFirstSpeaker(utterance);
if (speaker != null)
for (FigureMention mention : JCasUtil.selectCovered(jcas, FigureMention.class, utterance)) {
if (speaker.getFigure() != null && mention.getFigure() != null) {
if (!graph.containsVertex(speaker.getFigure()))
graph.addVertex(speaker.getFigure());
if (!graph.containsVertex(mention.getFigure()))
graph.addVertex(mention.getFigure());
graph.addEdge(speaker.getFigure(), mention.getFigure());
}
}
}
return graph;
}
示例3: initDependencyGraph
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
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;
}
示例4: getSpeciesInHierarchicalOrder
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
private Iterable<SpeciesDescription> getSpeciesInHierarchicalOrder(final ModelDescription model) {
final DirectedGraph<SpeciesDescription, Object> hierarchy = new SimpleDirectedGraph<>(Object.class);
final DescriptionVisitor visitor = new DescriptionVisitor<SpeciesDescription>() {
@Override
public boolean visit(final SpeciesDescription desc) {
if (desc instanceof ModelDescription)
return true;
final SpeciesDescription sd = desc.getParent();
if (sd == null || sd == desc)
return false;
hierarchy.addVertex(desc);
if (!sd.isBuiltIn()) {
hierarchy.addVertex(sd);
hierarchy.addEdge(sd, desc);
}
return true;
}
};
model.visitAllSpecies(visitor);
return () -> new TopologicalOrderIterator<>(hierarchy);
}
示例5: replaceVertex
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
private static void replaceVertex(DirectedGraph<IAtomicDerivedStateProcessor<? extends EObject>, DefaultEdge> graph,
IAtomicDerivedStateProcessor<? extends EObject> original,
IAtomicDerivedStateProcessor<? extends EObject> replacement) {
Collection<DefaultEdge> edgeDump = new ArrayList<>();
for (DefaultEdge incomingEdge : graph.incomingEdgesOf(original)) {
IAtomicDerivedStateProcessor<? extends EObject> edgeSource = graph.getEdgeSource(incomingEdge);
edgeDump.add(incomingEdge);
graph.addEdge(edgeSource, replacement);
}
for (DefaultEdge outgoingEdge : graph.outgoingEdgesOf(original)) {
// we do not insert the dependencies of the removed vertex because the new vertex has its own dependencies
edgeDump.add(outgoingEdge);
}
edgeDump.forEach(graph::removeEdge);
graph.removeVertex(original);
}
示例6: asDirectedGraph
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
public static DirectedGraph<Node, DefaultLink> asDirectedGraph(UndirectedGraph<Node, DefaultLink> undirectedGraph) {
if (undirectedGraph == null) {
logger.debug("graph is null.");
return null;
}
DirectedGraph<Node, DefaultLink> g = new DirectedWeightedMultigraph<Node, DefaultLink>(DefaultLink.class);
for (Node v : undirectedGraph.vertexSet())
g.addVertex(v);
for (DefaultLink e: undirectedGraph.edgeSet())
g.addEdge(e.getSource(), e.getTarget(), e);
return g;
}
示例7: buildEGDStrataGraph
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
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;
}
示例8: buildStrataGraph
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
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;
}
示例9: testBinaryTree
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testBinaryTree() {
DirectedGraph<String, DefaultEdge> g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
g.addVertex("a");
g.addVertex("b");
g.addVertex("c");
g.addVertex("d");
g.addVertex("e");
g.addEdge("a", "b");
g.addEdge("b", "c");
g.addEdge("b", "d");
g.addEdge("d", "e");
Assert.assertEquals("b", new TarjanLowestCommonAncestor<String, DefaultEdge>(g).calculate("a", "c", "e"));
Assert.assertEquals("b", new TarjanLowestCommonAncestor<String, DefaultEdge>(g).calculate("a", "b", "d"));
Assert.assertEquals("d", new TarjanLowestCommonAncestor<String, DefaultEdge>(g).calculate("a", "d", "e"));
}
示例10: buildDefinitionGraph
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
private void buildDefinitionGraph(Collection<DefinitionModel> definitions,
DirectedGraph<DefinitionModel, DefaultEdge> definitionGraph) {
for (DefinitionModel definition : definitions) {
if (!definitionGraph.containsVertex(definition)) {
definitionGraph.addVertex(definition);
buildDefinitionGraph(definition.getDependencies(), definitionGraph);
for (DefinitionModel dependency : definition.getDependencies()) {
definitionGraph.addEdge(definition, dependency);
}
}
}
}
示例11: detectCyclesInEntityGraph
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
/**
* Inspects the instance graph for cycles, any cycle is printed as an error. The nameToEntity parameter doesn't list expected
* instances, any instances that are not found in the nameToInstances map (they are looked for because they are referenced as a
* dependency by an instance in the map) and are not found by name in the definition's expectedInstances are treated as errors
* as well.
*
* @param definition definition being processed. Will uses it's expected list, any instances references as dependencies but
* not found, not listed as expected in this DefinitionModel, will be treated as errors.
* @param nameToEntity name to unique instanceModels, verified before call.
* @param errorListner accepts and displays all errors produced by analyzing the models
* @return true if an error occurred, false otherwise
*/
private boolean detectCyclesInEntityGraph(final DefinitionModel definition, final Map<String, InstanceModel> nameToEntity,
final Consumer<ErrorModel> errorListener) {
final Map<String, ExpectedModel> missing = new HashMap<>();
final DirectedGraph<BaseInstanceModel, DefaultEdge> entityGraph = new DefaultDirectedGraph<>(DefaultEdge.class);
for (BaseInstanceModel entity : nameToEntity.values()) {
if (!entityGraph.containsVertex(entity)) {
entityGraph.addVertex(entity);
}
if (InstanceModel.class.isAssignableFrom(entity.getClass())) {
InstanceModel instanceModel = (InstanceModel) entity;
for (InstanceDependencyModel instanceDependency : instanceModel.getDependencies()) {
BaseInstanceModel dependency = nameToEntity.get(instanceDependency.getIdentity());
if (dependency == null) {
dependency = missing.computeIfAbsent(instanceDependency.getIdentity(), s -> new ExpectedModel(s));
missing.get(instanceDependency.getIdentity())
.addDefinitionReferenceToType(instanceModel.getIdentity(), instanceDependency.getType());
}
if (!entityGraph.containsVertex(dependency)) {
entityGraph.addVertex(dependency);
}
entityGraph.addEdge(entity, dependency);
}
}
}
boolean errored = errorsForCycles(errorListener, entityGraph);
errored = testAllMissingEntitiesAreExpected(definition, errorListener, missing, entityGraph) || errored;
errored = errorUnusedExpectedsOnDefinition(definition, errorListener, missing) || errored;
return errored;
}
示例12: testBasicOrdering
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testBasicOrdering() {
String sp1 = "sp1";
String sp2 = "sp2";
String sp3 = "sp3";
String sp4 = "sp4";
String sp5 = "sp5";
DirectedGraph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
for (String vertex : Lists.mutable.with(sp1, sp2, sp3, sp4, sp5).toReversed()) {
graph.addVertex(vertex);
}
graph.addEdge(sp1, sp5);
graph.addEdge(sp3, sp5);
graph.addEdge(sp2, sp1);
graph.addEdge(sp5, sp4);
ListIterable<String> sorted = sorter.sortChanges(graph);
// First, compare the root topological order (i.e. ensure that the dependencies are respected)
assertEquals(5, sorted.size());
assertThat(sorted.indexOf(sp1), greaterThan(sorted.indexOf(sp2)));
assertThat(sorted.indexOf(sp5), greaterThan(sorted.indexOf(sp1)));
assertThat(sorted.indexOf(sp5), greaterThan(sorted.indexOf(sp3)));
assertThat(sorted.indexOf(sp4), greaterThan(sorted.indexOf(sp5)));
// Now check that we can achieve a consistent order too (for easier debuggability for clients)
assertEquals(Lists.immutable.with(sp2, sp1, sp3, sp5, sp4), sorted);
}
示例13: testBasicOrderingWithComparator
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testBasicOrderingWithComparator() {
SortableDependency sp1 = newVertex("sp1");
SortableDependency sp2 = newVertex("sp2");
SortableDependency sp3 = newVertex("sp3");
SortableDependency sp4 = newVertex("sp4");
SortableDependency sp5 = newVertex("sp5");
DirectedGraph<SortableDependency, DefaultEdge> graph = new DefaultDirectedGraph<SortableDependency, DefaultEdge>(DefaultEdge.class);
for (SortableDependency vertex : shuffledList(sp1, sp2, sp3, sp4, sp5)) {
graph.addVertex(vertex);
}
graph.addEdge(sp1, sp5);
graph.addEdge(sp3, sp5);
graph.addEdge(sp2, sp1);
graph.addEdge(sp5, sp4);
ListIterable<SortableDependency> sorted = sorter.sortChanges(graph, Comparators.fromFunctions(SortableDependency.TO_CHANGE_NAME));
// First, compare the root topological order (i.e. ensure that the dependencies are respected)
assertEquals(5, sorted.size());
assertThat(sorted.indexOf(sp1), greaterThan(sorted.indexOf(sp2)));
assertThat(sorted.indexOf(sp5), greaterThan(sorted.indexOf(sp1)));
assertThat(sorted.indexOf(sp5), greaterThan(sorted.indexOf(sp3)));
assertThat(sorted.indexOf(sp4), greaterThan(sorted.indexOf(sp5)));
// Now check that we can achieve a consistent order too (for easier debuggability for clients)
assertEquals(Lists.immutable.with(sp2, sp1, sp3, sp5, sp4), sorted);
}
示例14: testOrderingWithSubgraph
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testOrderingWithSubgraph() {
String sp1 = "sp1";
String sp2 = "sp2";
String sp3 = "sp3";
String sp4 = "sp4";
String sp5 = "sp5";
DirectedGraph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
for (String vertex : shuffledList(sp1, sp2, sp3, sp4, sp5)) {
graph.addVertex(vertex);
}
graph.addEdge(sp2, sp1);
graph.addEdge(sp5, sp4);
graph.addEdge(sp1, sp5);
graph.addEdge(sp3, sp5);
ImmutableList<String> sorted = sorter.sortChanges(graph, Lists.mutable.with(sp1, sp2, sp3));
// First, compare the root topological order (i.e. ensure that the dependencies are respected)
assertEquals(3, sorted.size());
assertThat(sorted.indexOf(sp1), greaterThan(sorted.indexOf(sp2)));
// Now check that we can achieve a consistent order too (for easier debuggability for clients)
assertEquals(Lists.immutable.with(sp2, sp1, sp3), sorted);
}
示例15: testCycleDetection
import org.jgrapht.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testCycleDetection() {
SortableDependency sp1 = newVertex("sp1");
SortableDependency sp2 = newVertex("sp2");
SortableDependency sp3 = newVertex("sp3");
SortableDependency sp4 = newVertex("sp4");
SortableDependency sp5 = newVertex("sp5");
SortableDependency sp6 = newVertex("sp6");
SortableDependency sp7 = newVertex("sp7");
SortableDependency sp8 = newVertex("sp8");
DirectedGraph<SortableDependency, DefaultEdge> graph = new DefaultDirectedGraph<SortableDependency, DefaultEdge>(DefaultEdge.class);
for (SortableDependency vertex : shuffledList(sp1, sp2, sp3, sp4, sp5, sp6, sp7, sp8)) {
graph.addVertex(vertex);
}
graph.addEdge(sp2, sp1);
graph.addEdge(sp5, sp4);
graph.addEdge(sp1, sp5);
graph.addEdge(sp3, sp5);
graph.addEdge(sp4, sp5);
graph.addEdge(sp7, sp6);
graph.addEdge(sp8, sp7);
graph.addEdge(sp6, sp8);
try {
sorter.sortChanges(graph);
fail("Expecting exception here: " + GraphCycleException.class);
} catch (GraphCycleException e) {
verifyCycleExists(e, Sets.immutable.with("sp4", "sp5"));
verifyCycleExists(e, Sets.immutable.with("sp6", "sp7", "sp8"));
Verify.assertSize(2, e.getCycleComponents());
}
}