How to use sparqlee - 10 common examples

To help you get started, we’ve selected a few sparqlee examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github comunica / comunica / packages / actor-query-operation-orderby-sparqlee / lib / ActorQueryOperationOrderBySparqlee.ts View on Github external
public async runOperation(pattern: Algebra.OrderBy, context: ActionContext)
    : Promise {

    const outputRaw = await this.mediatorQueryOperation.mediate({ operation: pattern.input, context });
    const output = ActorQueryOperation.getSafeBindings(outputRaw);

    const options = { window: this.window };
    const sparqleeConfig = { ...ActorQueryOperation.getExpressionContext(context) };
    let bindingsStream = output.bindingsStream;

    for (let expr of pattern.expressions) {
      const isAscending = this.isAscending(expr);
      expr = this.extractSortExpression(expr);

      // Transform the stream by annotating it with the expr result
      const evaluator = new AsyncEvaluator(expr, sparqleeConfig);
      interface IAnnotatedBinding { bindings: Bindings; result: Term; }
      const transform = async (bindings: Bindings, next: any) => {
        try {
          const result = await evaluator.evaluate(bindings);
          transformedStream._push({ bindings, result });
        } catch (err) {
          if (!isExpressionError(err)) {
            bindingsStream.emit('error', err);
          }
          transformedStream._push({ bindings, result: undefined });
        }
        next();
      };
      const transformedStream = bindingsStream.transform({ transform });

      // Sort the annoted stream
github comunica / comunica / packages / actor-query-operation-extend / lib / ActorQueryOperationExtend.ts View on Github external
const transform = async (bindings: Bindings, next: any) => {
      try {
        const result = await evaluator.evaluate(bindings);
        // Extend operation is undefined when the key already exists
        // We just override it here.
        const extended = bindings.set(extendKey, result);
        bindingsStream._push(extended);
      } catch (err) {
        if (isExpressionError(err)) {
          // Errors silently don't actually extend according to the spec
          bindingsStream._push(bindings);
          // But let's warn anyway
          this.logWarn(context, `Expression error for extend operation with bindings '${JSON.stringify(bindings)}'`);
        } else {
          bindingsStream.emit('error', err);
        }
      }
      next();
    };
github comunica / comunica / packages / actor-query-operation-filter-sparqlee / lib / ActorQueryOperationFilterSparqlee.ts View on Github external
public async testOperation(pattern: Algebra.Filter, context: ActionContext): Promise {
    // Will throw error for unsupported operators
    const config = { exists: this.createExistenceResolver(context) };
    const _ = new AsyncEvaluator(pattern.expression, config);
    return true;
  }
github comunica / comunica / packages / actor-query-operation-extend / lib / ActorQueryOperationExtend.ts View on Github external
public async runOperation(pattern: Algebra.Extend, context: ActionContext)
    : Promise {

    const { expression, input, variable } = pattern;

    const output: IActorQueryOperationOutputBindings = ActorQueryOperation.getSafeBindings(
      await this.mediatorQueryOperation.mediate({ operation: input, context }));

    const extendKey = termToString(variable);
    const config = { ...ActorQueryOperation.getExpressionContext(context) };
    const evaluator = new AsyncEvaluator(expression, config);

    // Transform the stream by extending each Bindings with the expression result
    const transform = async (bindings: Bindings, next: any) => {
      try {
        const result = await evaluator.evaluate(bindings);
        // Extend operation is undefined when the key already exists
        // We just override it here.
        const extended = bindings.set(extendKey, result);
        bindingsStream._push(extended);
      } catch (err) {
        if (isExpressionError(err)) {
          // Errors silently don't actually extend according to the spec
          bindingsStream._push(bindings);
          // But let's warn anyway
          this.logWarn(context, `Expression error for extend operation with bindings '${JSON.stringify(bindings)}'`);
        } else {
github comunica / comunica / packages / actor-query-operation-extend / lib / ActorQueryOperationExtend.ts View on Github external
public async testOperation(pattern: Algebra.Extend, context: ActionContext): Promise {
    // Will throw error for unsupported opperations
    const _ = !!new AsyncEvaluator(pattern.expression);
    return true;
  }
github comunica / comunica / packages / actor-query-operation-orderby-sparqlee / lib / ActorQueryOperationOrderBySparqlee.ts View on Github external
public async testOperation(pattern: Algebra.OrderBy, context: ActionContext): Promise {
    // Will throw error for unsupported operators
    for (let expr of pattern.expressions) {
      expr = this.extractSortExpression(expr);
      const _ = new AsyncEvaluator(expr);
    }
    return true;
  }
github comunica / comunica / packages / actor-query-operation-group / lib / GroupsState.ts View on Github external
public consumeBindings(bindings: Bindings) {
    // Select the bindings on which we group
    const grouper = bindings
      .filter((_, variable) => this.groupVariables.has(variable))
      .toMap();
    const groupHash = this.hashBindings(grouper);

    // First member of group -> create new group
    if (!this.groups.has(groupHash)) {
      // Initialize state for all aggregators for new group
      const aggregators: { [key: string]: AggregateEvaluator } = {};
      for (const i in this.pattern.aggregates) {
        const aggregate = this.pattern.aggregates[i];
        const key = termToString(aggregate.variable);
        aggregators[key] = new AggregateEvaluator(aggregate, this.sparqleeConfig);
        aggregators[key].put(bindings);
      }

      const group: IGroup = { aggregators, bindings: grouper };
      this.groups.set(groupHash, group);

      if (this.distinctHashes) {
        const bindingsHash = this.hashBindings(bindings);
        this.distinctHashes.set(groupHash, new Set([bindingsHash]));
      }

    } else {
      // Group already exists
      // Update all the aggregators with the input binding
      const group = this.groups.get(groupHash);
      for (const i in this.pattern.aggregates) {
github comunica / comunica / packages / actor-query-operation-group / lib / GroupsState.ts View on Github external
}
      }

      // Merge grouping bindings and aggregator bindings
      return groupBindings.merge(aggBindings);
    });

    // Case: No Input
    // Some aggregators still define an output on the empty input
    // Result is a single Bindings
    if (rows.length === 0) {
      const single: { [key: string]: Term } = {};
      for (const i in this.pattern.aggregates) {
        const aggregate = this.pattern.aggregates[i];
        const key = termToString(aggregate.variable);
        const value = AggregateEvaluator.emptyValue(aggregate);
        if (value !== undefined) {
          single[key] = value;
        }
      }
      rows = [Bindings(single)];
    }

    return rows;
  }
github comunica / comunica / packages / actor-query-operation-group / lib / ActorQueryOperationGroup.ts View on Github external
public async testOperation(pattern: Algebra.Group, context: ActionContext): Promise {
    for (const i in pattern.aggregates) {
      // Will throw for unsupported expressions
      const _ = new SyncEvaluator(pattern.aggregates[i].expression);
    }
    return true;
  }
github comunica / comunica / packages / actor-query-operation-filter-sparqlee / lib / ActorQueryOperationFilterSparqlee.ts View on Github external
public async runOperation(pattern: Algebra.Filter, context: ActionContext)
    : Promise {

    const outputRaw = await this.mediatorQueryOperation.mediate({ operation: pattern.input, context });
    const output = ActorQueryOperation.getSafeBindings(outputRaw);
    ActorQueryOperation.validateQueryOutput(output, 'bindings');
    const { variables, metadata } = output;

    const expressionContext = ActorQueryOperation.getExpressionContext(context);
    const config = {
      ...expressionContext,
      exists: this.createExistenceResolver(context),
    };
    const evaluator = new AsyncEvaluator(pattern.expression, config);

    const transform = async (item: Bindings, next: any) => {
      try {
        const result = await evaluator.evaluateAsEBV(item);
        if (result) {
          bindingsStream._push(item);
        }
      } catch (err) {
        if (!isExpressionError(err)) {
          bindingsStream.emit('error', err);
        }
      }
      next();
    };

    const bindingsStream = output.bindingsStream.transform({ transform });

sparqlee

A simple SPARQL expression evaluator library

MIT
Latest version published 9 months ago

Package Health Score

48 / 100
Full package analysis