Skip to content

Commit

Permalink
Assume differrently structured profile-JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminaaron committed May 7, 2024
1 parent 6f98df9 commit 6405b99
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
35 changes: 26 additions & 9 deletions dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -147,5 +164,5 @@ async function devValidateSingleDatafieldValue() {
// devValidateOneStrings()
// devValidateUserProfile()
// devExtractMedatada()
// devConvertUserProfileToTurtle()
devValidateSingleDatafieldValue()
devConvertUserProfileToTurtle()
// devValidateSingleDatafieldValue()
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.2",
"version": "0.4.3",
"description": "Checks eligibilities by validating a user profile against requirement profiles",
"author": "@foerderfunke",
"license": "MIT",
Expand Down
49 changes: 42 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,53 @@ 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(
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),
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) => {
Expand All @@ -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)
Expand Down

0 comments on commit 6405b99

Please sign in to comment.