@incremunica/bus-bindings-aggregator-factory
Version:
An Incremunica bus for bindings aggregator factories.
125 lines • 4.88 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AggregateEvaluator = void 0;
const context_entries_1 = require("@comunica/context-entries");
const Eval = __importStar(require("@comunica/utils-expression-evaluator"));
const context_entries_2 = require("@incremunica/context-entries");
/**
* This is the base class for all aggregators.
* NOTE: The wildcard count aggregator significantly differs from the others and overloads parts of this class.
*/
class AggregateEvaluator {
constructor(evaluator, distinct, throwError = false) {
this.evaluator = evaluator;
this.distinct = distinct;
this.throwError = throwError;
this.errorOccurred = false;
this.lastResult = undefined;
this.errorOccurred = false;
this.superTypeProvider = evaluator.context.getSafe(context_entries_1.KeysExpressionEvaluator.superTypeProvider);
this.termTransformer = new Eval.TermTransformer(this.superTypeProvider);
}
emptyValueTerm() {
return undefined;
}
/**
* The spec says to throw an error when a set function is called on an empty
* set (unless explicitly mentioned otherwise like COUNT).
* However, aggregate error handling says to not bind the result in case of an
* error. So to simplify logic in the caller, we return undefined by default.
*/
emptyValue() {
const val = this.emptyValueTerm();
if (val === undefined && this.throwError) {
throw new Eval.EmptyAggregateError();
}
return val;
}
/**
* Base implementation of putBindings, that evaluates to a term and then calls putTerm.
* The WildcardCountAggregator will completely discard this implementation.
* @param bindings
*/
async putBindings(bindings) {
if (this.errorOccurred) {
return;
}
try {
const term = await this.evaluator.evaluate(bindings);
if (!term || this.errorOccurred) {
return;
}
if (bindings.getContextEntry(context_entries_2.KeysBindings.isAddition) ?? true) {
this.putTerm(term);
}
else {
this.removeTerm(term);
}
}
catch (error) {
this.safeThrow(error);
}
}
/**
* Base implementation of result, that calls termResult and caches the result.
* @returns {RDF.Term | undefined | null}
* The result term, or undefined if no new result is available, or null if an error occurred.
*/
result() {
if (this.errorOccurred) {
if (this.lastResult === null) {
return undefined;
}
this.lastResult = null;
return null;
}
const result = this.termResult();
if ((result === undefined && this.lastResult === undefined) || result?.equals(this.lastResult)) {
return undefined;
}
this.lastResult = result;
return result;
}
safeThrow(err) {
if (this.throwError) {
throw err;
}
else {
this.errorOccurred = true;
}
}
termToNumericOrError(term) {
if (term.termType !== 'Literal') {
throw new Error(`Term with value ${term.value} has type ${term.termType} and is not a numeric literal`);
}
else if (!Eval.isSubTypeOf(term.datatype.value, Eval.TypeAlias.SPARQL_NUMERIC, this.superTypeProvider)) {
throw new Error(`Term datatype ${term.datatype.value} with value ${term.value} has type ${term.termType} and is not a numeric literal`);
}
return this.termTransformer.transformLiteral(term);
}
}
exports.AggregateEvaluator = AggregateEvaluator;
//# sourceMappingURL=AggregateEvaluator.js.map