本文整理汇总了Java中org.jgrapht.DirectedGraph类的典型用法代码示例。如果您正苦于以下问题:Java DirectedGraph类的具体用法?Java DirectedGraph怎么用?Java DirectedGraph使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DirectedGraph类属于org.jgrapht包,在下文中一共展示了DirectedGraph类的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: testAllMissingEntitiesAreExpected
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
private boolean testAllMissingEntitiesAreExpected(final DefinitionModel definition, final Consumer<ErrorModel> errorListner,
final Map<String, ExpectedModel> missing, final DirectedGraph<BaseInstanceModel, DefaultEdge> entityGraph) {
//check computed expected are actually expected
boolean errored = false;
List<String> expectedMissing = definition.getExpectedDefinitions().stream().map(em -> em.getIdentity())
.collect(Collectors.toList());
for (ExpectedModel expected : missing.values()) {
if (!expectedMissing.contains(expected.getIdentity())) {
List<AbstractModel> dependsOnMissing = Stream.concat(
Stream.of(definition),
entityGraph.incomingEdgesOf(expected).stream().map(edge -> entityGraph.getEdgeSource(edge))
.filter(m -> m.getSourceElement().isPresent()))
.collect(Collectors.toList());
errored = true;
errorListner.accept(new ErrorModel(ErrorType.MISSING_BEAN_DEFINITIONS, Arrays.asList(expected), dependsOnMissing));
} else {
definition.addComputedExpected(expected);
}
}
return errored;
}
示例3: getConnectedTo
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
@Override
public Object[] getConnectedTo(Object entity) {
if (entity instanceof PgStatement){
DirectedGraph<PgStatement, DefaultEdge> currentGraph = depRes.getOldGraph();
if (currentGraph != null){
List<PgStatement> connected = new ArrayList<>();
for (DefaultEdge e : currentGraph.outgoingEdgesOf((PgStatement)entity)){
PgStatement connectedVertex = currentGraph.getEdgeTarget(e);
if (!(connectedVertex instanceof PgColumn)) {
connected.add(connectedVertex);
}
}
return connected.toArray();
}
}
return null;
}
示例4: intersect
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
@Override
public DirectedGraph<Node, Triple> intersect(DirectedGraph<Node, Triple> baseGraph, DirectedGraph<Node, Triple> removalGraph) {
DirectedGraph<Node, Triple> result = new DirectedSubgraph<>(baseGraph, removalGraph.vertexSet(), removalGraph.edgeSet());
//Materialize the intersection
//DirectedGraph<Node, Triple> tmp = createNew();
//Graphs.addGraph(tmp, result);
//result = tmp
return result;
}
示例5: handleChanges
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
private MutableList<ExecuteChangeCommand> handleChanges(MutableCollection<Change> fromSourceList) {
final MutableList<ExecuteChangeCommand> commands = Lists.mutable.empty();
DirectedGraph<Change, DefaultEdge> graph = enricher.createDependencyGraph(fromSourceList, false);
if (graph != null) {
ConnectivityInspector<Change, DefaultEdge> connectivityInspector
= new ConnectivityInspector<Change, DefaultEdge>(graph);
for (Set<Change> connectedSet : connectivityInspector.connectedSets()) {
// once we have a connectedSet, sort within those changes to ensure that we still sort them in the
// right order (i.e. via topological sort)
ImmutableList<Change> fullChanges = sorter.sortChanges(graph, SetAdapter.adapt(connectedSet), SortableDependency.GRAPH_SORTER_COMPARATOR);
commands.add(changeCommandFactory.createDeployCommand(new GroupChange(fullChanges)));
}
}
return commands;
}
示例6: getCycleComponents
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
/**
* Returns the components of the graph that are cycles.
* Taken from the implementation of {@link CycleDetector#findCycles()}. (EPL)
*/
private static <T, E> ListIterable<Set<T>> getCycleComponents(final DirectedGraph<T, E> graph) {
StrongConnectivityInspector<T, E> inspector =
new StrongConnectivityInspector<T, E>(graph);
return ListAdapter.adapt(inspector.stronglyConnectedSets()).select(new Predicate<Set<T>>() {
@Override
public boolean accept(Set<T> each) {
if (each.size() > 1) {
// multi-vertex strongly-connected component is a cycle
return true;
}
// vertex with an edge to itself is a cycle
T vertex = each.iterator().next();
return graph.containsEdge(vertex, vertex);
}
});
}
示例7: sortChanges
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
/**
* Sorts the graph to provide a consistent topological ordering.
*
* @param graph The input graph
* @param subsetVertices The subset vertices of the graph we want to sort
* @param comparator The comparator on which to order the vertices to guarantee a consistent topological ordering
*/
public <T> ImmutableList<T> sortChanges(DirectedGraph<T, DefaultEdge> graph, RichIterable<T> subsetVertices, Comparator<? super T> comparator) {
if (subsetVertices.toSet().size() != subsetVertices.size()) {
throw new IllegalStateException("Unexpected state - have some dupe elements here: " + subsetVertices);
}
DirectedGraph<T, DefaultEdge> subsetGraph = new DirectedSubgraph<T, DefaultEdge>(
graph, subsetVertices.toSet(), null);
// At one point, we _thought_ that the DirectedSubGraph was dropping vertices that don't have edges, so we
// manually add them back to the graph to ensure that we can still order them.
// However, that no longer seems to be the case. We add a check here just in case this comes up again.
if (subsetVertices.size() != subsetGraph.vertexSet().size()) {
throw new IllegalArgumentException("This case should never happen! [subsetVertices: " + subsetVertices + ", subsetGraphVertices: " + subsetGraph.vertexSet());
}
return sortChanges(subsetGraph, comparator);
}
示例8: testSchemaObjectDependencies
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
/**
* The test data in this class is all written w/ case-sensitivy as the default.
* If we pass caseInsensitive == true, then we enable that mode in the graph enricher and tweak the object names
* a bit so that we can verify that the resolution works either way.
*/
public void testSchemaObjectDependencies(boolean caseInsensitive) {
this.enricher = new GraphEnricherImpl(caseInsensitive ? StringFunctions.toUpperCase() : Functions.getStringPassThru());
SortableDependencyGroup sch1Obj1 = newChange(schema1, type1, "obj1", Sets.immutable.with("obj3", schema2 + ".obj2"));
SortableDependencyGroup sch1Obj2 = newChange(schema1, type2, "obj2", Sets.immutable.<String>with());
// change the case of the object name to ensure others can still point to it
SortableDependencyGroup sch1Obj3 = newChange(schema1, type1, caseInsensitive ? "obj3".toUpperCase() : "obj3", Sets.immutable.with("obj2"));
// change the case of the dependency name to ensure that it can point to others
SortableDependencyGroup sch2Obj1 = newChange(schema2, type1, "obj1", Sets.immutable.with(caseInsensitive ? "obj2".toUpperCase() : "obj2"));
SortableDependencyGroup sch2Obj2 = newChange(schema2, type2, "obj2", Sets.immutable.with(schema1 + ".obj3"));
DirectedGraph<SortableDependencyGroup, DefaultEdge> sortGraph = enricher.createDependencyGraph(Lists.mutable.with(
sch1Obj1, sch1Obj2, sch1Obj3, sch2Obj1, sch2Obj2), false);
validateChange(sortGraph, sch1Obj1, Sets.immutable.with(sch1Obj3, sch2Obj2), Sets.immutable.<SortableDependencyGroup>with());
validateChange(sortGraph, sch1Obj2, Sets.immutable.<SortableDependencyGroup>with(), Sets.immutable.with(sch1Obj3));
validateChange(sortGraph, sch1Obj3, Sets.immutable.with(sch1Obj2), Sets.immutable.with(sch1Obj1, sch2Obj2));
validateChange(sortGraph, sch2Obj1, Sets.immutable.with(sch2Obj2), Sets.immutable.<SortableDependencyGroup>with());
validateChange(sortGraph, sch2Obj2, Sets.immutable.with(sch1Obj3), Sets.immutable.with(sch1Obj1, sch2Obj1));
}
示例9: testGetChangesCaseInsensitive
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
@Test
public void testGetChangesCaseInsensitive() {
this.enricher = new GraphEnricherImpl(StringFunctions.toUpperCase());
String schema1 = "schema1";
String schema2 = "schema2";
String type1 = "type1";
SortableDependencyGroup sp1 = newChange(schema1, type1, "SP1", "n/a", 0, Sets.immutable.with("sp2"));
SortableDependencyGroup sp2 = newChange(schema1, type1, "SP2", Sets.immutable.<String>with());
SortableDependencyGroup sp3 = newChange(schema1, type1, "SP3", Sets.immutable.with("sp1", "sp2"));
SortableDependencyGroup spA = newChange(schema1, type1, "SPA", Sets.immutable.with("sp3"));
SortableDependencyGroup sp1Schema2 = newChange(schema2, type1, "sp1", "n/a", 0, Sets.immutable.with("sp2", schema1 + ".sp3"));
SortableDependencyGroup sp2Schema2 = newChange(schema2, type1, "sP2", "n/a", 0, Sets.immutable.<String>with());
DirectedGraph<SortableDependencyGroup, DefaultEdge> sortGraph = enricher.createDependencyGraph(Lists.mutable.with(
sp1, sp2, sp3, spA, sp1Schema2, sp2Schema2), false);
validateChange(sortGraph, sp1, Sets.immutable.with(sp2), Sets.immutable.with(sp3));
validateChange(sortGraph, sp2, Sets.immutable.<SortableDependencyGroup>with(), Sets.immutable.with(sp1, sp3));
validateChange(sortGraph, sp3, Sets.immutable.with(sp1, sp2), Sets.immutable.with(spA, sp1Schema2));
validateChange(sortGraph, spA, Sets.immutable.with(sp3), Sets.immutable.<SortableDependencyGroup>with());
validateChange(sortGraph, sp1Schema2, Sets.immutable.with(sp2Schema2, sp3), Sets.immutable.<SortableDependencyGroup>with());
validateChange(sortGraph, sp2Schema2, Sets.immutable.<SortableDependencyGroup>with(), Sets.immutable.with(sp1Schema2));
}
示例10: determineAndSolveConflicts
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
protected static void determineAndSolveConflicts(final PathBlockSet resultBlocks) {
// determine conflicts
final PathBlockSet deletedBlocks = new PathBlockSet();
final DirectedGraph<Block, BlockConflict> blockConflictGraph = new SimpleDirectedGraph<>(BlockConflict::of);
for (final Block block : resultBlocks.getBlocks()) {
blockConflictGraph.addVertex(block);
}
for (final Block x : blockConflictGraph.vertexSet()) {
createArcs(blockConflictGraph, resultBlocks, x);
}
// solve conflicts
while (true) {
final Optional<BlockConflict> mostUsefulConflictsFirst =
blockConflictGraph.edgeSet().stream().max(Comparator.comparingInt(BlockConflict::getQuality));
if (!mostUsefulConflictsFirst.isPresent()) {
break;
}
final BlockConflict blockConflict = mostUsefulConflictsFirst.get();
solveConflict(blockConflict, blockConflictGraph, resultBlocks, deletedBlocks);
}
}
示例11: solveConflict
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
protected static void solveConflict(final BlockConflict blockConflict,
final DirectedGraph<Block, BlockConflict> blockConflictGraph, final PathBlockSet resultBlocks,
final PathBlockSet deletedBlocks) {
final Block replaceBlock = blockConflict.getReplaceBlock();
final Set<FilterInstance> xWOcfi =
replaceBlock.getFlatFilterInstances().stream().filter(negate(blockConflict.getCfi()::contains))
.collect(toSet());
resultBlocks.remove(replaceBlock);
// remove replaceBlock and update qualities
removeArc(blockConflictGraph, blockConflict);
// find the horizontally maximal blocks within xWOcfi
final PathBlockSet newBlocks = findAllMaximalBlocksInReducedScope(xWOcfi, new PathBlockSet());
// for every such block,
for (final Block block : newBlocks.getBlocks()) {
if (!deletedBlocks.isContained(block)) {
if (resultBlocks.addDuringConflictResolution(block)) {
blockConflictGraph.addVertex(block);
createArcs(blockConflictGraph, resultBlocks, block);
}
}
}
deletedBlocks.addDuringConflictResolution(replaceBlock);
}
示例12: getParameterOrdering
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
private List<Field> getParameterOrdering(DirectedGraph<Field, DefaultEdge> dependencyGraph) {
CycleDetector<Field, DefaultEdge> cycleDetector = new CycleDetector<Field, DefaultEdge>(dependencyGraph);
if (cycleDetector.detectCycles()) {
throw new RuntimeException("Cyclic dependency detected!");
} else {
TopologicalOrderIterator<Field, DefaultEdge> orderIterator;
Field f;
orderIterator = new TopologicalOrderIterator<Field, DefaultEdge>(dependencyGraph);
List<Field> output = new ArrayList<>(dependencyGraph.vertexSet().size());
while (orderIterator.hasNext()) {
f = orderIterator.next();
output.add(f);
}
return output;
}
}
示例13: getAverageTwoTermReliability
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
/**
* Returns the average two-term reliability (A2TR) of the network. A2TR is computed
* as the ratio between the number of node-pair for which a path can be found
* and the same number when the network is connected (<i>Nx(N-1)</i>, where
* <i>N</i> is the number of nodes in the network). The value is in range [0, 1].
*
* @return Average two-term reliability
*/
public double getAverageTwoTermReliability()
{
if (E == 0) return 0;
DirectedGraph<Node, Link> graph = getGraph_JGraphT();
StrongConnectivityInspector<Node, Link> ci = new StrongConnectivityInspector<Node, Link>(graph);
List<Set<Node>> connectedComponents = ci.stronglyConnectedSets();
double sum = 0;
Iterator<Set<Node>> it = connectedComponents.iterator();
while (it.hasNext())
{
int componentSize = it.next().size();
sum += componentSize * (componentSize - 1);
}
return sum / (N * (N - 1));
}
示例14: getLinkConnectivity
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
/**
* <p>Returns the link connectivity. The link connectivity is equal to the smallest
* number of link-disjoint paths between each node pair.</p>
*
* <p>Internally it makes use of the Edmonds-Karp algorithm to compute the maximum
* flow between each node pair, assuming a link capacity equal to one for every link.</p>
*
* @return Link connectivity
*/
public int getLinkConnectivity()
{
if (E == 0) return 0;
DirectedGraph<Node, Link> graph = getGraph_JGraphT();
EdmondsKarpMaximumFlow<Node, Node> ek = new EdmondsKarpMaximumFlow(graph);
int k = Integer.MAX_VALUE;
for (Node originNode : nodes)
{
for (Node destinationNode : nodes)
{
if (originNode.equals(destinationNode)) continue;
ek.calculateMaximumFlow(originNode, destinationNode);
k = Math.min(k, ek.getMaximumFlowValue().intValue());
if (k == 0) break;
}
}
return k == Integer.MAX_VALUE ? 0 : k;
}
示例15: getNodeConnectivity
import org.jgrapht.DirectedGraph; //导入依赖的package包/类
/**
* Returns the node connectivity. The node connectivity is equal to the smallest
* number of node-disjoint paths between each node pair.
*
* <p>Internally it makes use of the (modified) Edmonds-Karp algorithm to compute the maximum
* flow between each node pair, assuming a link capacity equal to one for every link.</p>
*
* @return Node connectivity
*/
public int getNodeConnectivity()
{
if (E == 0) return 0;
DirectedGraph<Node, Link> graph = getGraph_JGraphT();
int k = Integer.MAX_VALUE;
for (Node originNode : nodes)
{
for (Node destinationNode : nodes)
{
if (originNode.equals(destinationNode)) continue;
DirectedGraph<Node, Link> auxGraph = (DirectedGraph<Node, Link>) JGraphTUtils.buildAuxiliaryNodeDisjointGraph(graph, originNode, destinationNode);
EdmondsKarpMaximumFlow<Node, Node> ek = new EdmondsKarpMaximumFlow(auxGraph);
ek.calculateMaximumFlow(originNode, destinationNode);
k = Math.min(k, ek.getMaximumFlowValue().intValue());
if (k == 0) break;
}
}
return k == Integer.MAX_VALUE ? 0 : k;
}