|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.verifier; |
| 16 | + |
| 17 | +import com.google.auto.value.AutoValue; |
| 18 | +import com.google.common.collect.ImmutableMap; |
| 19 | +import com.google.errorprone.annotations.Immutable; |
| 20 | +import dev.cel.common.types.CelType; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Optional; |
| 23 | +import org.jspecify.annotations.Nullable; |
| 24 | + |
| 25 | +/** Encapsulates a structured variable assignment model produced by formal verification. */ |
| 26 | +@AutoValue |
| 27 | +@AutoValue.CopyAnnotations |
| 28 | +@Immutable |
| 29 | +public abstract class CelCounterexample { |
| 30 | + |
| 31 | + /** Represents a single variable binding within a counterexample. */ |
| 32 | + @AutoValue |
| 33 | + @AutoValue.CopyAnnotations |
| 34 | + @Immutable |
| 35 | + @SuppressWarnings("Immutable") // Values are deeply immutable. |
| 36 | + public abstract static class Binding { |
| 37 | + /** Returns the name of the variable. */ |
| 38 | + public abstract String name(); |
| 39 | + |
| 40 | + /** Returns the inferred CEL type of the variable. */ |
| 41 | + public abstract CelType type(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns the native Java representation of the value (e.g., Long, Boolean, String, Instant, |
| 45 | + * Duration, ImmutableList, ImmutableMap, Message, etc.), or empty if unassigned or unavailable. |
| 46 | + */ |
| 47 | + public abstract Optional<Object> nativeValue(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns the CEL literal representation of the value (e.g., "80", "\"admin\"", "true", "[1, |
| 51 | + * 2]"). |
| 52 | + */ |
| 53 | + public abstract String celString(); |
| 54 | + |
| 55 | + public static Binding of( |
| 56 | + String name, CelType type, @Nullable Object nativeValue, String celString) { |
| 57 | + return new AutoValue_CelCounterexample_Binding( |
| 58 | + name, type, Optional.ofNullable(nativeValue), celString); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** Returns all variable bindings keyed by variable name. */ |
| 63 | + public abstract ImmutableMap<String, Binding> bindings(); |
| 64 | + |
| 65 | + /** Returns true if this counterexample was derived from an approximate solver model. */ |
| 66 | + public abstract boolean isApproximate(); |
| 67 | + |
| 68 | + /** Returns true if this model represents a satisfying assignment rather than a counterexample. */ |
| 69 | + public abstract boolean isSatisfyingInput(); |
| 70 | + |
| 71 | + /** Returns the formatted display string representation. */ |
| 72 | + public abstract String toDisplayString(); |
| 73 | + |
| 74 | + /** Looks up a variable binding by name. */ |
| 75 | + public Optional<Binding> get(String variableName) { |
| 76 | + return Optional.ofNullable(bindings().get(variableName)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns a native Java variable map suitable for evaluating expressions in CelRuntime (e.g., |
| 81 | + * Cel.createProgram().eval(toEvaluationContext())). |
| 82 | + */ |
| 83 | + public ImmutableMap<String, Object> toEvaluationContext() { |
| 84 | + ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder(); |
| 85 | + for (Binding binding : bindings().values()) { |
| 86 | + binding.nativeValue().ifPresent(value -> builder.put(binding.name(), value)); |
| 87 | + } |
| 88 | + return builder.buildOrThrow(); |
| 89 | + } |
| 90 | + |
| 91 | + public static CelCounterexample create( |
| 92 | + Map<String, Binding> bindings, |
| 93 | + boolean isApproximate, |
| 94 | + boolean isSatisfyingInput, |
| 95 | + String toDisplayString) { |
| 96 | + return new AutoValue_CelCounterexample( |
| 97 | + ImmutableMap.copyOf(bindings), isApproximate, isSatisfyingInput, toDisplayString); |
| 98 | + } |
| 99 | +} |
0 commit comments