Skip to content

Commit

Permalink
wip: property service
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Jun 26, 2024
1 parent 9d85ffc commit 18b2043
Show file tree
Hide file tree
Showing 26 changed files with 426 additions and 676 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@
"devDependencies": {
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@openhps/core": ">=0.7.7",
"@openhps/rdf": ">=0.4.68",
"@comunica/config-query-sparql": "^3.1.0",
"@openhps/core": ">=0.7.8",
"@openhps/rdf": ">=0.4.69",
"@types/chai": "^4.3.16",
"@types/cookie-session": "^2.0.49",
"@types/express": "^4.17.21",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.8",
"@types/node": "^20.14.9",
"@typescript-eslint/eslint-plugin": "^7.14.1",
"@typescript-eslint/parser": "^7.14.1",
"buffer": "^6.0.3",
Expand All @@ -123,7 +124,7 @@
"eslint-plugin-jsdoc": "^48.4.0",
"eslint-plugin-prettier": "^5.1.3",
"husky": "^9.0.11",
"mocha": "^10.5.0",
"mocha": "^10.5.1",
"mocha-junit-reporter": "^2.2.1",
"node-fetch": "^3.3.2",
"npm-check-updates": "^16.14.20",
Expand Down
5 changes: 4 additions & 1 deletion src/common/QueryEngine.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { QueryEngineBase } from '@comunica/actor-init-query';
import type { ActorInitQueryBase } from '@comunica/actor-init-query';
import type { IQueryContextCommon } from '@comunica/types';

/**
* A Comunica SPARQL query engine.
*/
export class QueryEngine extends QueryEngineBase {
export class QueryEngine<
QueryContext extends IQueryContextCommon = IQueryContextCommon,
> extends QueryEngineBase<QueryContext> {
public constructor(engine: ActorInitQueryBase) {
super(engine);
}
Expand Down
7 changes: 4 additions & 3 deletions src/common/SolidDataDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from '@openhps/rdf';
import type { QueryStringContext } from '@comunica/types';
import { QueryEngine } from './QueryEngine';
import { object } from '@openhps/rdf/dist/types/vocab/schema';

export class SolidDataDriver<T extends DataObject | DataFrame> extends SPARQLDataDriver<T> {
public model: Model;
Expand All @@ -22,8 +21,10 @@ export class SolidDataDriver<T extends DataObject | DataFrame> extends SPARQLDat

constructor(dataType: Constructor<T>, options?: SolidDataDriverOptions<T>) {
super(dataType, options);
this.options.engine = require('./engine-default'); // eslint-disable-line
this.engine = new QueryEngine(this.options.engine);
this.options.engine = this.options.engine ?? require('./engine-default')(); // eslint-disable-line
if (this.options.engine) {
this.engine = new QueryEngine(this.options.engine);
}
this.options.lenient = true;
this.options.uriPrefix = this.options.uriPrefix || '/openhps';
this.options.serialize = defaultThingSerializer;
Expand Down
106 changes: 66 additions & 40 deletions src/common/SolidPropertyService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { DataService } from '@openhps/core';
import { Property, RDFSerializer, sosa, ssn } from '@openhps/rdf';
import { Property, RDFSerializer, dcterms, rdfs, sosa, ssn, IriString } from '@openhps/rdf';
import { SolidProfileObject } from './SolidProfileObject';
import { SolidDataDriver } from './SolidDataDriver';
import { SolidService, SolidSession } from './SolidService';
import { IriString } from '@inrupt/solid-client';
import { Observation } from '../models/sosa';
import { Observation } from '@openhps/rdf/models';

export class SolidPropertyService extends DataService<string, any> {
protected driver: SolidDataDriver<any>;
Expand All @@ -30,23 +29,30 @@ export class SolidPropertyService extends DataService<string, any> {
*/
fetchProperties(session: SolidSession, profile: SolidProfileObject): Promise<Property[]> {
return new Promise((resolve, reject) => {
this.driver
.queryBindings(
`
SELECT ?property WHERE {
?property <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <${ssn.Property}>
const query = `SELECT DISTINCT ?property ?name WHERE {
?me <${ssn.hasProperty}> ?property .
OPTIONAL {
?property <${rdfs.label}> ?name .
}
`,
session,
{
sources: [profile.webId],
lenient: true,
},
)
FILTER(?me = <${profile.webId}>)
}`;
this.driver
.queryBindings(query, session, {
sources: [profile.webId],
lenient: true,
})
.then((bindings) => {
console.log(bindings);
const properties: Property[] = [];

bindings.forEach((binding) => {
const url = binding.get('property').value as IriString;
const name = binding.has('name') ? binding.get('name').value : undefined;
const description = binding.has('description') ? binding.get('description').value : undefined;
const property = new Property();
property.id = url;
property.label = name;
property.description = description;
properties.push(property);
});
resolve(properties);
})
.catch(reject);
Expand All @@ -62,43 +68,63 @@ export class SolidPropertyService extends DataService<string, any> {
createProperty(session: SolidSession, property: Property): Promise<IriString> {
return new Promise((resolve, reject) => {
const thing = RDFSerializer.serialize(property);
this.service
.createThing(session, thing as any)
.then((dataset) => {
// Link the property to the profile
resolve(thing.value);
})
.catch(reject);
// this.service.createThing(session, thing).then(() => {
// resolve(property.id as IriString);
// }).catch(reject);
});
}

/**
* Add an observation to a property
* @param session
* @param property
* @param observation
* @returns
* @param session
* @param property
* @param observation
* @returns
*/
addObservation(session: SolidSession, property: Property, observation: Observation): Promise<void> {
return new Promise((resolve, reject) => {

});
return new Promise((resolve, reject) => {});
}

/**
* Fetch all observations for a property
* @param session
* @param property
* @param after
* @returns
* @param session
* @param property
* @param after
* @returns
*/
fetchObservations(session: SolidSession, property: Property, after?: Date): Promise<Observation[]> {
return new Promise((resolve, reject) => {
const query = `
SELECT ?observation WHERE {
?observation <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <${sosa.Observation}>
}
`;
this.driver
.queryBindings(
`SELECT ?observation ?date ?result WHERE {
?observation a <${sosa.Observation}> .
OPTIONAL {
?observation <${sosa.resultTime}> ?date .
}
OPTIONAL {
?observation <${dcterms.created}> ?date .
}
?observation <${sosa.Result}> ?result .
}`,
session,
{
sources: [property.id],
lenient: true,
},
)
.then((bindings) => {
const observations: Observation[] = [];
bindings.forEach((binding) => {
const url = binding.get('observation').value as IriString;
const date = binding.has('date') ? new Date(binding.get('date').value) : undefined;
const result = binding.has('result') ? binding.get('result').value : undefined;
const observation = new Observation();
observation.id = url;
observations.push(observation);
});
resolve(observations);
})
.catch(reject);
});
}
}
Loading

0 comments on commit 18b2043

Please sign in to comment.