Skip to content

Commit

Permalink
wip: properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Nov 15, 2024
1 parent 87d926c commit 5d573c5
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 324 deletions.
35 changes: 24 additions & 11 deletions src/common/SolidPropertyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { EventStream } from '../models/ldes';
import { Node } from '../models/tree';
import { tree } from '../terms';
import { GreaterThanOrEqualToRelation } from '../models/tree/Relation';
import { Collection } from '../models/tree/Collection';

export class SolidPropertyService extends DataService<string, any> {
protected driver: SolidDataDriver<any>;
Expand Down Expand Up @@ -131,6 +132,7 @@ export class SolidPropertyService extends DataService<string, any> {
stream.setTimestampPath(sosa.resultTime);
stream.view = new Node(viewURL.href as IriString);
meta.addQuads(RDFSerializer.serializeToQuads(stream));
store.addQuads(RDFSerializer.serializeToQuads(stream.view));
return this.service.saveDatasetStore(session, `${propertyContainer.href}/property.ttl`, store)
.then(() => this.service.saveDatasetStore(session, `${propertyContainer.href}/.meta`, meta));
})
Expand All @@ -141,14 +143,18 @@ export class SolidPropertyService extends DataService<string, any> {
});
}

protected createTreeNode(session: SolidSession, node: Node): Promise<Node> {
protected createTreeNode(session: SolidSession, node: Node, collection?: Collection): Promise<Node> {
return new Promise((resolve, reject) => {
const nodeURL = new URL(node.id);
nodeURL.hash = '';
const isContainer = !nodeURL.href.endsWith('.ttl');
this.service.getDatasetStore(session, `${nodeURL.href}${isContainer ? '/' : ''}.meta`).then(meta => {
meta.addQuads(RDFSerializer.serializeToQuads(node));
return this.service.saveDatasetStore(session, `${nodeURL.href}${isContainer ? '/' : ''}.meta`, meta);
const datasetURL = `${nodeURL.href}${isContainer ? `${nodeURL.href.endsWith('/') ? '' : '/'}.meta` : ''}`
this.service.getDatasetStore(session, datasetURL).then(dataset => {
dataset.addQuads(RDFSerializer.serializeToQuads(node));
if (collection) {
dataset.addQuads(RDFSerializer.serializeToQuads(collection));
}
return this.service.saveDatasetStore(session, datasetURL, dataset);
}).then(() => resolve(node)).catch(reject);
});
}
Expand All @@ -171,29 +177,36 @@ export class SolidPropertyService extends DataService<string, any> {
const bindings = await this.driver.queryBindings(`SELECT DISTINCT ?node WHERE {
?node a <${tree.Node}> .
}`, undefined, {
sources: [meta]
sources: [store]
});
if (bindings.length === 0) {
// No root node
return reject(new Error('No root node found'));
return reject(new Error('Root node not found'));
}
const rootNode: Node = RDFSerializer.deserializeFromStore(DataFactory.namedNode(bindings[0].get('node').value as IriString), meta);
const rootNode: Node = RDFSerializer.deserializeFromStore(DataFactory.namedNode(bindings[0].get('node').value as IriString), store);
if (!rootNode) {
// Root node not found
return reject(new Error('Root node not found, but it was in the query result'));
}
let childNode = rootNode.getChildNode(observation.resultTime);
if (!childNode) {
// Create node
childNode = new Node();
childNode.id = `${property.id}/${observation.resultTime.getTime()}` as IriString;
childNode.id = `${property.id}/${observation.resultTime.getTime()}/` as IriString;
await this.service.createContainer(session, childNode.id);

rootNode.relations.push(new GreaterThanOrEqualToRelation(observation.resultTime));
// Add relation from root node to child node
rootNode.relations.push(new GreaterThanOrEqualToRelation(observation.resultTime, childNode)
.setPath(sosa.resultTime));
// Save root node
await this.createTreeNode(session, rootNode);
}

observation.id = `${childNode.id}/${this.generateUUID()}.ttl` as IriString;
childNode.members.push(observation);
const collection = new Collection(property.id);
collection.members.push(observation);
// Save child node
await this.createTreeNode(session, childNode);
await this.createTreeNode(session, childNode, collection);
// Save observation
return this.service.saveDatasetStore(session, `${observation.id}`, RDFSerializer.serializeToStore(observation));
}).then(() => {
Expand Down
18 changes: 4 additions & 14 deletions src/common/SolidService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ export abstract class SolidService extends RemoteService {
const fetchedDataset = await this.getDataset(session, dataset);
return await this.deleteRecursively(session, fetchedDataset as SolidDataset & WithResourceInfo);
}
if (!dataset) {
return Promise.resolve();
}
const containedResourceUrls = getContainedResourceUrlAll(dataset as SolidDataset & WithResourceInfo);
const containedDatasets = await Promise.all(containedResourceUrls.map(async resourceUrl => {
try {
Expand Down Expand Up @@ -438,25 +441,12 @@ export abstract class SolidService extends RemoteService {
return new Promise((resolve, reject) => {
const documentURL = new URL(uri);
documentURL.hash = '';
// Clear quads from store that do not match the URI
const quads = store.getQuads(null, null, null, null);
quads.forEach((quad) => {
if (quad.subject.termType === 'NamedNode') {
// Remove quads that do not start with the URI
const subjectURL = new URL(quad.subject.value);
subjectURL.hash = '';
if (subjectURL.href !== documentURL.href &&
subjectURL.href !== documentURL.href.replace(".meta", "")) {
store.removeQuad(quad);
}
}
});

const additions = store.additions;
const deletions = store.deletions;

if (additions.length === 0 && deletions.length === 0) {
resolve(this.storeToDataset(store));
resolve(this.getDataset(session, documentURL.href));
return;
}

Expand Down
Loading

0 comments on commit 5d573c5

Please sign in to comment.