npm.io
1.5.0-alpha.6 • Published 4d ago

@traqula/algebra-sparql-1-2

Licence
MIT
Version
1.5.0-alpha.6
Deps
4
Size
82 kB
Vulns
0
Weekly
0
Stars
28

Traqula logo

A query language transpiler framework for JavaScript

SPARQL 1.2 AST, SPARQL 1.2 Algebra transformer

npm version

Modular transformer to transform a SPARQL 1.2 AST generated by Traqula to SPARQL 1.2 algebra. There is also support for 'non-algebra' entities such as ASK, FROM, etc. to make sure the output contains all relevant information from the query.

Installation

npm install @traqula/algebra-sparql-1-2

or

yarn add @traqula/algebra-sparql-1-2

Import

Either through ESM import:

import { toAst, toAlgebra } from '@traqula/algebra-sparql-1-2';

or CJS require:

const { toAst, toAlgebra } = require('@traqula/algebra-sparql-1-2');

Usage

Input for the translation function should be a Traqula AST, by calling Traqula parser. Algebra operations are modeled as objects of the structure { name: string; input: Operation[] }. The transformation is detailed by section 18.3 of the SPARQL 1.2 specification.

The example bellow demonstrates this package usage where we parse a query string and transform it to algebra, from the algebra we will go back to a query string. Note that unlike the AST level, we do not provide round tripping on algebra level.

import { Parser } from '@traqula/parser-sparql-1-2';
import { Generator } from '@traqula/generator-sparql-1-2';
import { toAlgebra, fromAlgebra } from '@traqula/algebra-sparql-1-2';
// Initialize required variables.
const parser = new Parser();
const generator = new Generator();
const query = `SELECT * { ?s ?p ?o }`;

const ast = parser.parse(query);
const algebra = toAlgebra(ast);

const generatedAst = toAst(algebra);
const generatedQuery = generator.generate(generatedAst);

The algebra transformer for SPARQL 1.2 is a modification of the SPARQL 1.1 algebra transformer; therefore, much of the documentation of that transformer holds here too.

Modifying the built algebra transformer

Minor versions keep this engine's API and behavior, and the name, signature and behavior of the rules in its builders, but can add rules or change how existing rules are implemented, such as which rules they call. This only affects projects that patch, delete or individually register rules. See versioning.