context.h
Go to the documentation of this file.
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_VALIDATION_USD_VALIDATION_CONTEXT_H
8 #define PXR_USD_VALIDATION_USD_VALIDATION_CONTEXT_H
9 
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/plug/plugin.h"
15 #include "pxr/base/tf/token.h"
16 #include "pxr/usd/usd/primRange.h"
17 #include "pxr/usdValidation/usdValidation/api.h"
18 #include "pxr/usdValidation/usdValidation/error.h"
21 
22 #include <mutex>
23 
24 PXR_NAMESPACE_OPEN_SCOPE
25 
26 class TfType;
27 class WorkDispatcher;
28 class Usd_PrimFlagsPredicate;
29 
76 {
77 public:
91  USDVALIDATION_API
92  explicit UsdValidationContext(const TfTokenVector &keywords,
93  bool includeAllAncestors = true);
94 
108  USDVALIDATION_API
109  explicit UsdValidationContext(const PlugPluginPtrVector &plugins,
110  bool includeAllAncestors = true);
111 
125  USDVALIDATION_API
126  explicit UsdValidationContext(
127  const UsdValidationValidatorMetadataVector &metadata,
128  bool includeAllAncestors = true);
129 
140  USDVALIDATION_API
141  explicit UsdValidationContext(const std::vector<TfType> &schemaTypes);
142 
148  USDVALIDATION_API
149  explicit UsdValidationContext(
150  const std::vector<const UsdValidationValidator *> &validators);
151 
157  USDVALIDATION_API
158  explicit UsdValidationContext(
159  const std::vector<const UsdValidationValidatorSuite *> &suites);
160 
177  USDVALIDATION_API
178  UsdValidationErrorVector Validate(const SdfLayerHandle &layer) const;
179 
201  USDVALIDATION_API
202  UsdValidationErrorVector Validate(
203  const UsdStagePtr &stage,
204  const Usd_PrimFlagsPredicate &predicate,
205  const UsdValidationTimeRange &timeRange) const;
206 
216  USDVALIDATION_API
217  UsdValidationErrorVector Validate(
218  const UsdStagePtr &stage,
219  const Usd_PrimFlagsPredicate &predicate) const;
220 
233  USDVALIDATION_API
234  UsdValidationErrorVector Validate(const UsdStagePtr &stage) const;
235 
247  USDVALIDATION_API
248  UsdValidationErrorVector Validate(
249  const UsdStagePtr &stage,
250  const UsdValidationTimeRange &timeRange) const;
251 
263  USDVALIDATION_API
264  UsdValidationErrorVector Validate(
265  const UsdStagePtr &stage,
266  const Usd_PrimFlagsPredicate &predicate,
267  const std::vector<UsdTimeCode> &timeCodes) const;
268 
283  USDVALIDATION_API
284  UsdValidationErrorVector Validate(
285  const UsdStagePtr &stage,
286  const std::vector<UsdTimeCode> &timeCodes) const;
287 
304  USDVALIDATION_API
305  UsdValidationErrorVector Validate(
306  const std::vector<UsdPrim> &prims,
307  const UsdValidationTimeRange &timeRange = {}) const;
308 
325  USDVALIDATION_API
326  UsdValidationErrorVector Validate(
327  const UsdPrimRange &prims,
328  const UsdValidationTimeRange &timeRange = {}) const;
329 
342  USDVALIDATION_API
343  UsdValidationErrorVector Validate(
344  const std::vector<UsdPrim> &prims,
345  const std::vector<UsdTimeCode> &timeCodes) const;
346 
359  USDVALIDATION_API
360  UsdValidationErrorVector Validate(
361  const UsdPrimRange &prims,
362  const std::vector<UsdTimeCode> &timeCodes) const;
363 
364 private:
365  // helper to initialize UsdValidationContext, given a vector of metadata
366  // and a flag to include all ancestors.
367  void _InitializeFromValidatorMetadata(
368  const UsdValidationValidatorMetadataVector &metadata,
369  bool includeAllAncestors);
370 
371  // Distribute the validators into different groups based on the type of
372  // validation to be performed. This method distributes the selected
373  // validators into _layerValidators, _stageValidators, _primValidators and
374  // _schemaTypeValidators.
375  void _DistributeValidators(
376  const std::vector<const UsdValidationValidator *> &validators);
377 
378  // Helper enum to specify the state of time dependency for validation.
379  // This is used to determine if context need to run just time dependent,
380  // just non-time dependent or all validators.
381  enum class _TimeDependencyState {
382  DoTimeDependent,
383  DoNonTimeDependent,
384  All
385  };
386 
387  // Private helper functions to validate layers, stages and prims.
388  void _ValidateLayer(WorkDispatcher &dispatcher, const SdfLayerHandle &layer,
389  UsdValidationErrorVector *errors,
390  std::mutex *errorsMutex) const;
391 
392  // Helper function to validate a stage.
393  // - Layer Validators for all used layers in the stage
394  // - Stage Validators for the stage
395  // - Prim Validators for all prims in the stage, using the predicate
396  // to traverse the prims on the stage.
397  //
398  // Depending on whether an explicit timeRange is provided or a vector
399  // of timeCodes are provided, this function will do the following:
400  // - if timeRange is provided, it uses that to validate the stage and
401  // its prims using the predicate.
402  // - if timeCodes are provided:
403  // - Run all non-time dependent validators once.
404  // - Run all time dependent validators for all timeCodes.
405  void _ValidateStage(WorkDispatcher &dispatcher,
406  const UsdStagePtr &stage,
407  UsdValidationErrorVector *errors,
408  std::mutex *errorsMutex,
409  const Usd_PrimFlagsPredicate &predicate,
410  const std::variant<UsdValidationTimeRange,
411  std::vector<UsdTimeCode>> &times) const;
412 
413  // Helper function to validate prims. Generalized for UsdPrimRange and
414  // vector of UsdPrims.
415  template <typename T>
416  void _ValidatePrims(
417  WorkDispatcher &dispatcher, const T &prims,
418  UsdValidationErrorVector *errors, std::mutex *errorsMutex,
419  UsdValidationTimeRange timeRange,
420  _TimeDependencyState timeDependencyState =
421  _TimeDependencyState::All) const;
422 
423  // Helper function to run _ValidatePrims within a scoped dispatcher.
424  // Templated to handle both PrimRange and vector of UsdPrims.
425  // Depending on whether an explicit timeRange is provided or a vector
426  // of timeCodes are provided, this function will do the following:
427  // - if timeRange is provided, it uses that to validate the prims.
428  // - if timeCodes are provided:
429  // - Run all non-time dependent validators once.
430  // - Run all time dependent validators for all timeCodes.
431  template <typename T>
432  void _RunValidatePrims(
433  const T &prims, UsdValidationErrorVector *errors,
434  std::mutex *errorsMutex,
435  const std::variant<UsdValidationTimeRange,
436  std::vector<UsdTimeCode>> &times) const;
437 
438  // Validators catering to a specific schemaType
439  using _SchemaTypeValidatorPair
440  = std::pair<TfToken, std::vector<const UsdValidationValidator *>>;
441  using _SchemaTypeValidatorPairVector
442  = std::vector<_SchemaTypeValidatorPair>;
443 
444  // Vectors of selected sets of validators, which will be run for this
445  // UsdValidationContext. Validation tasks will be enqueued for each of these
446  // validators on a given layer / stage or prims (traversed or explicitly
447  // specified).
448  std::vector<const UsdValidationValidator *> _layerValidators;
449  std::vector<const UsdValidationValidator *> _stageValidators;
450  std::vector<const UsdValidationValidator *> _primValidators;
451 
452  // validators here will be used to validate prims based on their schema
453  // types, such that:
454  // - For every typed schemaType found in here, prim being validated will be
455  // checked if it satisfies the IsA<schemaType> and validation task will be
456  // enqueued.
457  // - For every applied schemaType found in here, prim's appliedAPISchemas
458  // will be checked and if found, validation task will be enqueued for the
459  // prim.
460  _SchemaTypeValidatorPairVector _schemaTypeValidators;
461 };
462 
463 PXR_NAMESPACE_CLOSE_SCOPE
464 
465 #endif // PXR_USD_VALIDATION_USD_VALIDATION_CONTEXT_H
A class which describes the interval at which validation is to be performed.
Definition: timeRange.h:37
Definitions of basic string utilities in tf.
USDVALIDATION_API UsdValidationContext(const TfTokenVector &keywords, bool includeAllAncestors=true)
Create a UsdValidationContext by collecting validators using the specified keywords.
USDVALIDATION_API UsdValidationErrorVector Validate(const SdfLayerHandle &layer) const
Run validation on the given valid layer by executing the selected validators for this UsdValidationCo...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:489
An forward-iterable range that traverses a subtree of prims rooted at a given prim in depth-first ord...
Definition: primRange.h:101
TfType represents a dynamic runtime type.
Definition: type.h:47
UsdValidationContext provides an interface for managing and running validators on USD layers,...
Definition: context.h:75
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...