Skip to content

Commit

Permalink
Allow context modification in optimize-query-operation bus
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoscaz committed Apr 14, 2021
1 parent cbe3091 commit 8137320
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/actor-init-sparql/lib/ActorInitSparql-browser.ts
Expand Up @@ -199,7 +199,9 @@ export class ActorInitSparql extends ActorInit implements IActorInitSparqlArgs,
}

// Optimize the query operation
operation = (await this.mediatorOptimizeQueryOperation.mediate({ context, operation })).operation;
const mediatorResult = await this.mediatorOptimizeQueryOperation.mediate({ context, operation });
operation = mediatorResult.operation;
context = mediatorResult.context || context;

// Pre-processing the context, this time with the operation
context = (await this.mediatorContextPreprocess.mediate({ context, operation })).context;
Expand Down
21 changes: 21 additions & 0 deletions packages/actor-init-sparql/test/ActorInitSparql-test.ts
Expand Up @@ -621,6 +621,27 @@ describe('ActorInitSparql', () => {
'@comunica/actor-init-sparql:baseIRI': 'myBaseIRI',
});
});

it('should pass down the provided context if optimize actors do not return one', async() => {
mediatorOptimizeQueryOperation.mediate = (action: any) => {
return Promise.resolve({ ...action, context: undefined });
};
const result = await actor.query('SELECT * WHERE { ?s ?p ?o }', { 'the-answer': 42 });
expect(result).toHaveProperty('context');
expect(result.context!.get('the-answer')).toEqual(42);
});

it('should allow optimize actors to modify the context', async() => {
mediatorOptimizeQueryOperation.mediate = (action: any) => {
return Promise.resolve({
...action,
context: action.context.set('the-answer', 42),
});
};
const result = await actor.query('SELECT * WHERE { ?s ?p ?o }');
expect(result).toHaveProperty('context');
expect(result.context!.get('the-answer')).toEqual(42);
});
});

it('bindings() should collect all bindings until "end" event occurs on triples', async() => {
Expand Down
Expand Up @@ -27,7 +27,7 @@ export class ActorHttpInvalidateListenable extends ActorHttpInvalidate {
for (const listener of this.invalidateListeners) {
listener(action);
}
return true;
return {};
}
}

Expand Down
@@ -1,5 +1,6 @@
import type { IAction, IActorArgs, IActorOutput, IActorTest } from '@comunica/core';
import { Actor } from '@comunica/core';
import type { ActionContext } from '@comunica/types';
import type { Algebra } from 'sparqlalgebrajs';

/**
Expand All @@ -26,4 +27,5 @@ export interface IActionOptimizeQueryOperation extends IAction {

export interface IActorOptimizeQueryOperationOutput extends IActorOutput {
operation: Algebra.Operation;
context?: ActionContext;
}
1 change: 1 addition & 0 deletions packages/bus-optimize-query-operation/package.json
Expand Up @@ -31,6 +31,7 @@
"index.js"
],
"dependencies": {
"@comunica/types": "^1.20.0",
"sparqlalgebrajs": "^2.5.5"
},
"peerDependencies": {
Expand Down
Expand Up @@ -27,7 +27,7 @@ export class MediatorCombinePipeline<A extends Actor<H, T, H>, H extends IAction
// and each actor output as input to the following actor.
let handle: H = action;
for (const actor of testResults.map(result => result.actor)) {
handle = await actor.runObservable(handle);
handle = { ...handle, ...await actor.runObservable(handle) };
}

// Return the final actor output
Expand Down
@@ -1,5 +1,5 @@
import type { IAction, IActorOutput, IActorTest } from '@comunica/core';
import { Actor, Bus, Mediator } from '@comunica/core';
import { Actor, Bus, Mediator, ActionContext } from '@comunica/core';
import { MediatorCombinePipeline } from '../lib/MediatorCombinePipeline';

describe('MediatorCombinePipeline', () => {
Expand Down Expand Up @@ -39,6 +39,21 @@ describe('MediatorCombinePipeline', () => {
it('should mediate', () => {
return expect(mediator.mediate({ field: 1 })).resolves.toEqual({ field: 2_101 });
});

it('should mediate without changing the context', async() => {
const context = ActionContext({});
const result = await mediator.mediate({ field: 1, context });
expect(result).toEqual({ field: 2_101, context });
});

it('should mediate changing the context', async() => {
new DummyActorContextOutput(1_000, bus);
const context = ActionContext({});
const result = await mediator.mediate({ field: 1, context });
expect(result).toHaveProperty('context');
expect(result.context).not.toEqual(context);
expect(result.context!.get('id')).toEqual(1_000);
});
});

describe('An MediatorCombinePipeline instance without actors', () => {
Expand All @@ -54,10 +69,10 @@ describe('MediatorCombinePipeline', () => {
});
});

class DummyActor extends Actor<IDummyAction, IActorTest, IDummyAction> {
class DummyActor extends Actor<IDummyAction, IActorTest, IDummyOutput> {
public readonly id: number;

public constructor(id: number, bus: Bus<DummyActor, IDummyAction, IActorTest, IDummyAction>) {
public constructor(id: number, bus: Bus<DummyActor, IDummyAction, IActorTest, IDummyOutput>) {
super({ name: `dummy${id}`, bus });
this.id = id;
}
Expand All @@ -66,11 +81,25 @@ class DummyActor extends Actor<IDummyAction, IActorTest, IDummyAction> {
return true;
}

public async run(action: IDummyAction): Promise<IDummyAction> {
public async run(action: IDummyAction): Promise<IDummyOutput> {
return { field: action.field * this.id + this.id };
}
}

interface IDummyAction extends IAction, IActorOutput {
class DummyActorContextOutput extends DummyActor {
public async run(action: IDummyAction): Promise<IDummyOutput> {
return {
...super.run(action),
context: (action.context || ActionContext({})).set('id', this.id),
};
}
}

interface IDummyAction extends IAction {
field: number;
}

interface IDummyOutput extends IActorOutput {
field: number;
context?: ActionContext;
}

0 comments on commit 8137320

Please sign in to comment.