gnodet-bot commented on code in PR #27494: URL: https://github.com/apache/camel/pull/27494#discussion_r4215913878
########## components/camel-ai/camel-semantic/src/main/java/org/apache/camel/semantic/SemanticEvaluations.java: ########## @@ -0,0 +1,177 @@ +/* + * 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.semantic; + +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; +import java.util.function.Consumer; + +import org.apache.camel.CamelContext; +import org.apache.camel.language.semantic.SemanticLanguage; +import org.apache.camel.spi.Resource; + +/** Context-local named evaluations, replaced atomically per source when a route resource is reloaded. */ +public final class SemanticEvaluations { + private static final Object CREATION_LOCK = new Object(); + private final CamelContext context; + private final Map<String, Map<String, SemanticEvaluation>> sources = new HashMap<>(); + private final Map<String, Resource> resources = new HashMap<>(); + private volatile Map<String, SemanticEvaluation> evaluations = Map.of(); + private final Map<Consumer<Map<String, SemanticEvaluation>>, List<String>> validators = new WeakHashMap<>(); + + private SemanticEvaluations(CamelContext context) { + this.context = context; + } + + /** Validate every declaration, including evaluations not referenced by a route. */ + public void validate() { + if (!evaluations.isEmpty()) { + ((SemanticLanguage) context.resolveLanguage("semantic")).validateDeclarations(evaluations); + } + } + + /** + * Validate current declarations and weakly track a callback owned by its initialized expression. The callback + * receives the complete immutable snapshot so nested references can be validated against the same replacement. + */ + public synchronized void setValidator(List<String> names, Consumer<Map<String, SemanticEvaluation>> validator) { + // A replacement may have occurred between the expression's initial compilation and registration. + get(names); + validator.accept(evaluations); Review Comment: 💡 **Suggestion:** The comment above the method explains *what* the snapshot does, but doesn't document *why* it's safe for `validator.accept(evaluations)` to see the pre-replacement snapshot here even though `setValidator` is `synchronized`. Consider a short inline note: ```suggestion validator.accept(evaluations); // evaluations is volatile; reads the published snapshot atomically ``` The invariant being relied on (volatile read of `evaluations` is atomic and sees the last-published snapshot) is non-obvious when the method is read in isolation. A one-liner saves the next reader a reasoning trip through the field declaration. ########## components/camel-ai/camel-semantic/src/main/java/org/apache/camel/language/semantic/SemanticLanguage.java: ########## @@ -290,8 +296,57 @@ private Object discoverAdapter(CamelContext context) throws IOException { return resolved; } - private ResolvedExpert expert(String name, SemanticQuestion question) { - String reference = question.getExpert() != null ? question.getExpert() : defaultExpert; + /** Validate declarations, including unused evaluations, without starting managed expert resources. */ + public void validateDeclarations(Map<String, SemanticEvaluation> declarations) { + validateDeclarations(declarations, null); + } + + /** Validate changed declarations against the complete candidate snapshot without publishing it. */ + public void validateDeclarations( + Map<String, SemanticEvaluation> declarations, Map<String, SemanticEvaluation> snapshot) { + validationOnly(snapshot, () -> { + declarations.forEach((name, evaluation) -> { + expert(name, evaluation); + String selector = evaluation.getState() != null ? evaluation.getState() : defaultState; + if (selector == null || selector.isBlank()) { + throw new IllegalArgumentException("Semantic evaluation '" + name + "': state must not be blank"); + } + Expression state = getCamelContext().resolveLanguage("simple") + .createExpression(getCamelContext().resolvePropertyPlaceholders(selector)); + state.init(getCamelContext()); + }); + return null; + }); + } + + private <T> T validationOnly(Map<String, SemanticEvaluation> snapshot, Supplier<T> action) { + Validation previous = validation.get(); + validation.set(snapshot != null || previous == null ? new Validation(snapshot) : previous); + try { Review Comment: 💡 **Suggestion:** The ternary `snapshot != null || previous == null ? new Validation(snapshot) : previous` encodes a non-trivial truth table. A brief comment would help readers follow the intent without re-deriving all four cases: ```suggestion // Explicit snapshot overrides; re-entrant calls inherit the outer validation scope unless a new snapshot is provided. validation.set(snapshot != null || previous == null ? new Validation(snapshot) : previous); ``` -- 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]
