Skip to content

Commit

Permalink
Redefine random nodes query template to sample directly (#449 #501)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Nov 10, 2021
1 parent 72d8675 commit ab3ccc6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
12 changes: 4 additions & 8 deletions frontend/src/sparql/compile-query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Model from '../core/model';
import { nsTable, nsMap } from '../common-rdf/ns';
import { source, item, nsTable, nsMap } from '../common-rdf/ns';
import userChannel from '../common-user/user-radio';

import itemsTemplate from './query-templates/items-for-source-template';
Expand Down Expand Up @@ -52,13 +52,9 @@ export function nodesByUserQuery(
}

export function randomNodesQuery(
randomNodes: Model[], lastNode: Model, options: SPARQLQueryOptions = {}
itemQuery: boolean, options: SPARQLQueryOptions = { limit: 10 }
) {
const data = {
...defaultOptions,
randomNodes: randomNodes.map( model => model.get('value')),
lastNode: lastNode.get('value'),
...options,
};
const nsLength = (itemQuery ? source() : item()).length;
const data = { ...defaultOptions, itemQuery, nsLength, ...options };
return randomNodesTemplate(data);
}
29 changes: 23 additions & 6 deletions frontend/src/sparql/query-templates/random-nodes-template.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX oa: <http://www.w3.org/ns/oa#>
CONSTRUCT {?node ?p ?o}
WHERE {
?node ?p ?o.
FILTER ({{#each randomNodes}}?node = <{{this}}> ||{{/each}} ?node = <{{lastNode}}>)
{{>sparqlPreamble}}

construct {
?subject ?prop ?val
} where {
?subject ?prop ?val
{
select distinct ?subject where {
{{#itemQuery}}
?anno oa:hasBody ?subject.
?subject a ?x.
{{/itemQuery}}
{{^itemQuery}}
?subject a vocab:Source.
{{/itemQuery}}
bind (substr(str(?subject), {{nsLength}}) as ?serial)
bind (strlen(?serial) as ?cap)
bind (substr(?serial, ?cap) as ?suffix)
bind (substr(?serial, 1, ?cap - 1) as ?prefix)
}
order by md5(concat(?suffix, substr(str(rand()), 2), ?prefix))
limit {{limit}}
}
}

0 comments on commit ab3ccc6

Please sign in to comment.