Skip to content

Commit

Permalink
wip: tree creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Jul 4, 2024
1 parent 503e60b commit 2210926
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 465 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"@commitlint/config-conventional": "^19.2.2",
"@comunica/config-query-sparql": "^3.1.0",
"@openhps/core": ">=0.7.10",
"@openhps/rdf": ">=0.4.82",
"@openhps/rdf": ">=0.4.83",
"@types/chai": "^4.3.16",
"@types/cookie-session": "^2.0.49",
"@types/express": "^4.17.21",
Expand All @@ -124,7 +124,7 @@
"eslint-plugin-jsdoc": "^48.5.0",
"eslint-plugin-prettier": "^5.1.3",
"husky": "^9.0.11",
"mocha": "^10.5.2",
"mocha": "^10.6.0",
"mocha-junit-reporter": "^2.2.1",
"node-fetch": "^3.3.2",
"npm-check-updates": "^16.14.20",
Expand Down
69 changes: 62 additions & 7 deletions src/common/SolidPropertyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { SolidService, SolidSession } from './SolidService';
import { Observation } from '@openhps/rdf/models';
import { EventStream } from '../models/ldes';
import { Node } from '../models/tree';
import { tree } from '../terms';
import { GreaterThanOrEqualToRelation } from '../models/tree/Relation';

export class SolidPropertyService extends DataService<string, any> {
protected driver: SolidDataDriver<any>;
Expand Down Expand Up @@ -98,19 +100,24 @@ export class SolidPropertyService extends DataService<string, any> {
})
.then(() => {
// Create a new property dataset
return this.service.getDatasetStore(session, property.id);
return Promise.all([
this.service.getDatasetStore(session, `${property.id}`),
this.service.getDatasetStore(session, `${property.id}.meta`),
]);
})
.then((store) => {
.then(([store, meta]) => {
// Add the property
store.addQuads(RDFSerializer.serializeToQuads(property));
// Add the eventstream to the metadata
const eventStreamURL = new URL(property.id);
eventStreamURL.hash = 'EventStream';
const viewURL = new URL(property.id);
viewURL.hash = 'Node';
viewURL.hash = 'root';
const stream = new EventStream(eventStreamURL.href as IriString);
stream.setTimestampPath(sosa.resultTime);
stream.view = new Node(viewURL.href as IriString);
store.addQuads(RDFSerializer.serializeToQuads(property));
store.addQuads(RDFSerializer.serializeToQuads(stream));
return this.service.saveDatasetStore(session, property.id, store);
meta.addQuads(RDFSerializer.serializeToQuads(stream));
return this.service.saveDatasetStore(session, property.id, store).then(() => this.service.saveDatasetStore(session, `${property.id}.meta`, meta));
})
.then(() => {
resolve(property.id as IriString);
Expand All @@ -119,6 +126,17 @@ export class SolidPropertyService extends DataService<string, any> {
});
}

protected createTreeNode(session: SolidSession, node: Node): Promise<Node> {
return new Promise((resolve, reject) => {
const nodeURL = new URL(node.id);
nodeURL.hash = '';
this.service.getDatasetStore(session, `${nodeURL.href}.meta`).then(meta => {
meta.addQuads(RDFSerializer.serializeToQuads(node));
return this.service.saveDatasetStore(session, `${nodeURL.href}.meta`, meta);
}).then(() => resolve(node)).catch(reject);
});
}

/**
* Add an observation to a property
* @param session
Expand All @@ -127,7 +145,44 @@ export class SolidPropertyService extends DataService<string, any> {
* @returns
*/
addObservation(session: SolidSession, property: Property, observation: Observation): Promise<void> {
return new Promise((resolve, reject) => {});
return new Promise((resolve, reject) => {
Promise.all([
this.service.getDatasetStore(session, `${property.id}`),
this.service.getDatasetStore(session, `${property.id}.meta`),
])
.then(async ([store, meta]) => {
// Get the root node of the dataset
const bindings = await this.driver.queryBindings(`SELECT DISTINCT ?node WHERE {
?node a <${tree.Node}> .
}`, undefined, {
sources: [meta]
});
if (bindings.length === 0) {
// No root node
return reject(new Error('No root node found'));
}
const rootNode: Node = RDFSerializer.deserializeFromStore(DataFactory.namedNode(bindings[0].get('node').value as IriString), meta);
let childNode = rootNode.getChildNode(observation.resultTime);
if (!childNode) {
// Create node
childNode = new Node();
childNode.id = `${property.id}/${observation.resultTime.getTime()}` as IriString;
rootNode.relations.push(new GreaterThanOrEqualToRelation(observation.resultTime));
// Save root node
await this.createTreeNode(session, rootNode);
}

observation.id = `${childNode.id}/${this.generateUUID()}` as IriString;
console.log(childNode);
childNode.members.push(observation);
// Save child node
await this.createTreeNode(session, childNode);
// Save observation

resolve();
})
.catch(reject);
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/common/SolidService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export abstract class SolidService extends RemoteService {
this.getDataset(session, documentURL.href)
.then((dataset: SolidDataset & WithResourceInfo) => {
dummyDataset.internal_resourceInfo = dataset.internal_resourceInfo;
console.log('Saving dataset', dataset);
return saveSolidDatasetAt(
documentURL.href,
dummyDataset,
Expand Down
Loading

0 comments on commit 2210926

Please sign in to comment.