Search in sources :

Example 1 with GraphValidationException

use of org.eclipse.elk.core.validation.GraphValidationException in project elk by eclipse.

the class ElkUtil method applyVisitorsWithValidation.

/**
 * Apply the given graph element visitors to the content of the given graph. If validators are involved
 * and at least one error is found, a {@link GraphValidationException} is thrown.
 *
 * @param graph the graph the visitors shall be applied to.
 * @param visitors the visitors to apply.
 * @throws GraphValidationException if an error is found while validating the graph
 */
public static void applyVisitorsWithValidation(final ElkNode graph, final IGraphElementVisitor... visitors) throws GraphValidationException {
    applyVisitors(graph, visitors);
    // Gather validator results and generate an error message
    List<GraphIssue> allIssues = null;
    for (int i = 0; i < visitors.length; i++) {
        if (visitors[i] instanceof IValidatingGraphElementVisitor) {
            Collection<GraphIssue> issues = ((IValidatingGraphElementVisitor) visitors[i]).getIssues();
            if (!issues.isEmpty()) {
                if (allIssues == null) {
                    allIssues = new ArrayList<GraphIssue>(issues);
                } else {
                    allIssues.addAll(issues);
                }
            }
        }
    }
    // TODO print warnings to log if there are no errors
    if (allIssues != null && allIssues.stream().anyMatch(issue -> issue.getSeverity() == GraphIssue.Severity.ERROR)) {
        StringBuilder message = new StringBuilder();
        for (GraphIssue issue : allIssues) {
            if (message.length() > 0) {
                message.append("\n");
            }
            message.append(issue.getSeverity()).append(": ").append(issue.getMessage()).append("\n\tat ");
            ElkUtil.printElementPath(issue.getElement(), message);
        }
        throw new GraphValidationException(message.toString(), allIssues);
    }
}
Also used : CoreOptions(org.eclipse.elk.core.options.CoreOptions) Iterables(com.google.common.collect.Iterables) ListIterator(java.util.ListIterator) PortSide(org.eclipse.elk.core.options.PortSide) ContentAlignment(org.eclipse.elk.core.options.ContentAlignment) ElkGraphFactory(org.eclipse.elk.graph.ElkGraphFactory) ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) PortLabelPlacement(org.eclipse.elk.core.options.PortLabelPlacement) KVectorChain(org.eclipse.elk.core.math.KVectorChain) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) ElkRectangle(org.eclipse.elk.core.math.ElkRectangle) Strings(com.google.common.base.Strings) LabelAdapter(org.eclipse.elk.core.util.adapters.GraphAdapters.LabelAdapter) Lists(com.google.common.collect.Lists) Map(java.util.Map) GraphValidationException(org.eclipse.elk.core.validation.GraphValidationException) PortAdapter(org.eclipse.elk.core.util.adapters.GraphAdapters.PortAdapter) Iterator(java.util.Iterator) PortConstraints(org.eclipse.elk.core.options.PortConstraints) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVector(org.eclipse.elk.core.math.KVector) Collection(java.util.Collection) SizeOptions(org.eclipse.elk.core.options.SizeOptions) Set(java.util.Set) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) EObject(org.eclipse.emf.ecore.EObject) ElkShape(org.eclipse.elk.graph.ElkShape) Maps(com.google.common.collect.Maps) File(java.io.File) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) List(java.util.List) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) NodeLabelPlacement(org.eclipse.elk.core.options.NodeLabelPlacement) ElkBendPoint(org.eclipse.elk.graph.ElkBendPoint) Direction(org.eclipse.elk.core.options.Direction) IValidatingGraphElementVisitor(org.eclipse.elk.core.validation.IValidatingGraphElementVisitor) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) EdgeLabelPlacement(org.eclipse.elk.core.options.EdgeLabelPlacement) GraphIssue(org.eclipse.elk.core.validation.GraphIssue) ElkEdge(org.eclipse.elk.graph.ElkEdge) Pattern(java.util.regex.Pattern) ElkGraphUtil(org.eclipse.elk.graph.util.ElkGraphUtil) GraphValidationException(org.eclipse.elk.core.validation.GraphValidationException) IValidatingGraphElementVisitor(org.eclipse.elk.core.validation.IValidatingGraphElementVisitor) GraphIssue(org.eclipse.elk.core.validation.GraphIssue) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) ElkBendPoint(org.eclipse.elk.graph.ElkBendPoint)

Aggregations

Strings (com.google.common.base.Strings)1 Iterables (com.google.common.collect.Iterables)1 Iterators (com.google.common.collect.Iterators)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Map (java.util.Map)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 ElkRectangle (org.eclipse.elk.core.math.ElkRectangle)1 KVector (org.eclipse.elk.core.math.KVector)1 KVectorChain (org.eclipse.elk.core.math.KVectorChain)1 ContentAlignment (org.eclipse.elk.core.options.ContentAlignment)1 CoreOptions (org.eclipse.elk.core.options.CoreOptions)1 Direction (org.eclipse.elk.core.options.Direction)1