Assemble JsonLogicEvaluationContext In ActionValidationService
Introduction
In the context of Action Validation, it is crucial to assemble the correct JsonLogicEvaluationContext required for evaluating prerequisite rules. This context object is essential for the ConditionEvaluatorService to evaluate the prerequisite JSON Logic rules. In this article, we will delve into the process of assembling the JsonLogicEvaluationContext object within the ActionValidationService.
Background
The ActionValidationService is responsible for validating actions, and it needs to gather all necessary contextual data into the specific JsonLogicEvaluationContext structure expected by the ConditionEvaluatorService. This ticket focuses on assembling the context object at the appropriate point within the validation flow. The actual evaluation call happens in a subsequent ticket.
Tasks
Identify Location
The first step is to locate the section within the ActionValidationService's isValid
method (or its equivalent) where prerequisite checks are performed. This is typically done in Step 4, where the action definition's prerequisites are iterated through. Before the loop that calls _checkSinglePrerequisite
, we need to ensure that the necessary dependencies are imported.
Import Dependencies
To assemble the JsonLogicEvaluationContext object, we need to import the necessary types and functions. Specifically, we need to import the JsonLogicEvaluationContext type definition and potentially the createJsonLogicContext
factory function (if used). Additionally, we need to ensure that the EntityManager is available, which is likely already injected.
Gather Contextual Data
Within the isValid
method, we need to ensure access to the following contextual data:
actorEntity
: The validated actor entity instance.targetEntity
: The resolved target entity instance (which might be null if the target context type is not 'entity' or resolution failed earlier).targetContext
: The input ActionTargetContext object containing type, entityId, or direction.EntityManager
: The service instance.ILogger
: The logger instance.
Assemble Context Object
Before iterating through the prerequisites array, we need to create the JsonLogicEvaluationContext object. This involves:
- Populating actor: Create the JsonLogicEntityContext for the actorEntity (ID and component accessor). Use the
createComponentAccessor
helper if available. Handle the case where the actorEntity might somehow be invalid (though prior validation should prevent this). - Populating target: If the targetEntity exists (i.e., the targetContext type is 'entity' and resolution succeeded), create the JsonLogicEntityContext for the targetEntity. Otherwise, set the target to null.
- Populating event: This field is less relevant in the Action Validation context compared to System Rule evaluation. Define a placeholder or minimal structure (e.g., type }) or determine if it's needed based on expected JSON Logic rules. If not needed, potentially omit or set to {}. Decision needed: Clarify if event data is expected/useful for action prerequisites. Assume minimal/placeholder for now.
- Populating context: Initialize as an empty object {}. Action validation prerequisites typically don't rely on prior query results within the same validation check.
- Populating globals/entities: Initialize as empty objects {} unless global state access is explicitly required for prerequisites (unlikely for initial implementation### Pass Context
Ensure that the created JsonLogicEvaluationContext object is available to be passed into the prerequisite evaluation step (the loop calling _checkSinglePrerequisite
or its replacement in a subsequent ticket).
Acceptance Criteria
The ActionValidationService's isValid
method correctly assembles a JsonLogicEvaluationContext object before prerequisite evaluation begins. The actor property of the context is correctly populated with the actor's ID and a component accessor, or is null if the actor is invalid. The target property of the context is correctly populated with the target's ID and component accessor if a valid targetEntity exists, otherwise it's null. The event, context, globals, and entities properties are initialized appropriately (e.g., minimal placeholders or empty objects). The assembled context object is available for use in the subsequent prerequisite evaluation loop.
Dependencies
This ticket depends on the definition of JsonLogicEvaluationContext and potentially JsonLogicEntityContext and createComponentAccessor
in Ticket 2 (Runtime Evaluator). It also requires EntityManager access.
Estimate
The estimate for this task is medium, as it requires understanding context assembly and interaction with EntityManager/component access.
Conclusion
Assembling the JsonLogicEvaluationContext object within the ActionValidationService is a crucial step in evaluating prerequisite rules. By following the tasks outlined in this article, we can ensure that the correct context object is created and passed into the prerequisite evaluation step. This will enable the ConditionEvaluatorService to evaluate the prerequisite JSON Logic rules correctly.
Introduction
In our previous article, we explored the process of assembling the JsonLogicEvaluationContext object within the ActionValidationService. This context object is essential for the ConditionEvaluatorService to evaluate the prerequisite JSON Logic rules. In this Q&A article, we will address some common questions and concerns related to this process.
Q: What is the purpose of the JsonLogicEvaluationContext object?
A: The JsonLogicEvaluationContext object is a data structure that contains all the necessary contextual information required for evaluating prerequisite rules. It provides a standardized way of passing data between the ActionValidationService and the ConditionEvaluatorService.
Q: Why is it necessary to assemble the JsonLogicEvaluationContext object?
A: Assembling the JsonLogicEvaluationContext object ensures that the ConditionEvaluatorService receives the correct and complete data required for evaluating the prerequisite rules. This is crucial for accurate and reliable evaluation results.
Q: What are the key components of the JsonLogicEvaluationContext object?
A: The key components of the JsonLogicEvaluationContext object include:
- Actor: The validated actor entity instance.
- Target: The resolved target entity instance (or null if not applicable).
- Event: A placeholder or minimal structure (e.g., type }).
- Context: An empty object {}.
- Globals: Empty objects {} (unless global state access is explicitly required).
- Entities: Empty objects {} (unless entity access is explicitly required).
Q: How do I handle invalid actorEntity instances?
A: If the actorEntity instance is invalid, you should handle it by setting the actor property of the JsonLogicEvaluationContext object to null.
Q: What if the targetEntity instance is not found or is invalid?
A: If the targetEntity instance is not found or is invalid, you should set the target property of the JsonLogicEvaluationContext object to null.
Q: Do I need to populate the event property of the JsonLogicEvaluationContext object?
A: The event property is less relevant in the Action Validation context compared to System Rule evaluation. You can define a placeholder or minimal structure (e.g., type }) or determine if it's needed based on expected JSON Logic rules.
Q: How do I ensure that the JsonLogicEvaluationContext object is passed correctly to the prerequisite evaluation step?
A: Ensure that the created JsonLogicEvaluationContext object is available to be passed into the prerequisite evaluation step (the loop calling _checkSinglePrerequisite
or its replacement in a subsequent ticket).
Q: What are the dependencies required for this task?
A: This task depends on the definition of JsonLogicEvaluationContext and potentially JsonLogicEntityContext and createComponentAccessor
in Ticket 2 (Runtime Evaluator). It also requires EntityManager access.
Q: What is the estimated effort required for this task?
A: The estimate for this task is medium, as it requires understanding context assembly and interaction with EntityManager/component access.
Conclusion
Assembling the JsonLogicEvaluationContext object within the ActionValidationService is a crucial step in evaluating prerequisite rules. By addressing these common questions and concerns, we can ensure that the correct context object is created and passed into the prerequisite evaluation step. This will enable the ConditionEvaluatorService to evaluate the prerequisite JSON Logic rules correctly.