Skip to content

Commit

Permalink
Changed name to sparql-result-converter
Browse files Browse the repository at this point in the history
  • Loading branch information
aljoshakoecher committed Jan 16, 2020
1 parent f8e039d commit 90caa65
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# rdf4j-result-converter
A little utility class that helps you to transform the table-like results that you get from an rdf4j REST API into a nested JSON tree
# sparql-result-converter
A little utility class that helps you to transform the table-like results that you get from a SPARQL query into a nested JSON tree with a user-defined structure.

# How to use it
Just install as a dependecy via `npm install rdf4j-result-converter`.
Just install as a dependecy via `npm install sparql-result-converter`.
In your code you can then use the converter like this:
```javascript
// Require the converter
const Rdf4JResultConverter = require('rdf4j-result-converter');
const converter = new Rdf4JResultConverter();
const SparqlResultConverter = require('sparql-result-converter');
const converter = new SparqlResultConverter();

// convert an RDF4J result
const convertedResult = converter.convert(rdf4jResult, convertStructure);
// convert a SPARQL result
const convertedResult = converter.convert(sparqlResult, convertStructure);
```
You have to pass two parameters to `convert()`:
1) `rdf4jResult` are the results you get from an RDF4J REST API (see below for an example)
1) `sparqlResult` are the results you get from your SPARQL query
2) `convertStructure` is an array that describes how your converted result should look like. It looks like this:

```javascript
Expand All @@ -30,11 +30,11 @@ You have to pass two parameters to `convert()`:
`childRoot`: The name of the root element of the subordinate array. After grouping your `objectToGroup`, the child elements will be added as an array. This array will be given the property name `childRoot`.</br>

## A more detailed description
When you query RDF4J-Databases via their REST API, you get somewhat ugly results. The REST-API returns a direct representation of the result-table in JSON. This is not very useful when you want to use your query-results in a frontend to dynamically display your data. Let's look at an example:
When you query Triple stores with their REST API, you get a tabular structure which can be somewhat ugly if the result actually is a nested tree-like structure. The REST-API returns a direct representation of the result-table in JSON. This is not very useful when you want to use your query-results in a frontend to dynamically display your data. Let's look at an example:
![Example Graph](https://github.com/aljoshakoecher/rdf4j-result-converter/raw/documentation/images/docu-images/example-graph.png)
![Example Graph](https://github.com/aljoshakoecher/sparql-result-converter/raw/documentation/images/docu-images/example-graph.png)
There are three pet owners with varying numbers of pets. While Peter has only one pet, mary has three. If we'd like to find all pet owners, their pets and the type of pet(s) they have, we migh send the following query against our RDF4J database:
There are three pet owners with varying numbers of pets. While Peter has only one pet, mary has three. If we'd like to find all pet owners, their pets and the type of pet(s) they have, we migh send the following query against our triple store:
```SPARQL
PREFIX ex: <http://example.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
Expand Down Expand Up @@ -158,7 +158,7 @@ The returned JSON just represents this table, the JSON looks like this:
}
```

The result is an array consisting of objects for each row of the table. Now if you want to show all owners and all pets of each owner, a nested structure is better suited. This is what rdf4j-result-converter does. It converts the flat array into a nested structure by grouping on certain properties. You can decide which properties should be grouped by passing an array representing your desired structure to the converter function. </br>
The result is an array consisting of objects for each row of the table. Now if you want to show all owners and all pets of each owner, a nested structure is better suited. This is what sparql-result-converter gives you. It converts the flat array into a nested structure by grouping on certain properties. You can decide which properties should be grouped by passing an array representing your desired structure to the converter function. </br>
In this example, we want to group on the owners and would therefore pass the following array:
```javascript
const convertStructure = [
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"name": "rdf4j-result-converter",
"name": "sparql-result-converter",
"version": "1.0.1",
"description": "A little utility class to transform the tabular results that you get from an rdf4j REST API into a nested JSON tree",
"description": "Little utility function to that converts the table-like result of a SPARQL query into a JSON tree with a user-defined structure",
"scripts": {
"clean": "rimraf lib",
"test": "npm run lint && npm run cover",
"test:prod": "cross-env BABEL_ENV=production npm run test",
"test:only": "mocha --require babel-register --require babel-core/register --require babel-polyfill --recursive",
"test:watch": "npm test -- --watch",
"test:examples": "node examples/",
"cover": "nyc --check-coverage npm run test:only",
"lint": "eslint src test",
"build": "cross-env BABEL_ENV=production babel src --out-dir lib",
Expand All @@ -18,24 +17,26 @@
"lib",
"src"
],
"main": "./lib/rdf4j-result-converter.js",
"main": "./lib/sparql-result-converter.js",
"repository": {
"type": "git",
"url": "git+https://github.com/aljoshakoecher/rdf4j-result-converter.git"
"url": "git+https://github.com/aljoshakoecher/sparql-result-converter.git"
},
"keywords": [
"rdf4j",
"SPARQL",
"OWL",
"ontologies",
"SPARQL-Query",
"SPARQL-Result",
"rdf4j",
"json"
],
"author": "Aljosha Koecher",
"license": "MIT",
"bugs": {
"url": "https://github.com/aljoshakoecher/rdf4j-result-converter/issues"
"url": "https://github.com/aljoshakoecher/sparql-result-converter/issues"
},
"homepage": "https://github.com/aljoshakoecher/rdf4j-result-converter#readme",
"homepage": "https://github.com/aljoshakoecher/sparql-result-converter#readme",
"dependencies": {
"json-groupby": "^1.1.0",
"lodash": "^4.17.15"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const transformArray = require('./array-util').transformArray;
const allEntriesContainGroupingProperty = require('./array-util').allEntriesContainGroupingProperty;

// Maps the query result of "select_allModules" to an array of Modules
class Rdf4jResultConverter {
class SparqlResultConverter {
/**
* Groups a table-structure and converts it to a tree-like structure
* @param {*} inputArray An array representing data structured as a table
Expand Down Expand Up @@ -92,4 +92,4 @@ class Rdf4jResultConverter {
}


module.exports = Rdf4jResultConverter;
module.exports = SparqlResultConverter;
12 changes: 4 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import {
assert
} from 'chai';
import { assert } from 'chai';

const Rdf4JResultConverter = require('../lib/rdf4j-result-converter');
const SparqlResultConverter = require('../lib/sparql-result-converter');
const testData = require('./test-data');

const resultConverter = new Rdf4JResultConverter();

// This is the table-structure we might get from a DB
const resultConverter = new SparqlResultConverter();


describe('The test', () => {
it('should test table-to-tree-conversion', () => {
// Object that describes how the result should be structured
// Object that defines the structure of the result
const resultObject = [
{
objectToGroup: 'owner',
Expand Down

0 comments on commit 90caa65

Please sign in to comment.