@comunica/actor-optimize-query-operation-group-sources 4.2.0 → 4.4.0

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.
@@ -155,6 +155,10 @@
155
155
  {
156
156
  "@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources__member_moveSourceAnnotationUpwardsIfPossible",
157
157
  "memberFieldName": "moveSourceAnnotationUpwardsIfPossible"
158
+ },
159
+ {
160
+ "@id": "caoqogs:components/ActorOptimizeQueryOperationGroupSources.jsonld#ActorOptimizeQueryOperationGroupSources__member_isPossibleToMoveSourceAnnotationUpwards",
161
+ "memberFieldName": "isPossibleToMoveSourceAnnotationUpwards"
158
162
  }
159
163
  ],
160
164
  "constructorArguments": [
@@ -1,7 +1,7 @@
1
1
  import type { IActionOptimizeQueryOperation, IActorOptimizeQueryOperationOutput, IActorOptimizeQueryOperationArgs } from '@comunica/bus-optimize-query-operation';
2
2
  import { ActorOptimizeQueryOperation } from '@comunica/bus-optimize-query-operation';
3
3
  import type { IActorTest, TestResult } from '@comunica/core';
4
- import type { IActionContext, IQuerySourceWrapper } from '@comunica/types';
4
+ import type { FragmentSelectorShape, IActionContext, IQuerySourceWrapper } from '@comunica/types';
5
5
  import { Algebra } from 'sparqlalgebrajs';
6
6
  /**
7
7
  * A comunica Group Sources Optimize Query Operation Actor.
@@ -34,4 +34,15 @@ export declare class ActorOptimizeQueryOperationGroupSources extends ActorOptimi
34
34
  * @param context The action context.
35
35
  */
36
36
  moveSourceAnnotationUpwardsIfPossible<O extends Algebra.Operation>(operation: O, inputs: Algebra.Operation[], source: IQuerySourceWrapper | undefined, context: IActionContext): Promise<O>;
37
+ /**
38
+ * Checks if it's possible to move the source annotation upwards using the following rules:
39
+ * - If the shape doesn't accept the operation, then it's not possible.
40
+ * - If it does and the operation does not contain extension functions or
41
+ * comunica doesn't support them, then it's possible.
42
+ * - If comunica does support them, then it's possible only if the shape accepts the extension function expressions.
43
+ * @param operation A grouped operation consisting of all given input operations.
44
+ * @param shape The common source's shape.
45
+ * @param context The action context.
46
+ */
47
+ isPossibleToMoveSourceAnnotationUpwards<O extends Algebra.Operation>(operation: O, shape: FragmentSelectorShape, context: IActionContext): boolean;
37
48
  }
@@ -41,11 +41,7 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
41
41
  const groupedInput = await this.groupOperation(operation.input, context);
42
42
  if (groupedInput.metadata?.scopedSource) {
43
43
  const source = (0, utils_query_operation_1.getOperationSource)(groupedInput);
44
- if ((0, utils_query_operation_1.doesShapeAcceptOperation)(await source.source.getSelectorShape(context), operation)) {
45
- this.logDebug(context, `Hoist 1 source-specific operation into a single ${operation.type} operation for ${source.source.toString()}`);
46
- (0, utils_query_operation_1.removeOperationSource)(groupedInput);
47
- operation = (0, utils_query_operation_1.assignOperationSource)(operation, source);
48
- }
44
+ operation = await this.moveSourceAnnotationUpwardsIfPossible(operation, [groupedInput], source, context);
49
45
  }
50
46
  return { ...operation, input: groupedInput };
51
47
  }
@@ -146,7 +142,7 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
146
142
  * @param context The action context.
147
143
  */
148
144
  async moveSourceAnnotationUpwardsIfPossible(operation, inputs, source, context) {
149
- if (source && (0, utils_query_operation_1.doesShapeAcceptOperation)(await source.source.getSelectorShape(context), operation)) {
145
+ if (source && this.isPossibleToMoveSourceAnnotationUpwards(operation, await source.source.getSelectorShape(context), context)) {
150
146
  this.logDebug(context, `Hoist ${inputs.length} source-specific operations into a single ${operation.type} operation for ${source.source.toString()}`);
151
147
  operation = (0, utils_query_operation_1.assignOperationSource)(operation, source);
152
148
  for (const input of inputs) {
@@ -155,6 +151,27 @@ class ActorOptimizeQueryOperationGroupSources extends bus_optimize_query_operati
155
151
  }
156
152
  return operation;
157
153
  }
154
+ /**
155
+ * Checks if it's possible to move the source annotation upwards using the following rules:
156
+ * - If the shape doesn't accept the operation, then it's not possible.
157
+ * - If it does and the operation does not contain extension functions or
158
+ * comunica doesn't support them, then it's possible.
159
+ * - If comunica does support them, then it's possible only if the shape accepts the extension function expressions.
160
+ * @param operation A grouped operation consisting of all given input operations.
161
+ * @param shape The common source's shape.
162
+ * @param context The action context.
163
+ */
164
+ isPossibleToMoveSourceAnnotationUpwards(operation, shape, context) {
165
+ if ((0, utils_query_operation_1.doesShapeAcceptOperation)(shape, operation)) {
166
+ const extensionFunctions = context.get(context_entries_1.KeysInitQuery.extensionFunctions);
167
+ const expression = operation.expression;
168
+ if (!extensionFunctions || expression?.expressionType !== sparqlalgebrajs_1.Algebra.expressionTypes.NAMED ||
169
+ !(expression?.name.value in extensionFunctions) || (0, utils_query_operation_1.doesShapeAcceptOperation)(shape, expression)) {
170
+ return true;
171
+ }
172
+ }
173
+ return false;
174
+ }
158
175
  }
159
176
  exports.ActorOptimizeQueryOperationGroupSources = ActorOptimizeQueryOperationGroupSources;
160
177
  //# sourceMappingURL=ActorOptimizeQueryOperationGroupSources.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ActorOptimizeQueryOperationGroupSources.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationGroupSources.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,+DAA0D;AAE1D,yCAAwD;AAExD,2EAKyC;AACzC,qDAAmD;AAEnD;;GAEG;AACH,MAAa,uCAAwC,SAAQ,0DAA2B;IACtF,YAAmB,IAAsC;QACvD,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,OAAO,EAAE,SAA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O,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC;YACjG,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,CAAC,MAAM,6CAA6C,SAAS,CAAC,IAAI,kBAAkB,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;YACtJ,SAAS,GAAG,IAAA,6CAAqB,EAAC,SAAS,EAAE,MAAM,CAAC,CAAC;YACrD,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBAC3B,IAAA,6CAAqB,EAAC,KAAK,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAzKD,0FAyKC","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 } from '@comunica/context-entries';\nimport type { IActorTest, TestResult } from '@comunica/core';\nimport { failTest, passTestVoid } from '@comunica/core';\nimport type { ComunicaDataFactory, IActionContext, IQuerySourceWrapper } from '@comunica/types';\nimport {\n assignOperationSource,\n doesShapeAcceptOperation,\n getOperationSource,\n removeOperationSource,\n} from '@comunica/utils-query-operation';\nimport { Algebra, Factory } from 'sparqlalgebrajs';\n\n/**\n * A comunica Group Sources Optimize Query Operation Actor.\n */\nexport class ActorOptimizeQueryOperationGroupSources extends ActorOptimizeQueryOperation {\n public constructor(args: IActorOptimizeQueryOperationArgs) {\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 return { operation: await this.groupOperation(action.operation, action.context), context: action.context };\n }\n\n /**\n * Group operations belonging to the same source together, only if that source accepts the grouped operations.\n * This grouping will be done recursively for the whole operation tree.\n * Operations annotated with sources are considered leaves in the tree.\n * @param operation An operation to group.\n * @param context The action context.\n */\n public async groupOperation(operation: Algebra.Operation, context: IActionContext): Promise<Algebra.Operation> {\n const dataFactory: ComunicaDataFactory = context.getSafe(KeysInitQuery.dataFactory);\n const algebraFactory = new Factory(dataFactory);\n\n // Return operation as-is if the operation already has a single source, or if the operation has no children.\n if (getOperationSource(operation) ?? !('input' in operation)) {\n return operation;\n }\n\n // If operation has a single input, move source annotation upwards if the source can handle it.\n if (!Array.isArray(operation.input)) {\n const groupedInput = await this.groupOperation(operation.input, context);\n if (groupedInput.metadata?.scopedSource) {\n const source: IQuerySourceWrapper = <IQuerySourceWrapper> getOperationSource(groupedInput);\n if (doesShapeAcceptOperation(await source.source.getSelectorShape(context), operation)) {\n this.logDebug(context, `Hoist 1 source-specific operation into a single ${operation.type} operation for ${source.source.toString()}`);\n removeOperationSource(groupedInput);\n operation = assignOperationSource(operation, source);\n }\n }\n return <Algebra.Operation> { ...operation, input: groupedInput };\n }\n\n // If operation has multiple inputs, cluster source annotations.\n const inputs: Algebra.Operation[] = await Promise.all(operation.input\n .map(subInput => this.groupOperation(subInput, context)));\n const clusters = this.clusterOperationsWithEqualSources(inputs);\n\n // If we just have a single cluster, move the source annotation upwards\n if (clusters.length === 1) {\n const newInputs = clusters[0];\n const source = getOperationSource(clusters[0][0])!;\n return <Algebra.Operation> {\n ...await this.moveSourceAnnotationUpwardsIfPossible(operation, newInputs, source, context),\n input: newInputs,\n };\n }\n\n // If the number of clusters is equal to the number of original inputs, do nothing.\n if (clusters.length === inputs.length) {\n return <Algebra.Operation> { ...operation, input: inputs };\n }\n\n // If we have multiple clusters, created nested multi-operations\n let multiFactoryMethod: (children: Algebra.Operation[], flatten: boolean) => Algebra.Operation;\n switch (operation.type) {\n case Algebra.types.JOIN:\n multiFactoryMethod = algebraFactory.createJoin.bind(algebraFactory);\n break;\n case Algebra.types.UNION:\n multiFactoryMethod = algebraFactory.createUnion.bind(algebraFactory);\n break;\n case Algebra.types.ALT:\n multiFactoryMethod = <any> algebraFactory.createAlt.bind(algebraFactory);\n break;\n case Algebra.types.SEQ:\n multiFactoryMethod = <any> algebraFactory.createSeq.bind(algebraFactory);\n break;\n default:\n // While LeftJoin and Minus are also multi-operations,\n // these can never occur because they only have 2 inputs,\n // so these cases will always be captured by one of the 2 if-cases above\n // (clusters.length === 1 or clusters.length === input.length)\n\n // In all other cases, error\n throw new Error(`Unsupported operation '${operation.type}' detected while grouping sources`);\n }\n return await this.groupOperationMulti(clusters, multiFactoryMethod, context);\n }\n\n protected async groupOperationMulti(\n clusters: Algebra.Operation[][],\n factoryMethod: (children: Algebra.Operation[], flatten: boolean) => Algebra.Operation,\n context: IActionContext,\n ): Promise<Algebra.Operation> {\n let flatten = true;\n const nestedMerges = await Promise.all(clusters.map(async(cluster) => {\n const source = getOperationSource(cluster[0])!;\n const merged = await this\n .moveSourceAnnotationUpwardsIfPossible(factoryMethod(cluster, true), cluster, source, context);\n if (getOperationSource(merged)) {\n flatten = false;\n }\n return merged;\n }));\n return factoryMethod(nestedMerges, flatten);\n }\n\n /**\n * Cluster the given operations by equal source annotations.\n * @param operationsIn An array of operations to cluster.\n */\n public clusterOperationsWithEqualSources(operationsIn: Algebra.Operation[]): Algebra.Operation[][] {\n // Operations can have a source, or no source at all\n const sourceOperations: Map<IQuerySourceWrapper, Algebra.Operation[]> = new Map();\n const sourcelessOperations: Algebra.Operation[] = [];\n\n // Cluster by source\n for (const operation of operationsIn) {\n const source = getOperationSource(operation);\n if (source) {\n if (!sourceOperations.has(source)) {\n sourceOperations.set(source, []);\n }\n sourceOperations.get(source)!.push(operation);\n } else {\n sourcelessOperations.push(operation);\n }\n }\n\n // Return clusters\n const clusters: Algebra.Operation[][] = [];\n if (sourcelessOperations.length > 0) {\n clusters.push(sourcelessOperations);\n }\n for (const [ source, operations ] of sourceOperations.entries()) {\n clusters.push(operations\n .map(operation => assignOperationSource(operation, source)));\n }\n return clusters;\n }\n\n /**\n * If the given source accepts the grouped operation, annotate the grouped operation with the source,\n * and remove the source annotation from the seperate input operations.\n * Otherwise, return the grouped operation unchanged.\n * @param operation A grouped operation consisting of all given input operations.\n * @param inputs An array of operations that share the same source annotation.\n * @param source The common source.\n * @param context The action context.\n */\n public async moveSourceAnnotationUpwardsIfPossible<O extends Algebra.Operation>(\n operation: O,\n inputs: Algebra.Operation[],\n source: IQuerySourceWrapper | undefined,\n context: IActionContext,\n ): Promise<O> {\n if (source && doesShapeAcceptOperation(await source.source.getSelectorShape(context), operation)) {\n this.logDebug(context, `Hoist ${inputs.length} source-specific operations into a single ${operation.type} operation for ${source.source.toString()}`);\n operation = assignOperationSource(operation, source);\n for (const input of inputs) {\n removeOperationSource(input);\n }\n }\n return operation;\n }\n}\n"]}
1
+ {"version":3,"file":"ActorOptimizeQueryOperationGroupSources.js","sourceRoot":"","sources":["ActorOptimizeQueryOperationGroupSources.ts"],"names":[],"mappings":";;;AAKA,yFAAqF;AACrF,+DAA0D;AAE1D,yCAAwD;AAExD,2EAKyC;AACzC,qDAAmD;AAEnD;;GAEG;AACH,MAAa,uCAAwC,SAAQ,0DAA2B;IACtF,YAAmB,IAAsC;QACvD,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,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,MAAM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AC;YAC/B,CAAC;QACH,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;;;;;;OASG;IACI,uCAAuC,CAC5C,SAAY,EACZ,KAA4B,EAC5B,OAAuB;QAEvB,IAAI,IAAA,gDAAwB,EAAC,KAAK,EAAE,SAAS,CAAC,EAAE,CAAC;YAC/C,MAAM,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC,+BAAa,CAAC,kBAAkB,CAAC,CAAC;YACzE,MAAM,UAAU,GAAmC,SAAS,CAAC,UAAU,CAAC;YACxE,IAAI,CAAC,kBAAkB,IAAI,UAAU,EAAE,cAAc,KAAK,yBAAO,CAAC,eAAe,CAAC,KAAK;gBACrF,CAAC,CAAC,UAAU,EAAE,IAAI,CAAC,KAAK,IAAI,kBAAkB,CAAC,IAAI,IAAA,gDAAwB,EAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;gBACjG,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAnMD,0FAmMC","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 } from '@comunica/context-entries';\nimport type { IActorTest, TestResult } from '@comunica/core';\nimport { failTest, passTestVoid } from '@comunica/core';\nimport type { ComunicaDataFactory, FragmentSelectorShape, IActionContext, IQuerySourceWrapper } from '@comunica/types';\nimport {\n assignOperationSource,\n doesShapeAcceptOperation,\n getOperationSource,\n removeOperationSource,\n} from '@comunica/utils-query-operation';\nimport { Algebra, Factory } from 'sparqlalgebrajs';\n\n/**\n * A comunica Group Sources Optimize Query Operation Actor.\n */\nexport class ActorOptimizeQueryOperationGroupSources extends ActorOptimizeQueryOperation {\n public constructor(args: IActorOptimizeQueryOperationArgs) {\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 return { operation: await this.groupOperation(action.operation, action.context), context: action.context };\n }\n\n /**\n * Group operations belonging to the same source together, only if that source accepts the grouped operations.\n * This grouping will be done recursively for the whole operation tree.\n * Operations annotated with sources are considered leaves in the tree.\n * @param operation An operation to group.\n * @param context The action context.\n */\n public async groupOperation(operation: Algebra.Operation, context: IActionContext): Promise<Algebra.Operation> {\n const dataFactory: ComunicaDataFactory = context.getSafe(KeysInitQuery.dataFactory);\n const algebraFactory = new Factory(dataFactory);\n\n // Return operation as-is if the operation already has a single source, or if the operation has no children.\n if (getOperationSource(operation) ?? !('input' in operation)) {\n return operation;\n }\n\n // If operation has a single input, move source annotation upwards if the source can handle it.\n if (!Array.isArray(operation.input)) {\n const groupedInput = await this.groupOperation(operation.input, context);\n if (groupedInput.metadata?.scopedSource) {\n const source: IQuerySourceWrapper = <IQuerySourceWrapper> getOperationSource(groupedInput);\n operation = await this.moveSourceAnnotationUpwardsIfPossible(operation, [ groupedInput ], source, context);\n }\n return <Algebra.Operation> { ...operation, input: groupedInput };\n }\n\n // If operation has multiple inputs, cluster source annotations.\n const inputs: Algebra.Operation[] = await Promise.all(operation.input\n .map(subInput => this.groupOperation(subInput, context)));\n const clusters = this.clusterOperationsWithEqualSources(inputs);\n\n // If we just have a single cluster, move the source annotation upwards\n if (clusters.length === 1) {\n const newInputs = clusters[0];\n const source = getOperationSource(clusters[0][0])!;\n return <Algebra.Operation> {\n ...await this.moveSourceAnnotationUpwardsIfPossible(operation, newInputs, source, context),\n input: newInputs,\n };\n }\n\n // If the number of clusters is equal to the number of original inputs, do nothing.\n if (clusters.length === inputs.length) {\n return <Algebra.Operation> { ...operation, input: inputs };\n }\n\n // If we have multiple clusters, created nested multi-operations\n let multiFactoryMethod: (children: Algebra.Operation[], flatten: boolean) => Algebra.Operation;\n switch (operation.type) {\n case Algebra.types.JOIN:\n multiFactoryMethod = algebraFactory.createJoin.bind(algebraFactory);\n break;\n case Algebra.types.UNION:\n multiFactoryMethod = algebraFactory.createUnion.bind(algebraFactory);\n break;\n case Algebra.types.ALT:\n multiFactoryMethod = <any> algebraFactory.createAlt.bind(algebraFactory);\n break;\n case Algebra.types.SEQ:\n multiFactoryMethod = <any> algebraFactory.createSeq.bind(algebraFactory);\n break;\n default:\n // While LeftJoin and Minus are also multi-operations,\n // these can never occur because they only have 2 inputs,\n // so these cases will always be captured by one of the 2 if-cases above\n // (clusters.length === 1 or clusters.length === input.length)\n\n // In all other cases, error\n throw new Error(`Unsupported operation '${operation.type}' detected while grouping sources`);\n }\n return await this.groupOperationMulti(clusters, multiFactoryMethod, context);\n }\n\n protected async groupOperationMulti(\n clusters: Algebra.Operation[][],\n factoryMethod: (children: Algebra.Operation[], flatten: boolean) => Algebra.Operation,\n context: IActionContext,\n ): Promise<Algebra.Operation> {\n let flatten = true;\n const nestedMerges = await Promise.all(clusters.map(async(cluster) => {\n const source = getOperationSource(cluster[0])!;\n const merged = await this\n .moveSourceAnnotationUpwardsIfPossible(factoryMethod(cluster, true), cluster, source, context);\n if (getOperationSource(merged)) {\n flatten = false;\n }\n return merged;\n }));\n return factoryMethod(nestedMerges, flatten);\n }\n\n /**\n * Cluster the given operations by equal source annotations.\n * @param operationsIn An array of operations to cluster.\n */\n public clusterOperationsWithEqualSources(operationsIn: Algebra.Operation[]): Algebra.Operation[][] {\n // Operations can have a source, or no source at all\n const sourceOperations: Map<IQuerySourceWrapper, Algebra.Operation[]> = new Map();\n const sourcelessOperations: Algebra.Operation[] = [];\n\n // Cluster by source\n for (const operation of operationsIn) {\n const source = getOperationSource(operation);\n if (source) {\n if (!sourceOperations.has(source)) {\n sourceOperations.set(source, []);\n }\n sourceOperations.get(source)!.push(operation);\n } else {\n sourcelessOperations.push(operation);\n }\n }\n\n // Return clusters\n const clusters: Algebra.Operation[][] = [];\n if (sourcelessOperations.length > 0) {\n clusters.push(sourcelessOperations);\n }\n for (const [ source, operations ] of sourceOperations.entries()) {\n clusters.push(operations\n .map(operation => assignOperationSource(operation, source)));\n }\n return clusters;\n }\n\n /**\n * If the given source accepts the grouped operation, annotate the grouped operation with the source,\n * and remove the source annotation from the seperate input operations.\n * Otherwise, return the grouped operation unchanged.\n * @param operation A grouped operation consisting of all given input operations.\n * @param inputs An array of operations that share the same source annotation.\n * @param source The common source.\n * @param context The action context.\n */\n public async moveSourceAnnotationUpwardsIfPossible<O extends Algebra.Operation>(\n operation: O,\n inputs: Algebra.Operation[],\n source: IQuerySourceWrapper | undefined,\n context: IActionContext,\n ): Promise<O> {\n if (source && this.isPossibleToMoveSourceAnnotationUpwards(\n operation,\n await source.source.getSelectorShape(context),\n context,\n )) {\n this.logDebug(context, `Hoist ${inputs.length} source-specific operations into a single ${operation.type} operation for ${source.source.toString()}`);\n operation = assignOperationSource(operation, source);\n for (const input of inputs) {\n removeOperationSource(input);\n }\n }\n return operation;\n }\n\n /**\n * Checks if it's possible to move the source annotation upwards using the following rules:\n * - If the shape doesn't accept the operation, then it's not possible.\n * - If it does and the operation does not contain extension functions or\n * comunica doesn't support them, then it's possible.\n * - If comunica does support them, then it's possible only if the shape accepts the extension function expressions.\n * @param operation A grouped operation consisting of all given input operations.\n * @param shape The common source's shape.\n * @param context The action context.\n */\n public isPossibleToMoveSourceAnnotationUpwards<O extends Algebra.Operation>(\n operation: O,\n shape: FragmentSelectorShape,\n context: IActionContext,\n ): boolean {\n if (doesShapeAcceptOperation(shape, operation)) {\n const extensionFunctions = context.get(KeysInitQuery.extensionFunctions);\n const expression: Algebra.Expression | undefined = operation.expression;\n if (!extensionFunctions || expression?.expressionType !== Algebra.expressionTypes.NAMED ||\n !(expression?.name.value in extensionFunctions) || doesShapeAcceptOperation(shape, expression)) {\n return true;\n }\n }\n return false;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@comunica/actor-optimize-query-operation-group-sources",
3
- "version": "4.2.0",
3
+ "version": "4.4.0",
4
4
  "description": "A group-sources optimize-query-operation actor",
5
5
  "lsd:module": true,
6
6
  "license": "MIT",
@@ -41,12 +41,12 @@
41
41
  "build:components": "componentsjs-generator"
42
42
  },
43
43
  "dependencies": {
44
- "@comunica/bus-optimize-query-operation": "^4.2.0",
45
- "@comunica/context-entries": "^4.2.0",
46
- "@comunica/core": "^4.2.0",
47
- "@comunica/types": "^4.2.0",
48
- "@comunica/utils-query-operation": "^4.2.0",
44
+ "@comunica/bus-optimize-query-operation": "^4.4.0",
45
+ "@comunica/context-entries": "^4.4.0",
46
+ "@comunica/core": "^4.4.0",
47
+ "@comunica/types": "^4.4.0",
48
+ "@comunica/utils-query-operation": "^4.4.0",
49
49
  "sparqlalgebrajs": "^4.3.8"
50
50
  },
51
- "gitHead": "94e1eacab069551590cc250074b36bce08720c4c"
51
+ "gitHead": "27acde56dfe0c1ff1162c1f46818ad752296ca88"
52
52
  }