@comunica/actor-optimize-query-operation-prune-empty-source-operations 3.3.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld +44 -12
- package/components/components.jsonld +1 -1
- package/components/context.jsonld +7 -1
- package/lib/ActorOptimizeQueryOperationPruneEmptySourceOperations.d.ts +5 -4
- package/lib/ActorOptimizeQueryOperationPruneEmptySourceOperations.js +16 -16
- package/lib/ActorOptimizeQueryOperationPruneEmptySourceOperations.js.map +1 -1
- package/package.json +12 -9
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"@context": [
|
|
3
|
-
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-optimize-query-operation-prune-empty-source-operations/^
|
|
4
|
-
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/bus-optimize-query-operation/^
|
|
5
|
-
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/core/^
|
|
3
|
+
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-optimize-query-operation-prune-empty-source-operations/^4.0.0/components/context.jsonld",
|
|
4
|
+
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/bus-optimize-query-operation/^4.0.0/components/context.jsonld",
|
|
5
|
+
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/core/^4.0.0/components/context.jsonld"
|
|
6
6
|
],
|
|
7
7
|
"@id": "npmd:@comunica/actor-optimize-query-operation-prune-empty-source-operations",
|
|
8
8
|
"components": [
|
|
@@ -41,29 +41,37 @@
|
|
|
41
41
|
"genericTypeInstances": [
|
|
42
42
|
{
|
|
43
43
|
"@type": "ParameterRangeGenericTypeReference",
|
|
44
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
44
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
|
|
45
45
|
},
|
|
46
46
|
{
|
|
47
47
|
"@type": "ParameterRangeGenericTypeReference",
|
|
48
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
48
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
|
|
49
49
|
},
|
|
50
50
|
{
|
|
51
51
|
"@type": "ParameterRangeGenericTypeReference",
|
|
52
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
52
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"@type": "ParameterRangeGenericTypeReference",
|
|
56
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
|
|
53
57
|
}
|
|
54
58
|
]
|
|
55
59
|
},
|
|
56
60
|
{
|
|
57
61
|
"@type": "ParameterRangeGenericTypeReference",
|
|
58
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
62
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"@type": "ParameterRangeGenericTypeReference",
|
|
66
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
|
|
59
67
|
},
|
|
60
68
|
{
|
|
61
69
|
"@type": "ParameterRangeGenericTypeReference",
|
|
62
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
70
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
|
|
63
71
|
},
|
|
64
72
|
{
|
|
65
73
|
"@type": "ParameterRangeGenericTypeReference",
|
|
66
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
74
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
|
|
67
75
|
}
|
|
68
76
|
]
|
|
69
77
|
},
|
|
@@ -73,6 +81,20 @@
|
|
|
73
81
|
},
|
|
74
82
|
"comment": "The bus this actor subscribes to."
|
|
75
83
|
},
|
|
84
|
+
{
|
|
85
|
+
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_busFailMessage",
|
|
86
|
+
"range": {
|
|
87
|
+
"@type": "ParameterRangeUnion",
|
|
88
|
+
"parameterRangeElements": [
|
|
89
|
+
"xsd:string",
|
|
90
|
+
{
|
|
91
|
+
"@type": "ParameterRangeUndefined"
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
},
|
|
95
|
+
"default": "Query optimization failed: none of the configured actors were able to optimize",
|
|
96
|
+
"comment": "The message that will be configured in the bus for reporting failures. This message may be a template string that contains references to the executed `action`. For example, the following templated string is allowed: \"RDF dereferencing failed: no actors could handle ${action.handle.mediaType}\""
|
|
97
|
+
},
|
|
76
98
|
{
|
|
77
99
|
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_beforeActors",
|
|
78
100
|
"range": {
|
|
@@ -86,15 +108,19 @@
|
|
|
86
108
|
"genericTypeInstances": [
|
|
87
109
|
{
|
|
88
110
|
"@type": "ParameterRangeGenericTypeReference",
|
|
89
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
111
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_I"
|
|
90
112
|
},
|
|
91
113
|
{
|
|
92
114
|
"@type": "ParameterRangeGenericTypeReference",
|
|
93
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
115
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_T"
|
|
94
116
|
},
|
|
95
117
|
{
|
|
96
118
|
"@type": "ParameterRangeGenericTypeReference",
|
|
97
|
-
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^
|
|
119
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_O"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"@type": "ParameterRangeGenericTypeReference",
|
|
123
|
+
"parameterRangeGenericType": "npmd:@comunica/actor-abstract-mediatyped/^4.0.0/components/ActorAbstractMediaTyped.jsonld#ActorAbstractMediaTyped__generic_TS"
|
|
98
124
|
}
|
|
99
125
|
]
|
|
100
126
|
}
|
|
@@ -163,6 +189,12 @@
|
|
|
163
189
|
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_bus"
|
|
164
190
|
}
|
|
165
191
|
},
|
|
192
|
+
{
|
|
193
|
+
"keyRaw": "busFailMessage",
|
|
194
|
+
"value": {
|
|
195
|
+
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_busFailMessage"
|
|
196
|
+
}
|
|
197
|
+
},
|
|
166
198
|
{
|
|
167
199
|
"keyRaw": "beforeActors",
|
|
168
200
|
"value": {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"@context": [
|
|
3
|
-
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-optimize-query-operation-prune-empty-source-operations/^
|
|
3
|
+
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-optimize-query-operation-prune-empty-source-operations/^4.0.0/components/context.jsonld"
|
|
4
4
|
],
|
|
5
5
|
"@id": "npmd:@comunica/actor-optimize-query-operation-prune-empty-source-operations",
|
|
6
6
|
"@type": "Module",
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"https://linkedsoftwaredependencies.org/bundles/npm/componentsjs/^6.0.0/components/context.jsonld",
|
|
4
4
|
{
|
|
5
5
|
"npmd": "https://linkedsoftwaredependencies.org/bundles/npm/",
|
|
6
|
-
"caoqopeso": "npmd:@comunica/actor-optimize-query-operation-prune-empty-source-operations/^
|
|
6
|
+
"caoqopeso": "npmd:@comunica/actor-optimize-query-operation-prune-empty-source-operations/^4.0.0/",
|
|
7
7
|
"ActorOptimizeQueryOperationPruneEmptySourceOperations": {
|
|
8
8
|
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations",
|
|
9
9
|
"@prefix": true,
|
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
"args_bus": {
|
|
18
18
|
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_bus"
|
|
19
19
|
},
|
|
20
|
+
"args_busFailMessage": {
|
|
21
|
+
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_busFailMessage"
|
|
22
|
+
},
|
|
20
23
|
"args_beforeActors": {
|
|
21
24
|
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_beforeActors",
|
|
22
25
|
"@container": "@list"
|
|
@@ -30,6 +33,9 @@
|
|
|
30
33
|
"bus": {
|
|
31
34
|
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_bus"
|
|
32
35
|
},
|
|
36
|
+
"busFailMessage": {
|
|
37
|
+
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_busFailMessage"
|
|
38
|
+
},
|
|
33
39
|
"beforeActors": {
|
|
34
40
|
"@id": "caoqopeso:components/ActorOptimizeQueryOperationPruneEmptySourceOperations.jsonld#ActorOptimizeQueryOperationPruneEmptySourceOperations_args_beforeActors",
|
|
35
41
|
"@container": "@list"
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import type { IActionOptimizeQueryOperation, IActorOptimizeQueryOperationOutput, IActorOptimizeQueryOperationArgs } from '@comunica/bus-optimize-query-operation';
|
|
2
2
|
import { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';
|
|
3
|
-
import type { IActorTest } from '@comunica/core';
|
|
3
|
+
import type { IActorTest, TestResult } from '@comunica/core';
|
|
4
4
|
import type { IActionContext, IQuerySourceWrapper } from '@comunica/types';
|
|
5
|
-
import { Algebra } from 'sparqlalgebrajs';
|
|
5
|
+
import { Algebra, Factory } from 'sparqlalgebrajs';
|
|
6
6
|
/**
|
|
7
7
|
* A comunica Prune Empty Source Operations Optimize Query Operation Actor.
|
|
8
8
|
*/
|
|
9
9
|
export declare class ActorOptimizeQueryOperationPruneEmptySourceOperations extends ActorOptimizeQueryOperation {
|
|
10
10
|
private readonly useAskIfSupported;
|
|
11
11
|
constructor(args: IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs);
|
|
12
|
-
test(action: IActionOptimizeQueryOperation): Promise<IActorTest
|
|
12
|
+
test(action: IActionOptimizeQueryOperation): Promise<TestResult<IActorTest>>;
|
|
13
13
|
run(action: IActionOptimizeQueryOperation): Promise<IActorOptimizeQueryOperationOutput>;
|
|
14
14
|
protected static hasEmptyOperation(operation: Algebra.Operation): boolean;
|
|
15
15
|
protected collectMultiOperationInputs(inputs: Algebra.Operation[], collectedOperations: (Algebra.Pattern | Algebra.Link)[], inputType: (Algebra.Pattern | Algebra.Link)['type']): void;
|
|
@@ -19,11 +19,12 @@ export declare class ActorOptimizeQueryOperationPruneEmptySourceOperations exten
|
|
|
19
19
|
};
|
|
20
20
|
/**
|
|
21
21
|
* Check if the given query operation will produce at least one result in the given source.
|
|
22
|
+
* @param algebraFactory The algebra factory.
|
|
22
23
|
* @param source A query source.
|
|
23
24
|
* @param input A query operation.
|
|
24
25
|
* @param context The query context.
|
|
25
26
|
*/
|
|
26
|
-
hasSourceResults(source: IQuerySourceWrapper, input: Algebra.Operation, context: IActionContext): Promise<boolean>;
|
|
27
|
+
hasSourceResults(algebraFactory: Factory, source: IQuerySourceWrapper, input: Algebra.Operation, context: IActionContext): Promise<boolean>;
|
|
27
28
|
}
|
|
28
29
|
export interface IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs extends IActorOptimizeQueryOperationArgs {
|
|
29
30
|
/**
|
|
@@ -2,12 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ActorOptimizeQueryOperationPruneEmptySourceOperations = void 0;
|
|
4
4
|
const bus_optimize_query_operation_1 = require("@comunica/bus-optimize-query-operation");
|
|
5
|
-
const bus_query_operation_1 = require("@comunica/bus-query-operation");
|
|
6
5
|
const context_entries_1 = require("@comunica/context-entries");
|
|
7
|
-
const
|
|
6
|
+
const core_1 = require("@comunica/core");
|
|
7
|
+
const utils_query_operation_1 = require("@comunica/utils-query-operation");
|
|
8
8
|
const sparqlalgebrajs_1 = require("sparqlalgebrajs");
|
|
9
|
-
const AF = new sparqlalgebrajs_1.Factory();
|
|
10
|
-
const DF = new rdf_data_factory_1.DataFactory();
|
|
11
9
|
/**
|
|
12
10
|
* A comunica Prune Empty Source Operations Optimize Query Operation Actor.
|
|
13
11
|
*/
|
|
@@ -16,12 +14,14 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
16
14
|
super(args);
|
|
17
15
|
}
|
|
18
16
|
async test(action) {
|
|
19
|
-
if (
|
|
20
|
-
|
|
17
|
+
if ((0, utils_query_operation_1.getOperationSource)(action.operation)) {
|
|
18
|
+
return (0, core_1.failTest)(`Actor ${this.name} does not work with top-level operation sources.`);
|
|
21
19
|
}
|
|
22
|
-
return
|
|
20
|
+
return (0, core_1.passTestVoid)();
|
|
23
21
|
}
|
|
24
22
|
async run(action) {
|
|
23
|
+
const dataFactory = action.context.getSafe(context_entries_1.KeysInitQuery.dataFactory);
|
|
24
|
+
const algebraFactory = new sparqlalgebrajs_1.Factory(dataFactory);
|
|
25
25
|
let operation = action.operation;
|
|
26
26
|
// Collect all operations with source types
|
|
27
27
|
// Only consider unions of patterns or alts of links, since these are created during exhaustive source assignment.
|
|
@@ -45,9 +45,9 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
45
45
|
const emptyOperations = new Set();
|
|
46
46
|
await Promise.all(collectedOperations.map(async (collectedOperation) => {
|
|
47
47
|
const checkOperation = collectedOperation.type === 'link' ?
|
|
48
|
-
|
|
48
|
+
algebraFactory.createPattern(dataFactory.variable('?s'), collectedOperation.iri, dataFactory.variable('?o')) :
|
|
49
49
|
collectedOperation;
|
|
50
|
-
if (!await this.hasSourceResults(
|
|
50
|
+
if (!await this.hasSourceResults(algebraFactory, (0, utils_query_operation_1.getOperationSource)(collectedOperation), checkOperation, action.context)) {
|
|
51
51
|
emptyOperations.add(collectedOperation);
|
|
52
52
|
}
|
|
53
53
|
}));
|
|
@@ -62,7 +62,7 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
62
62
|
[sparqlalgebrajs_1.Algebra.types.ALT](subOperation, factory) {
|
|
63
63
|
return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createAlt(children));
|
|
64
64
|
},
|
|
65
|
-
});
|
|
65
|
+
}, algebraFactory);
|
|
66
66
|
// Identify and remove operations that have become empty now due to missing variables
|
|
67
67
|
operation = sparqlalgebrajs_1.Util.mapOperation(operation, {
|
|
68
68
|
[sparqlalgebrajs_1.Algebra.types.PROJECT](subOperation, factory) {
|
|
@@ -91,7 +91,7 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
91
91
|
result: subOperation,
|
|
92
92
|
};
|
|
93
93
|
},
|
|
94
|
-
});
|
|
94
|
+
}, algebraFactory);
|
|
95
95
|
}
|
|
96
96
|
return { operation, context: action.context };
|
|
97
97
|
}
|
|
@@ -126,7 +126,7 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
126
126
|
}
|
|
127
127
|
collectMultiOperationInputs(inputs, collectedOperations, inputType) {
|
|
128
128
|
for (const input of inputs) {
|
|
129
|
-
if (
|
|
129
|
+
if ((0, utils_query_operation_1.getOperationSource)(input) && input.type === inputType) {
|
|
130
130
|
collectedOperations.push(input);
|
|
131
131
|
}
|
|
132
132
|
}
|
|
@@ -148,20 +148,20 @@ class ActorOptimizeQueryOperationPruneEmptySourceOperations extends bus_optimize
|
|
|
148
148
|
}
|
|
149
149
|
/**
|
|
150
150
|
* Check if the given query operation will produce at least one result in the given source.
|
|
151
|
+
* @param algebraFactory The algebra factory.
|
|
151
152
|
* @param source A query source.
|
|
152
153
|
* @param input A query operation.
|
|
153
154
|
* @param context The query context.
|
|
154
155
|
*/
|
|
155
|
-
async hasSourceResults(source, input, context) {
|
|
156
|
+
async hasSourceResults(algebraFactory, source, input, context) {
|
|
156
157
|
// Traversal sources should never be considered empty at optimization time.
|
|
157
158
|
if (source.context?.get(context_entries_1.KeysQuerySourceIdentify.traverse)) {
|
|
158
159
|
return true;
|
|
159
160
|
}
|
|
160
161
|
// Send an ASK query
|
|
161
162
|
if (this.useAskIfSupported) {
|
|
162
|
-
const askOperation =
|
|
163
|
-
if (
|
|
164
|
-
.doesShapeAcceptOperation(await source.source.getSelectorShape(context), askOperation)) {
|
|
163
|
+
const askOperation = algebraFactory.createAsk(input);
|
|
164
|
+
if ((0, utils_query_operation_1.doesShapeAcceptOperation)(await source.source.getSelectorShape(context), askOperation)) {
|
|
165
165
|
return source.source.queryBoolean(askOperation, context);
|
|
166
166
|
}
|
|
167
167
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ActorOptimizeQueryOperationPruneEmptySourceOperations.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationPruneEmptySourceOperations.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,uEAAoE;AACpE,+DAAoE;AAGpE,uDAA+C;AAC/C,qDAAyD;AAEzD,MAAM,EAAE,GAAG,IAAI,yBAAO,EAAE,CAAC;AACzB,MAAM,EAAE,GAAG,IAAI,8BAAW,EAAE,CAAC;AAE7B;;GAEG;AACH,MAAa,qDAAsD,SAAQ,0DAA2B;IAGpG,YAAmB,IAAgE;QACjF,KAAK,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,MAAqC;QACrD,IAAI,yCAAmB,CAAC,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;YAC7D,MAAM,IAAI,KAAK,CAAC,SAAS,IAAI,CAAC,IAAI,kDAAkD,CAAC,CAAC;QACxF,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,MAAqC;QACpD,IAAI,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAEjC,2CAA2C;QAC3C,kHAAkH;QAClH,MAAM,mBAAmB,GAAuC,EAAE,CAAC;QACnE,4CAA4C;Q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CAAC;IAC1E,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,gBAAgB,CAC3B,MAA2B,EAC3B,KAAwB,EACxB,OAAuB;QAEvB,2EAA2E;QAC3E,IAAI,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,yCAAuB,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC1D,OAAO,IAAI,CAAC;QACd,CAAC;QAED,oBAAoB;QACpB,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC3B,MAAM,YAAY,GAAG,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YACzC,IAAI,yCAAmB;iBACpB,wBAAwB,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,CAAC,EAAE,YAAY,CAAC,EAAE,CAAC;gBACzF,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;YAC3D,CAAC;QACH,CAAC;QAED,8DAA8D;QAC9D,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACnE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACnC,cAAc,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC,QAA0B,EAAE,EAAE;gBACpE,cAAc,CAAC,OAAO,EAAE,CAAC;gBACzB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;YAC1C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAvMD,sHAuMC","sourcesContent":["import type {\n IActionOptimizeQueryOperation,\n IActorOptimizeQueryOperationOutput,\n IActorOptimizeQueryOperationArgs,\n} from '@comunica/bus-optimize-query-operation';\nimport { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';\nimport { ActorQueryOperation } from '@comunica/bus-query-operation';\nimport { KeysQuerySourceIdentify } from '@comunica/context-entries';\nimport type { IActorTest } from '@comunica/core';\nimport type { IActionContext, IQuerySourceWrapper, MetadataBindings } from '@comunica/types';\nimport { DataFactory } from 'rdf-data-factory';\nimport { Algebra, Factory, Util } from 'sparqlalgebrajs';\n\nconst AF = new Factory();\nconst DF = new DataFactory();\n\n/**\n * A comunica Prune Empty Source Operations Optimize Query Operation Actor.\n */\nexport class ActorOptimizeQueryOperationPruneEmptySourceOperations extends ActorOptimizeQueryOperation {\n private readonly useAskIfSupported: boolean;\n\n public constructor(args: IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs) {\n super(args);\n }\n\n public async test(action: IActionOptimizeQueryOperation): Promise<IActorTest> {\n if (ActorQueryOperation.getOperationSource(action.operation)) {\n throw new Error(`Actor ${this.name} does not work with top-level operation sources.`);\n }\n return true;\n }\n\n public async run(action: IActionOptimizeQueryOperation): Promise<IActorOptimizeQueryOperationOutput> {\n let operation = action.operation;\n\n // Collect all operations with source types\n // Only consider unions of patterns or alts of links, since these are created during exhaustive source assignment.\n const collectedOperations: (Algebra.Pattern | Algebra.Link)[] = [];\n // eslint-disable-next-line ts/no-this-alias\n const self = this;\n Util.recurseOperation(operation, {\n [Algebra.types.UNION](subOperation) {\n self.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.types.PATTERN);\n return true;\n },\n [Algebra.types.ALT](subOperation) {\n self.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.types.LINK);\n return false;\n },\n [Algebra.types.SERVICE]() {\n return false;\n },\n });\n\n // Determine in an async manner whether or not these sources return non-empty results\n const emptyOperations: Set<Algebra.Operation> = new Set();\n await Promise.all(collectedOperations.map(async(collectedOperation) => {\n const checkOperation = collectedOperation.type === 'link' ?\n AF.createPattern(DF.variable('?s'), collectedOperation.iri, DF.variable('?o')) :\n collectedOperation;\n if (!await this.hasSourceResults(\n ActorQueryOperation.getOperationSource(collectedOperation)!,\n checkOperation,\n action.context,\n )) {\n emptyOperations.add(collectedOperation);\n }\n }));\n\n // Only perform next mapping if we have at least one empty operation\n if (emptyOperations.size > 0) {\n this.logDebug(action.context, `Pruning ${emptyOperations.size} source-specific operations`);\n // Rewrite operations by removing the empty children\n operation = Util.mapOperation(operation, {\n [Algebra.types.UNION](subOperation, factory) {\n return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createUnion(children));\n },\n [Algebra.types.ALT](subOperation, factory) {\n return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createAlt(children));\n },\n });\n\n // Identify and remove operations that have become empty now due to missing variables\n operation = Util.mapOperation(operation, {\n [Algebra.types.PROJECT](subOperation, factory) {\n // Remove projections that have become empty now due to missing variables\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation)) {\n return {\n recurse: false,\n result: factory.createUnion([]),\n };\n }\n return {\n recurse: true,\n result: subOperation,\n };\n },\n [Algebra.types.LEFT_JOIN](subOperation) {\n // Remove left joins with empty right operation\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[1])) {\n return {\n recurse: true,\n result: subOperation.input[0],\n };\n }\n return {\n recurse: true,\n result: subOperation,\n };\n },\n });\n }\n\n return { operation, context: action.context };\n }\n\n protected static hasEmptyOperation(operation: Algebra.Operation): boolean {\n // If union (or alt) is empty, consider it empty (`Array.every` on an empty array always returns true)\n // But if we find a union with multiple children,\n // *all* of the children must be empty before the full operation is considered empty.\n let emptyOperation = false;\n Util.recurseOperation(operation, {\n [Algebra.types.UNION](subOperation) {\n if (subOperation.input.every(subSubOperation => ActorOptimizeQueryOperationPruneEmptySourceOperations\n .hasEmptyOperation(subSubOperation))) {\n emptyOperation = true;\n }\n return false;\n },\n [Algebra.types.ALT](subOperation) {\n if (subOperation.input.length === 0) {\n emptyOperation = true;\n }\n return false;\n },\n [Algebra.types.LEFT_JOIN](subOperation) {\n // Only recurse into left part of left-join\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[0])) {\n emptyOperation = true;\n }\n return false;\n },\n });\n return emptyOperation;\n }\n\n protected collectMultiOperationInputs(\n inputs: Algebra.Operation[],\n collectedOperations: (Algebra.Pattern | Algebra.Link)[],\n inputType: (Algebra.Pattern | Algebra.Link)['type'],\n ): void {\n for (const input of inputs) {\n if (ActorQueryOperation.getOperationSource(input) && input.type === inputType) {\n collectedOperations.push(input);\n }\n }\n }\n\n protected mapMultiOperation<O extends Algebra.Union | Algebra.Alt>(\n operation: O,\n emptyOperations: Set<Algebra.Operation>,\n multiOperationFactory: (input: O['input']) => Algebra.Operation,\n ): {\n result: Algebra.Operation;\n recurse: boolean;\n } {\n // Determine which operations return non-empty results\n const nonEmptyInputs = operation.input.filter(input => !emptyOperations.has(input));\n\n // Remove empty operations\n if (nonEmptyInputs.length === operation.input.length) {\n return { result: operation, recurse: true };\n }\n if (nonEmptyInputs.length === 0) {\n return { result: multiOperationFactory([]), recurse: false };\n }\n if (nonEmptyInputs.length === 1) {\n return { result: nonEmptyInputs[0], recurse: true };\n }\n return { result: multiOperationFactory(nonEmptyInputs), recurse: true };\n }\n\n /**\n * Check if the given query operation will produce at least one result in the given source.\n * @param source A query source.\n * @param input A query operation.\n * @param context The query context.\n */\n public async hasSourceResults(\n source: IQuerySourceWrapper,\n input: Algebra.Operation,\n context: IActionContext,\n ): Promise<boolean> {\n // Traversal sources should never be considered empty at optimization time.\n if (source.context?.get(KeysQuerySourceIdentify.traverse)) {\n return true;\n }\n\n // Send an ASK query\n if (this.useAskIfSupported) {\n const askOperation = AF.createAsk(input);\n if (ActorQueryOperation\n .doesShapeAcceptOperation(await source.source.getSelectorShape(context), askOperation)) {\n return source.source.queryBoolean(askOperation, context);\n }\n }\n\n // Send the operation as-is and check the response cardinality\n const bindingsStream = source.source.queryBindings(input, context);\n return new Promise((resolve, reject) => {\n bindingsStream.on('error', reject);\n bindingsStream.getProperty('metadata', (metadata: MetadataBindings) => {\n bindingsStream.destroy();\n resolve(metadata.cardinality.value > 0);\n });\n });\n }\n}\n\nexport interface IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs extends IActorOptimizeQueryOperationArgs {\n /**\n * If true, ASK queries will be sent to the source instead of COUNT queries to check emptiness for patterns.\n * This will only be done for sources that accept ASK queries.\n * @default {false}\n */\n useAskIfSupported: boolean;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ActorOptimizeQueryOperationPruneEmptySourceOperations.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationPruneEmptySourceOperations.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,+DAAmF;AAEnF,yCAAwD;AAExD,2EAA+F;AAC/F,qDAAyD;AAEzD;;GAEG;AACH,MAAa,qDAAsD,SAAQ,0DAA2B;IAGpG,YAAmB,IAAgE;QACjF,KAAK,CAAC,IAAI,CAAC,CAAC;IACd,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,MAAqC;QACrD,IAAI,IAAA,0CAAkB,EAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC;YACzC,OAAO,IAAA,eAAQ,EAAC,SAAS,IAAI,CAAC,IAAI,kDAAkD,CAAC,CAAC;QACxF,CAAC;QACD,OAAO,IAAA,mBAAY,GAAE,CAAC;IACxB,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,MAAqC;QACpD,MAAM,WAAW,GAAwB,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,+BAAa,CAAC,WAAW,CAAC,CAAC;QAC3F,MAAM,cAAc,GAAG,IAAI,yBAAO,CAAC,WAAW,CAAC,CAAC;QAEhD,IAAI,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAEjC,2CAA2C;QAC3C,kHAAkH;QAClH,MAAM,mBAAmB,GAAuC,EAAE,CAAC;QACnE,4CAA4C;QAC5C,MAAM,IAAI,GAAG,IAAI,CAAC;QAClB,sBAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE;YAC/B,CAAC,y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EACvB,MAA2B,EAC3B,KAAwB,EACxB,OAAuB;QAEvB,2EAA2E;QAC3E,IAAI,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,yCAAuB,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC1D,OAAO,IAAI,CAAC;QACd,CAAC;QAED,oBAAoB;QACpB,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC3B,MAAM,YAAY,GAAG,cAAc,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YACrD,IAAI,IAAA,gDAAwB,EAAC,MAAM,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,CAAC,EAAE,YAAY,CAAC,EAAE,CAAC;gBAC1F,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;YAC3D,CAAC;QACH,CAAC;QAED,8DAA8D;QAC9D,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACnE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACnC,cAAc,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC,QAA0B,EAAE,EAAE;gBACpE,cAAc,CAAC,OAAO,EAAE,CAAC;gBACzB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;YAC1C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA5MD,sHA4MC","sourcesContent":["import type {\n IActionOptimizeQueryOperation,\n IActorOptimizeQueryOperationOutput,\n IActorOptimizeQueryOperationArgs,\n} from '@comunica/bus-optimize-query-operation';\nimport { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';\nimport { KeysInitQuery, KeysQuerySourceIdentify } from '@comunica/context-entries';\nimport type { IActorTest, TestResult } from '@comunica/core';\nimport { failTest, passTestVoid } from '@comunica/core';\nimport type { ComunicaDataFactory, IActionContext, IQuerySourceWrapper, MetadataBindings } from '@comunica/types';\nimport { doesShapeAcceptOperation, getOperationSource } from '@comunica/utils-query-operation';\nimport { Algebra, Factory, Util } from 'sparqlalgebrajs';\n\n/**\n * A comunica Prune Empty Source Operations Optimize Query Operation Actor.\n */\nexport class ActorOptimizeQueryOperationPruneEmptySourceOperations extends ActorOptimizeQueryOperation {\n private readonly useAskIfSupported: boolean;\n\n public constructor(args: IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs) {\n super(args);\n }\n\n public async test(action: IActionOptimizeQueryOperation): Promise<TestResult<IActorTest>> {\n if (getOperationSource(action.operation)) {\n return failTest(`Actor ${this.name} does not work with top-level operation sources.`);\n }\n return passTestVoid();\n }\n\n public async run(action: IActionOptimizeQueryOperation): Promise<IActorOptimizeQueryOperationOutput> {\n const dataFactory: ComunicaDataFactory = action.context.getSafe(KeysInitQuery.dataFactory);\n const algebraFactory = new Factory(dataFactory);\n\n let operation = action.operation;\n\n // Collect all operations with source types\n // Only consider unions of patterns or alts of links, since these are created during exhaustive source assignment.\n const collectedOperations: (Algebra.Pattern | Algebra.Link)[] = [];\n // eslint-disable-next-line ts/no-this-alias\n const self = this;\n Util.recurseOperation(operation, {\n [Algebra.types.UNION](subOperation) {\n self.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.types.PATTERN);\n return true;\n },\n [Algebra.types.ALT](subOperation) {\n self.collectMultiOperationInputs(subOperation.input, collectedOperations, Algebra.types.LINK);\n return false;\n },\n [Algebra.types.SERVICE]() {\n return false;\n },\n });\n\n // Determine in an async manner whether or not these sources return non-empty results\n const emptyOperations: Set<Algebra.Operation> = new Set();\n await Promise.all(collectedOperations.map(async(collectedOperation) => {\n const checkOperation = collectedOperation.type === 'link' ?\n algebraFactory.createPattern(dataFactory.variable('?s'), collectedOperation.iri, dataFactory.variable('?o')) :\n collectedOperation;\n if (!await this.hasSourceResults(\n algebraFactory,\n getOperationSource(collectedOperation)!,\n checkOperation,\n action.context,\n )) {\n emptyOperations.add(collectedOperation);\n }\n }));\n\n // Only perform next mapping if we have at least one empty operation\n if (emptyOperations.size > 0) {\n this.logDebug(action.context, `Pruning ${emptyOperations.size} source-specific operations`);\n // Rewrite operations by removing the empty children\n operation = Util.mapOperation(operation, {\n [Algebra.types.UNION](subOperation, factory) {\n return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createUnion(children));\n },\n [Algebra.types.ALT](subOperation, factory) {\n return self.mapMultiOperation(subOperation, emptyOperations, children => factory.createAlt(children));\n },\n }, algebraFactory);\n\n // Identify and remove operations that have become empty now due to missing variables\n operation = Util.mapOperation(operation, {\n [Algebra.types.PROJECT](subOperation, factory) {\n // Remove projections that have become empty now due to missing variables\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation)) {\n return {\n recurse: false,\n result: factory.createUnion([]),\n };\n }\n return {\n recurse: true,\n result: subOperation,\n };\n },\n [Algebra.types.LEFT_JOIN](subOperation) {\n // Remove left joins with empty right operation\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[1])) {\n return {\n recurse: true,\n result: subOperation.input[0],\n };\n }\n return {\n recurse: true,\n result: subOperation,\n };\n },\n }, algebraFactory);\n }\n\n return { operation, context: action.context };\n }\n\n protected static hasEmptyOperation(operation: Algebra.Operation): boolean {\n // If union (or alt) is empty, consider it empty (`Array.every` on an empty array always returns true)\n // But if we find a union with multiple children,\n // *all* of the children must be empty before the full operation is considered empty.\n let emptyOperation = false;\n Util.recurseOperation(operation, {\n [Algebra.types.UNION](subOperation) {\n if (subOperation.input.every(subSubOperation => ActorOptimizeQueryOperationPruneEmptySourceOperations\n .hasEmptyOperation(subSubOperation))) {\n emptyOperation = true;\n }\n return false;\n },\n [Algebra.types.ALT](subOperation) {\n if (subOperation.input.length === 0) {\n emptyOperation = true;\n }\n return false;\n },\n [Algebra.types.LEFT_JOIN](subOperation) {\n // Only recurse into left part of left-join\n if (ActorOptimizeQueryOperationPruneEmptySourceOperations.hasEmptyOperation(subOperation.input[0])) {\n emptyOperation = true;\n }\n return false;\n },\n });\n return emptyOperation;\n }\n\n protected collectMultiOperationInputs(\n inputs: Algebra.Operation[],\n collectedOperations: (Algebra.Pattern | Algebra.Link)[],\n inputType: (Algebra.Pattern | Algebra.Link)['type'],\n ): void {\n for (const input of inputs) {\n if (getOperationSource(input) && input.type === inputType) {\n collectedOperations.push(input);\n }\n }\n }\n\n protected mapMultiOperation<O extends Algebra.Union | Algebra.Alt>(\n operation: O,\n emptyOperations: Set<Algebra.Operation>,\n multiOperationFactory: (input: O['input']) => Algebra.Operation,\n ): {\n result: Algebra.Operation;\n recurse: boolean;\n } {\n // Determine which operations return non-empty results\n const nonEmptyInputs = operation.input.filter(input => !emptyOperations.has(input));\n\n // Remove empty operations\n if (nonEmptyInputs.length === operation.input.length) {\n return { result: operation, recurse: true };\n }\n if (nonEmptyInputs.length === 0) {\n return { result: multiOperationFactory([]), recurse: false };\n }\n if (nonEmptyInputs.length === 1) {\n return { result: nonEmptyInputs[0], recurse: true };\n }\n return { result: multiOperationFactory(nonEmptyInputs), recurse: true };\n }\n\n /**\n * Check if the given query operation will produce at least one result in the given source.\n * @param algebraFactory The algebra factory.\n * @param source A query source.\n * @param input A query operation.\n * @param context The query context.\n */\n public async hasSourceResults(\n algebraFactory: Factory,\n source: IQuerySourceWrapper,\n input: Algebra.Operation,\n context: IActionContext,\n ): Promise<boolean> {\n // Traversal sources should never be considered empty at optimization time.\n if (source.context?.get(KeysQuerySourceIdentify.traverse)) {\n return true;\n }\n\n // Send an ASK query\n if (this.useAskIfSupported) {\n const askOperation = algebraFactory.createAsk(input);\n if (doesShapeAcceptOperation(await source.source.getSelectorShape(context), askOperation)) {\n return source.source.queryBoolean(askOperation, context);\n }\n }\n\n // Send the operation as-is and check the response cardinality\n const bindingsStream = source.source.queryBindings(input, context);\n return new Promise((resolve, reject) => {\n bindingsStream.on('error', reject);\n bindingsStream.getProperty('metadata', (metadata: MetadataBindings) => {\n bindingsStream.destroy();\n resolve(metadata.cardinality.value > 0);\n });\n });\n }\n}\n\nexport interface IActorOptimizeQueryOperationPruneEmptySourceOperationsArgs extends IActorOptimizeQueryOperationArgs {\n /**\n * If true, ASK queries will be sent to the source instead of COUNT queries to check emptiness for patterns.\n * This will only be done for sources that accept ASK queries.\n * @default {false}\n */\n useAskIfSupported: boolean;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@comunica/actor-optimize-query-operation-prune-empty-source-operations",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "A prune-empty-source-operations optimize-query-operation actor",
|
|
5
5
|
"lsd:module": true,
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"funding": {
|
|
8
|
+
"type": "opencollective",
|
|
9
|
+
"url": "https://opencollective.com/comunica-association"
|
|
10
|
+
},
|
|
7
11
|
"homepage": "https://comunica.dev/",
|
|
8
12
|
"repository": {
|
|
9
13
|
"type": "git",
|
|
@@ -37,13 +41,12 @@
|
|
|
37
41
|
"build:components": "componentsjs-generator"
|
|
38
42
|
},
|
|
39
43
|
"dependencies": {
|
|
40
|
-
"@comunica/bus-optimize-query-operation": "^
|
|
41
|
-
"@comunica/
|
|
42
|
-
"@comunica/
|
|
43
|
-
"@comunica/
|
|
44
|
-
"@comunica/
|
|
45
|
-
"
|
|
46
|
-
"sparqlalgebrajs": "^4.3.7"
|
|
44
|
+
"@comunica/bus-optimize-query-operation": "^4.0.1",
|
|
45
|
+
"@comunica/context-entries": "^4.0.1",
|
|
46
|
+
"@comunica/core": "^4.0.1",
|
|
47
|
+
"@comunica/types": "^4.0.1",
|
|
48
|
+
"@comunica/utils-query-operation": "^4.0.1",
|
|
49
|
+
"sparqlalgebrajs": "^4.3.8"
|
|
47
50
|
},
|
|
48
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "67831ba79e45ea1ced41fe3f83c5d6e8802dbeaa"
|
|
49
52
|
}
|