Skip to content

Commit

Permalink
Generalize ItemGraph, consistently query the same graph (#501 #163)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Nov 10, 2021
1 parent 5950cba commit 87b420f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
35 changes: 31 additions & 4 deletions frontend/src/common-adapters/item-graph.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { extend } from 'lodash';
import { extend, result, isString } from 'lodash';

import { item } from '../common-rdf/ns';
import Node from '../common-rdf/node';
import Graph from '../common-rdf/graph';
import { asURI } from '../utilities/linked-data-utilities';

import { sparqlRoot } from 'config.json';
import { nsRoot, sparqlRoot } from 'config.json';

/**
* Using query parameters is DEPRECATED in favor of SPARQL queries.
Expand Down Expand Up @@ -84,9 +84,36 @@ function isLiteralQuery(params: QueryParams): params is QueryParamsLiteral {
* See also the query and sparqlQuery methods below for querying the server.
*/
export default class ItemGraph extends Graph {
sparqlEndpoint: string;

// Only defined if a query has been issued.
promise: JQuery.jqXHR;

/**
* We allow client code to override `this.url` through the `graph` option.
For convenience, it is also possible to pass just the graph URL directly
as the only argument.
*
* From `this.url`, we derive the matching `this.sparqlEndpoint`.
*/
constructor(graph: string);
constructor(models?: Node[] | Object[], options?: any);
constructor(models?: Node[] | Object[] | string, options?) {
if (isString(models)) {
options = { graph: models };
models = null;
}
super(models, options);
}

preinitialize(models?, options?: any): void {
super.preinitialize(models, options);
if (options.graph) this.url = options.graph;
const url = result(this, 'url') as string;
const graphName = url.slice(nsRoot.length, -1);
this.sparqlEndpoint = `${sparqlRoot}${graphName}/query`;
}

/**
* DEPRECATED you still need this for the download parameter, but use
* sparqlQuery instead if you don't use that parameter.
Expand All @@ -113,8 +140,8 @@ export default class ItemGraph extends Graph {
* result from the given CONSTRUCT query. For best results, make sure that
* the query produces complete items.
*/
sparqlQuery(query: string, fromGraph: string): JQuery.jqXHR {
return this.promise = this.fetch({ url: sparqlRoot + fromGraph, data: $.param({ query: query }), remove: false });
sparqlQuery(query: string): JQuery.jqXHR {
return this.promise = this.fetch({ url: this.sparqlEndpoint, data: $.param({ query: query }), remove: false });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/explorer/explorer-event-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ExplorerEventController {
const items = new ItemGraph();
model.when(
'query',
(model, query) => items.sparqlQuery(modelToQuery(query), 'item/query')
(model, query) => items.sparqlQuery(modelToQuery(query)),
);
if (model.isNew()) model.save();
const collection = new FlatItemCollection(items);
Expand Down Expand Up @@ -339,7 +339,7 @@ export default ExplorerEventController;
export function getItems(source: Node): ItemGraph {
const sparqlItems = new ItemGraph();
let queryString = itemsForSourceQuery(asURI(source), {});
sparqlItems.sparqlQuery(queryString, 'item/query');
sparqlItems.sparqlQuery(queryString);
return sparqlItems;
}

Expand Down
5 changes: 3 additions & 2 deletions frontend/src/global/source-list.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Collection from '../core/collection';
import ItemGraph from '../common-adapters/item-graph';
import ldChannel from '../common-rdf/radio';
import Collection from '../core/collection';
import { source } from '../common-rdf/ns';
import { parseResponse, nodeListFactory, userNodesFactory } from '../utilities/prefetch-utilities';

const sourceList = new Collection();
sourceList.parse = parseResponse;
const getNodeList = nodeListFactory();

const userSources = new ItemGraph();
const userSources = new ItemGraph(source());
const getUserNodes = userNodesFactory();

/**
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/panel-browse/browse-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CompositeView } from "../core/view";
import { randomNodesQuery } from "../sparql/compile-query";
import explorerChannel from '../explorer/explorer-radio';
import ldChannel from '../common-rdf/radio';
import { item, source } from '../common-rdf/ns';

import Collection from '../core/collection';
import FlatItem from '../common-adapters/flat-item-model';
Expand Down Expand Up @@ -36,9 +37,9 @@ export default class BrowseView extends CompositeView {
queryingItems: boolean;

async initialize(options: ViewOptions) {
this.sparqlItems = new ItemGraph();
this.queryingItems = options.queryMode === 'items'? true : false;
this.endpoint = this.queryingItems? 'item/query' : 'source/query';
this.queryingItems = options.queryMode === 'items';
const graphName = this.queryingItems? item() : source();
this.sparqlItems = new ItemGraph(graphName);
if (options.landing) {
this.title = "My " + options.queryMode;
await this.getUserNodes();
Expand All @@ -64,7 +65,7 @@ export default class BrowseView extends CompositeView {
async getRandomNodes(nodes: Collection) {
const randomNodes = sampleSize(nodes.models, nSamples);
const randomQuery = randomNodesQuery(randomNodes.slice(-randomNodes.length, -1), randomNodes.pop(), {});
this.sparqlItems.sparqlQuery(randomQuery, this.endpoint);
this.sparqlItems.sparqlQuery(randomQuery);
}

async getUserNodes() {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/semantic-search/filter-input-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class FilterInput extends CompositeView {
const rangeId = range.first().id;
if (!rangeId.startsWith(xsd())) {
items = new ItemGraph();
items.sparqlQuery(filterInputQueryTemplate({ type: rangeId }), 'item/query');
items.sparqlQuery(filterInputQueryTemplate({ type: rangeId }));
this.subviews.push(new Select2Picker({ collection: items }));
}
}
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/utilities/prefetch-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ export function userNodesFactory() {

function getUserNodes(userNodes: ItemGraph, queryingItems: boolean): PromiseLike<ItemGraph> {
if (!promise) {
const endpoint = queryingItems ? 'item/query' : 'source/query';
promise = nodesByUserQuery(queryingItems, {}).then( (query) => {
if (!query) {
return handleError('user not authenticated');
}
return userNodes.sparqlQuery(query, endpoint).then(
return userNodes.sparqlQuery(query).then(
() => handleSuccess(userNodes), handleError
);
});
Expand Down

0 comments on commit 87b420f

Please sign in to comment.