Skip to content

Commit

Permalink
Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
umeshma committed Jan 30, 2025
1 parent 63d0ece commit f2e7d49
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
46 changes: 35 additions & 11 deletions ts/packages/knowPro/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,32 +314,33 @@ export class WhereSemanticRefExpr
);
filtered.setMatches(
accumulator.getMatches((match) =>
this.matchPredicatesOr(
this.evalPredicates(
context,
accumulator.queryTermMatches,
this.predicates,
match,
),
),
);
return filtered;
}

private matchPredicatesOr(
private evalPredicates(
context: QueryEvalContext,
queryTermMatches: QueryTermAccumulator,
predicates: IQuerySemanticRefPredicate[],
match: Match<SemanticRefIndex>,
) {
for (let i = 0; i < this.predicates.length; ++i) {
for (let i = 0; i < predicates.length; ++i) {
const semanticRef = context.getSemanticRef(match.value);
if (
this.predicates[i].eval(context, queryTermMatches, semanticRef)
) {
if (predicates[i].eval(context, queryTermMatches, semanticRef)) {
return true;
}
}
return false;
}
}

export interface IQuerySemanticRefPredicate {
eval(
context: QueryEvalContext,
Expand Down Expand Up @@ -431,19 +432,28 @@ export class ActionPredicate implements IQuerySemanticRefPredicate {
}
}

export class ApplyTagScopeExpr implements IQueryOpExpr<SemanticRefAccumulator> {
constructor(public sourceExpr: IQueryOpExpr<SemanticRefAccumulator>) {}
export class ScopeExpr implements IQueryOpExpr<SemanticRefAccumulator> {
constructor(
public sourceExpr: IQueryOpExpr<SemanticRefAccumulator>,
public predicates: IQuerySemanticRefPredicate[],
) {}

public async eval(
context: QueryEvalContext,
): Promise<SemanticRefAccumulator> {
let accumulator = await this.sourceExpr.eval(context);
const tagScope = new TextRangeAccumulator();
for (const semanticRef of accumulator.getSemanticRefs(
for (const inScopeRef of accumulator.getSemanticRefs(
context.semanticRefs,
(sr) => sr.knowledgeType === "tag",
(sr) =>
this.evalPredicates(
context,
accumulator.queryTermMatches,
this.predicates,
sr,
),
)) {
tagScope.addRange(semanticRef.range);
tagScope.addRange(inScopeRef.range);
}
if (tagScope.size > 0) {
accumulator = accumulator.selectInScope(
Expand All @@ -453,6 +463,20 @@ export class ApplyTagScopeExpr implements IQueryOpExpr<SemanticRefAccumulator> {
}
return Promise.resolve(accumulator);
}

private evalPredicates(
context: QueryEvalContext,
queryTermMatches: QueryTermAccumulator,
predicates: IQuerySemanticRefPredicate[],
semanticRef: SemanticRef,
) {
for (let i = 0; i < predicates.length; ++i) {
if (predicates[i].eval(context, queryTermMatches, semanticRef)) {
return true;
}
}
return false;
}
}

function isPropertyMatch(
Expand Down
4 changes: 3 additions & 1 deletion ts/packages/knowPro/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ function createTermsMatch(
? new q.ResolveRelatedTermsExpr(queryTerms)
: queryTerms,
);
termsMatchExpr = new q.ApplyTagScopeExpr(termsMatchExpr);
termsMatchExpr = new q.ScopeExpr(termsMatchExpr, [
new q.KnowledgeTypePredicate("tag"),
]);
if (wherePredicates !== undefined && wherePredicates.length > 0) {
termsMatchExpr = new q.WhereSemanticRefExpr(
termsMatchExpr,
Expand Down

0 comments on commit f2e7d49

Please sign in to comment.