Skip to content

Commit

Permalink
feat(cube): componentsValues: componentValues for many components
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Oct 31, 2019
1 parent 8b0173f commit 10015f9
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/components-values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// run this example with: $ ts-node examples/components-values.ts
import { inspect } from "util";
import { DataCubeEntryPoint } from "../src/entrypoint";

function prettyPrint(obj) {
return inspect(obj, false, 10000, true);
}
function printTitle(str) {
return `\n\n---- ${str} ----\n`;
}

(async () => {
// instantiate an RDF Data Cube
const entryPoint = new DataCubeEntryPoint(
"https://trifid-lindas.test.cluster.ldbar.ch/query",
{ languages: ["de"] },
);
const dataCubes = await entryPoint.dataCubes();
const dataCube = dataCubes[0];

const dimensions = await dataCube.dimensions();

const values = await dataCube.componentsValues(dimensions);
console.log(prettyPrint(values));
})();
80 changes: 80 additions & 0 deletions src/datacube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,86 @@ export class DataCube {
* @param {Component} component
* @returns {Promise<Array<{label: Literal, value: NamedNode}>>}
*/
public async componentsValues(components: Component[]):
Promise<Map<Component, Array<{label: Literal, value: NamedNode}>>> {
const valueBinding = variable("value");
const labelBinding = variable("label");
const observation = variable("observation");
const componentBinding = variable("component");

const componentIRIs = components.map((comp) => comp.iri);

const query: SelectQuery = {
prefixes,
queryType: "SELECT",
variables: [componentBinding, valueBinding, labelBinding],
distinct: true,
from: { default: [namedNode(this.graphIri)], named: [] },
where: [
{
type: "bgp",
triples: [
{
subject: observation,
predicate: namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
object: cube("Observation"),
},
{
subject: observation,
predicate: componentBinding,
object: valueBinding,
},
{
subject: observation,
predicate: cube("dataSet"),
object: namedNode(this.iri),
},
],
},
{
type: "filter",
expression: {
type: "operation",
operator: "in",
args: [
componentBinding,
componentIRIs,
],
},
},
...generateLangOptionals(valueBinding, labelBinding, labelPredicate, this.languages),
generateLangCoalesce(labelBinding, this.languages),
],
type: "query",
};

const generator = new SparqlGenerator({ allPrefixes: true });
const sparql = generator.stringify(query);
const rawResult: Array<{component: NamedNode, label: Literal, value: NamedNode}>
= await this.fetcher.select(sparql);
const resultMap = new Map();
components.forEach((comp) => {
const values = rawResult.reduce((acc, row) => {
if (row.component.equals(comp.iri)) {
acc.push({ label: row.label, value: row.value });
}
return acc;
}, []);
resultMap.set(comp, values);
});
return resultMap;
}

/**
* Retrieve all the possible values a [[Component]] ([[Dimension]], [[Measure]], [[Attribute]]) can have.
* Similar to [[DataCube.componentsValues]] but for a single Component.
* See also [[Query.componentValues]] and the examples folder.
* ```js
* const values = await dataCube.componentValues(sizeClasses);
* ```
* @param {Component} component
* @returns {Promise<Array<{label: Literal, value: NamedNode}>>}
*/
public async componentValues(component: Component): Promise<Array<{label: Literal, value: NamedNode}>> {
if (!component || !component.componentType) {
throw new Error(`dataCube#componentValues expects valid component, got ${component} instead`);
Expand Down

0 comments on commit 10015f9

Please sign in to comment.