Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Add first draft of a CLI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminaaron committed Oct 26, 2023
1 parent 4047971 commit 871107d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# CKG CLI

## Setup

```sh
npm install
```

## Run

```sh
npm start run-query-on-profile solar-funding user-a
```
46 changes: 46 additions & 0 deletions cli/ckg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require("fs")
const path = require("path")

async function importDependencies() {
const [{ default: rdfExt }, { default: ParserN3 }, { default: SHACLValidator }] = await Promise.all([
import("rdf-ext"),
import("@rdfjs/parser-n3"),
import("rdf-validate-shacl")
])
return { factory: rdfExt, ParserN3, SHACLValidator }
}

const profilesDir = path.join(__dirname, "profiles")
const queriesDir = path.join(__dirname, "queries")

async function loadDataset(filePath) {
const { factory, ParserN3 } = await importDependencies()
const stream = fs.createReadStream(filePath)
const parser = new ParserN3()
return factory.dataset().import(parser.import(stream))
}

async function runQueryOnProfile(queryName, profileName) {
const { factory, SHACLValidator } = await importDependencies()
const shapes = await loadDataset(path.join(queriesDir, `${queryName}.ttl`))
const data = await loadDataset(path.join(profilesDir, `${profileName}.ttl`))

const validator = new SHACLValidator(shapes, { factory })
const report = await validator.validate(data)

// get report details: https://github.com/zazuko/rdf-validate-shacl#usage

console.log("--> " + profileName + " is" + (report.conforms ? " " : " not ") + "eligible for " + queryName)
}

const commands = {
"run-query-on-profile": runQueryOnProfile,
}

const [, , command, ...args] = process.argv

if (commands[command]) {
commands[command](...args)
} else {
console.error("Unknown command:", command)
}
16 changes: 16 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ckg-cli",
"version": "0.0.1",
"bin": {
"ckg": "./ckg.js"
},
"scripts": {
"start": "node ckg.js"
},
"main": "ckg.js",
"dependencies": {
"@rdfjs/parser-n3": "^2.0.1",
"rdf-ext": "^2.4.0",
"rdf-validate-shacl": "^0.5.1"
}
}
7 changes: 7 additions & 0 deletions cli/profiles/user-a.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@prefix ckg: <http://ckg.de/default#> .

ckg:House1 a ckg:House;
ckg:roofArea 110;
ckg:houseAge 35 .

ckg:mainPerson ckg:owns ckg:House1 .
21 changes: 21 additions & 0 deletions cli/queries/solar-funding.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@prefix ckg: <http://ckg.de/default#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ckg:metadata ckg:author ckg:benjaminaaron .

ckg:RoofAreaPropertyShape a sh:PropertyShape ;
sh:path ckg:roofArea ;
sh:datatype xsd:integer ;
sh:minInclusive 100 ;
sh:message "Roof area is below the minimum required" .

ckg:HouseAgePropertyShape a sh:PropertyShape ;
sh:path ckg:houseAge ;
sh:datatype xsd:integer ;
sh:minInclusive 30 ;
sh:message "House age is below the minimum required" .

ckg:EligibleHouseShape a sh:NodeShape ;
sh:targetClass ckg:House ;
sh:property ckg:RoofAreaPropertyShape, ckg:HouseAgePropertyShape .

0 comments on commit 871107d

Please sign in to comment.