-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract hextuple processor from Libro
- Loading branch information
Thom van Kalkeren
committed
Oct 13, 2021
1 parent
ff958fc
commit 9e8e30f
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Quad } from "@ontologies/core"; | ||
|
||
import { LinkedRenderStore } from "../LinkedRenderStore"; | ||
import { | ||
ResponseAndFallbacks, | ||
ResponseTransformer, | ||
} from "../types"; | ||
import { hextupleTransformer } from "../utilities/hextupleProcessor"; | ||
|
||
export const hextupleProcessor = { | ||
acceptValue: 1.0, | ||
mediaTypes: ["application/hex+x-ndjson"], | ||
|
||
transformer: (store: LinkedRenderStore<any>): ResponseTransformer => (res: ResponseAndFallbacks): Promise<Quad[]> => { | ||
const isExpedited = res.hasOwnProperty("expedite") | ||
? (res as any).expedite | ||
: false; | ||
|
||
return hextupleTransformer(res) | ||
.then((delta) => store.queueDelta(delta, isExpedited)) | ||
.then(() => []); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { linkedDeltaProcessor } from "./linked-delta"; | ||
import { hextupleProcessor } from "./hextuples"; | ||
import { createProcessRDF } from "./rdf-formats-common"; | ||
|
||
export const transformers = { | ||
createProcessRDF, | ||
hextupleProcessor, | ||
linkedDeltaProcessor, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import rdf, { BlankNode, Quad, SomeTerm } from "@ontologies/core"; | ||
import * as rdfx from "@ontologies/rdf"; | ||
// @ts-ignore | ||
import NdjsonStream from "can-ndjson-stream"; | ||
|
||
import { | ||
ExtensionResponse, | ||
RDFLibFetcherResponse, | ||
ResponseAndFallbacks, | ||
ResponseTransformer, | ||
} from "../types"; | ||
|
||
enum HexPosition { | ||
Subject = 0, | ||
Predicate, | ||
Value, | ||
Datatype, | ||
Language, | ||
Graph, | ||
} | ||
|
||
let hasReadableStreamConstructor = false; | ||
|
||
try { | ||
// tslint:disable-next-line:typedef no-empty no-unused-expression | ||
new ReadableStream({ start() {} }); | ||
hasReadableStreamConstructor = true; | ||
} catch (e) { | ||
// ignore | ||
} | ||
|
||
export const hextupleTransformer: ResponseTransformer = async (res: ResponseAndFallbacks): Promise<Quad[]> => { | ||
const bnMap: { [s: string]: BlankNode } = {}; | ||
// Skip the (expensive) proxy object when parsing | ||
const quad = rdf.quad.bind(rdf); | ||
const literal = rdf.literal.bind(rdf); | ||
const namedNode = rdf.namedNode.bind(rdf); | ||
const blankNodeF = rdf.blankNode.bind(rdf); | ||
const defaultGraph = rdf.defaultGraph.bind(rdf); | ||
|
||
const blankNode = (v: string): BlankNode => { | ||
if (!bnMap[v]) { | ||
bnMap[v] = blankNodeF(); | ||
} | ||
|
||
return bnMap[v]; | ||
}; | ||
|
||
const object = (v: string, dt: string, l: string): SomeTerm => { | ||
if (l) { | ||
return literal(v, l); | ||
} else if (dt === rdfx.ns("namedNode").value) { | ||
return namedNode(v); | ||
} else if (dt === rdfx.ns("blankNode").value) { | ||
return blankNode(v); | ||
} | ||
|
||
return literal(v, namedNode(dt)); | ||
}; | ||
|
||
const lineToQuad = (h: string[]): Quad => quad( | ||
h[HexPosition.Subject].startsWith("_:") ? blankNode(h[HexPosition.Subject]) : namedNode(h[HexPosition.Subject]), | ||
namedNode(h[HexPosition.Predicate]), | ||
object(h[HexPosition.Value], h[HexPosition.Datatype], h[HexPosition.Language]), | ||
h[HexPosition.Graph] ? namedNode(h[HexPosition.Graph]) : defaultGraph(), | ||
); | ||
|
||
const delta: Quad[] = []; | ||
let parse; | ||
|
||
if (res instanceof Response && hasReadableStreamConstructor) { | ||
const stream = new NdjsonStream(res.body); | ||
const reader = stream.getReader(); | ||
|
||
let read: any; | ||
parse = reader | ||
.read() | ||
.then(read = (result: { done: boolean, value: string[] }) => { | ||
if (result.done) { | ||
return; | ||
} | ||
|
||
delta.push(lineToQuad(result.value)); | ||
|
||
return reader.read().then(read); | ||
}); | ||
} else { | ||
let body; | ||
|
||
if (res instanceof Response) { | ||
body = res.text(); | ||
} else if (typeof XMLHttpRequest !== "undefined" | ||
&& res instanceof XMLHttpRequest | ||
|| typeof (res as any).responseText === "string") { | ||
body = Promise.resolve((res as XMLHttpRequest | RDFLibFetcherResponse).responseText); | ||
} else { | ||
body = Promise.resolve((res as ExtensionResponse).body); | ||
} | ||
|
||
parse = body | ||
.then((text) => { | ||
for (const line of text.split("\n")) { | ||
if (line.length > 0) { | ||
delta.push(lineToQuad(JSON.parse(line))); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
await parse; | ||
|
||
return delta; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters