Java Code Examples for org.jgrapht.Graph#vertexSet()

The following examples show how to use org.jgrapht.Graph#vertexSet() . 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: GraphComparator.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
private void checkPresenceOfExpectedNodes(final Graph<Node, Edge> givenGraph, final Graph<Node, Edge> expectedGraph,
        final AssertionErrorCollector errorCollector) {
    for (final Node expectedNode : expectedGraph.vertexSet()) {

        final List<String> attributesToExclude = expectedNode.getLabels().stream().map(toExclude::getColumns).flatMap(List::stream)
                .distinct().collect(toList());

        final List<Node> availableNodesOfExpectedType = givenGraph.vertexSet().stream()
                .filter(n -> n.getType().equals(expectedNode.getType())).collect(toList());

        final List<Node> foundNodes = availableNodesOfExpectedType.stream().filter(n -> n.isSame(expectedNode, attributesToExclude))
                .collect(toList());

        if (foundNodes.isEmpty()) {
            errorCollector.collect(expectedNode.asString() + " was expected, but is not present");
        } else if (foundNodes.size() > 1) {
            errorCollector.collect("Ambiguouty detected for node " + expectedNode.asString() + " for given attribute filter");
        }
    }
}
 
Example 2
Source File: GraphFunctions.java    From symja_android_library with GNU General Public License v3.0 6 votes vote down vote up
private static void vertexToVisjs(Map<IExpr, Integer> map, StringBuilder buf, Graph<IExpr, ?> g) {
	Set<IExpr> vertexSet = g.vertexSet();
	IASTAppendable vertexes = F.ListAlloc(vertexSet.size());
	buf.append("var nodes = new vis.DataSet([\n");
	boolean first = true;
	int counter = 1;
	for (IExpr expr : vertexSet) {
		// {id: 1, label: 'Node 1'},
		if (first) {
			buf.append("  {id: ");
		} else {
			buf.append(", {id: ");
		}
		buf.append(counter);
		map.put(expr, counter++);
		buf.append(", label: '");
		buf.append(expr.toString());
		buf.append("'}\n");
		first = false;
	}
	buf.append("]);\n");
}
 
Example 3
Source File: GraphComparator.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void shouldBeEmpty(final Graph<Node, Edge> givenGraph, final AssertionErrorCollector errorCollector) {
    final Map<String, Integer> unexpectedNodesOccurence = new HashMap<>();

    for (final Node node : givenGraph.vertexSet()) {
        unexpectedNodesOccurence.compute(node.getType(), (k, v) -> v == null ? 1 : v + 1);
    }

    for (final Entry<String, Integer> nodeEntries : unexpectedNodesOccurence.entrySet()) {
        if (nodeEntries.getValue() != 0) {
            errorCollector.collect("No nodes with " + nodeEntries.getKey() + " labels were expected, but there are <"
                    + nodeEntries.getValue() + "> nodes present.");
        }
    }
}
 
Example 4
Source File: GraphComparator.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void checkAbsenseOfNotExpectedNodes(final Graph<Node, Edge> givenGraph, final Graph<Node, Edge> expectedGraph,
        final AssertionErrorCollector errorCollector) {

    final List<List<String>> expectedNodeLables = expectedGraph.vertexSet().stream().map(Node::getLabels).distinct().collect(toList());

    for (final List<String> labels : expectedNodeLables) {
        final List<Node> expectedNodes = expectedGraph.vertexSet().stream().filter(node -> node.getLabels().containsAll(labels))
                .collect(toList());

        final List<String> attributesToExclude = labels.stream().map(toExclude::getColumns).flatMap(List::stream).distinct()
                .collect(toList());

        for (final Node givenNode : givenGraph.vertexSet()) {
            if (!givenNode.getLabels().containsAll(labels)) {
                continue;
            }

            final boolean nodePresent = expectedNodes.stream().anyMatch(node -> {
                final Set<Attribute> attributesToRetain = node.getAttributes().stream()
                        .filter(a -> !attributesToExclude.contains(a.getName())).collect(toSet());

                return givenNode.getAttributes().containsAll(attributesToRetain);
            });

            if (!nodePresent) {
                errorCollector.collect(givenNode.asString() + " was not expected, but is present");
            }
        }
    }
}
 
Example 5
Source File: DeleteAllOperation.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(final Connection connection, final Graph<Node, Edge> graph) throws SQLException {
    for (final Node node : graph.vertexSet()) {

        final StartNext match = match(node.toPath().withId("n").build());

        executeQuery(connection, match.toString() + " DETACH DELETE n");
    }
}
 
Example 6
Source File: NFAAnalysisTools.java    From RegexStaticAnalysis with MIT License 5 votes vote down vote up
/**
 * Constructs a list of NFA graphs each representing a strongly connected
 * component in the graph given as parameter.
 * 
 * @param m
 *            The NFA graph to find the strongly connected components in.
 * @return A list containing all the strongly connected components.
 * @throws InterruptedException 
 */
public static LinkedList<NFAGraph> getStronglyConnectedComponents(NFAGraph m) throws InterruptedException {
	KosarajuStrongConnectivityInspector<NFAVertexND, NFAEdge> sci = new KosarajuStrongConnectivityInspector<NFAVertexND, NFAEdge>(m);
	List<Graph<NFAVertexND, NFAEdge>> sccs = sci.getStronglyConnectedComponents();
	LinkedList<NFAGraph> sccNFAs = new LinkedList<NFAGraph>();

	for (Graph<NFAVertexND, NFAEdge> scc : sccs) {
		if (isInterrupted()) {
			throw new InterruptedException();
		}

		/* scc's consisting of no edges are irrelevant for our purpose */
		if (scc.edgeSet().size() > 0) {

			NFAGraph currentNFAG = new NFAGraph();
			for (NFAVertexND v : scc.vertexSet()) {
				if (isInterrupted()) {
					throw new InterruptedException();
				}
				currentNFAG.addVertex(v);
			}
			for (NFAEdge e : scc.edgeSet()) {
				if (isInterrupted()) {
					throw new InterruptedException();
				}
				currentNFAG.addEdge(e);
			}

			sccNFAs.add(currentNFAG);
		}

	}
	return sccNFAs;
}
 
Example 7
Source File: GraphFunctions.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
private static IASTAppendable vertexToIExpr(Graph<IExpr, ?> g) {
	Set<IExpr> vertexSet = g.vertexSet();
	IASTAppendable vertexes = F.ListAlloc(vertexSet.size());
	for (IExpr expr : vertexSet) {
		vertexes.append(expr);
	}
	return vertexes;
}
 
Example 8
Source File: GraphFunctions.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
public static IAST graphToAdjacencyMatrix(Graph<IExpr, ExprEdge> g) {
	Set<IExpr> vertexSet = g.vertexSet();
	int size = vertexSet.size();
	IExpr[] array = new IExpr[size];
	int indx = 0;
	for (IExpr expr : vertexSet) {
		array[indx++] = expr;
	}
	return F.matrix((i, j) -> g.containsEdge(array[i], array[j]) ? F.C1 : F.C0, size, size);
}
 
Example 9
Source File: QuatSymmetryDetector.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Returns a List of LOCAL symmetry results. This means that a subset of the
 * {@link SubunitCluster} is left out of the symmetry calculation. Each
 * element of the List is one possible LOCAL symmetry result.
 * <p>
 * Determine local symmetry if global structure is: (1) asymmetric, C1; (2)
 * heteromeric (belongs to more than 1 subunit cluster); (3) more than 2
 * subunits (heteromers with just 2 chains cannot have local symmetry)
 *
 * @param globalComposition
 *            {@link Stoichiometry} object that contains global clustering results
 * @param symmParams
 *            quaternary symmetry parameters
 * @return List of LOCAL quaternary structure symmetry results. Empty if
 *         none.
 */

public static List<QuatSymmetryResults> calcLocalSymmetries(Stoichiometry globalComposition, QuatSymmetryParameters symmParams) {

	Set<Set<Integer>> knownCombinations = new HashSet<>();
	List<SubunitCluster> clusters = globalComposition.getClusters();
	//more than one subunit per cluster required for symmetry
	List<SubunitCluster> nontrivialClusters =
			clusters.stream().
				filter(cluster -> (cluster.size()>1)).
				collect(Collectors.toList());

	QuatSymmetrySubunits consideredSubunits = new QuatSymmetrySubunits(nontrivialClusters);
	if (consideredSubunits.getSubunitCount() < 2)
		return new ArrayList<>();

	Graph<Integer, DefaultEdge> graph = initContactGraph(nontrivialClusters);
	Stoichiometry nontrivialComposition = new Stoichiometry(nontrivialClusters,false);

	List<Integer> allSubunitIds = new ArrayList<>(graph.vertexSet());
	Collections.sort(allSubunitIds);
	List<Integer> allSubunitClusterIds = consideredSubunits.getClusterIds();

	// since clusters are rearranged and trimmed, we need a reference to the original data
	// to maintain consistent IDs of clusters and subunits across all solutions
	Map<Integer, List<Integer>> clusterIdToSubunitIds =
			allSubunitIds.stream().
				collect(Collectors.
					groupingBy(allSubunitClusterIds::get, Collectors.toList()));

	List<QuatSymmetryResults> redundantSymmetries = new ArrayList<>();
	// first, find symmetries for single clusters and their groups
	// grouping is done based on symmetries found (i.e., no exhaustive permutation search is performed)
	if (clusters.size()>1) {

		List<QuatSymmetryResults> clusterSymmetries =
				calcLocalSymmetriesCluster(nontrivialComposition, clusterIdToSubunitIds,symmParams, knownCombinations);
		redundantSymmetries.addAll(clusterSymmetries);
	}
	//find symmetries for groups based on connectivity of subunits
	// disregarding initial clustering
	List<QuatSymmetryResults> graphSymmetries = calcLocalSymmetriesGraph(nontrivialComposition,
																		allSubunitClusterIds,
																		clusterIdToSubunitIds,
																		symmParams,
																		knownCombinations,
																		graph);

	redundantSymmetries.addAll(graphSymmetries);

	// find symmetries which are not superseded by any other symmetry
	// e.g., we have global stoichiometry of A3B3C,
	// the local symmetries found are C3 with stoichiometries A3, B3, A3B3;
	// then output only A3B3.

	List<QuatSymmetryResults> outputSymmetries =
			redundantSymmetries.stream().
				filter(a -> redundantSymmetries.stream().
					noneMatch(b -> a!=b && a.isSupersededBy(b))).
					collect(Collectors.toList());

	if(symmParams.isLocalLimitsExceeded(knownCombinations)) {
		logger.warn("Exceeded calculation limits for local symmetry detection. The results may be incomplete.");
	}

	return outputSymmetries;
}