gnodet-bot commented on code in PR #28040:
URL: https://github.com/apache/camel/pull/28040#discussion_r4306039807


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/HeaderChecks.java:
##########
@@ -57,6 +57,15 @@ private HeaderChecks() {
      */
     static final Map<String, String> METADATA_OPTION = Map.of("timer", 
"includeMetadata");
 
+    /**
+     * A comparison of the timer's counter with a number: the counter counts 
the fires from 1, so {@code == 0} is never
+     * true (CAMEL-25744: in 8 of 10 benchmark runs a model compared it with 0 
to pick the first fire). The Simple forms
+     * (${exchangeProperty.CamelTimerCounter} == 0, ${header.CamelTimerCounter 
== 0 ? ...}) and the Groovy ones
+     * (exchange.getProperty('CamelTimerCounter', Integer) == 0).
+     */
+    static final Pattern TIMER_COUNTER_COMPARISON = Pattern.compile(
+            
"CamelTimerCounter(?:\\}|['\"]\\s*(?:,\\s*[\\w.]+\\s*)?\\))?\\s*(==|!=|<=|<)\\s*['\"]?(\\d+)['\"]?");

Review Comment:
   ⚠️ **Missing operators:** The regex alternation covers `==|!=|<=|<` but not 
`>` or `>=`. `CamelTimerCounter > 0` is always true (counter ≥ 1), as are `>= 
0` and `>= 1`. A model writing `CamelTimerCounter > 0` as a guard won't be 
flagged. These are the symmetric "always true" counterparts of the `== 0` / `<= 
0` / `< 1` cases already handled.
   
   ```suggestion
               "CamelTimerCounter(?:\\}|['\"]\\ *(?:,\\ *[\\w.]+\\ 
*)?\\))?\\s*(==|!=|<=|<|>=|>)\\s*['\"" + ""]?(\\d+)['\"" + ""]?");
   ```



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/HeaderChecks.java:
##########
@@ -106,11 +115,36 @@ static String intentHint(String name) {
         return null;
     }
 
+    /**
+     * The comparisons of CamelTimerCounter that can never be true (== 0, <= 
0, < 1) or never be false (!= 0), as the
+     * counter is 1 on the first fire.
+     */
+    static List<String> timerCounterComparisons(String content) {
+        List<String> msgs = new ArrayList<>();
+        String[] lines = content.split("\n", -1);
+        for (int i = 0; i < lines.length; i++) {
+            Matcher m = TIMER_COUNTER_COMPARISON.matcher(lines[i]);
+            while (m.find()) {
+                String op = m.group(1);
+                int n = Integer.parseInt(m.group(2));
+                boolean never = ("==".equals(op) || "<=".equals(op)) && n == 0 
|| "<".equals(op) && n <= 1;
+                boolean always = "!=".equals(op) && n == 0;

Review Comment:
   ⚠️ **Missing `>` / `>=` cases in the condition:** if `>` and `>=` are added 
to the regex, extend the `always` predicate to cover `> 0` (always true, since 
counter ≥ 1), `>= 0` (always true), and `>= 1` (always true):
   
   ```suggestion
                   boolean never = ("==".equals(op) || "<=".equals(op)) && n == 
0 || "<".equals(op) && n <= 1;
                   boolean always = "!=".equals(op) && n == 0
                                    || ">".equals(op) && n == 0 || 
">=".equals(op) && n <= 1;
   ```



-- 
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