Skip to content

Commit

Permalink
chore: redo my work & just give the apply async option to make it the…
Browse files Browse the repository at this point in the history
… default later
  • Loading branch information
jitsedesmet committed Oct 23, 2023
1 parent 64a487a commit 8a5aac9
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 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.apply([ this.state.sum, internalTerm ],
this.state.sum = <E.NumericLiteral> this.summer.syncApply([ 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.apply([ this.state.sum, count ], <ExpressionEvaluator> this.evaluator);
const result = this.divider.syncApply([ 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.apply([ this.state, internalTerm ],
this.state = <E.NumericLiteral> this.summer.syncApply([ 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.apply([ myLitA, myLitB ], this)).typedValue) {
if ((<E.BooleanLiteral> isEqual.syncApply([ myLitA, myLitB ], this)).typedValue) {
return 0;
}
if ((<E.BooleanLiteral> isGreater.apply([ myLitA, myLitB ], this)).typedValue) {
if ((<E.BooleanLiteral> isGreater.syncApply([ myLitA, myLitB ], this)).typedValue) {
return 1;
}
return -1;
Expand Down
15 changes: 8 additions & 7 deletions packages/expression-evaluator/lib/functions/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import * as Err from '../util/Errors';
import type { ISuperTypeProvider } from '../util/TypeHandling';
import type { FunctionArgumentsCache, ImplementationFunction, OverloadTree } from './OverloadTree';

export interface IEvalSharedContext extends ICompleteEEContext{
export interface IEvalContext extends ICompleteEEContext {
args: E.Expression[];
mapping: RDF.Bindings;
}
export interface IEvalContext<Term> extends IEvalSharedContext {
evaluate: ExpressionEvaluator;
}

export type EvalContextAsync = IEvalContext<Promise<E.TermExpression>>;
export type EvalContextAsync = IEvalContext;

// ----------------------------------------------------------------------------
// Overloaded Functions
Expand Down Expand Up @@ -44,13 +42,16 @@ export abstract class BaseFunction<Operator> {
* instance depending on the runtime types. We then just apply this function
* to the args.
*/
public apply = (args: E.TermExpression[], exprEval: ExpressionEvaluator):
E.TermExpression => {
public syncApply(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: E.TermExpression[], exprEval: ExpressionEvaluator): Promise<E.TermExpression> {
return this.syncApply(args, 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.apply([ (<Quad> left).subject, (<Quad> right).subject ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.apply([ (<Quad> left).predicate, (<Quad> right).predicate ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.apply([ (<Quad> left).object, (<Quad> right).object ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.apply([ (<Quad> left).graph, (<Quad> right).graph ], exprEval)).coerceEBV(),
(<E.BooleanLiteral> op.syncApply([ (<Quad> left).subject, (<Quad> right).subject ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.syncApply([ (<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 123. Maximum allowed is 120
(<E.BooleanLiteral> op.syncApply([ (<Quad> left).object, (<Quad> right).object ], exprEval)).coerceEBV() &&
(<E.BooleanLiteral> op.syncApply([ (<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]
.apply([ first, second ], expressionEvaluator)).typedValue))
.syncApply([ 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].apply([ second, first ], expressionEvaluator))
regularFunctions[C.RegularOperator.LT].syncApply([ 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].apply([ first, second ], exprEval)).typedValue ||
(<E.BooleanLiteral> regularFunctions[C.RegularOperator.EQUAL].apply([ first, second ], exprEval)).typedValue,
(<E.BooleanLiteral> regularFunctions[C.RegularOperator.LT].syncApply([ 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 122. Maximum allowed is 120
(<E.BooleanLiteral> regularFunctions[C.RegularOperator.EQUAL].syncApply([ 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 123. 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].apply([ second, first ], exprEval))
regularFunctions[C.RegularOperator.LTE].syncApply([ second, first ], exprEval))
.collect(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ async function inRecursiveAsync(
// We know this will not be undefined because we check args.length === 0
const next = await evaluate.evaluator.evaluate(nextExpression!, mapping);
const isEqual = regularFunctions[C.RegularOperator.EQUAL];
if ((<E.BooleanLiteral> isEqual.apply([ needle, next ], evaluate)).typedValue) {
if ((<E.BooleanLiteral> isEqual.syncApply([ needle, next ], evaluate)).typedValue) {
return bool(true);
}
return inRecursiveAsync(needle, context, [ ...results, false ]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class AlgebraTransformer extends TermTransformer implements IAlgebraTrans
if (!AlgebraTransformer.hasCorrectArity(regularArgs, regularFunc.arity)) {
throw new Err.InvalidArity(regularArgs, regularOp);
}
return new E.Operator(regularArgs, args => regularFunc.apply(args, this.expressionEvaluator));
return new E.Operator(regularArgs, args => regularFunc.syncApply(args, this.expressionEvaluator));
}

private wrapAsyncFunction(func: AsyncExtensionFunction, name: string): AsyncExtensionApplication {
Expand All @@ -98,7 +98,7 @@ export class AlgebraTransformer extends TermTransformer implements IAlgebraTrans
// Return a basic named expression
const op = <C.NamedOperator>expr.name.value;
const namedFunc = namedFunctions[op];
return new E.Named(expr.name, namedArgs, args => namedFunc.apply(args, this.expressionEvaluator));
return new E.Named(expr.name, namedArgs, args => namedFunc.syncApply(args, this.expressionEvaluator));
}
// The expression might be an extension function, check this.
const asyncExtensionFunc = this.creatorConfig.creator(expr.name);
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['+'].apply([ one, two ], expressionEvaluator);
const res = regularFunctions['+'].syncApply([ one, two ], expressionEvaluator);
expect(res.str()).toEqual('3');
// One time lookup + one time add
expect(functionArgumentsCache['+']).not.toBeUndefined();
regularFunctions['+'].apply([ two, one ], expressionEvaluator);
regularFunctions['+'].syncApply([ two, one ], expressionEvaluator);

const innerSpy = jest.fn();
const spy = jest.fn(() => innerSpy);
functionArgumentsCache['+']!.cache![TypeURL.XSD_INTEGER].cache![TypeURL.XSD_INTEGER]!.func = spy;
regularFunctions['+'].apply([ one, two ], expressionEvaluator);
regularFunctions['+'].syncApply([ 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.apply([ apple, one, two ], expressionEvaluator).str()).toBe('ap');
expect(regularFunctions.substr.syncApply([ 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.apply([ apple, one ], expressionEvaluator).str()).toBe(String('apple'));
expect(regularFunctions.substr.syncApply([ 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 8a5aac9

Please sign in to comment.