Mercurial > code > home > repos > streamed-graph
annotate src/layout/json_ld_quads.ts @ 106:2468f2227d22
make src/layout/ and src/render/ separation
author | drewp@bigasterisk.com |
---|---|
date | Sun, 13 Mar 2022 22:00:30 -0700 |
parents | src/json_ld_quads.ts@77aa2e9234ed |
children | 5a1a79f54779 |
rev | line source |
---|---|
35 | 1 import * as jsonld from "jsonld"; |
36 | 2 import { JsonLd, JsonLdArray } from "jsonld/jsonld-spec"; |
3 import { Quad, NamedNode, DataFactory } from "n3"; | |
13 | 4 const { literal, quad, namedNode } = DataFactory; |
5 | |
20
9ec3cbc8791a
build is running, but no tests, and lots of code is disabled
drewp@bigasterisk.com
parents:
15
diff
changeset
|
6 // const { rdf } = ns; |
22
e90d9021c6a0
add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents:
20
diff
changeset
|
7 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
|
8 |
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
|
9 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
|
10 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
|
11 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
|
12 } 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
|
13 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
|
14 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
|
15 } |
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 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
|
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 } |
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 function parsePred( |
36 | 21 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
|
22 graphNode: NamedNode, |
36 | 23 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
|
24 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
|
25 subjGroup: any |
36 | 26 ) { |
27 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
|
28 if (predKey === "@type") { |
51 | 29 subjGroup["@type"].forEach((aType: string) => { |
30 onQuad(quad(subjNode, namedNode(rdf.type), namedNode(aType), graphNode)); | |
31 }); | |
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
|
32 return; |
36 | 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 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
|
35 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
|
36 const objNode = parseObjNode(obj); |
36 | 37 onQuad(quad(subjNode, predNode, objNode, graphNode)); |
38 }); | |
35 | 39 } |
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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 ) { |
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 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
|
46 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
|
47 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
|
48 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
|
49 } |
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 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
|
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 } |
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 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
|
54 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
|
55 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
|
56 (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
|
57 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
|
58 }); |
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 } |
13 | 60 |
36 | 61 export async function eachJsonLdQuad( |
62 jsonLdObj: object, | |
63 onQuad: (q: Quad) => void | |
64 ) { | |
65 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
|
66 (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
|
67 } |