Skip to content

Commit

Permalink
fix: LDES and TREE property structuring improvements and container fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Dec 5, 2024
1 parent 54ba316 commit 27487b9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 24 deletions.
12 changes: 12 additions & 0 deletions src/common/SolidDataDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export class SolidDataDriver<T extends DataObject | DataFrame> extends SPARQLDat
return this.service.getDataset(session, query.uri);
})
.then((dataset) => {
if (!dataset) {
return undefined;
}
const subjects = Object.values(dataset.graphs.default);
const quads = RDFSerializer.subjectsToQuads(subjects);
const store = new Store(quads);
Expand All @@ -111,6 +114,9 @@ export class SolidDataDriver<T extends DataObject | DataFrame> extends SPARQLDat
return this.service.getDataset(session, query.uri);
})
.then((dataset) => {
if (!dataset) {
return [];
}
const subjects = Object.values(dataset.graphs.default);
const quads = RDFSerializer.subjectsToQuads(subjects);
const store = new Store(quads);
Expand All @@ -134,6 +140,9 @@ export class SolidDataDriver<T extends DataObject | DataFrame> extends SPARQLDat
return this.service.getDataset(session, query.uri);
})
.then((dataset) => {
if (!dataset) {
return 0;
}
const subjects = Object.values(dataset.graphs.default);
const quads = RDFSerializer.subjectsToQuads(subjects);
const store = new Store(quads);
Expand Down Expand Up @@ -178,6 +187,9 @@ export class SolidDataDriver<T extends DataObject | DataFrame> extends SPARQLDat
this.service
.getDataset(session, documentURL.href)
.then((dataset) => {
if (!dataset) {
dataset = this.service.createDataset();
}
let promise = Promise.resolve(dataset);
for (let i = 0; i < items.length; i++) {
promise = promise.then(
Expand Down
52 changes: 37 additions & 15 deletions src/common/SolidPropertyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DataService } from '@openhps/core';
import {
Property,
RDFSerializer,
dcterms,
rdfs,
sosa,
ssn,
Expand All @@ -20,10 +19,13 @@ import { Node } from '../models/tree';
import { tree } from '../terms';
import { GreaterThanOrEqualToRelation } from '../models/tree/Relation';
import { Collection } from '../models/tree/Collection';
import { isContainer } from '@inrupt/solid-client';

/**
*
* @param node
* Default filter function for nodes
* This is the function that splits data over nodes based on the amount of members,
* date or other properties.
* @param node Tree node to filter
*/
function defaultFilter(node: Node): boolean {
// Filter false if node has 50 or more children
Expand Down Expand Up @@ -185,8 +187,7 @@ export class SolidPropertyService extends DataService<string, any> {
return new Promise((resolve, reject) => {
const nodeURL = new URL(node.id);
nodeURL.hash = '';
const isContainer = !nodeURL.href.endsWith('.ttl');
const datasetURL = `${nodeURL.href}${isContainer ? `${nodeURL.href.endsWith('/') ? '' : '/'}.meta` : ''}`;
const datasetURL = `${nodeURL.href}${isContainer(nodeURL.href) ? `${nodeURL.href.endsWith('/') ? '' : '/'}.meta` : ''}`;
this.service
.getDatasetStore(session, datasetURL)
.then((dataset) => {
Expand All @@ -205,8 +206,7 @@ export class SolidPropertyService extends DataService<string, any> {
return new Promise((resolve, reject) => {
const nodeURL = new URL(node.id);
nodeURL.hash = '';
const isContainer = !nodeURL.href.endsWith('.ttl');
const datasetURL = `${nodeURL.href}${isContainer ? `${nodeURL.href.endsWith('/') ? '' : '/'}.meta` : ''}`;
const datasetURL = `${nodeURL.href}${isContainer(nodeURL.href) ? `${nodeURL.href.endsWith('/') ? '' : '/'}.meta` : ''}`;
this.service
.getDatasetStore(session, datasetURL)
.then((dataset) => {
Expand Down Expand Up @@ -234,13 +234,12 @@ export class SolidPropertyService extends DataService<string, any> {
}

/**
* Add an observation to a property
* @param session
* @param property
* @param observation
* @returns
* Find the root node of a property
* @param session Solid session
* @param property Property
* @returns {Promise<Node>} Root node
*/
addObservation(session: SolidSession, property: Property, observation: Observation): Promise<void> {
findRootNode(session: SolidSession, property: Property): Promise<Node> {
return new Promise((resolve, reject) => {
Promise.all([
this.service.getDatasetStore(session, `${property.id}/property.ttl`),
Expand All @@ -259,15 +258,38 @@ export class SolidPropertyService extends DataService<string, any> {
);
if (bindings.length === 0) {
// No root node
return reject(new Error('Root node not found'));
resolve(undefined);
return;
}
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'));
resolve(undefined);
return;
}
resolve(rootNode);
})
.catch(reject);
});
}

/**
* Add an observation to a property
* @param session
* @param property
* @param observation
* @returns
*/
addObservation(session: SolidSession, property: Property, observation: Observation): Promise<void> {
return new Promise((resolve, reject) => {
this.findRootNode(session, property)
.then(async (rootNode) => {
// Create/repair root node if it does not exist
if (!rootNode) {
await this.createProperty(session, property);
}

// Check relations to make sure there is no issue
Expand Down
44 changes: 35 additions & 9 deletions src/common/SolidService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getThing,
saveSolidDatasetAt,
createContainerAt,
isContainer,
deleteSolidDataset,
setStringNoLocale,
setThing,
Expand Down Expand Up @@ -234,6 +235,9 @@ export abstract class SolidService extends RemoteService {
return new Promise((resolve, reject) => {
this.getDataset(session, uri)
.then((dataset) => {
if (!dataset) {
dataset = createSolidDataset();
}
const quads: Quad[] = Object.keys(dataset.graphs)
.map((key) => {
const graph = dataset.graphs[key];
Expand All @@ -247,6 +251,10 @@ export abstract class SolidService extends RemoteService {
});
}

createDataset(): SolidDataset {
return createSolidDataset();
}

/**
* Get a Solid dataset
* @param {SolidSession} session Solid session to get a thing from
Expand All @@ -269,7 +277,7 @@ export abstract class SolidService extends RemoteService {
.catch((ex: FetchError) => {
if (ex.response.status === 404) {
// Create dataset when 404 (not found)
resolve(createSolidDataset());
resolve(undefined);
} else {
reject(ex);
}
Expand Down Expand Up @@ -313,14 +321,23 @@ export abstract class SolidService extends RemoteService {
*/
createContainer(session: SolidSession, url: IriString): Promise<SolidDataset> {
return new Promise((resolve, reject) => {
createContainerAt(
url,
session
? {
fetch: session.fetch,
}
: undefined,
)
// First check if the container does not exist yet
this.getDataset(session, url)
.then((dataset: SolidDataset & WithResourceInfo) => {
if (dataset && isContainer(dataset)) {
resolve(dataset);
return;
}
// Create container (can still fail based on permissions)
return createContainerAt(
url,
session
? {
fetch: session.fetch,
}
: undefined,
);
})
.then(resolve)
.catch(reject);
});
Expand Down Expand Up @@ -361,6 +378,9 @@ export abstract class SolidService extends RemoteService {
): Promise<void> {
if (typeof dataset === 'string') {
const fetchedDataset = await this.getDataset(session, dataset);
if (!fetchedDataset) {
return Promise.resolve();
}
return await this.deleteRecursively(session, fetchedDataset as SolidDataset & WithResourceInfo);
}
if (!dataset) {
Expand Down Expand Up @@ -467,6 +487,9 @@ export abstract class SolidService extends RemoteService {
};
this.getDataset(session, documentURL.href)
.then((dataset: SolidDataset & WithResourceInfo) => {
if (!dataset) {
dataset = createSolidDataset() as SolidDataset & WithResourceInfo;
}
dummyDataset.internal_resourceInfo = dataset.internal_resourceInfo;
return saveSolidDatasetAt(
documentURL.href,
Expand Down Expand Up @@ -605,6 +628,9 @@ export abstract class SolidService extends RemoteService {
} else {
this.getDataset(session, documentURL.href)
.then((dataset) => {
if (!dataset) {
dataset = createSolidDataset();
}
setThingInDataset.bind(this)(dataset);
})
.catch(reject);
Expand Down
7 changes: 7 additions & 0 deletions test/specs/solid.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,12 @@ describe('SolidService', () => {
}).catch(done);
});

it('should not throw an error when creating an existing container', (done) => {
service.getDocumentURL(session, "/test/abc/").then(url => {
return service.createContainer(session, url.href as IriString);
}).then(() => {
done();
}).catch(done);
});
});
});

0 comments on commit 27487b9

Please sign in to comment.