Skip to content

Commit b6da736

Browse files
committed
add toAst docs and sanity
1 parent 6ff8e44 commit b6da736

12 files changed

Lines changed: 259 additions & 188 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Algebra from './algebra';
22
import Factory from './factory';
33
import translate from './toAlgebra';
4-
import { toSparql } from './toAst/sparql';
4+
import { toSparql } from './toAst/toAst';
55
import Util from './util';
66

77
export { translate, Algebra, Factory, toSparql, Util };

packages/algebra-transformer-1-1/lib/toAlgebra/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ export function isTriple(triple: any): triple is RDF.Quad {
5050
return triple.subject && triple.predicate && triple.object;
5151
}
5252

53-
export function isVariable(term: any): term is RDF.Variable {
53+
export function isVariable(term: RDF.Term): term is RDF.Variable {
5454
return term?.termType === 'Variable';
5555
}

packages/algebra-transformer-1-1/lib/toAst/aggregate.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/algebra-transformer-1-1/lib/toAst/core.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,25 @@ import * as Algebra from '../algebra';
66
import Factory from '../factory';
77

88
export interface AstContext {
9+
/**
10+
* Whether we are contained in a projection
11+
*/
912
project: boolean;
13+
/**
14+
* All extends found in our suboperations
15+
*/
1016
extend: Algebra.Extend[];
17+
/**
18+
* All groups found in our suboperations
19+
*/
1120
group: RDF.Variable[];
21+
/**
22+
* All aggregates found in our suboperations
23+
*/
1224
aggregates: Algebra.BoundAggregate[];
25+
/**
26+
* All orderings found in our suboperations
27+
*/
1328
order: Algebra.Expression[];
1429
factory: Factory;
1530
astFactory: AstFactory;

packages/algebra-transformer-1-1/lib/toAst/expression.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function translateExistenceExpression(
3232
const F = c.astFactory;
3333
return F.expressionPatternOperation(
3434
expr.not ? 'notexists' : 'exists',
35+
// TranslateOperation can give an array
3536
F.patternGroup(Util.flatten([ translateOperation(c, expr.input) ]), F.gen()),
3637
F.gen(),
3738
);
@@ -63,13 +64,12 @@ export function translateOperatorExpression(
6364
);
6465
}
6566

66-
// eslint-disable-next-line unused-imports/no-unused-vars
67-
export function translateWildcardExpression(c: AstContext, expr: Algebra.WildcardExpression): Wildcard {
67+
export function translateWildcardExpression(c: AstContext, _expr: Algebra.WildcardExpression): Wildcard {
6868
const F = c.astFactory;
6969
return F.wildcard(F.gen());
7070
}
7171

72-
export function arrayToPattern(c: AstContext, input: Pattern[]): PatternGroup {
72+
export function wrapInPatternGroup(c: AstContext, input: Pattern[] | Pattern): PatternGroup {
7373
const F = c.astFactory;
7474
if (!Array.isArray(input)) {
7575
return F.patternGroup([ input ], F.gen());

packages/algebra-transformer-1-1/lib/toAst/general.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import type * as RDF from '@rdfjs/types';
22
import type {
33
DatasetClauses,
44
Pattern,
5-
PatternBgp,
5+
PatternGroup,
6+
QuerySelect,
67
TermBlank,
78
TermIri,
89
TermLiteral,
910
TermVariable,
1011
TripleNesting,
1112
} from '@traqula/rules-sparql-1-1';
12-
import type * as Algebra from '../algebra';
13+
import type { Algebra } from '../index';
1314
import Util from '../util';
1415
import type { AstContext } from './core';
15-
import { translatePathComponent } from './path';
1616
import { translateExpression, translateOperation } from './pattern';
1717

1818
type RdfTermToAst<T extends RDF.Term> = T extends RDF.Variable ? TermVariable :
@@ -41,6 +41,12 @@ export function translateTerm<T extends RDF.Term>(c: AstContext, term: T): RdfTe
4141
throw new Error(`invalid term type: ${term.termType}`);
4242
}
4343

44+
/**
45+
* Extend is for example a bind, or an aggregator.
46+
* The result is thus registered to be tackled at the project level,
47+
* or if we are not in project scope, we give it as a patternBind
48+
* - of course, the pattern bind is scoped with the other operations at this level
49+
*/
4450
export function translateExtend(c: AstContext, op: Algebra.Extend): Pattern[] {
4551
const F = c.astFactory;
4652
if (c.project) {
@@ -69,22 +75,14 @@ export function translateDatasetClauses(
6975
], F.gen());
7076
}
7177

72-
export function translateOrderBy(c: AstContext, op: Algebra.OrderBy): any {
78+
/**
79+
* An order by is just registered to be handled in the creation of your QueryBase
80+
*/
81+
export function translateOrderBy(c: AstContext, op: Algebra.OrderBy): ReturnType<typeof translateOperation> {
7382
c.order.push(...op.expressions);
7483
return translateOperation(c, op.input);
7584
}
7685

77-
export function translatePath(c: AstContext, op: Algebra.Path): PatternBgp {
78-
const F = c.astFactory;
79-
return F.patternBgp([
80-
F.triple(
81-
translateTerm(c, op.subject),
82-
translatePathComponent(c, op.predicate),
83-
translateTerm(c, op.object),
84-
),
85-
], F.gen());
86-
}
87-
8886
export function translatePattern(c: AstContext, op: Algebra.Pattern): TripleNesting {
8987
const F = c.astFactory;
9088
return F.triple(
@@ -94,9 +92,22 @@ export function translatePattern(c: AstContext, op: Algebra.Pattern): TripleNest
9492
);
9593
}
9694

97-
export function translateReduced(c: AstContext, op: Algebra.Reduced): Pattern {
98-
const result = translateOperation(c, op.input);
99-
// Project is nested in group object
100-
result.patterns[0].reduced = true;
95+
/**
96+
* Reduced is wrapped around a project, set the query contained to be distinct
97+
*/
98+
export function translateReduced(c: AstContext, op: Algebra.Reduced): PatternGroup {
99+
const result: PatternGroup = translateOperation(c, op.input);
100+
const select = <QuerySelect>result.patterns[0];
101+
select.reduced = true;
102+
return result;
103+
}
104+
105+
/**
106+
* District is wrapped around a project, set the query contained to be distinct
107+
*/
108+
export function translateDistinct(c: AstContext, op: Algebra.Distinct): PatternGroup {
109+
const result: PatternGroup = translateOperation(c, op.input);
110+
const select = <QuerySelect>result.patterns[0];
111+
select.distinct = true;
101112
return result;
102113
}

packages/algebra-transformer-1-1/lib/toAst/path.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import type {
55
PathModified,
66
PathNegatedElt,
77
PathPure,
8+
PatternBgp,
89
PropertyPathChain,
910
TermIri,
1011
} from '@traqula/rules-sparql-1-1';
11-
import type { Algebra } from '../index';
12+
import type * as Algebra from '../algebra';
1213
import { types } from '../toAlgebra/core';
1314
import Util from '../util';
1415
import type { AstContext } from './core';
@@ -29,7 +30,7 @@ export function translatePathComponent(c: AstContext, path: Algebra.Operation):
2930
}
3031
}
3132

32-
export function translateAlt(c: AstContext, path: Algebra.Alt): any {
33+
export function translateAlt(c: AstContext, path: Algebra.Alt): Path {
3334
const F = c.astFactory;
3435
const mapped = path.input.map(x => translatePathComponent(c, x));
3536
if (mapped.every(entry => F.isPathOfType(entry, [ '!' ]))) {
@@ -104,3 +105,14 @@ export function translateZeroOrOnePath(c: AstContext, path: Algebra.ZeroOrOnePat
104105
const F = c.astFactory;
105106
return F.path('?', [ translatePathComponent(c, path.path) ], F.gen());
106107
}
108+
109+
export function translatePath(c: AstContext, op: Algebra.Path): PatternBgp {
110+
const F = c.astFactory;
111+
return F.patternBgp([
112+
F.triple(
113+
translateTerm(c, op.subject),
114+
translatePathComponent(c, op.predicate),
115+
translateTerm(c, op.object),
116+
),
117+
], F.gen());
118+
}

0 commit comments

Comments
 (0)