7#ifndef PXR_USD_SDF_PATH_EXPRESSION_EVAL_H
8#define PXR_USD_SDF_PATH_EXPRESSION_EVAL_H
11#include "pxr/usd/sdf/api.h"
12#include "pxr/usd/sdf/path.h"
13#include "pxr/usd/sdf/pathExpression.h"
14#include "pxr/usd/sdf/predicateExpression.h"
15#include "pxr/usd/sdf/predicateLibrary.h"
16#include "pxr/usd/sdf/predicateProgram.h"
18#include "pxr/base/arch/regex.h"
19#include "pxr/base/tf/functionRef.h"
25PXR_NAMESPACE_OPEN_SCOPE
28template <
class DomainType>
32template <
class DomainType>
38class Sdf_PathExpressionEvalBase;
43class Sdf_PathExpressionEvalBase
47 Sdf_MakePathExpressionEvalImpl(
48 Sdf_PathExpressionEvalBase &eval,
55 bool IsEmpty()
const {
60 explicit operator bool()
const {
65 class _PatternImplBase;
67 class _PatternIncrSearchState {
68 friend class _PatternImplBase;
70 SDF_API
void Pop(
int newDepth);
72 std::vector<int> _segmentMatchDepths;
73 int _constantDepth = -1;
74 bool _constantValue =
false;
77 class _PatternImplBase {
92 _Match(
SdfPath const &path, _RunNthPredFn runNthPredicate)
const;
96 _Next(_PatternIncrSearchState &searchState,
97 SdfPath const &path, _RunNthPredFn runNthPredicate)
const;
100 enum _ComponentType {
113 bool IsEmpty()
const {
return begin == end; }
114 bool StartsAt(
size_t idx)
const {
return begin == idx; }
115 bool EndsAt(
size_t idx)
const {
return end == idx; }
116 size_t GetSize()
const {
return end - begin; }
120 bool _IsBarePredicate(_Component
const &comp)
const {
123 return comp.type == ExplicitName &&
124 _explicitNames[comp.patternIndex].empty() &&
125 comp.predicateIndex != -1;
128 size_t _SegmentMinMatchElts(_Segment
const &seg)
const {
132 return seg.GetSize() - (_IsBarePredicate(
133 _components[seg.begin]) ? 1 : 0);
142 _CheckExactMatch(_Segment
const &seg,
143 _RunNthPredFn runNthPredicate,
144 SdfPathVector::const_iterator pathIterEnd,
145 SdfPathVector::const_iterator &pathIterInOut)
const;
157 _CheckMatch(_Segment
const &seg,
158 _RunNthPredFn runNthPredicate,
159 SdfPathVector::const_iterator pathIterBegin,
160 SdfPathVector::const_iterator pathIterEnd,
161 SdfPathVector::const_iterator &pathIterInOut)
const;
165 std::vector<_Component> _components;
166 std::vector<_Segment> _segments;
167 std::vector<std::string> _explicitNames;
168 std::vector<ArchRegex> _regexes;
174 _MatchPrimOrProp, _MatchPrimOnly, _MatchPropOnly
188 enum _Op { EvalPattern, Not, Open, Close, Or, And };
190 std::vector<_Op> _ops;
201template <
class DomainType>
205 class _PatternImpl :
public _PatternImplBase {
207 _PatternImpl() =
default;
213 _predicates.push_back(
214 SdfLinkPredicateExpression(predExpr, predLib));
215 return _predicates.size()-1;
217 _Init(pattern, linkPredicate);
221 template <
class PathToObject>
224 PathToObject
const &pathToObj)
const {
225 auto runNthPredicate =
226 [
this, &pathToObj](
int i,
SdfPath const &path) {
227 return _predicates[i](pathToObj(path));
229 return _Match(objPath, runNthPredicate);
233 template <
class PathToObject>
236 _PatternIncrSearchState &search,
237 PathToObject
const &pathToObj)
const {
238 auto runNthPredicate =
239 [
this, &pathToObj](
int i,
SdfPath const &path) {
240 return _predicates[i](pathToObj(path));
242 return _Next(search, objPath, runNthPredicate);
246 std::vector<SdfPredicateProgram<DomainType>> _predicates;
253 SdfMakePathExpressionEval<DomainType>(
257 bool IsEmpty()
const {
258 return _patternImpls.empty();
262 template <
class PathToObject>
265 PathToObject
const &pathToObj)
const {
269 auto patternImplIter = _patternImpls.cbegin();
270 auto evalPattern = [&](
bool skip) {
272 (*patternImplIter++).Match(objPath, pathToObj);
274 return _EvalExpr(evalPattern);
284 template <
class PathToObject>
290 PathToObject
const &p2o)
292 , _incrSearchStates(_eval->_patternImpls.size())
294 , _lastPathDepth(0) {}
299 , _incrSearchStates(_eval->_patternImpls.size())
300 , _pathToObj(std::move(p2o))
301 , _lastPathDepth(0) {}
315 auto patternImplIter = _eval->_patternImpls.begin();
316 auto stateIter = _incrSearchStates.begin();
318 const bool pop = newDepth <= _lastPathDepth;
319 auto patternStateNext = [&](
bool skip) {
321 stateIter->Pop(newDepth);
323 auto const &patternImpl = *patternImplIter++;
324 auto &state = *stateIter++;
327 : patternImpl.Next(objPath, state, _pathToObj);
329 _lastPathDepth = newDepth;
330 return _eval->_EvalExpr(patternStateNext);
341 std::vector<_PatternIncrSearchState> _incrSearchStates;
343 PathToObject _pathToObj;
350 template <
class PathToObject>
351 IncrementalSearcher<std::decay_t<PathToObject>>
354 this, std::forward<PathToObject>(pathToObj));
358 std::vector<_PatternImpl> _patternImpls;
372template <
class DomainType>
382 auto translatePattern = [&](Expr::PathPattern
const &pattern) {
385 eval._patternImpls.emplace_back(pattern, lib);
386 eval._ops.push_back(Eval::EvalPattern);
389 if (!Sdf_MakePathExpressionEvalImpl(eval, expr, translatePattern)) {
396PXR_NAMESPACE_CLOSE_SCOPE
This class implements stateful incremental search over DomainType objects in depth-first order.
void Reset()
Reset this object's incremental search state so that a new round of searching may begin.
SdfPredicateFunctionResult Next(SdfPath const &objPath)
Advance the search to the next objPath, and return the result of evaluating the expression on it.
Objects of this class evaluate complete SdfPathExpressions.
friend SdfPathExpressionEval SdfMakePathExpressionEval(SdfPathExpression const &expr, SdfPredicateLibrary< DomainType > const &lib)
Make an SdfPathExpressionEval object to evaluate expr using lib to link any embedded predicate expres...
IncrementalSearcher< std::decay_t< PathToObject > > MakeIncrementalSearcher(PathToObject &&pathToObj) const
Create an IncrementalSearcher object, using pathToObject to map DomainType instances to their paths.
SdfPredicateFunctionResult Match(SdfPath const &objPath, PathToObject const &pathToObj) const
Test objPath for a match with this expression.
Objects of this class represent a logical expression syntax tree consisting of SdfPathPattern s,...
A path value used to locate objects in layers or scenegraphs.
SDF_API size_t GetPathElementCount() const
Returns the number of path elements in this path.
Objects of this class represent SdfPath matching patterns, consisting of an SdfPath prefix followed b...
Represents a logical expression syntax tree consisting of predicate function calls joined by the logi...
Represents the result of a predicate function: a pair of the boolean result and a Constancy token ind...
static SdfPredicateFunctionResult MakeConstant(bool value)
Create with value and 'ConstantOverDescendants'.
Represents a library of predicate functions for use with SdfPredicateExpression.
This class provides a non-owning reference to a type-erased callable object with a specified signatur...