-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from mff-uk/feature/wikidata-adapter
Wikidata experimental adapter
- Loading branch information
Showing
30 changed files
with
5,092 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
lib | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Wikidata experimental adapter | ||
|
||
## What can it do? | ||
|
||
- full hierarchy | ||
- children | ||
- parents | ||
- surroundings | ||
- parents in height 1 | ||
- children in depth 1 | ||
- attributes (wikidata properties that do not point to items based on subject contraint) | ||
- associations | ||
- internaly there are outward pimAssociations but I need to add pimClasses for the endpoints. | ||
## How to start it up? | ||
|
||
1. `> git clone repository` | ||
2. `> cd repository` | ||
3. `> git fetch` just in case | ||
4. `> git switch feature/wikidata-adapter` | ||
5. `> npm install` | ||
6. `> npx lerna bootstrap` | ||
7. `> npx lerna run build` | ||
8. `> cd ./application/client` | ||
9. `> npm run build:watch` | ||
10. open your localhost at 3000 | ||
11. to see changes in code do: | ||
1. from root run `> cd packages/wikidata-experimental-feature` | ||
2. `> npm run build:watch` simultaneously with 7. command inside client. | ||
3. to build queries run `> npm run prebuild` and then again `> npm run build:watch` |
32 changes: 32 additions & 0 deletions
32
packages/wikidata-experimental-adapter/build/compile-sparql.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { join, dirname } = require('path'); | ||
const { existsSync, readdirSync, lstatSync, readFileSync, writeFileSync, mkdirSync } = require('fs'); | ||
const { process } = require('./sparql-loader'); | ||
|
||
const found = []; | ||
|
||
function fromDir(startPath, filter, found) { | ||
if (!existsSync(startPath)) { | ||
return; | ||
} | ||
|
||
var files = readdirSync(startPath); | ||
for (var i = 0; i < files.length; i++) { | ||
var filename = join(startPath, files[i]); | ||
var stat = lstatSync(filename); | ||
if (stat.isDirectory()) { | ||
fromDir(filename, filter, found); //recurse | ||
} else if (filename.endsWith(filter)) { | ||
found.push(filename); | ||
}; | ||
}; | ||
}; | ||
|
||
fromDir('./src', '.sparql', found); | ||
|
||
for (const file of found) { | ||
const rawData = readFileSync(file); | ||
const transformed = process(rawData.toString()).code; | ||
const newFile = 'lib' + file.substring('src'.length) + '.js'; | ||
mkdirSync(dirname(newFile), {recursive: true}); | ||
writeFileSync(newFile, transformed); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require("rimraf").sync("lib"); | ||
require("./compile-sparql"); |
29 changes: 29 additions & 0 deletions
29
packages/wikidata-experimental-adapter/build/sparql-loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Converts .sparql files into .js files that can be imported | ||
|
||
function processSparql(input) { | ||
const minified = input.replace(/^\s*#.*$/gm, "").replace(/\s+/g, " "); | ||
const parts = minified | ||
.split(/%[A-Z_]+%/) | ||
.map(part => JSON.stringify(part)); | ||
const variables = [...minified | ||
.matchAll(/%([A-Z_]+)%/g)] | ||
.map(([,variable]) => | ||
variable | ||
.toLowerCase() | ||
.replace(/_+(.)/g, (_, chr) => chr.toUpperCase()) | ||
).map(key => `p.${key}`); | ||
|
||
// zip the arrays | ||
const zipped = parts.flatMap((part, i) => [part, variables[i] ?? ""]); | ||
|
||
return zipped.filter(p => p.length > 0).join(" +\n\t"); | ||
} | ||
|
||
module.exports = { | ||
process: content => ({ | ||
code: `"use strict";\n` + | ||
`Object.defineProperty(exports, "__esModule", { value: true });\n` + | ||
`var a = p => ${processSparql(content)};\n` + | ||
`exports.default = a;\n` | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
"verbose": true, | ||
"moduleFileExtensions": [ | ||
"js", | ||
"ts", | ||
], | ||
"transform": { | ||
"^.+\\.[t|j]sx?$": "ts-jest", | ||
"\\.sparql$": "./build/sparql-loader.js", | ||
}, | ||
"roots": [ | ||
"src", | ||
], | ||
}; |
Oops, something went wrong.