From 6405b999d491d6efc3fb1a9f8ac114e429c62d7b Mon Sep 17 00:00:00 2001 From: Benjamin Degenhart Date: Tue, 7 May 2024 12:27:08 +0200 Subject: [PATCH] Assume differrently structured profile-JSON --- dev/dev.js | 35 ++++++++++++++++++++++++++--------- package.json | 2 +- src/utils.js | 49 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/dev/dev.js b/dev/dev.js index 2e70ea6..8f44d14 100644 --- a/dev/dev.js +++ b/dev/dev.js @@ -116,14 +116,31 @@ async function devExtractMedatada() { } async function devConvertUserProfileToTurtle() { - let userProfileJsonArrayStr = JSON.stringify({ - triples: [{ - subject: "https://foerderfunke.org/default#mainPerson", - predicate: "https://foerderfunke.org/default#hasResidence", - object: "Berlin" - }] + let userProfileJsonStr = JSON.stringify({ + "ff:hasFirstNames": "Max", + "ff:hasFamilyName": "Mustermann", + "ff:hasBirthday": "1992-05-17", + "ff:paysRentCold": 900, + "ff:hasLivingArea": 80, + "ff:hasResidence": "Berlin", + "ff:hasParentingSetup": "ff:Elternpaar", + "ff:receivesWohngeld": 450, + "ff:hasIncomeBrutto": 2700, + "ff:hasIncomeNetto": 1600, + "ff:hasChild": [ + { + "ff:hasBirthday": "2013-01-23", + "ff:hasMaritalStatus": "LD", + "ff:receivesKindergeld": 250 + }, + { + "ff:hasBirthday": "2008-02-15", + "ff:hasMaritalStatus": "LD", + "ff:receivesKindergeld": 250 + } + ] }) - let turtleStr = await convertUserProfileToTurtle(JSON.parse(userProfileJsonArrayStr)) + let turtleStr = await convertUserProfileToTurtle(JSON.parse(userProfileJsonStr)) console.log(turtleStr) } @@ -147,5 +164,5 @@ async function devValidateSingleDatafieldValue() { // devValidateOneStrings() // devValidateUserProfile() // devExtractMedatada() -// devConvertUserProfileToTurtle() -devValidateSingleDatafieldValue() +devConvertUserProfileToTurtle() +// devValidateSingleDatafieldValue() diff --git a/package.json b/package.json index 392c68e..6b001c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@foerderfunke/matching-engine", - "version": "0.4.2", + "version": "0.4.3", "description": "Checks eligibilities by validating a user profile against requirement profiles", "author": "@foerderfunke", "license": "MIT", diff --git a/src/utils.js b/src/utils.js index af1f493..1ed1746 100644 --- a/src/utils.js +++ b/src/utils.js @@ -172,8 +172,9 @@ export async function extractDatafieldsMetadata(datafieldsStr) { return metadata } -export async function convertUserProfileToTurtle(userProfileJsonArray) { +export async function convertUserProfileToTurtle(userProfileJson) { const writer = new N3Writer({ prefixes: { + xsd: "http://www.w3.org/2001/XMLSchema#", ff: "https://foerderfunke.org/default#" }}) writer.addQuad( @@ -181,12 +182,43 @@ export async function convertUserProfileToTurtle(userProfileJsonArray) { 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), - determineObjectType(triple.object) - ) + + for (let [key, value] of Object.entries(userProfileJson)) { + if (key.startsWith("ff:")) key = "https://foerderfunke.org/default#" + key.slice(3) + + if (Array.isArray(value)) { + for (let i = 0; i < value.length; i++) { + let arrayElement = value[i] + let instanceClass = "https://foerderfunke.org/default#Child" // read from datafields.ttl TODO + let instanceLocalName = instanceClass.split("#")[1].toLowerCase() + i + let subject = namedNode("https://foerderfunke.org/default#" + instanceLocalName) + writer.addQuad( + namedNode("https://foerderfunke.org/default#mainPerson"), + namedNode(key), + subject + ) + writer.addQuad( + subject, + namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), + namedNode(instanceClass) + ) + // do this recursively instead TODO + for (let [arrayElementKey, arrayElementValue] of Object.entries(arrayElement)) { + if (arrayElementKey.startsWith("ff:")) arrayElementKey = "https://foerderfunke.org/default#" + arrayElementKey.slice(3) + writer.addQuad( + subject, + namedNode(arrayElementKey), + determineObjectType(arrayElementValue) + ) + } + } + } else { + writer.addQuad( + namedNode("https://foerderfunke.org/default#mainPerson"), + namedNode(key), + determineObjectType(value) + ) + } } return new Promise((resolve, reject) => { writer.end((error, result) => { @@ -197,7 +229,10 @@ export async function convertUserProfileToTurtle(userProfileJsonArray) { } function determineObjectType(objectStr) { + objectStr = objectStr.toString() if (objectStr.startsWith("http")) return namedNode(objectStr) + if (objectStr.startsWith("ff:")) return namedNode("https://foerderfunke.org/default#" + objectStr.slice(3)) + if (/^\d{4}-\d{2}-\d{2}$/.test(objectStr)) return literal(objectStr, { value: "xsd:date" }) const num = Number(objectStr) if (!isNaN(num)) return literal(num) return literal(objectStr)