Package com.tnsai.invariants
Class InvariantChecker
java.lang.Object
com.tnsai.invariants.InvariantChecker
- All Implemented Interfaces:
com.tnsai.invariants.InvariantCheckerHandle
Runtime invariant checker for TnsAI roles.
Standalone checker capabilities:
- @ActionSpec.precondition() - before action execution
- @ActionSpec.postcondition() - after action execution
- @State.invariants() - after any state change
ActionExecutor calls only checkStateInvariants() for an
explicit @InvariantCheck. Direct callers may evaluate
planner expressions with checkPrecondition(Method) and
checkPostcondition(Method), but those expressions do not gate
runtime action dispatch. Runtime contracts belong on @Contract.
Method-level @ActionSpec.invariants() was removed in 0.9.2 —
use @State.invariants for state predicates and
@ActionSpec.mustAlways/mustNever for behavioral constraints.
Usage
InvariantChecker checker = new InvariantChecker(role);
// Before action
checker.checkPrecondition(method);
// Execute action...
// After action
checker.checkPostcondition(method);
checker.checkStateInvariants();
- Author:
- TnsAI Team
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.tnsai.invariants.InvariantCheckerHandle
com.tnsai.invariants.InvariantCheckerHandle.Factory -
Constructor Summary
ConstructorsConstructorDescriptionInvariantChecker(Object target) Creates a checker for the given target object.InvariantChecker(Object target, boolean strict) Creates a checker for the given target object. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidChecks invariants for a method on the given target.static voidcheckAfter(Object target, String methodName) Checks invariants after action execution.voidcheckAfterAction(Method method) Checks all conditions after action execution.voidcheckBeforeAction(Method method) Checks all conditions before action execution.voidcheckPostcondition(Method method) Checks the postcondition of an action.voidcheckPrecondition(Method method) Checks the precondition of an action.voidChecks all @State field invariants.collectViolations(Method method) Collects all violations without throwing exceptions.setStateField(String fieldName) Sets the state field name used for conditional rule effect checking.
-
Constructor Details
-
InvariantChecker
Creates a checker for the given target object.- Parameters:
target- the object to check invariants on
-
InvariantChecker
Creates a checker for the given target object.- Parameters:
target- the object to check invariants onstrict- if true, throws exceptions; if false, only logs warnings
-
-
Method Details
-
setStateField
Sets the state field name used for conditional rule effect checking.- Parameters:
fieldName- the field name (default: "state")- Returns:
- this for chaining
-
checkPrecondition
Checks the precondition of an action.- Specified by:
checkPreconditionin interfacecom.tnsai.invariants.InvariantCheckerHandle- Parameters:
method- the action method- Throws:
com.tnsai.invariants.InvariantViolationException- if precondition fails (in strict mode)
-
checkPostcondition
Checks the postcondition of an action.- Specified by:
checkPostconditionin interfacecom.tnsai.invariants.InvariantCheckerHandle- Parameters:
method- the action method- Throws:
com.tnsai.invariants.InvariantViolationException- if postcondition fails (in strict mode)
-
checkStateInvariants
public void checkStateInvariants()Checks all @State field invariants.- Specified by:
checkStateInvariantsin interfacecom.tnsai.invariants.InvariantCheckerHandle- Throws:
com.tnsai.invariants.InvariantViolationException- if any state invariant fails (in strict mode)
-
checkBeforeAction
Checks all conditions before action execution.- Parameters:
method- the action method
-
checkAfterAction
Checks all conditions after action execution.- Parameters:
method- the action method
-
collectViolations
Collects all violations without throwing exceptions.- Parameters:
method- the action method- Returns:
- list of violation messages
-
check
Checks invariants for a method on the given target.- Parameters:
target- the target objectmethodName- the method name- Throws:
com.tnsai.invariants.InvariantViolationException- if any check fails
-
checkAfter
Checks invariants after action execution.- Parameters:
target- the target objectmethodName- the method name- Throws:
com.tnsai.invariants.InvariantViolationException- if any check fails
-