Skip to content

Commit

Permalink
chore: put evaluation in the apply call
Browse files Browse the repository at this point in the history
  • Loading branch information
jitsedesmet committed Oct 23, 2023
1 parent 262692e commit a5a9cfd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class AverageAggregator extends AggregateEvaluator implements IBindingsAg
this.state = { sum, count: 1 };
} else {
const internalTerm = this.termToNumericOrError(term);
this.state.sum = <E.NumericLiteral> this.summer.syncApply([ this.state.sum, internalTerm ],
this.state.sum = <E.NumericLiteral> this.summer.applyOnTerms([ this.state.sum, internalTerm ],
<ExpressionEvaluator> this.evaluator);
this.state.count++;
}
Expand All @@ -44,7 +44,7 @@ export class AverageAggregator extends AggregateEvaluator implements IBindingsAg
return this.emptyValue();
}
const count = new E.IntegerLiteral(this.state.count);
const result = this.divider.syncApply([ this.state.sum, count ], <ExpressionEvaluator> this.evaluator);
const result = this.divider.applyOnTerms([ this.state.sum, count ], <ExpressionEvaluator> this.evaluator);
return result.toRDF();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SumAggregator extends AggregateEvaluator {
this.state = this.termToNumericOrError(term);
} else {
const internalTerm = this.termToNumericOrError(term);
this.state = <E.NumericLiteral> this.summer.syncApply([ this.state, internalTerm ],
this.state = <E.NumericLiteral> this.summer.applyOnTerms([ this.state, internalTerm ],
<ExpressionEvaluator> this.evaluator);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ export class ExpressionEvaluator implements IExpressionEvaluator {
const myLitB = termTransformer.transformLiteral(litB);

try {
if ((<E.BooleanLiteral> isEqual.syncApply([ myLitA, myLitB ], this)).typedValue) {
if ((<E.BooleanLiteral> isEqual.applyOnTerms([ myLitA, myLitB ], this)).typedValue) {
return 0;
}
if ((<E.BooleanLiteral> isGreater.syncApply([ myLitA, myLitB ], this)).typedValue) {
if ((<E.BooleanLiteral> isGreater.applyOnTerms([ myLitA, myLitB ], this)).typedValue) {
return 1;
}
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class AsyncRecursiveEvaluator {

private async evalOperator(expr: E.Operator, mapping: RDF.Bindings): Promise<E.Term> {
const context: IEvalContext = {
args: await Promise.all(expr.args.map(arg => this.evaluate(arg, mapping))),
args: expr.args,
mapping,
exprEval: this.expressionEvaluator,

Expand All @@ -90,15 +90,10 @@ export class AsyncRecursiveEvaluator {
return expr.apply(context);
}

private async _evalAsyncArgs(args: E.Expression[], mapping: RDF.Bindings): Promise<E.TermExpression[]> {
const argPromises = args.map(arg => this.evaluate(arg, mapping));
return await Promise.all(argPromises);
}

private async evalNamed(expr: E.Named, mapping: RDF.Bindings): Promise<E.Term> {
return expr.apply(
{
args: await this._evalAsyncArgs(expr.args, mapping),
args: expr.args,
mapping,
exprEval: this.expressionEvaluator,

Expand All @@ -109,7 +104,7 @@ export class AsyncRecursiveEvaluator {

private async evalAsyncExtension(expr: AsyncExtension, mapping: RDF.Bindings): Promise<E.Term> {
return await expr.apply({
args: await this._evalAsyncArgs(expr.args, mapping),
args: expr.args,
mapping,
exprEval: this.expressionEvaluator,

Expand Down
9 changes: 6 additions & 3 deletions packages/expression-evaluator/lib/functions/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ export abstract class BaseFunction<Operator> implements IFunctionExpression {
* instance depending on the runtime types. We then just apply this function
* to the args.
*/
public syncApply(args: E.TermExpression[], exprEval: ExpressionEvaluator): E.TermExpression {
public applyOnTerms(args: E.TermExpression[], exprEval: ExpressionEvaluator): E.TermExpression {
const concreteFunction =
this.monomorph(args, exprEval.context.superTypeProvider, exprEval.context.functionArgumentsCache) ||
this.handleInvalidTypes(args);
return concreteFunction(exprEval)(args);
}

public async apply({ args, exprEval }: IEvalContext): Promise<E.TermExpression> {
return this.syncApply(<E.TermExpression[]> args, exprEval);
public async apply({ args, exprEval, mapping }: IEvalContext): Promise<E.TermExpression> {
return this.applyOnTerms(
await Promise.all(args.map(arg => exprEval.evaluator.evaluate(arg, mapping))),
exprEval,
);
}

protected abstract handleInvalidTypes(args: E.TermExpression[]): never;
Expand Down
18 changes: 9 additions & 9 deletions packages/expression-evaluator/lib/functions/RegularFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ const equality = {
exprEval => ([ left, right ]) => {
const op: RegularFunction = new RegularFunction(RegularOperator.EQUAL, equality);
return bool(
(<E.BooleanLiteral> op.syncApply([ (<Quad> left).subject, (<Quad> right).subject ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.syncApply([ (<Quad> left).predicate, (<Quad> right).predicate ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.syncApply([ (<Quad> left).object, (<Quad> right).object ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.syncApply([ (<Quad> left).graph, (<Quad> right).graph ], exprEval)).coerceEBV(),
(<E.BooleanLiteral> op.applyOnTerms([ (<Quad> left).subject, (<Quad> right).subject ], exprEval)).coerceEBV() &&

Check failure on line 201 in packages/expression-evaluator/lib/functions/RegularFunctions.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 122. Maximum allowed is 120
(<E.BooleanLiteral> op.applyOnTerms([ (<Quad> left).predicate, (<Quad> right).predicate ], exprEval)).coerceEBV() &&

Check failure on line 202 in packages/expression-evaluator/lib/functions/RegularFunctions.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 126. Maximum allowed is 120
(<E.BooleanLiteral> op.applyOnTerms([ (<Quad> left).object, (<Quad> right).object ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.applyOnTerms([ (<Quad> left).graph, (<Quad> right).graph ], exprEval)).coerceEBV(),
);
},
false,
Expand Down Expand Up @@ -243,7 +243,7 @@ const inequality = {
.set([ 'term', 'term' ], expressionEvaluator =>
([ first, second ]) =>
bool(!(<E.BooleanLiteral> regularFunctions[C.RegularOperator.EQUAL]
.syncApply([ first, second ], expressionEvaluator)).typedValue))
.applyOnTerms([ first, second ], expressionEvaluator)).typedValue))
.collect(),
};

Expand Down Expand Up @@ -290,7 +290,7 @@ const greaterThan = {
.set([ 'term', 'term' ], expressionEvaluator =>
([ first, second ]) =>
// X < Y -> Y > X
regularFunctions[C.RegularOperator.LT].syncApply([ second, first ], expressionEvaluator))
regularFunctions[C.RegularOperator.LT].applyOnTerms([ second, first ], expressionEvaluator))
.collect(),
};

Expand All @@ -303,8 +303,8 @@ const lesserThanEqual = {
// First check if the first is lesser than the second, then check if they are equal.
// Doing this, the correct error will be thrown, each type that has a lesserThanEqual has a matching lesserThan.
bool(
(<E.BooleanLiteral> regularFunctions[C.RegularOperator.LT].syncApply([ first, second ], exprEval)).typedValue ||
(<E.BooleanLiteral> regularFunctions[C.RegularOperator.EQUAL].syncApply([ first, second ], exprEval)).typedValue,
(<E.BooleanLiteral> regularFunctions[C.RegularOperator.LT].applyOnTerms([ first, second ], exprEval)).typedValue ||

Check failure on line 306 in packages/expression-evaluator/lib/functions/RegularFunctions.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 125. Maximum allowed is 120
(<E.BooleanLiteral> regularFunctions[C.RegularOperator.EQUAL].applyOnTerms([ first, second ], exprEval)).typedValue,

Check failure on line 307 in packages/expression-evaluator/lib/functions/RegularFunctions.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 126. Maximum allowed is 120
))
.collect(),
};
Expand All @@ -315,7 +315,7 @@ const greaterThanEqual = {
.set([ 'term', 'term' ], exprEval =>
([ first, second ]) =>
// X >= Y -> Y <= X
regularFunctions[C.RegularOperator.LTE].syncApply([ second, first ], exprEval))
regularFunctions[C.RegularOperator.LTE].applyOnTerms([ second, first ], exprEval))
.collect(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async function inRecursiveAsync(
const nextExpression = args.shift()!;
const next = await exprEval.evaluator.evaluate(nextExpression, mapping);
const isEqual = regularFunctions[C.RegularOperator.EQUAL];
if ((<E.BooleanLiteral> isEqual.syncApply([ needle, next ], exprEval)).typedValue) {
if ((<E.BooleanLiteral> isEqual.applyOnTerms([ needle, next ], exprEval)).typedValue) {
return bool(true);
}
return inRecursiveAsync(needle, context, [ ...results, false ]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ describe('OverloadTree', () => {
const one = new IntegerLiteral(1);
const two = new IntegerLiteral(2);
expect(functionArgumentsCache['+']).toBeUndefined();
const res = regularFunctions['+'].syncApply([ one, two ], expressionEvaluator);
const res = regularFunctions['+'].applyOnTerms([ one, two ], expressionEvaluator);
expect(res.str()).toEqual('3');
// One time lookup + one time add
expect(functionArgumentsCache['+']).not.toBeUndefined();
regularFunctions['+'].syncApply([ two, one ], expressionEvaluator);
regularFunctions['+'].applyOnTerms([ two, one ], expressionEvaluator);

const innerSpy = jest.fn();
const spy = jest.fn(() => innerSpy);
functionArgumentsCache['+']!.cache![TypeURL.XSD_INTEGER].cache![TypeURL.XSD_INTEGER]!.func = spy;
regularFunctions['+'].syncApply([ one, two ], expressionEvaluator);
regularFunctions['+'].applyOnTerms([ one, two ], expressionEvaluator);
expect(spy).toHaveBeenCalled();
expect(innerSpy).toHaveBeenCalled();
});
Expand All @@ -117,15 +117,15 @@ describe('OverloadTree', () => {
const one = new IntegerLiteral(1);
const two = new IntegerLiteral(2);
expect(functionArgumentsCache.substr).toBeUndefined();
expect(regularFunctions.substr.syncApply([ apple, one, two ], expressionEvaluator).str()).toBe('ap');
expect(regularFunctions.substr.applyOnTerms([ apple, one, two ], expressionEvaluator).str()).toBe('ap');

expect(functionArgumentsCache.substr).toBeDefined();
const interestCache = functionArgumentsCache.substr
.cache![TypeURL.XSD_STRING].cache![TypeURL.XSD_INTEGER];
expect(interestCache.func).toBeUndefined();
expect(interestCache.cache![TypeURL.XSD_INTEGER]).toBeDefined();

expect(regularFunctions.substr.syncApply([ apple, one ], expressionEvaluator).str()).toBe(String('apple'));
expect(regularFunctions.substr.applyOnTerms([ apple, one ], expressionEvaluator).str()).toBe(String('apple'));
const interestCacheNew = functionArgumentsCache.substr
.cache![TypeURL.XSD_STRING].cache![TypeURL.XSD_INTEGER];
expect(interestCacheNew).toBeDefined();
Expand Down

0 comments on commit a5a9cfd

Please sign in to comment.