Mercurial > code > home > repos > streamed-graph
annotate src/layout/json_ld_quads.ts @ 128:5a1a79f54779
big rewrite
author | drewp@bigasterisk.com |
---|---|
date | Fri, 05 May 2023 21:26:36 -0700 |
parents | 2468f2227d22 |
children |
rev | line source |
---|---|
128 | 1 // unused? |
2 | |
35 | 3 import * as jsonld from "jsonld"; |
36 | 4 import { JsonLd, JsonLdArray } from "jsonld/jsonld-spec"; |
5 import { Quad, NamedNode, DataFactory } from "n3"; | |
13 | 6 const { literal, quad, namedNode } = DataFactory; |
7 | |
20
9ec3cbc8791a
build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents:
15
diff
changeset
|
8 // const { rdf } = ns; |
22
e90d9021c6a0
add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents:
20
diff
changeset
|
9 const rdf = { type: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" }; |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
10 |
46
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
11 function parseObjNode(obj: any) { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
12 if (obj["@id"]) { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
13 return namedNode(obj["@id"]); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
14 } else { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
15 if (obj["@value"] === undefined) { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
16 throw new Error("no @id or @value"); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
17 } |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
18 return literal(obj["@value"], obj["@language"] || obj["@type"]); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
19 } |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
20 } |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
21 |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
22 function parsePred( |
36 | 23 onQuad: (q: Quad) => void, |
46
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
24 graphNode: NamedNode, |
36 | 25 subjNode: NamedNode, |
46
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
26 predKey: string, |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
27 subjGroup: any |
36 | 28 ) { |
29 let predNode: NamedNode; | |
46
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
30 if (predKey === "@type") { |
51 | 31 subjGroup["@type"].forEach((aType: string) => { |
32 onQuad(quad(subjNode, namedNode(rdf.type), namedNode(aType), graphNode)); | |
33 }); | |
46
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
34 return; |
36 | 35 } |
46
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
36 predNode = namedNode(predKey); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
37 subjGroup[predKey].forEach(function(obj: any) { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
38 const objNode = parseObjNode(obj); |
36 | 39 onQuad(quad(subjNode, predNode, objNode, graphNode)); |
40 }); | |
35 | 41 } |
46
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
42 function parseSubj( |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
43 onQuad: (q: Quad) => void, |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
44 graphNode: NamedNode, |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
45 subjGroup: { [predOrId: string]: any } |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
46 ) { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
47 const subjNode = namedNode(subjGroup["@id"]); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
48 for (let predKey in subjGroup) { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
49 if (predKey === "@id") { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
50 continue; |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
51 } |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
52 parsePred(onQuad, graphNode, subjNode, predKey, subjGroup); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
53 } |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
54 } |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
55 function parseGraph(onQuad: (q: Quad) => void, g: JsonLd) { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
56 var graph = (g as { "@id": string })["@id"]; |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
57 var graphNode = namedNode(graph); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
58 (g as { "@graph": JsonLdArray })["@graph"].forEach(subj => { |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
59 parseSubj(onQuad, graphNode, subj); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
60 }); |
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
61 } |
13 | 62 |
36 | 63 export async function eachJsonLdQuad( |
64 jsonLdObj: object, | |
65 onQuad: (q: Quad) => void | |
66 ) { | |
67 const expanded = await jsonld.expand(jsonLdObj); | |
46
709e305dbd4f
bug on rdf:type stmts, i think, so I refactored a bunch and now maybe it's more testable and typeable
drewp@bigasterisk.com
parents:
36
diff
changeset
|
68 (expanded as JsonLdArray).forEach((g: JsonLd) => parseGraph(onQuad, g)); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
69 } |