parent
d19437b34f
commit
d78858dbbc
File diff suppressed because it is too large
Load Diff
@ -1,187 +0,0 @@ |
||||
import type { DatasetChanges } from "../src"; |
||||
import { createSubscribableDataset } from "../src"; |
||||
import { quad, namedNode, literal } from "@rdfjs/data-model"; |
||||
import type { Dataset } from "@rdfjs/types"; |
||||
|
||||
// Create an empty subscribable dataset
|
||||
const subscribableDataset = createSubscribableDataset(); |
||||
// Add some initial quads
|
||||
subscribableDataset.addAll([ |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Zuko"), |
||||
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), |
||||
namedNode("http://example.org/cartoons#Firebender"), |
||||
namedNode("http://example.org/cartoons"), |
||||
), |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Zuko"), |
||||
namedNode("http://example.org/cartoons#name"), |
||||
literal("Zuko"), |
||||
namedNode("http://example.org/cartoons"), |
||||
), |
||||
]); |
||||
// Set up listeners
|
||||
// Listener that will trigger whenever a quad containing the named
|
||||
// node "http://example.org/cartoons#Zuko" is added or removed.
|
||||
subscribableDataset.on( |
||||
namedNode("http://example.org/cartoons#Zuko"), |
||||
(zukoQuads: Dataset, changes: DatasetChanges) => { |
||||
console.log("ZUKO NODE CHANGED ============"); |
||||
console.log(zukoQuads.toString()); |
||||
console.log("Added Quads:"); |
||||
console.log(changes.added?.toString()); |
||||
console.log("Removed Quads:"); |
||||
console.log(changes.removed?.toString()); |
||||
console.log("\n\n"); |
||||
}, |
||||
); |
||||
// Listener that will trigger whenever a quad containing the named
|
||||
// node "http://example.org/cartoons" is added or removed. This is
|
||||
// useful for keeping track of the cartoons graph.
|
||||
subscribableDataset.on( |
||||
namedNode("http://example.org/cartoons"), |
||||
(cartoonGraphQuads: Dataset, changes: DatasetChanges) => { |
||||
console.log("CARTOON GRAPH CHANGED ============"); |
||||
console.log(cartoonGraphQuads.toString()); |
||||
console.log("Added Quads:"); |
||||
console.log(changes.added?.toString()); |
||||
console.log("Removed Quads:"); |
||||
console.log(changes.removed?.toString()); |
||||
console.log("\n\n"); |
||||
}, |
||||
); |
||||
|
||||
// Modify the dataset
|
||||
/* |
||||
Prints: |
||||
CARTOON GRAPH CHANGED ============ |
||||
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Waterbender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#name> "Katara" <http://example.org/cartoons> . |
||||
|
||||
Added Quads: |
||||
<http://example.org/cartoons#Katara> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Waterbender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#name> "Katara" <http://example.org/cartoons> . |
||||
|
||||
Removed Quads: |
||||
undefined |
||||
*/ |
||||
subscribableDataset.addAll([ |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Katara"), |
||||
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), |
||||
namedNode("http://example.org/cartoons#Waterbender"), |
||||
namedNode("http://example.org/cartoons"), |
||||
), |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Katara"), |
||||
namedNode("http://example.org/cartoons#name"), |
||||
literal("Katara"), |
||||
namedNode("http://example.org/cartoons"), |
||||
), |
||||
]); |
||||
|
||||
/* |
||||
Prints: |
||||
ZUKO NODE CHANGED ============ |
||||
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
|
||||
Added Quads: |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
|
||||
Removed Quads: |
||||
undefined |
||||
|
||||
CARTOON GRAPH CHANGED ============ |
||||
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Waterbender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#name> "Katara" <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
|
||||
Added Quads: |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
|
||||
Removed Quads: |
||||
undefined |
||||
*/ |
||||
subscribableDataset.addAll([ |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Katara"), |
||||
namedNode("http://example.org/cartoons#hasEnemy"), |
||||
namedNode("http://example.org/cartoons#Zuko"), |
||||
namedNode("http://example.org/cartoons"), |
||||
), |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Zuko"), |
||||
namedNode("http://example.org/cartoons#hasEnemy"), |
||||
namedNode("http://example.org/cartoons#Katara"), |
||||
namedNode("http://example.org/cartoons"), |
||||
), |
||||
]); |
||||
|
||||
// If there are many operation you want to do at once, use transactions.
|
||||
// An update will not be triggered until the transaction is committed.
|
||||
const transactionalDataset = subscribableDataset.startTransaction(); |
||||
// Delete all triples with a "hasEnemy" predicate
|
||||
transactionalDataset.deleteMatches( |
||||
undefined, |
||||
namedNode("http://example.org/cartoons#hasEnemy"), |
||||
undefined, |
||||
undefined, |
||||
); |
||||
// Add "hasFrient" predicate
|
||||
transactionalDataset.addAll([ |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Katara"), |
||||
namedNode("http://example.org/cartoons#hasFriend"), |
||||
namedNode("http://example.org/cartoons#Zuko"), |
||||
namedNode("http://example.org/cartoons"), |
||||
), |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Zuko"), |
||||
namedNode("http://example.org/cartoons#hasFriend"), |
||||
namedNode("http://example.org/cartoons#Katara"), |
||||
namedNode("http://example.org/cartoons"), |
||||
), |
||||
]); |
||||
/* |
||||
Prints: |
||||
ZUKO NODE CHANGED ============ |
||||
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
|
||||
Added Quads: |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
|
||||
Removed Quads: |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
|
||||
CARTOON GRAPH CHANGED ============ |
||||
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Waterbender> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#name> "Katara" <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
|
||||
Added Quads: |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
|
||||
Removed Quads: |
||||
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> . |
||||
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> . |
||||
*/ |
||||
transactionalDataset.commit(); |
@ -1,52 +0,0 @@ |
||||
import type { Dataset } from "@rdfjs/types"; |
||||
import type { WriterOptions } from "n3"; |
||||
import { Writer } from "n3"; |
||||
// import SerializerJsonld from "@rdfjs/serializer-jsonld";
|
||||
// import { Readable } from "readable-stream";
|
||||
|
||||
export async function datasetToString( |
||||
dataset: Dataset, |
||||
options: WriterOptions, |
||||
): Promise<string> { |
||||
return new Promise<string>((resolve, reject) => { |
||||
const writer = new Writer(options); |
||||
for (const quad of dataset) { |
||||
writer.addQuad(quad); |
||||
} |
||||
writer.end(async (error, parsedString: string) => { |
||||
/* istanbul ignore if */ |
||||
if (error) { |
||||
return reject(error); |
||||
} |
||||
return resolve(parsedString); |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
// export async function datasetToJsonLd(
|
||||
// dataset: Dataset,
|
||||
// context: ContextDefinition
|
||||
// ): Promise<JsonLdDocument> {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// const serializerJsonld = new SerializerJsonld();
|
||||
// const input = new Readable({
|
||||
// objectMode: true,
|
||||
// read: () => {
|
||||
// dataset.forEach((quad) => {
|
||||
// input.push(quad);
|
||||
// });
|
||||
// input.push(null);
|
||||
// },
|
||||
// });
|
||||
// const output = serializerJsonld.import(input);
|
||||
|
||||
// output.on("data", (jsonld) => {
|
||||
// resolve(jsonld);
|
||||
// });
|
||||
// /* istanbul ignore next */
|
||||
// output.on("error", (err) => {
|
||||
// /* istanbul ignore next */
|
||||
// reject(err);
|
||||
// });
|
||||
// });
|
||||
// }
|
@ -0,0 +1,43 @@ |
||||
import type { Quad } from "@rdfjs/types"; |
||||
import type { ParserOptions as ParserOptionsImport } from "n3"; |
||||
import { Parser } from "n3"; |
||||
|
||||
export type ParserOptions = ParserOptionsImport; |
||||
|
||||
export async function serializedToQuads( |
||||
data: string, |
||||
options?: ParserOptions, |
||||
): Promise<Quad[]> { |
||||
// JSON-LD Parsing
|
||||
if (options && options.format === "application/json-ld") { |
||||
throw new Error("Not Implemented"); |
||||
// return new Promise((resolve, reject) => {
|
||||
// JSON.parse(data);
|
||||
// const parserJsonld = new ParserJsonld();
|
||||
|
||||
// const input = new Readable({
|
||||
// read: () => {
|
||||
// input.push(data);
|
||||
// input.push(null);
|
||||
// },
|
||||
// });
|
||||
|
||||
// const output = parserJsonld.import(input);
|
||||
// const quads: Quad[] = [];
|
||||
// output.on("data", (quad) => {
|
||||
// quads.push(quad);
|
||||
// });
|
||||
// output.on("end", () => {
|
||||
// resolve((datasetFactory.dataset(quads) as unknown) as ReturnDataset);
|
||||
// });
|
||||
// /* istanbul ignore next */
|
||||
// output.on("error", (err) => {
|
||||
// /* istanbul ignore next */
|
||||
// reject(err);
|
||||
// });
|
||||
// });
|
||||
} |
||||
// N3 Parsing
|
||||
const parser = new Parser(options as ParserOptions); |
||||
return parser.parse(data); |
||||
} |
@ -1,5 +1,31 @@ |
||||
import { SolidLdoDataset } from "./SolidLdoDataset"; |
||||
import type { Dataset } from "@ldo/rdf-utils"; |
||||
import type { SolidLdoDataset } from "./SolidLdoDataset"; |
||||
import { AccessRulesStore } from "./document/accessRules/AccessRulesStore"; |
||||
import { BinaryResourceStore } from "./document/resource/binaryResource/BinaryResourceStore"; |
||||
import { DataResourceStore } from "./document/resource/dataResource/DataResourceStore"; |
||||
import { ContainerResourceStore } from "./document/resource/dataResource/containerResource/ContainerResourceStore"; |
||||
|
||||
export function createSolidLdoDataset(): SolidLdoDataset { |
||||
throw new Error("Not Implemented"); |
||||
export interface CreateSolidLdoDatasetOptions { |
||||
fetch?: typeof fetch; |
||||
dataset?: Dataset; |
||||
} |
||||
|
||||
export function createSolidLdoDataset( |
||||
options?: CreateSolidLdoDatasetOptions, |
||||
): SolidLdoDataset { |
||||
const finalFetch = fetch || vbhyg |
||||
|
||||
// Ingnoring this because we're setting up circular dependencies
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const dependencies: LdoContextData = { |
||||
onDocumentError, |
||||
fetch: finalFetch, |
||||
dataset: ldoDataset, |
||||
updateManager: new UpdateManager(), |
||||
}; |
||||
const binaryResourceStore = new BinaryResourceStore(dependencies); |
||||
const dataResourceStore = new DataResourceStore(dependencies); |
||||
const containerResourceStore = new ContainerResourceStore(dependencies); |
||||
const accessRulesStore = new AccessRulesStore(dependencies); |
||||
} |
||||
|
@ -1,65 +0,0 @@ |
||||
import { createDataset } from "../src"; |
||||
import { quad, namedNode, literal } from "@rdfjs/data-model"; |
||||
// Required for advanced features:
|
||||
import { dataset as initializeDatasetCore } from "@rdfjs/dataset"; |
||||
import { ExtendedDatasetFactory } from "../src"; |
||||
import type { |
||||
Dataset, |
||||
Quad, |
||||
DatasetCoreFactory, |
||||
DatasetCore, |
||||
} from "@rdfjs/types"; |
||||
|
||||
/** |
||||
* Create a dataset with default settings |
||||
*/ |
||||
const defaultDataset = createDataset(); |
||||
|
||||
/** |
||||
* Create a dataset with default settings and initialized values |
||||
*/ |
||||
const initializedQuads = [ |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Tom"), |
||||
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), |
||||
namedNode("http://example.org/cartoons#Cat"), |
||||
), |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Tom"), |
||||
namedNode("http://example.org/cartoons#name"), |
||||
literal("Tom"), |
||||
), |
||||
]; |
||||
const defaultDataset2 = createDataset(initializedQuads); |
||||
|
||||
/** |
||||
* (Advanced Feature) Create a dataset by injecting a chosen datasetCore and datasetCoreFactory |
||||
*/ |
||||
const datasetFactory: DatasetCoreFactory = { |
||||
dataset: (quads?: Dataset<Quad> | Quad[]): DatasetCore => { |
||||
return initializeDatasetCore( |
||||
Array.isArray(quads) ? quads : quads?.toArray(), |
||||
); |
||||
}, |
||||
}; |
||||
const extendedDatasetFactory = new ExtendedDatasetFactory(datasetFactory); |
||||
const customDataset = extendedDatasetFactory.dataset(initializedQuads); |
||||
|
||||
/** |
||||
* Do all the methods of the RDFJS Dataset interface. For a full list of methods, go to |
||||
* https://rdf.js.org/dataset-spec/#data-interfaces
|
||||
*/ |
||||
defaultDataset.add( |
||||
quad( |
||||
namedNode("http://example.org/cartoons#Miuki"), |
||||
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), |
||||
namedNode("http://example.org/cartoons#Cat"), |
||||
), |
||||
); |
||||
const combinedDataset = defaultDataset.union(defaultDataset2); |
||||
const differenceDataset = combinedDataset.difference(customDataset); |
||||
// Prints true because "defaultDataset2" and "customDataset" have equal values
|
||||
// combinedDataset = defaultDataset ∪ defaultDataset2
|
||||
// differenceDatasset = defaultDataset \ customDataset
|
||||
// Therefore differenceDataset == defaultDataset
|
||||
console.log(differenceDataset.equals(defaultDataset)); |
@ -1,45 +0,0 @@ |
||||
import { serializedToDataset, serializedToSubscribableDataset } from "../src"; |
||||
|
||||
async function run(): Promise<void> { |
||||
// Create an ExtendedDataset using Turtle
|
||||
const turtleData = ` |
||||
@prefix : <#>. |
||||
@prefix elem: <http://purl.org/dc/elements/1.1/>. |
||||
@prefix card: </profile/card#>. |
||||
|
||||
:this |
||||
elem:author card:me. |
||||
`;
|
||||
const turtleDataset = await serializedToDataset(turtleData, { |
||||
baseIRI: |
||||
"https://jackson.solidcommunity.net/IndividualChats/jackson.solidcommunity.net/index.ttl#", |
||||
// NOTE: the "format" field isn't required because Turtle is the default parser
|
||||
}); |
||||
|
||||
// Create a SubcribableDataset using JSON-LD
|
||||
const jsonLdData = [ |
||||
{ |
||||
"@id": |
||||
"https://jackson.solidcommunity.net/IndividualChats/jackson.solidcommunity.net/index.ttl#this", |
||||
"http://purl.org/dc/elements/1.1/author": [ |
||||
{ |
||||
"@id": "https://jackson.solidcommunity.net/profile/card#me", |
||||
}, |
||||
], |
||||
}, |
||||
{ |
||||
"@id": "https://jackson.solidcommunity.net/profile/card#me", |
||||
}, |
||||
]; |
||||
const jsonLdDataset = await serializedToSubscribableDataset( |
||||
JSON.stringify(jsonLdData), |
||||
{ |
||||
baseIRI: |
||||
"https://jackson.solidcommunity.net/IndividualChats/jackson.solidcommunity.net/index.ttl#", |
||||
format: "application/json-ld", |
||||
}, |
||||
); |
||||
// Returns true because the input data describes the same triple.
|
||||
console.log(turtleDataset.equals(jsonLdDataset)); |
||||
} |
||||
run(); |
Loading…
Reference in new issue