@comunica/expression-evaluator

中文文档
3.2.1发布于 2024/08/07MITTypeScriptCommonJS近 30 天下载 1.7万

A simple SPARQL expression evaluator

原始使用文档
文档目录7 节
  1. Install
  2. Exposed classes
  3. Development
  4. Setup locally
  5. Adding or fixing functions
  6. Layout and control flow
  7. Testing

Comunica Expression Evaluator

npm version

Previously called sparqlee - sparql expression evaluator. A simple spec-compliant SPARQL 1.1 expression evaluator package.

Learn more about the expression evaluator.

This module is part of the Comunica framework, and should only be used by developers that want to build their own query engine.

Click here if you just want to query with Comunica.

Install

$ yarn add @comunica/expression-evaluator

Exposed classes

Development

Setup locally

  1. Install yarn and node.
  2. Run yarn install.
  3. Use these evident commands (or check package.json):
    • building once: yarn run build
    • benchmarking: yarn run bench

Adding or fixing functions

Functions are defined in the functions directory, and you can add or fix them there. All definitions are defined using a builder model defined in Helpers.ts.

Three kinds exists:

  • Regular functions: Functions with a uniform interface, that only need their arguments to calculate their result.
  • Special functions: whose behaviour deviates enough from the norm to warrant the implementations taking full control over type checking and evaluation (these are mostly the functional forms). They are seperated from the regular functions because they are able to take control over the evaluation. Since we have support both async and sync evaluations, each having a distinct context, special functions require an implementation for both. For regular functions, the sync and async evaluation is the same, to avoid overhead, we differentiate between these two.
  • Named functions: which correspond to the SPARQLAlgebra Named Expressions.

Layout and control flow

The only important external facing API is creating an Evaluator. When you create one, the SPARQL Algebra expression that is passed will be transformed to an internal representation (see AlgebraTransformer.ts). This will build objects (see expressions module) that contain all the logic and data for evaluation, for example the implementations for SPARQL functions (see functions module). After transformation, the evaluator will recursively evaluate all the expressions.

Testing

The testing environment is set up to do a lot of tests with little code. The files responsible for fluent behaviour reside in the test/util module. Most tests can be run by running the runTestTable method in utils. This method expects a TestTable. Multiple test are run over a TestTable (one for every line). A TestTable may contain aliases if the aliases are also provided (Some handy aliases reside in Aliases.ts). This means that when testing something like "3"^^xsd:integer equals "3"^^xsd:integer is "true"^^xsd:boolean. We would write a small table (for this example some more tests are added) and test it like this:

import { bool, merge, numeric } from './util/Aliases';
import { Notation } from './util/TruthTable';
import { runTestTable } from './util/utils';
runTestTable({
  testTable: `
       3i 3i = true
       3i -5i = false
       -0f 0f = true
       NaN  NaN = false
   `,
  arity: 2,
  operation: '=',
  aliases: merge(numeric, bool),
  notation: Notation.Infix,
});

More options can be provided and are explained with the type definition of the argument of runTestTable.

We can also provide an errorTable to the runTestTable method. This is used when we want to test if calling certain functions on certain arguments throws the error we want. An example is testing whether Unknown named operator error is thrown when we don't provide the implementation for an extension function.

import { bool, merge, numeric } from './util/Aliases';
import { Notation } from './util/TruthTable';
import { runTestTable } from './util/utils';
runTestTable({
  errorTable: `
       3i 3i = 'Unknown named operator'
       3i -5i = 'Unknown named operator'
       -0f 0f = 'Unknown named operator'
       NaN  NaN = 'Unknown named operator'
   `,
  arity: 2,
  operation: '<https://example.org/functions#equal>',
  aliases: merge(numeric, bool),
  notation: Notation.Infix,
});

When you don't care what the error is, you can just test for ''.

In case the tables are too restrictive for your test, and you need an evaluation. You should still use the generalEvaluate function from generalEvaluation.ts. This function will automatically run both async and sync when possible. This increases your tests' coverage.

版本与发布记录

@comunica/expression-evaluator 共发布 9 个版本,首次发布于 2023/09/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.2.1latest2024/08/072 年前
3.1.02024/05/112 年前
3.0.12024/03/192 年前
3.0.1-alpha.46.0next2024/03/052 年前
3.0.1-alpha.45.02024/02/202 年前
3.0.1-alpha.44.02024/02/192 年前

关于 @comunica/expression-evaluator 的常见问题

@comunica/expression-evaluator 是什么?

A simple SPARQL expression evaluator

如何安装 @comunica/expression-evaluator?

在项目目录运行 npm install @comunica/expression-evaluator,或使用 pnpm add / yarn add / bun add。

@comunica/expression-evaluator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@comunica/expression-evaluator 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@comunica/expression-evaluator 可以商用吗?

@comunica/expression-evaluator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@comunica/expression-evaluator 的下载量有多少?

根据 npm 官方下载统计,@comunica/expression-evaluator 在 2026/10/11 – 2026/11/09 这 30 天内被下载 17,153 次,日均约 572 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@comunica/expression-evaluator 还在维护吗?

最新版本 3.2.1 发布于 2024/08/07(2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/09/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。