davsclaus commented on code in PR #28586:
URL: https://github.com/apache/camel/pull/28586#discussion_r4392933353


##########
components/camel-diagram/src/main/java/org/apache/camel/diagram/RouteDiagramLayoutEngine.java:
##########
@@ -564,9 +620,77 @@ public LayoutRoute layoutRoute(RouteInfo route, int 
startY) {
         lr.maxX = extent.maxX + overhang;
         lr.maxY = Math.max(lr.maxY, extent.maxY);
 
+        if (problems) {
+            layoutProblems(lr);
+        }
         return lr;
     }
 
+    private void layoutProblems(LayoutRoute lr) {
+        // in the order of the steps in the route (the order of the source)
+        List<LayoutNode> withProblems = lr.nodes.stream()
+                .filter(ln -> !ln.tableRow && ln.treeNode != null && 
!ln.treeNode.info.problems.isEmpty())
+                .toList();
+        if (withProblems.isEmpty()) {
+            return;
+        }
+        int lineHeight = fontMetrics.getHeight();
+        lr.problemList = lr.nodes.stream().anyMatch(ln -> 
isBranchingEip(ln.type));
+        if (!lr.problemList) {
+            // a column of callouts beside the route, each at the height of 
its step, below the one above it
+            int x = lr.maxX + CALLOUT_GAP;
+            int width = calloutWidth(nodeWidth);
+            int bottom = Integer.MIN_VALUE;
+            // without branches the order of the steps is from the top down
+            for (LayoutNode ln : withProblems) {
+                Callout c = new Callout();
+                c.node = ln;
+                c.lines = problemLines(ln.treeNode.info.problems, width - 2 * 
CALLOUT_PAD);
+                c.x = x;
+                c.width = width;
+                c.height = c.lines.size() * lineHeight + 2 * CALLOUT_PAD;
+                c.y = Math.max(ln.y + ln.height / 2 - c.height / 2, bottom + 
CALLOUT_PAD);
+                bottom = c.y + c.height;
+                lr.callouts.add(c);
+                lr.maxY = Math.max(lr.maxY, bottom);
+            }
+            lr.maxX = x + width;
+        } else {
+            // a numbered list below the steps, as wide as the route
+            int width = Math.max(lr.maxX - PADDING, nodeWidth * 2);
+            int y = lr.maxY + V_GAP / 2;
+            int number = 0;
+            for (LayoutNode ln : withProblems) {
+                Callout c = new Callout();
+                c.node = ln;
+                c.number = ++number;
+                c.lines = problemLines(ln.treeNode.info.problems, width - 
PROBLEM_NUMBER_WIDTH);
+                c.x = PADDING;
+                c.y = y;
+                c.width = width;
+                c.height = c.lines.size() * lineHeight;
+                y += c.height + CALLOUT_PAD;
+                lr.callouts.add(c);
+            }
+            lr.maxY = y;
+            lr.maxX = Math.max(lr.maxX, PADDING + width);
+        }
+    }
+
+    /** The problems wrapped to the width, at most six lines: the renderer 
cuts the last one with an ellipsis. */
+    private List<String> problemLines(List<String> problems, int width) {
+        List<String> lines = new ArrayList<>();
+        for (String problem : problems) {
+            lines.addAll(wrapLabel(problem, width, MAX_CALLOUT_LINES));
+        }
+        if (lines.size() > MAX_CALLOUT_LINES) {
+            String rest = String.join(" ", lines.subList(MAX_CALLOUT_LINES - 
1, lines.size()));
+            lines = new ArrayList<>(lines.subList(0, MAX_CALLOUT_LINES - 1));
+            lines.add(rest);
+        }
+        return lines;
+    }

Review Comment:
   Fixed in 1a06eabafe82. Each problem now starts on a line of its own and gets 
only the lines that are left (`wrapLabel(problem, width, room)`). A problem 
that is cut keeps the rest of its own text on its last line, and the renderer 
cuts that line with an ellipsis. Problems after that are left out, and ` …` on 
the last line says so. Lines of different problems are never joined. Test: 
`RouteDiagramCalloutTest.eachProblemFromALineOfItsOwn`.
   
   _Claude Code on behalf of davsclaus_



##########
components/camel-diagram/src/test/java/org/apache/camel/diagram/RouteDiagramCalloutTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.diagram;
+
+import java.awt.Color;
+import java.awt.image.BufferedImage;
+import java.util.List;
+
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.Callout;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.LayoutRoute;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.NodeInfo;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.RouteInfo;
+import org.apache.camel.diagram.RouteDiagramRenderer.DiagramColors;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * The problems of the steps in a route diagram image: callouts beside a 
route, a numbered list below a route with
+ * branches.
+ */
+class RouteDiagramCalloutTest {
+
+    private static final String GROOVY = "groovy reads fields of the body 
(body.items), but the body here is the file as"
+                                         + " it was read (a GenericFile), not 
parsed data - unmarshal it first";
+
+    private static NodeInfo node(String type, String code, int level, 
String... problems) {
+        NodeInfo n = new NodeInfo();
+        n.type = type;
+        n.id = type + level + code.length();
+        n.code = code;
+        n.level = level;
+        n.problems = List.of(problems);
+        return n;
+    }
+
+    private static RouteInfo orders() {
+        RouteInfo r = new RouteInfo();
+        r.routeId = "orders";
+        r.nodes.add(node("route", "route[orders]", 0));
+        r.nodes.add(node("from", "from[file:orders]", 1));
+        r.nodes.add(node("setBody", "setBody[groovy{body.items}]", 2, GROOVY));
+        r.nodes.add(node("to", "to[http:pricing]", 2, "the http producer 
cannot send a Map"));
+        r.nodes.add(node("to", "to[jms:priced]", 2));
+        return r;
+    }
+
+    private static RouteInfo choice() {
+        RouteInfo r = new RouteInfo();
+        r.routeId = "choice";
+        r.nodes.add(node("route", "route[choice]", 0));
+        r.nodes.add(node("from", "from[timer:tick]", 1));
+        r.nodes.add(node("choice", "choice", 2));
+        r.nodes.add(node("when", "when[simple{${header.kind} == 0}]", 3));
+        r.nodes.add(node("to", "to[kafka:zero?brokerz=localhost]", 4, "kafka 
has no option brokerz"));
+        r.nodes.add(node("otherwise", "otherwise", 3));
+        r.nodes.add(node("marshal", "marshal[jackson]", 4, "the body is a 
String here"));
+        return r;
+    }
+
+    private static RouteDiagramLayoutEngine engine(boolean problems) {
+        RouteDiagramLayoutEngine engine = new RouteDiagramLayoutEngine();
+        engine.setCardStyle(true);
+        engine.setProblems(problems);
+        return engine;
+    }
+
+    @Test
+    void calloutsBesideARoute() {
+        RouteDiagramLayoutEngine engine = engine(true);
+        LayoutRoute lr = engine.layoutRoute(orders(), 
RouteDiagramLayoutEngine.PADDING);
+
+        assertThat(lr.problemList).isFalse();
+        assertThat(lr.callouts).hasSize(2);
+        Callout groovy = lr.callouts.get(0);
+        Callout http = lr.callouts.get(1);
+        assertThat(groovy.node.type).isEqualTo("setBody");
+        assertThat(groovy.number).isZero();
+        // right of the steps, the text wrapped to more than one line
+        int right = groovy.node.x + engine.getNodeWidth();
+        assertThat(groovy.x).isEqualTo(right + 
RouteDiagramLayoutEngine.CALLOUT_GAP);
+        assertThat(groovy.lines).hasSizeGreaterThan(1);
+        assertThat(String.join(" ", groovy.lines)).contains("GenericFile");
+        // one below the other, and room for them in the image
+        assertThat(http.y).isGreaterThanOrEqualTo(groovy.y + groovy.height);
+        assertThat(lr.maxX).isEqualTo(groovy.x + groovy.width);
+
+        // without problems shown, the route does not change
+        LayoutRoute plain = engine(false).layoutRoute(orders(), 
RouteDiagramLayoutEngine.PADDING);
+        assertThat(plain.callouts).isEmpty();
+        assertThat(plain.maxX).isLessThan(lr.maxX);
+    }
+
+    @Test
+    void numberedListBelowARouteWithBranches() {
+        RouteDiagramLayoutEngine engine = engine(true);
+        LayoutRoute plain = engine(false).layoutRoute(choice(), 
RouteDiagramLayoutEngine.PADDING);
+        LayoutRoute lr = engine.layoutRoute(choice(), 
RouteDiagramLayoutEngine.PADDING);
+
+        assertThat(lr.problemList).isTrue();
+        assertThat(lr.callouts).hasSize(2);
+        // numbered in the order of the steps in the route, below the steps
+        assertThat(lr.callouts.get(0).number).isEqualTo(1);
+        assertThat(lr.callouts.get(0).node.type).isEqualTo("to");
+        assertThat(lr.callouts.get(1).number).isEqualTo(2);
+        assertThat(lr.callouts.get(1).node.type).isEqualTo("marshal");
+        assertThat(lr.callouts.get(0).y).isGreaterThan(plain.maxY);
+        assertThat(lr.maxY).isGreaterThan(plain.maxY);
+    }
+
+    @Test
+    void drawTheProblems() {

Review Comment:
   Added `RouteDiagramCalloutTest.drawTheNumberedList`. It renders the 
branching route and checks the warning colour of the numbered badge in front of 
the list entry, the warning border of the card, and the badge on the card.
   
   _Claude Code on behalf of davsclaus_



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelRouteDiagramAction.java:
##########
@@ -283,6 +291,7 @@ protected Integer doWatchCall() throws Exception {
         // an image draws the steps as cards: a title and its detail beside an 
icon
         engine.setCardStyle(!isTextTheme());
         engine.setBodyTypes(bodyTypes && !isTextTheme());
+        engine.setProblems(problems && !isTextTheme());

Review Comment:
   Applied: `engine.setProblems(problems && sourceMode && !isTextTheme())`. The 
problems come from checking the source files, which only happens in source mode.
   
   _Claude Code on behalf of davsclaus_



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/RouteDiagramProblems.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands.action;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.NodeInfo;
+import org.apache.camel.diagram.RouteDiagramLayoutEngine.RouteInfo;
+import org.apache.camel.dsl.jbang.core.commands.ai.RouteAssist;
+import org.apache.camel.dsl.jbang.core.commands.ai.SourceValidator;
+
+/**
+ * The problems of the source files of a route diagram, on the steps they are 
of: the checks of {@code camel validate}
+ * for a YAML route, and for Java and XML the checks that read the source 
without compiling it ({@link RouteAssist}), as
+ * the route diagram reads Java routes without compiling them.
+ */
+final class RouteDiagramProblems {
+
+    /** A problem of a line of the source; line 0 for a problem of the file. */
+    record Problem(int line, String text) {
+    }
+
+    private static final Pattern MESSAGE_LINE = Pattern.compile("^Line (\\d+): 
(.*)$", Pattern.DOTALL);
+    // the step and route a message names first, which the diagram shows 
already: "setBody: route orders: "
+    private static final Pattern STEP_PREFIX = 
Pattern.compile("^[A-Za-z][\\w-]*: route [^:\\s]+: ");
+
+    private RouteDiagramProblems() {
+    }
+
+    /**
+     * Puts the problems of the source files on the steps of their routes 
({@link NodeInfo#problems}): a problem on the
+     * step whose line is the last one before it (the line of the option or 
expression that is wrong is inside its
+     * step), a problem of the file on the first step.
+     */
+    static void attach(List<RouteInfo> routes, List<String> files) {
+        CamelCatalog catalog = null;
+        for (String file : files) {
+            Path path = Path.of(file);
+            if (!Files.isRegularFile(path)) {
+                continue;
+            }
+            String name = path.getFileName().toString();
+            List<NodeInfo> nodes = routes.stream()
+                    .filter(r -> sourceOf(r, name))
+                    .flatMap(r -> r.nodes.stream())
+                    .toList();
+            if (nodes.isEmpty()) {
+                continue;
+            }
+            String content;
+            try {
+                content = Files.readString(path);
+            } catch (IOException e) {
+                continue;

Review Comment:
   Changed so that it is no longer silent. An IOException now becomes a problem 
of the file on the first step, for example `the file cannot be read to check 
it: it is not UTF-8 text`. The image then says why its problems are missing, 
instead of looking as if the file has none. Throwing would fail the whole 
diagram over a check that is only a bonus. Test: 
`RouteDiagramProblemsTest.aFileThatCannotBeRead`.
   
   _Claude Code on behalf of davsclaus_



##########
components/camel-diagram/src/main/java/org/apache/camel/diagram/RouteDiagramRenderer.java:
##########
@@ -185,6 +186,7 @@ public static DiagramColors parse(String spec) {
             c.warning = parseColor(map.getOrDefault("warning", "#d29922"));
             c.warningBg = parseColor(map.getOrDefault("warningBg", "#272115"));
             c.warningText = parseColor(map.getOrDefault("warningText", 
"#e3b341"));
+            c.badgeText = parseColor(map.getOrDefault("badgeText", "#1f2328"));

Review Comment:
   Added `badgeText=#1f2328` to the dark, light and transparent presets, and 
documented it in the comment above them.
   
   _Claude Code on behalf of davsclaus_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to