@rmlio/rmlmapper-java-wrapper
Advanced tools
Comparing version 0.1.1 to 1.0.0
@@ -8,2 +8,13 @@ # Changelog | ||
## Unreleased | ||
## [1.0.0] - 2020-01-06 | ||
### Added | ||
- Provide the RML rules as quads (see [issue 6](https://github.com/RMLio/rmlmapper-java-wrapper-js/issues/6)). | ||
### Fixed | ||
- Require in example in README (see [issue 5](https://github.com/RMLio/rmlmapper-java-wrapper-js/issues/5)). | ||
- Updated dependency: mocha. | ||
## [0.1.1] - 2019-07-16 | ||
@@ -43,3 +54,4 @@ | ||
[0.1.0]: https://github.com/RMLio/rmlmapper-java-wrapper-js/compare/v0.1.0...v0.1.1 | ||
[1.0.0]: https://github.com/RMLio/rmlmapper-java-wrapper-js/compare/v0.1.1...v1.0.0 | ||
[0.1.1]: https://github.com/RMLio/rmlmapper-java-wrapper-js/compare/v0.1.0...v0.1.1 | ||
[0.1.0]: https://github.com/RMLio/rmlmapper-java-wrapper-js/compare/v0.0.5...v0.1.0 | ||
@@ -46,0 +58,0 @@ [0.0.5]: https://github.com/RMLio/rmlmapper-java-wrapper-js/compare/v0.0.4...v0.0.5 |
@@ -11,2 +11,3 @@ /** | ||
const { isomorphic } = require("rdf-isomorphic"); | ||
const N3 = require('n3'); | ||
@@ -92,2 +93,27 @@ const rmlmapperPath = './rmlmapper.jar'; | ||
}); | ||
it('Input: array of quads', () => { | ||
// GIVEN a wrapper and a simple CSV mapping generating one quad | ||
const wrapper = new RMLMapperWrapper(rmlmapperPath, tempFolderPath, true); | ||
const rml = fs.readFileSync('./test/tc01/mapping.ttl', 'utf-8'); | ||
const parser = new N3.Parser(); | ||
const rmlQuads = []; | ||
parser.parse(rml, async (error, quad) => { | ||
if (quad) { | ||
rmlQuads.push(quad); | ||
} else { | ||
const sources = { | ||
'student.csv': fs.readFileSync('./test/tc01/student.csv', 'utf-8') | ||
}; | ||
// WHEN generating the quads without the metadata and expected the results to by an array of quads | ||
const result = await wrapper.execute(rmlQuads, {sources, generateMetadata: false, asQuads: true}); | ||
// THEN the mapping should succeed and the output should match one of the file | ||
const expected = await strToQuads(fs.readFileSync('./test/tc01/output.nq', 'utf-8')); | ||
assert.ok(isomorphic(result.output, expected)); | ||
} | ||
}); | ||
}); | ||
}); |
@@ -10,3 +10,3 @@ /** | ||
const N3 = require('n3'); | ||
const { DataFactory } = N3; | ||
const { DataFactory, Parser, Writer } = N3; | ||
const { literal } = DataFactory; | ||
@@ -30,3 +30,19 @@ const { strToQuads } = require('./utils'); | ||
execute(rml, options = {}) { | ||
/** | ||
* This function returns the RDF generated by using the provided RML rules. | ||
* @param rml The RML rules are either provided as a string in an RDF format or an array of quads. | ||
* @param {Object} options Additional options that you can provide to this function. | ||
* @param {Object} options.sources A map of sources where the key is the value used in rml:source in the RML rules | ||
* and the value is the content of the source. (required) | ||
* @param {boolean} options.generateMetadata Set to true to generate metadata about the execution of the rules. (optional) | ||
* @param {boolean} options.asQuads Set to true to return the generated RDF as quads. (optional) | ||
* @param {serialization} options.serialization Set to a preferred serialization for the generated RDF. (optional) | ||
* This option can be not be combined with asQuads. | ||
* @returns {Promise<unknown>} | ||
*/ | ||
async execute(rml, options = {}) { | ||
if (typeof rml === 'string' || rml instanceof String) { | ||
rml = await this._convertRDFStringToQuads(rml); | ||
} | ||
options = Object.assign({ | ||
@@ -62,3 +78,3 @@ generateMetadata: false, | ||
try { | ||
const rmlStr = await self._setSourcesMappingFile(rml, sourceDirPrefix); | ||
const rmlStr = await self._setSourcesInRMLRules(rml, sourceDirPrefix); | ||
@@ -177,24 +193,50 @@ fs.writeFile(mappingFile, rmlStr, function (error) { | ||
async _setSourcesMappingFile(rml, prefix) { | ||
/** | ||
* This method converts an RDF string to an array of quads. | ||
* @param rdf The string with the RDF. | ||
* @returns {Promise<unknown>} | ||
* @private | ||
*/ | ||
_convertRDFStringToQuads(rdf) { | ||
return new Promise((resolve, reject) => { | ||
const parser = new N3.Parser(); | ||
const writer = new N3.Writer(); | ||
const parser = new Parser(); | ||
const quads = []; | ||
parser.parse(rml, (error, quad) => { | ||
parser.parse(rdf, (error, quad) => { | ||
if (quad) { | ||
if (quad.predicate.value === 'http://semweb.mmlab.be/ns/rml#source' && quad.object.termType === 'Literal') { | ||
quad.object = literal(prefix + quad.object.value); | ||
} | ||
writer.addQuad(quad); | ||
quads.push(quad); | ||
} else if (error) { | ||
reject(error); | ||
} else { | ||
writer.end(function (error, result) { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve(quads); | ||
} | ||
}); | ||
}); | ||
} | ||
resolve(result); | ||
}); | ||
/** | ||
* This method sets the sources in the RML rules to local files. | ||
* @param rmlQuads An array of quads with the RML rules. | ||
* @param prefix The string that needs to be added before the original value of the source. | ||
* @returns {Promise<unknown>} | ||
* @private | ||
*/ | ||
async _setSourcesInRMLRules(rmlQuads, prefix) { | ||
return new Promise((resolve, reject) => { | ||
rmlQuads.forEach(quad => { | ||
if (quad.predicate.value === 'http://semweb.mmlab.be/ns/rml#source' && quad.object.termType === 'Literal') { | ||
quad.object = literal(prefix + quad.object.value); | ||
} | ||
}); | ||
const writer = new Writer(); | ||
writer.addQuads(rmlQuads); | ||
writer.end((error, result) => { | ||
if (error) { | ||
reject(error); | ||
} | ||
resolve(result); | ||
}); | ||
}); | ||
@@ -201,0 +243,0 @@ } |
{ | ||
"name": "@rmlio/rmlmapper-java-wrapper", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "A JavaScript wrapper around the Java RMLMapper.", | ||
@@ -23,3 +23,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "^6.0.2" | ||
"mocha": "^6.2.2" | ||
}, | ||
@@ -26,0 +26,0 @@ "repository": { |
@@ -13,3 +13,3 @@ # JavaScript wrapper for Java RMLMapper | ||
```javascript | ||
const RMLMapperWrapper = require('rmlmapper-java-wrapper'); | ||
const RMLMapperWrapper = require('@rmlio/rmlmapper-java-wrapper'); | ||
const fs = require('fs'); | ||
@@ -26,7 +26,10 @@ | ||
const result = await wrapper.execute(rml, sources, false, true); | ||
const result = await wrapper.execute(rml, {sources, generateMetadata: false, serialization: 'turtle'}); | ||
``` | ||
Note that you can also provide an array of [quads](http://rdf.js.org/data-model-spec/) to `execute` instead of a string with RDF. | ||
An example can be found in the tests. | ||
## License | ||
This code is copyrighted by [Ghent University – imec](http://idlab.ugent.be/) and released under the [MIT license](http://opensource.org/licenses/MIT). |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
20853
375
1
34