import type { Algebra } from '@comunica/utils-algebra'; import type * as RDF from '@rdfjs/types'; import type { LRUCache } from 'lru-cache'; import type { ComunicaDataFactory } from './ComunicaDataFactory'; import type { IActionContext } from './IActionContext'; export interface ITimeZoneRepresentation { zoneHours: number; zoneMinutes: number; } export interface IDateRepresentation extends Partial { year: number; month: number; day: number; } export interface ITimeRepresentation extends Partial { hours: number; minutes: number; seconds: number; } export interface IDayTimeDurationRepresentation { hours: number; minutes: number; seconds: number; day: number; } export interface IYearMonthDurationRepresentation { year: number; month: number; } export type IDurationRepresentation = IYearMonthDurationRepresentation & IDayTimeDurationRepresentation; export type IDateTimeRepresentation = IDateRepresentation & ITimeRepresentation; export type AsyncExtensionFunction = (args: RDF.Term[]) => Promise; export type AsyncExtensionFunctionCreator = (functionNamedNode: RDF.NamedNode) => Promise; /** * The aggregator of an extension aggregate for a single group. * Errors thrown by it, and errors evaluating the arguments of the aggregate call, leave the result unbound. */ export interface IExtensionAggregator { /** * Adds the evaluated arguments of the aggregate call for a solution of the group. * Solutions for which an argument uses an unbound variable are skipped. * If the aggregate is called with DISTINCT, each distinct list of arguments is only put once. * @param args the evaluated arguments. */ put: (args: RDF.Term[]) => void | Promise; /** * The result of aggregating all arguments that have been put, or undefined to leave the result unbound. */ result: () => RDF.Term | undefined | Promise; } /** * Creates the aggregator of an extension aggregate for a single group. */ export type ExtensionAggregator = () => IExtensionAggregator; /** * Returns the extension aggregate with the given IRI, or undefined if the IRI is no extension aggregate. * Unlike an `AsyncExtensionFunctionCreator`, this is synchronous, * as parsing a query requires knowing which function calls are aggregates. */ export type ExtensionAggregatorCreator = (aggregateNamedNode: RDF.NamedNode) => ExtensionAggregator | undefined; /** * Resolves a SPARQL `EXISTS` or `NOT EXISTS` expression against the given bindings. * * The resolver receives the expression as it appears in the algebra, so it is responsible for both * materializing `expression.input` against the bindings (see `materializeOperation` in * `@comunica/utils-query-operation`) and for applying `expression.not`. * Throw an `ExpressionError` to have the failure treated as a SPARQL error, for example so that * `FILTER` drops the bindings instead of failing the query. */ export type ExistenceResolver = (expression: Algebra.ExistenceExpression, mapping: RDF.Bindings) => Promise; /** * The key 'term' is not included in these keys. Something that is just a term will map to number 0. */ export type GeneralSuperTypeDict = Record & { __depth: number; }; export type TypeCache = LRUCache; export type SuperTypeCallback = (unknownType: string) => string; export interface ISuperTypeProvider { cache: TypeCache; discoverer: SuperTypeCallback; } /** * An evaluator for RDF expressions. */ export interface IExpressionEvaluator extends IInternalEvaluator { /** * Evaluates the provided bindings in terms of the context the evaluator was created. * @param mapping the RDF bindings to evaluate against. */ evaluate: (mapping: RDF.Bindings) => Promise; /** * Evaluates the provided bindings in terms of the context the evaluator was created, * returning the effective boolean value. * @param mapping the RDF bindings to evaluate against. */ evaluateAsEBV: (mapping: RDF.Bindings) => Promise; evaluateAsEvaluatorExpression: (mapping: RDF.Bindings) => Promise; } /** * Orders RDF terms. */ export interface ITermComparator { /** * Orders two RDF terms according to: https://www.w3.org/TR/sparql11-query/#modOrderBy * @param termA the first term * @param termB the second term */ orderTypes: (termA: RDF.Term | undefined, termB: RDF.Term | undefined) => -1 | 0 | 1; } /** * Instances of this interface perform a specific aggregation of bindings. * You can put bindings and when all bindings have been put, request the result. */ export interface IBindingsAggregator { /** * Registers bindings to the aggregator. Each binding you put has the ability to change the aggregation result. * @param bindings the bindings to put. */ putBindings: (bindings: RDF.Bindings) => Promise; /** * Request the result term of aggregating the bindings you have put in the aggregator. */ result: () => Promise; } export interface IInternalEvaluator { evaluatorExpressionEvaluation: (expr: Expression, mapping: RDF.Bindings) => Promise; /** * Evaluate the given expression synchronously, which is possible for term or variable expressions. * @return The evaluated term, or undefined if the expression must be evaluated asynchronously. */ evaluateDirectly?: (expr: Expression, mapping: RDF.Bindings) => TermExpression | undefined; context: IActionContext; } export declare enum ExpressionType { Aggregate = "aggregate", Existence = "existence", Operator = "operator", Term = "term", Variable = "variable" } export type TermType = 'namedNode' | 'literal' | 'blankNode' | 'quad' | 'defaultGraph'; export type TermExpression = IExpressionProps & { expressionType: ExpressionType.Term; termType: TermType; str: () => string; coerceEBV: () => boolean; toRDF: (dataFactory: ComunicaDataFactory) => RDF.Term; }; export type Expression = AggregateExpression | ExistenceExpression | OperatorExpression | TermExpression | VariableExpression; export interface IExpressionProps { expressionType: ExpressionType; } export type AggregateExpression = IExpressionProps & { expressionType: ExpressionType.Aggregate; name: string; expression: Algebra.AggregateExpression; }; export type ExistenceExpression = IExpressionProps & { expressionType: ExpressionType.Existence; expression: Algebra.ExistenceExpression; }; export type OperatorExpression = IExpressionProps & { expressionType: ExpressionType.Operator; name: string; args: Expression[]; apply: FunctionApplication; }; export type VariableExpression = IExpressionProps & { expressionType: ExpressionType.Variable; name: string; }; export type SimpleApplication = (args: TermExpression[]) => TermExpression; export type SimpleApplicationTuple = (args: T) => TermExpression; export type FunctionApplication = (evalContext: IEvalContext) => Promise; export type ImplementationFunction = (expressionEvaluator: IInternalEvaluator) => SimpleApplication; export type ImplementationFunctionTuple = (expressionEvaluator: IInternalEvaluator) => SimpleApplicationTuple; export interface IFunctionArgumentsCacheObj { func?: ImplementationFunction; cache?: FunctionArgumentsCache; } export type FunctionArgumentsCache = Record; export interface IEvalContext { args: Expression[]; mapping: RDF.Bindings; exprEval: IInternalEvaluator; }