/
exists.ts
69 lines (61 loc) 路 2.9 KB
/
exists.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* file : exists.ts
MIT License
Copyright (c) 2018-2020 Thomas Minier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use strict'
import { Pipeline } from '../engine/pipeline/pipeline'
import { PipelineStage } from '../engine/pipeline/pipeline-engine'
import { Bindings, BindingBase } from '../rdf/bindings'
import { PlanBuilder } from '../engine/plan-builder'
import ExecutionContext from '../engine/context/execution-context'
interface ConditionalBindings {
bindings: Bindings,
output: boolean
}
/**
* Evaluates a SPARQL FILTER (NOT) EXISTS clause
* TODO this function could be simplified using a filterMap like operator, we should check if Rxjs offers that filterMap
* @author Thomas Minier
* @param source - Source {@link PipelineStage}
* @param groups - Content of the FILTER clause
* @param builder - Plan builder used to evaluate subqueries
* @param notexists - True if the filter is NOT EXISTS, False otherwise
* @param context - Execution context
* @return A {@link PipelineStage} which evaluate the FILTER (NOT) EXISTS operation
*/
export default function exists (source: PipelineStage<Bindings>, groups: any[], builder: PlanBuilder, notexists: boolean, context: ExecutionContext) {
const defaultValue: Bindings = new BindingBase()
defaultValue.setProperty('exists', false)
const engine = Pipeline.getInstance()
let evaluator = engine.mergeMap(source, (bindings: Bindings) => {
let op = builder._buildWhere(engine.of(bindings), groups, context)
op = engine.defaultValues(op, defaultValue)
op = engine.first(op)
return engine.map(op, (b: Bindings) => {
const exists: boolean = (!b.hasProperty('exists')) || b.getProperty('exists')
return {
bindings,
output: (exists && (!notexists)) || ((!exists) && notexists)
}
})
})
evaluator = engine.filter(evaluator, (b: ConditionalBindings) => {
return b.output
})
return engine.map(evaluator, (b: ConditionalBindings) => b.bindings)
}