Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 2x 2x 2x 2x 17x 17x 17x 25x 10x 15x 17x 1x 1x 1x 1x 2x 2x 2x 3x 1x 2x 1x 3x 1x 2x 1x 2x 1x | import { BlankNode, Dataset, NamedNode, Quad } from "@rdfjs/types";
import jsonldDatasetProxy from "jsonld-dataset-proxy";
import { LdoMethods, LinkedDataObject } from "./LinkedDataObject";
import {
createSubscribableDataset,
DatasetChanges,
TransactionalDataset,
} from "o-dataset-pack";
import { datasetToJsonLd, datasetToString } from "./datasetConverters";
import { ShapeType } from "./ShapeType";
import { JsonLdDocument } from "jsonld";
import { WriterOptions } from "n3";
export function createLinkedDataObject<Type>(
dataset: TransactionalDataset<Quad>,
entryNode: NamedNode | BlankNode,
shapeType: ShapeType<Type>
): LinkedDataObject<Type> {
const proxy = jsonldDatasetProxy<Type>(dataset, shapeType.context, entryNode);
const ldoMethods = getLdoMethods<Type>(dataset, entryNode, shapeType);
return new Proxy(proxy as unknown as object, {
get(target: object, key: string | symbol) {
// @ts-expect-error key casting required for methods
if (ldoMethods[key]) {
// @ts-expect-error key casting required for methods
return ldoMethods[key];
}
// @ts-expect-error key casting required for methods
return target[key];
},
}) as LinkedDataObject<Type>;
}
function getLdoMethods<Type>(
dataset: TransactionalDataset<Quad>,
entryNode: NamedNode | BlankNode,
shapeType: ShapeType<Type>
): LdoMethods<Type> {
return {
/**
* Clone
*/
$clone(): LinkedDataObject<Type> {
const subscribableDataset = createSubscribableDataset();
subscribableDataset.addAll(dataset);
return createLinkedDataObject(
subscribableDataset.startTransaction(),
entryNode,
shapeType
);
},
/**
* Changes
*/
$changes(): DatasetChanges {
return dataset.getChanges();
},
/**
* Dataset
*/
$dataset(): Dataset {
return dataset;
},
/**
* isValid
*/
$isValid(): boolean {
throw new Error("Not Implemented");
// // Transfer Quads into an N3 Datastore
// const g = new Store();
// dataset.forEach((quad) => {
// g.addQuad(quad);
// });
// // Validate
// const validationResult = ShexValidator.construct(
// shapeType.schema,
// ctor(g),
// {}
// ).validate([
// {
// node: entryNode.value,
// shape: shapeType.shape,
// },
// ]);
// return !validationResult.errors;
},
/**
* toSparqlUpdate
*/
async $toSparqlUpdate(): Promise<string> {
const changes = dataset.getChanges();
let output = "";
if (changes.removed) {
output += `DELETE DATA { ${await datasetToString(changes.removed, {
format: "N-Triples",
})} }`;
}
if (changes.added && changes.removed) {
output += "; ";
}
if (changes.added) {
output += `INSERT DATA { ${await datasetToString(changes.added, {
format: "N-Triples",
})} }`;
}
return output.replaceAll("\n", " ");
},
/**
* Serialize
*/
async $serialize(options: WriterOptions): Promise<string> {
return datasetToString(dataset, options);
},
/**
* toTurtle
*/
async $toTurtle(): Promise<string> {
return datasetToString(dataset, {});
},
/**
* toJsonLd
*/
async $toJsonLd(): Promise<JsonLdDocument> {
return datasetToJsonLd(dataset, shapeType.context);
},
/**
* toNTriples
*/
async $toNTriples(): Promise<string> {
return datasetToString(dataset, { format: "N-Triples" });
},
};
}
|