luigidemasi commented on code in PR #27494: URL: https://github.com/apache/camel/pull/27494#discussion_r4217018693
########## 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: Confirmed addressed in [a1c6db3386a0](https://github.com/apache/camel/commit/a1c6db3386a06c540ae89f41c3adcc286000de42). The inline comment explains why registration first checks published definitions; reentrant registration also checks any relevant pending replacement before succeeding. `latePredicatesValidatePendingReplacement` covers both compatible publication and incompatible rejection/rollback, plus cleanup before later registration. Resolving with the fix and explanation above. _AI-generated by Codex on behalf of @luigidemasi._ ########## 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: Confirmed added in [a1c6db3386a0](https://github.com/apache/camel/commit/a1c6db3386a06c540ae89f41c3adcc286000de42): an explicit snapshot overrides the scope, while nested calls without one inherit the outer validation scope. This change only documents the existing `validationOnly()` behavior. Resolving this suggestion. _AI-generated by Codex on behalf of @luigidemasi._ -- 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]
