Skip to content

Commit

Permalink
Add convertUserProfileToTurtle
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminaaron committed May 7, 2024
1 parent 96ffece commit c44abeb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
16 changes: 15 additions & 1 deletion dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from "url"
import fs, { promises as fsPromise } from "fs"
import { validateAll, validateOne, validateUserProfile } from "../src/index.js"
import {
convertUserProfileToTurtle,
extractDatafieldsMetadata,
extractRequirementProfilesMetadata,
runSparqlConstructQueryOnRdfString,
Expand Down Expand Up @@ -114,10 +115,23 @@ async function devExtractMedatada() {
console.log("Datafields metadata:", await extractDatafieldsMetadata(await fsPromise.readFile(DATAFIELDS, "utf8")))
}

async function devConvertUserProfileToTurtle() {
let userProfileJsonArrayStr = JSON.stringify({
triples: [{
subject: "https://foerderfunke.org/default#mainPerson",
predicate: "https://foerderfunke.org/default#hasResidence",
object: "Berlin"
}]
})
let turtleStr = await convertUserProfileToTurtle(JSON.parse(userProfileJsonArrayStr))
console.log(turtleStr)
}

// devRunSparqlSelectQueryOnRdfString()
// devRunSparqlConstructQueryOnRdfString()
devValidateAll()
// devValidateAll()
// devValidateOne()
// devValidateOneStrings()
// devValidateUserProfile()
// devExtractMedatada()
devConvertUserProfileToTurtle()
6 changes: 4 additions & 2 deletions global.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
rdfStringsToStore,
runSparqlSelectQueryOnStore,
extractRequirementProfilesMetadata,
extractDatafieldsMetadata
extractDatafieldsMetadata,
convertUserProfileToTurtle
} from "./src/utils.js";

window.MatchingEngine = {
Expand All @@ -20,5 +21,6 @@ window.MatchingEngine = {
rdfStringsToStore,
runSparqlSelectQueryOnStore,
extractRequirementProfilesMetadata,
extractDatafieldsMetadata
extractDatafieldsMetadata,
convertUserProfileToTurtle
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foerderfunke/matching-engine",
"version": "0.4.0",
"version": "0.4.1",
"description": "Checks eligibilities by validating a user profile against requirement profiles",
"author": "@foerderfunke",
"license": "MIT",
Expand Down
27 changes: 26 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Store, Parser as N3Parser, Writer as N3Writer } from "n3"
import { Store, Parser as N3Parser, Writer as N3Writer, DataFactory } from "n3"
const { namedNode, literal } = DataFactory
import SparqlParser from "sparqljs"
import Validator from "shacl-engine/Validator.js"
import rdf from "rdf-ext"
Expand Down Expand Up @@ -170,3 +171,27 @@ export async function extractDatafieldsMetadata(datafieldsStr) {
}
return metadata
}

export async function convertUserProfileToTurtle(userProfileJsonArray) {
const writer = new N3Writer({ prefixes: {
ff: "https://foerderfunke.org/default#"
}})
writer.addQuad(
namedNode("https://foerderfunke.org/default#mainPerson"),
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
namedNode("https://foerderfunke.org/default#Citizen")
)
for (let triple of userProfileJsonArray.triples) {
writer.addQuad(
namedNode(triple.subject),
namedNode(triple.predicate),
triple.object.startsWith("http") ? namedNode(triple.object) : literal(triple.object)
)
}
return new Promise((resolve, reject) => {
writer.end((error, result) => {
if (error) reject(error)
else resolve(result)
})
})
}

0 comments on commit c44abeb

Please sign in to comment.