Skip to content

Commit

Permalink
Make browser version of the SPARQL init actor consistent with Node
Browse files Browse the repository at this point in the history
When run in a browser environment, developers must now use `Comunica.newEngine().query(...)`
instead of `Comunica.evaluateQuery(...)`.
The latter function still exist, but is deprecated and will be removed in the next major update.
  • Loading branch information
rubensworks committed Jun 29, 2018
1 parent 6ed2f88 commit 8de84a6
Show file tree
Hide file tree
Showing 14 changed files with 1,934 additions and 171 deletions.
6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -48,7 +48,11 @@
"ts-jest": "^22.4.0",
"tslint": "^5.7.0",
"tslint-eslint-rules": "^5.1.0",
"typescript": "^2.5.3"
"typescript": "^2.5.3",
"lodash.assign": "^4.2.0",
"string-replace-webpack-plugin": "^0.1.3",
"webpack": "^4.11.1",
"webpack-cli": "^3.0.3"
},
"pre-commit": [
"build",
Expand Down
5 changes: 3 additions & 2 deletions packages/actor-init-sparql/README.md
Expand Up @@ -114,10 +114,11 @@ Include this file in your webpage as follows:
<script src="path/to/comunica-browser.js"></script>
```
After that, `Comunica.evaluateQuery` can be called via JavaScript.
After that, `Comunica.newEngine` can be called via JavaScript.
```javascript
Comunica.evaluateQuery('SELECT * { ?s ?p <http://dbpedia.org/resource/Belgium>. ?s ?p ?o } LIMIT 100',
const myEngine = Comunica.newEngine();
myEngine.query('SELECT * { ?s ?p <http://dbpedia.org/resource/Belgium>. ?s ?p ?o } LIMIT 100',
{ sources: [ { type: 'hypermedia', value: 'http://fragments.dbpedia.org/2015/en' } ] })
.then(function (result) {
result.bindingsStream.on('data', function (data) {
Expand Down
5 changes: 3 additions & 2 deletions packages/actor-init-sparql/bin/http.ts
Expand Up @@ -6,8 +6,9 @@ import * as http from 'http';
import minimist = require('minimist');
import * as querystring from 'querystring';
import * as url from 'url';
import {newEngineDynamic, QueryEngine} from '../lib/Query';
import {newEngineDynamic} from '../index';
import EventEmitter = NodeJS.EventEmitter;
import {ActorInitSparql} from "../lib/ActorInitSparql";

const MIME_PLAIN = 'text/plain';
const MIME_JSON = 'application/json';
Expand All @@ -30,7 +31,7 @@ const port = parseInt(args.p, 10) || 3000;

const options = { configResourceUrl: process.env.COMUNICA_CONFIG
? process.cwd() + '/' + process.env.COMUNICA_CONFIG : null };
newEngineDynamic(options).then(async (engine: QueryEngine) => {
newEngineDynamic(options).then(async (engine: ActorInitSparql) => {
const mediaTypes: {[id: string]: number} = await engine.getResultMediaTypes();
const variants: any = [];
for (const type of Object.keys(mediaTypes)) {
Expand Down
Expand Up @@ -78,7 +78,7 @@
"value": "cais:mediatorContextPreprocess"
},
{
"keyRaw": "query",
"keyRaw": "queryString",
"value": "cais:query"
},
{
Expand Down
15 changes: 15 additions & 0 deletions packages/actor-init-sparql/index-browser.ts
@@ -1 +1,16 @@
export * from './lib/ActorInitSparql-browser';

import {ActorInitSparql} from './lib/ActorInitSparql-browser';

/**
* Create a new comunica engine from the default config.
* @return {ActorInitSparql} A comunica engine.
*/
export function newEngine(): ActorInitSparql {
return require('./engine-default.js');
}

// TODO: remove in 2.0.0, this is just here for backwards-compatibility
export function evaluateQuery(query: string, context?: any): any {
return newEngine().query(query, context);
}
14 changes: 13 additions & 1 deletion packages/actor-init-sparql/index.ts
@@ -1,2 +1,14 @@
export * from './lib/ActorInitSparql';
export * from './lib/Query';
export {newEngine} from './index-browser';

import {ActorInitSparql} from "./lib/ActorInitSparql";
import {IQueryOptions, newEngineDynamicArged} from "./lib/QueryDynamic";

/**
* Create a new dynamic comunica engine from a given config file.
* @param {IQueryOptions} options Optional options on how to instantiate the query evaluator.
* @return {Promise<QueryEngine>} A promise that resolves to a fully wired comunica engine.
*/
export function newEngineDynamic(options?: IQueryOptions): Promise<ActorInitSparql> {
return newEngineDynamicArged(options || {}, __dirname, __dirname + '/config/config-default.json');
}
6 changes: 3 additions & 3 deletions packages/actor-init-sparql/lib/ActorInitSparql-browser.ts
Expand Up @@ -24,7 +24,7 @@ export class ActorInitSparql extends ActorInit implements IActorInitSparqlArgs {
IActorOutputRootSparqlParse>;
public readonly mediatorContextPreprocess: Mediator<Actor<IActionContextPreprocess, IActorTest,
IActorContextPreprocessOutput>, IActionContextPreprocess, IActorTest, IActorContextPreprocessOutput>;
public readonly query?: string;
public readonly queryString?: string;
public readonly context?: string;

constructor(args: IActorInitSparqlArgs) {
Expand All @@ -41,7 +41,7 @@ export class ActorInitSparql extends ActorInit implements IActorInitSparqlArgs {
* @param context An optional query context.
* @return {Promise<IActorQueryOperationOutput>} A promise that resolves to the query output.
*/
public async evaluateQuery(query: string, context?: any): Promise<IActorQueryOperationOutput> {
public async query(query: string, context?: any): Promise<IActorQueryOperationOutput> {
// Start, but don't await, context pre-processing
const combinationPromise = this.mediatorContextPreprocess.mediate({ context });

Expand Down Expand Up @@ -105,6 +105,6 @@ export interface IActorInitSparqlArgs extends IActorArgs<IActionInit, IActorTest
IActorOutputRootSparqlParse>;
mediatorContextPreprocess: Mediator<Actor<IActionContextPreprocess, IActorTest, IActorContextPreprocessOutput>,
IActionContextPreprocess, IActorTest, IActorContextPreprocessOutput>;
query?: string;
queryString?: string;
context?: string;
}
6 changes: 3 additions & 3 deletions packages/actor-init-sparql/lib/ActorInitSparql.ts
Expand Up @@ -18,7 +18,7 @@ export class ActorInitSparql extends ActorInitSparqlBrowser {

public async run(action: IActionInit): Promise<IActorOutputInit> {
const args = minimist(action.argv);
if (!args.listformats && (!this.query && (!(args.q || args.f) && args._.length < (args.c ? 1 : 2)
if (!args.listformats && (!this.queryString && (!(args.q || args.f) && args._.length < (args.c ? 1 : 2)
|| args._.length < (args.c ? 0 : 1) || args.h || args.help || args.v || args.version))) {
// Print version information
if (args.v || args.version) {
Expand Down Expand Up @@ -76,7 +76,7 @@ Options:
} else {
query = args._.pop();
if (!query) {
query = this.query;
query = this.queryString;
}
}

Expand Down Expand Up @@ -108,7 +108,7 @@ Options:
}

// Evaluate query
const queryResult: IActorQueryOperationOutput = await this.evaluateQuery(query, context);
const queryResult: IActorQueryOperationOutput = await this.query(query, context);

// Serialize output according to media type
const stdout: Readable = <Readable> (await this.resultToString(queryResult, args.t)).data;
Expand Down
114 changes: 0 additions & 114 deletions packages/actor-init-sparql/lib/Query.ts

This file was deleted.

65 changes: 65 additions & 0 deletions packages/actor-init-sparql/lib/QueryDynamic.ts
@@ -0,0 +1,65 @@
import {ISetupProperties, Runner, Setup} from "@comunica/runner";
import {existsSync} from "fs";
import {ActorInitSparql} from "./ActorInitSparql";

/**
* Create a new dynamic comunica engine.
* @param {IQueryOptions} options Optional options on how to instantiate the query evaluator.
* @param {string} moduleRootPath The path to the invoking module.
* @param {string} defaultConfigPath The path to the config file.
* @param {boolean} [inDevOverride] If the engine is running in a monorepo development environment.
* Default is determined based on the state of this package.
* @return {Promise<ActorInitSparql>} A promise that resolves to a fully wired comunica engine.
*/
export function newEngineDynamicArged(options: IQueryOptions, moduleRootPath: string, defaultConfigPath: string,
inDevOverride?: boolean): Promise<ActorInitSparql> {
if (!options.mainModulePath) {
// This makes sure that our configuration is found by Components.js
options.mainModulePath = inDevOverride || isInDev() ? moduleRootPath : moduleRootPath + '../';
}
const configResourceUrl: string = options.configResourceUrl || defaultConfigPath;
const instanceUri: string = options.instanceUri || 'urn:comunica:sparqlinit';

// Instantiate the main runner so that all other actors are instantiated as well,
// and find the SPARQL init actor with the given name
const runnerInstanceUri: string = options.runnerInstanceUri || 'urn:comunica:my';

// this needs to happen before any promise gets generated
Setup.preparePromises();
return Setup.instantiateComponent(configResourceUrl, runnerInstanceUri, options)
.then((runner: Runner) => {
let actor = null;
for (const runningActor of runner.actors) {
if (runningActor.name === instanceUri) {
actor = <any> runningActor;
}
}
if (!actor) {
throw new Error('No SPARQL init actor was found with the name "' + instanceUri + '" in runner "'
+ runnerInstanceUri + '".');
}
return actor;
});
}

function isInDev(): boolean {
return existsSync(__dirname + '/../test');
}

/**
* Options for configuring how the query evaluator must be instantiated.
*/
export interface IQueryOptions extends ISetupProperties {
/**
* The URL or local path to a Components.js config file.
*/
configResourceUrl?: string;
/**
* A URI identifying the component to instantiate.
*/
instanceUri?: string;
/**
* A URI identifying the runner component.
*/
runnerInstanceUri?: string;
}
10 changes: 3 additions & 7 deletions packages/actor-init-sparql/package.json
Expand Up @@ -118,11 +118,7 @@
},
"devDependencies": {
"@comunica/actor-query-operation-bgp-left-deep-smallest-sort": "^1.1.0",
"@comunica/actor-rdf-resolve-quad-pattern-hdt": "^1.1.0",
"lodash.assign": "^4.2.0",
"string-replace-webpack-plugin": "^0.1.3",
"webpack": "^4.11.1",
"webpack-cli": "^3.0.3"
"@comunica/actor-rdf-resolve-quad-pattern-hdt": "^1.1.0"
},
"jest": {
"transform": {
Expand All @@ -141,8 +137,8 @@
"lint": "node \"../../node_modules/tslint/bin/tslint\" lib/**/*.ts test/**/*.ts --exclude '**/*.d.ts'",
"build": "node \"../../node_modules/typescript/bin/tsc\"",
"validate": "npm ls",
"prepare": "componentsjs-compile-config urn:comunica:my -c config/config-default.json -e urn:comunica:sparqlinit > engine-default.js",
"browser": "npm run prepare && webpack --config webpack.config.js --mode production"
"prepare": "comunica-compile-config config/config-default.json > engine-default.js",
"browser": "npm run prepare && \"../../node_modules/webpack/bin/webpack.js\" --config webpack.config.js --mode production"
},
"browser": {
"./index.js": "./index-browser.js"
Expand Down

0 comments on commit 8de84a6

Please sign in to comment.